Skip to content

Commit

Permalink
unit tests for npm run-script
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 21, 2020
1 parent fe07e4a commit ace2b5f
Show file tree
Hide file tree
Showing 2 changed files with 415 additions and 42 deletions.
62 changes: 20 additions & 42 deletions lib/run-script.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,33 @@
module.exports = runScriptCmd

const run = require('@npmcli/run-script')
const npm = require('./npm.js')
const readJson = require('read-package-json-fast')
const { resolve, join } = require('path')
const output = require('./utils/output.js')
const log = require('npmlog')
const usage = require('./utils/usage')
const usageUtil = require('./utils/usage')
const didYouMean = require('./utils/did-you-mean')
const isWindowsShell = require('./utils/is-windows-shell.js')

runScriptCmd.usage = usage(
const usage = usageUtil(
'run-script',
'npm run-script <command> [-- <args>...]'
'npm run-script <command> [-- <args>]'
)

runScriptCmd.completion = function (opts, cb) {
// see if there's already a package specified.
var argv = opts.conf.argv.remain

if (argv.length >= 4) return cb()

if (argv.length === 3) {
// either specified a script locally, in which case, done,
// or a package, in which case, complete against its scripts
var json = join(npm.localPrefix, 'package.json')
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 || {})
if (scripts.indexOf(argv[2]) !== -1) return cb()
// ok, try to find out which package it was, then
var pref = npm.config.get('global') ? npm.config.get('prefix')
: npm.localPrefix
var pkgDir = resolve(pref, 'node_modules', argv[2], 'package.json')
readJson(pkgDir, function (er, d) {
if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
if (er) d = {}
var scripts = Object.keys(d.scripts || {})
return cb(null, scripts)
})
})
const completion = async (opts, cb) => {
const argv = opts.conf.argv.remain
if (argv.length === 2) {
// find the script name
const json = resolve(npm.localPrefix, 'package.json')
const { scripts = {} } = await readJson(json).catch(er => ({}))
return cb(null, Object.keys(scripts))
}

readJson(join(npm.localPrefix, 'package.json'), function (er, d) {
if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
d = d || {}
cb(null, Object.keys(d.scripts || {}))
})
// otherwise nothing to do, just let the system handle it
return cb()
}

function runScriptCmd (args, cb) {
const cmd = (args, cb) => {
const fn = args.length ? runScript : list
fn(args).then(() => cb()).catch(cb)
return fn(args).then(() => cb()).catch(cb)
}

const runScript = async (args) => {
Expand All @@ -64,10 +39,11 @@ const runScript = async (args) => {
const { _id, scripts = {} } = pkg

if (event === 'restart' && !scripts.restart) {
scripts.restart = 'npm stop && npm start'
scripts.restart = 'npm stop --if-present && npm start'
} else if (event === 'env') {
scripts.env = isWindowsShell ? 'SET' : 'env'
}
pkg.scripts = scripts

if (!scripts[event]) {
if (npm.config.get('if-present')) {
Expand Down Expand Up @@ -117,13 +93,13 @@ const list = async () => {
'start',
'restart',
'version'
].reduce((l, p) => l.concat(['pre' + p, p, 'post' + p]))
].reduce((l, p) => l.concat(['pre' + p, p, 'post' + p]), [])

if (!scripts) {
return []
}

const allScripts = scripts ? Object.keys(scripts) : []
const allScripts = Object.keys(scripts)
if (log.level === 'silent') {
return allScripts
}
Expand Down Expand Up @@ -165,3 +141,5 @@ const list = async () => {
}
return allScripts
}

module.exports = Object.assign(cmd, { completion, usage })
Loading

0 comments on commit ace2b5f

Please sign in to comment.