1- module . exports = runScriptCmd
2-
31const run = require ( '@npmcli/run-script' )
42const npm = require ( './npm.js' )
53const readJson = require ( 'read-package-json-fast' )
64const { resolve, join } = require ( 'path' )
75const output = require ( './utils/output.js' )
86const log = require ( 'npmlog' )
9- const usage = require ( './utils/usage' )
7+ const usageUtil = require ( './utils/usage' )
108const didYouMean = require ( './utils/did-you-mean' )
119const isWindowsShell = require ( './utils/is-windows-shell.js' )
1210
13- runScriptCmd . usage = usage (
11+ const usage = usageUtil (
1412 'run-script' ,
15- 'npm run-script <command> [-- <args>... ]'
13+ 'npm run-script <command> [-- <args>]'
1614)
1715
18- runScriptCmd . completion = function ( opts , cb ) {
19- // see if there's already a package specified.
20- var argv = opts . conf . argv . remain
21-
22- if ( argv . length >= 4 ) return cb ( )
23-
24- if ( argv . length === 3 ) {
25- // either specified a script locally, in which case, done,
26- // or a package, in which case, complete against its scripts
27- var json = join ( npm . localPrefix , 'package.json' )
28- return readJson ( json , function ( er , d ) {
29- if ( er && er . code !== 'ENOENT' && er . code !== 'ENOTDIR' ) return cb ( er )
30- if ( er ) d = { }
31- var scripts = Object . keys ( d . 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 . localPrefix
36- var pkgDir = resolve ( pref , 'node_modules' , argv [ 2 ] , 'package.json' )
37- readJson ( pkgDir , function ( er , d ) {
38- if ( er && er . code !== 'ENOENT' && er . code !== 'ENOTDIR' ) return cb ( er )
39- if ( er ) d = { }
40- var scripts = Object . keys ( d . scripts || { } )
41- return cb ( null , scripts )
42- } )
43- } )
16+ const completion = async ( opts , cb ) => {
17+ const argv = opts . conf . argv . remain
18+ if ( argv . length === 2 ) {
19+ // find the script name
20+ const json = resolve ( npm . localPrefix , 'package.json' )
21+ const { scripts = { } } = await readJson ( json ) . catch ( er => ( { } ) )
22+ return cb ( null , Object . keys ( scripts ) )
4423 }
45-
46- readJson ( join ( npm . localPrefix , 'package.json' ) , function ( er , d ) {
47- if ( er && er . code !== 'ENOENT' && er . code !== 'ENOTDIR' ) return cb ( er )
48- d = d || { }
49- cb ( null , Object . keys ( d . scripts || { } ) )
50- } )
24+ // otherwise nothing to do, just let the system handle it
25+ return cb ( )
5126}
5227
53- function runScriptCmd ( args , cb ) {
28+ const cmd = ( args , cb ) => {
5429 const fn = args . length ? runScript : list
55- fn ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
30+ return fn ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
5631}
5732
5833const runScript = async ( args ) => {
@@ -64,10 +39,11 @@ const runScript = async (args) => {
6439 const { _id, scripts = { } } = pkg
6540
6641 if ( event === 'restart' && ! scripts . restart ) {
67- scripts . restart = 'npm stop && npm start'
42+ scripts . restart = 'npm stop --if-present && npm start'
6843 } else if ( event === 'env' ) {
6944 scripts . env = isWindowsShell ? 'SET' : 'env'
7045 }
46+ pkg . scripts = scripts
7147
7248 if ( ! scripts [ event ] ) {
7349 if ( npm . config . get ( 'if-present' ) ) {
@@ -117,13 +93,13 @@ const list = async () => {
11793 'start' ,
11894 'restart' ,
11995 'version'
120- ] . reduce ( ( l , p ) => l . concat ( [ 'pre' + p , p , 'post' + p ] ) )
96+ ] . reduce ( ( l , p ) => l . concat ( [ 'pre' + p , p , 'post' + p ] ) , [ ] )
12197
12298 if ( ! scripts ) {
12399 return [ ]
124100 }
125101
126- const allScripts = scripts ? Object . keys ( scripts ) : [ ]
102+ const allScripts = Object . keys ( scripts )
127103 if ( log . level === 'silent' ) {
128104 return allScripts
129105 }
@@ -165,3 +141,5 @@ const list = async () => {
165141 }
166142 return allScripts
167143}
144+
145+ module . exports = Object . assign ( cmd , { completion, usage } )
0 commit comments