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

Split scripts list in scripts or run-scripts #7464

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 34 additions & 10 deletions lib/run-script.js
Expand Up @@ -84,31 +84,55 @@ function runScript (args, cb) {

function list(cb) {
var json = path.join(npm.localPrefix, "package.json")
var cmdList = [ "publish", "install", "uninstall"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For bonus points, something that abstracted this list of lifecycle scripts out into a separate function would be useful, instead of having this scattered around in a few different places.

, "test", "stop", "start", "restart"
].reduce(function (l, p) {
return l.concat(["pre" + p, p, "post" + p])
}, [])
return readJson(json, function(er, d) {
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
if (er) d = {}
var scripts = Object.keys(d.scripts || {})
var allScripts = Object.keys(d.scripts || {})
var scripts = []
var runScripts = []
allScripts.forEach(function (script) {
if (cmdList.indexOf(script) !== -1) scripts.push(script)
else runScripts.push(script)
})

if (log.level === "silent") {
return cb(null, scripts)
return cb(null, allScripts)
}

if (npm.config.get("json")) {
console.log(JSON.stringify(d.scripts || {}, null, 2))
return cb(null, scripts)
return cb(null, allScripts)
}

var s = ":"
var prefix = ""
if (!npm.config.get("parseable")) {
s = "\n "
prefix = " "
console.log("Available scripts in the %s package:", d.name)
if (npm.config.get("parseable")) {
allScripts.forEach(function(script) {
console.log(script + ":" + d.scripts[script])
})
return cb(null, allScripts)
}

var s = "\n "
var prefix = " "
var header = "Available scripts in the %s package"
if (scripts.length) console.log(header + ":", d.name)
scripts.forEach(function(script) {
console.log(prefix + script + s + d.scripts[script])
})
return cb(null, scripts)
if (!scripts.length && runScripts.length) {
console.log(header + " with run-scripts:", d.name)
}
else if (runScripts.length) {
console.log("\nwith run-scripts:")
}
runScripts.forEach(function(script) {
console.log(prefix + script + s + d.scripts[script])
})
return cb(null, allScripts)
})
}

Expand Down