Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit df4b0e7

Browse files
bcoeothiym23
authored andcommitted
making it so that you can pass arguments to run-scripts.
1 parent a5d2670 commit df4b0e7

10 files changed

Lines changed: 96 additions & 41 deletions

File tree

doc/cli/npm-restart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ npm-restart(1) -- Start a package
33

44
## SYNOPSIS
55

6-
npm restart <name>
6+
npm restart [-- <args>]
77

88
## DESCRIPTION
99

doc/cli/npm-run-script.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ npm-run-script(1) -- Run arbitrary package scripts
33

44
## SYNOPSIS
55

6-
npm run-script [<pkg>] [command]
7-
npm run [<pkg>] [command]
6+
npm run-script [command] [-- <args>]
7+
npm run [command] [-- <args>]
88

99
## DESCRIPTION
1010

doc/cli/npm-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ npm-start(1) -- Start a package
33

44
## SYNOPSIS
55

6-
npm start <name>
6+
npm start [-- <args>]
77

88
## DESCRIPTION
99

doc/cli/npm-stop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ npm-stop(1) -- Stop a package
33

44
## SYNOPSIS
55

6-
npm stop <name>
6+
npm stop [-- <args>]
77

88
## DESCRIPTION
99

doc/cli/npm-test.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ npm-test(1) -- Test a package
33

44
## SYNOPSIS
55

6-
npm test <name>
7-
npm tst <name>
6+
npm test [-- <args>]
7+
npm tst [-- <args>]
88

99
## DESCRIPTION
1010

lib/run-script.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var lifecycle = require("./utils/lifecycle.js")
88
, log = require("npmlog")
99
, chain = require("slide").chain
1010

11-
runScript.usage = "npm run-script [<pkg>] <command>"
11+
runScript.usage = "npm run-script <command> [-- <args>]"
1212

1313
runScript.completion = function (opts, cb) {
1414

@@ -69,13 +69,13 @@ runScript.completion = function (opts, cb) {
6969

7070
function runScript (args, cb) {
7171
if (!args.length) return list(cb)
72-
var pkgdir = args.length === 1 ? process.cwd()
73-
: path.resolve(npm.dir, args[0])
74-
, cmd = args.pop()
72+
73+
var pkgdir = process.cwd()
74+
, cmd = args.shift()
7575

7676
readJson(path.resolve(pkgdir, "package.json"), function (er, d) {
7777
if (er) return cb(er)
78-
run(d, pkgdir, cmd, cb)
78+
run(d, pkgdir, cmd, args, cb)
7979
})
8080
}
8181

@@ -109,7 +109,7 @@ function list(cb) {
109109
})
110110
}
111111

