UNPKG

3.15 kBJavaScriptView Raw
1
2module.exports = runScript
3
4var lifecycle = require("./utils/lifecycle.js")
5 , npm = require("./npm.js")
6 , path = require("path")
7 , readJson = require("read-package-json")
8 , log = require("npmlog")
9 , chain = require("slide").chain
10 , fs = require("graceful-fs")
11 , asyncMap = require("slide").asyncMap
12
13runScript.usage = "npm run-script [<pkg>] <command>"
14
15runScript.completion = function (opts, cb) {
16
17 // see if there's already a package specified.
18 var argv = opts.conf.argv.remain
19 , installedShallow = require("./utils/completion/installed-shallow.js")
20
21 if (argv.length >= 4) return cb()
22
23 if (argv.length === 3) {
24 // either specified a script locally, in which case, done,
25 // or a package, in which case, complete against its scripts
26 var json = path.join(npm.prefix, "package.json")
27 return readJson(json, function (er, d) {
28 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
29 if (er) d = {}
30 var scripts = Object.keys(d.scripts || {})
31 console.error("local scripts", scripts)
32 if (scripts.indexOf(argv[2]) !== -1) return cb()
33 // ok, try to find out which package it was, then
34 var pref = npm.config.get("global") ? npm.config.get("prefix")
35 : npm.prefix
36 var pkgDir = path.resolve( pref, "node_modules"
37 , argv[2], "package.json" )
38 readJson(pkgDir, function (er, d) {
39 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
40 if (er) d = {}
41 var scripts = Object.keys(d.scripts || {})
42 return cb(null, scripts)
43 })
44 })
45 }
46
47 // complete against the installed-shallow, and the pwd's scripts.
48 // but only packages that have scripts
49 var installed
50 , scripts
51 installedShallow(opts, function (d) {
52 return d.scripts
53 }, function (er, inst) {
54 installed = inst
55 next()
56 })
57
58 if (npm.config.get("global")) scripts = [], next()
59 else readJson(path.join(npm.prefix, "package.json"), function (er, d) {
60 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
61 d = d || {}
62 scripts = Object.keys(d.scripts || {})
63 next()
64 })
65
66 function next () {
67 if (!installed || !scripts) return
68 return cb(null, scripts.concat(installed))
69 }
70}
71
72function runScript (args, cb) {
73 if (!args.length) return cb(runScript.usage)
74 var pkgdir = args.length === 1 ? process.cwd()
75 : path.resolve(npm.dir, args[0])
76 , cmd = args.pop()
77
78 readJson(path.resolve(pkgdir, "package.json"), function (er, d) {
79 if (er) return cb(er)
80 run(d, pkgdir, cmd, cb)
81 })
82}
83
84function run (pkg, wd, cmd, cb) {
85 var cmds = []
86 if (!pkg.scripts) pkg.scripts = {}
87 if (cmd === "restart") {
88 cmds = ["prestop","stop","poststop"
89 ,"restart"
90 ,"prestart","start","poststart"]
91 } else {
92 cmds = [cmd]
93 }
94 if (!cmd.match(/^(pre|post)/)) {
95 cmds = ["pre"+cmd].concat(cmds).concat("post"+cmd)
96 }
97 log.verbose("run-script", cmds)
98 chain(cmds.map(function (c) {
99 // when running scripts explicitly, assume that they're trusted.
100 return [lifecycle, pkg, c, wd, true]
101 }), cb)
102}