1
- const { resolve } = require ( 'path' )
2
1
const runScript = require ( '@npmcli/run-script' )
3
2
const { isServerPackage } = runScript
4
- const rpj = require ( 'read- package-json-fast ' )
3
+ const pkgJson = require ( '@npmcli/ package-json' )
5
4
const log = require ( '../utils/log-shim.js' )
6
5
const didYouMean = require ( '../utils/did-you-mean.js' )
7
6
const { isWindowsShell } = require ( '../utils/is-windows.js' )
@@ -39,9 +38,8 @@ class RunScript extends BaseCommand {
39
38
async completion ( opts ) {
40
39
const argv = opts . conf . argv . remain
41
40
if ( argv . length === 2 ) {
42
- // find the script name
43
- const json = resolve ( this . npm . localPrefix , 'package.json' )
44
- const { scripts = { } } = await rpj ( json ) . catch ( er => ( { } ) )
41
+ const { content : { scripts = { } } } = await pkgJson . normalize ( this . npm . localPrefix )
42
+ . catch ( er => ( { content : { } } ) )
45
43
if ( opts . isFish ) {
46
44
return Object . keys ( scripts ) . map ( s => `${ s } \t${ scripts [ s ] . slice ( 0 , 30 ) } ` )
47
45
}
@@ -70,7 +68,10 @@ class RunScript extends BaseCommand {
70
68
// null value
71
69
const scriptShell = this . npm . config . get ( 'script-shell' ) || undefined
72
70
73
- pkg = pkg || ( await rpj ( `${ path } /package.json` ) )
71
+ if ( ! pkg ) {
72
+ const { content } = await pkgJson . normalize ( path )
73
+ pkg = content
74
+ }
74
75
const { scripts = { } } = pkg
75
76
76
77
if ( event === 'restart' && ! scripts . restart ) {
@@ -126,8 +127,8 @@ class RunScript extends BaseCommand {
126
127
}
127
128
128
129
async list ( args , path ) {
129
- path = path || this . npm . localPrefix
130
- const { scripts, name, _id } = await rpj ( ` ${ path } /package.json` )
130
+ /* eslint-disable-next-line max-len */
131
+ const { content : { scripts, name, _id } } = await pkgJson . normalize ( path || this . npm . localPrefix )
131
132
const pkgid = _id || name
132
133
133
134
if ( ! scripts ) {
@@ -197,7 +198,7 @@ class RunScript extends BaseCommand {
197
198
await this . setWorkspaces ( )
198
199
199
200
for ( const workspacePath of this . workspacePaths ) {
200
- const pkg = await rpj ( ` ${ workspacePath } /package.json` )
201
+ const { content : pkg } = await pkgJson . normalize ( workspacePath )
201
202
const runResult = await this . run ( args , {
202
203
path : workspacePath ,
203
204
pkg,
@@ -236,7 +237,7 @@ class RunScript extends BaseCommand {
236
237
if ( this . npm . config . get ( 'json' ) ) {
237
238
const res = { }
238
239
for ( const workspacePath of this . workspacePaths ) {
239
- const { scripts, name } = await rpj ( ` ${ workspacePath } /package.json` )
240
+ const { content : { scripts, name } } = await pkgJson . normalize ( workspacePath )
240
241
res [ name ] = { ...scripts }
241
242
}
242
243
this . npm . output ( JSON . stringify ( res , null , 2 ) )
@@ -245,7 +246,7 @@ class RunScript extends BaseCommand {
245
246
246
247
if ( this . npm . config . get ( 'parseable' ) ) {
247
248
for ( const workspacePath of this . workspacePaths ) {
248
- const { scripts, name } = await rpj ( ` ${ workspacePath } /package.json` )
249
+ const { content : { scripts, name } } = await pkgJson . normalize ( workspacePath )
249
250
for ( const [ script , cmd ] of Object . entries ( scripts || { } ) ) {
250
251
this . npm . output ( `${ name } :${ script } :${ cmd } ` )
251
252
}
0 commit comments