112-
function run (pkg, wd, cmd, cb) {
112+
function run (pkg, wd, cmd, args, cb) {
113113
var cmds = []
114114
if (!pkg.scripts) pkg.scripts = {}
115115
if (cmd === "restart") {
@@ -124,6 +124,11 @@ function run (pkg, wd, cmd, cb) {
124124
}
125125
log.verbose("run-script", cmds)
126126
chain(cmds.map(function (c) {
127+
// pass cli arguments after -- to script.
128+
if (args.length && pkg.scripts[c]) {
129+
pkg.scripts[c] = pkg.scripts[c] + " " + args.join(" ")
130+
}
131+
127132
// when running scripts explicitly, assume that they're trusted.
128133
return [lifecycle, pkg, c, wd, true]
129134
}), cb)

lib/utils/lifecycle.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,9 @@ function makeEnv (data, prefix, env) {
349349

350350
function cmd (stage) {
351351
function CMD (args, cb) {
352-
if (args.length) {
353-
chain(args.map(function (p) {
354-
return [npm.commands, "run-script", [p, stage]]
355-
}), cb)
356-
} else npm.commands["run-script"]([stage], cb)
352+
npm.commands["run-script"]([stage].concat(args), cb)
357353
}
358-
CMD.usage = "npm "+stage+" <name>"
354+
CMD.usage = "npm "+stage+" [-- <args>]"
359355
var installedShallow = require("./completion/installed-shallow.js")
360356
CMD.completion = function (opts, cb) {
361357
installedShallow(opts, function (d) {

test/run.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var path = require("path")
77
, testdir = __dirname
88
, fs = require("graceful-fs")
99
, npmpkg = path.dirname(testdir)
10-
, npmcli = path.join(__dirname, "bin", "npm-cli.js")
10+
, npmcli = path.resolve(npmpkg, "bin", "npm-cli.js")
1111

1212
var temp = process.env.TMPDIR
1313
|| process.env.TMP
@@ -63,7 +63,7 @@ function prefix (content, pref) {
6363
}
6464

6565
var execCount = 0
66-
function exec (cmd, shouldFail, cb) {
66+
function exec (cmd, cwd, shouldFail, cb) {
6767
if (typeof shouldFail === "function") {
6868
cb = shouldFail, shouldFail = false
6969
}
@@ -81,7 +81,7 @@ function exec (cmd, shouldFail, cb) {
8181
cmd = cmd.replace(/^npm /, npmReplace + " ")
8282
cmd = cmd.replace(/^node /, nodeReplace + " ")
8383

84-
child_process.exec(cmd, {env: env}, function (er, stdout, stderr) {
84+
child_process.exec(cmd, {cwd: cwd, env: env}, function (er, stdout, stderr) {
8585
if (stdout) {
8686
console.error(prefix(stdout, " 1> "))
8787
}
@@ -102,10 +102,8 @@ function exec (cmd, shouldFail, cb) {
102102
}
103103

104104
function execChain (cmds, cb) {
105-
chain(cmds.reduce(function (l, r) {
106-
return l.concat(r)
107-
}, []).map(function (cmd) {
108-
return [exec, cmd]
105+
chain(cmds.map(function (args) {
106+
return [exec].concat(args)
109107
}), cb)
110108
}
111109

@@ -118,9 +116,8 @@ function flatten (arr) {
118116
function setup (cb) {
119117
cleanup(function (er) {
120118
if (er) return cb(er)
121-
execChain([ "node \""+path.resolve(npmpkg, "bin", "npm-cli.js")
122-
+ "\" install \""+npmpkg+"\""
123-
, "npm config set package-config:foo boo"
119+
execChain([ [ "node \""+npmcli+"\" install \""+npmpkg+"\"", root ],
120+
[ "npm config set package-config:foo boo", root ]
124121
], cb)
125122
})
126123
}
@@ -134,6 +131,7 @@ function main (cb) {
134131
failures = 0
135132

136133
process.chdir(testdir)
134+
var base = path.resolve(root, path.join("lib", "node_modules"))
137135

138136
// get the list of packages
139137
var packages = fs.readdirSync(path.resolve(testdir, "packages"))
@@ -150,17 +148,17 @@ function main (cb) {
150148
packagesToRm.push("npm")
151149
}
152150

153-
chain
154-
( [ setup
155-
, [ exec, "npm install "+npmpkg ]
151+
chain(
152+
[ setup
153+
, [ exec, "npm install "+npmpkg, testdir ]
156154
, [ execChain, packages.map(function (p) {
157-
return "npm install packages/"+p
155+
return [ "npm install packages/"+p, testdir ]
158156
}) ]
159157
, [ execChain, packages.map(function (p) {
160-
return "npm test "+p
158+
return [ "npm test", path.resolve(base, p) ]
161159
}) ]
162160
, [ execChain, packagesToRm.map(function (p) {
163-
return "npm rm " + p
161+
return [ "npm rm "+p, root ]
164162
}) ]
165163
, installAndTestEach
166164
]
@@ -171,15 +169,15 @@ function main (cb) {
171169
function installAndTestEach (cb) {
172170
var thingsToChain = [
173171
setup
174-
, [ execChain, packages.map(function (p) {
175-
return [ "npm install packages/"+p
176-
, "npm test "+p
177-
, "npm rm "+p ]
178-
}) ]
172+
, [ execChain, flatten(packages.map(function (p) {
173+
return [ [ "npm install packages/"+p, testdir ]
174+
, [ "npm test", path.resolve(base, p) ]
175+
, [ "npm rm "+p, root ] ]
176+
})) ]
179177
]
180178
if (process.platform !== "win32") {
181179
// Windows can't handle npm rm npm due to file-in-use issues.
182-
thingsToChain.push([exec, "npm rm npm"])
180+
thingsToChain.push([exec, "npm rm npm", testdir])
183181
}
184182

185183
chain(thingsToChain, cb)

test/tap/run-script.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var common = require('../common-tap')
2+
, test = require('tap').test
3+
, path = require('path')
4+
, spawn = require('child_process').spawn
5+
, rimraf = require('rimraf')
6+
, mkdirp = require('mkdirp')
7+
, pkg = __dirname + '/run-script'
8+
, cache = pkg + '/cache'
9+
, tmp = pkg + '/tmp'
10+
, node = process.execPath
11+
, npm = path.resolve(__dirname, '../../cli.js')
12+
, opts = { cwd: pkg }
13+
14+
function testOutput (t, command, er, code, stdout, stderr) {
15+
if (er)
16+
throw er
17+
18+
if (stderr)
19+
throw new Error('npm ' + command + ' stderr: ' + stderr.toString())
20+
21+
stdout = stdout.trim().split('\n')
22+
stdout = stdout[stdout.length - 1]
23+
t.equal(stdout, command)
24+
t.end()
25+
}
26+
27+
function cleanup () {
28+
rimraf.sync(pkg + '/cache')
29+
rimraf.sync(pkg + '/tmp')
30+
}
31+
32+
test('setup', function (t) {
33+
cleanup()
34+
mkdirp.sync(pkg + '/cache')
35+
mkdirp.sync(pkg + '/tmp')
36+
t.end()
37+
})
38+
39+
test('npm run-script', function (t) {
40+
common.npm(['run-script', 'start'], opts, testOutput.bind(null, t, "start"))
41+
})
42+
43+
test('npm run-script with args', function (t) {
44+
common.npm(["run-script", "start", "--", "-e 'console.log(\"stop\")'"], opts, testOutput.bind(null, t, "stop"))
45+
})
46+
47+
test('cleanup', function (t) {
48+
cleanup()
49+
t.end()
50+
})

test/tap/run-script/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{"name":"runscript"
2+
,"version":"1.2.3"
3+
,"scripts":{
4+
"start":"node -e 'console.log(\"start\")'"
5+
}
6+
}

0 commit comments

Comments
 (0)