@@ -3,6 +3,7 @@ const fs = require('fs')
33const parseJSON = require ( 'json-parse-even-better-errors' )
44const rpj = require ( 'read-package-json-fast' )
55const { resolve } = require ( 'path' )
6+ const getWorkspaces = require ( './workspaces/get-workspaces.js' )
67
78const BaseCommand = require ( './base-command.js' )
89class SetScript extends BaseCommand {
@@ -11,6 +12,11 @@ class SetScript extends BaseCommand {
1112 return 'Set tasks in the scripts section of package.json'
1213 }
1314
15+ /* istanbul ignore next - see test/lib/load-all-commands.js */
16+ static get params ( ) {
17+ return [ 'workspace' , 'workspaces' ]
18+ }
19+
1420 /* istanbul ignore next - see test/lib/load-all-commands.js */
1521 static get name ( ) {
1622 return 'set-script'
@@ -31,49 +37,90 @@ class SetScript extends BaseCommand {
3137 }
3238 }
3339
34- exec ( args , cb ) {
35- this . set ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
36- }
37-
38- async set ( args ) {
40+ validate ( args ) {
3941 if ( process . env . npm_lifecycle_event === 'postinstall' )
4042 throw new Error ( 'Scripts can’t set from the postinstall script' )
4143
4244 // Parse arguments
4345 if ( args . length !== 2 )
4446 throw new Error ( `Expected 2 arguments: got ${ args . length } ` )
47+ }
4548
49+ exec ( args , cb ) {
50+ this . set ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
51+ }
52+
53+ async set ( args ) {
54+ this . validate ( args )
55+ const warn = this . setScript ( this . npm . localPrefix , args [ 0 ] , args [ 1 ] )
56+ if ( warn )
57+ log . warn ( 'set-script' , `Script "${ args [ 0 ] } " was overwritten` )
58+ }
59+
60+ execWorkspaces ( args , filters , cb ) {
61+ this . setWorkspaces ( args , filters ) . then ( ( ) => cb ( ) ) . catch ( cb )
62+ }
63+
64+ async setWorkspaces ( args , filters ) {
65+ this . validate ( args )
66+ const workspaces =
67+ await getWorkspaces ( filters , { path : this . npm . localPrefix } )
68+
69+ for ( const [ name , path ] of workspaces ) {
70+ try {
71+ const warn = this . setScript ( path , args [ 0 ] , args [ 1 ] )
72+ if ( warn ) {
73+ log . warn ( 'set-script' , `Script "${ args [ 0 ] } " was overwritten` )
74+ log . warn ( ` in workspace: ${ name } ` )
75+ log . warn ( ` at location: ${ path } ` )
76+ }
77+ } catch ( err ) {
78+ log . error ( 'set-script' , err . message )
79+ log . error ( ` in workspace: ${ name } ` )
80+ log . error ( ` at location: ${ path } ` )
81+ process . exitCode = 1
82+ }
83+ }
84+ }
85+
86+ // returns a Boolean that will be true if
87+ // the requested script was overwritten
88+ // and false if it was set as a new script
89+ setScript ( path , name , value ) {
4690 // Set the script
4791 let manifest
4892 let warn = false
93+
4994 try {
50- manifest = fs . readFileSync ( this . npm . localPrefix + '/ package.json', 'utf-8' )
95+ manifest = fs . readFileSync ( resolve ( path , ' package.json') , 'utf-8' )
5196 } catch ( error ) {
5297 throw new Error ( 'package.json not found' )
5398 }
99+
54100 try {
55101 manifest = parseJSON ( manifest )
56102 } catch ( error ) {
57103 throw new Error ( `Invalid package.json: ${ error } ` )
58104 }
105+
59106 if ( ! manifest . scripts )
60107 manifest . scripts = { }
61- if ( manifest . scripts [ args [ 0 ] ] && manifest . scripts [ args [ 0 ] ] !== args [ 1 ] )
108+
109+ if ( manifest . scripts [ name ] && manifest . scripts [ name ] !== value )
62110 warn = true
63- manifest . scripts [ args [ 0 ] ] = args [ 1 ]
111+ manifest . scripts [ name ] = value
112+
64113 // format content
65- const packageJsonInfo = await rpj ( this . npm . localPrefix + '/package.json' )
66114 const {
67115 [ Symbol . for ( 'indent' ) ] : indent ,
68116 [ Symbol . for ( 'newline' ) ] : newline ,
69- } = packageJsonInfo
70- const format = indent === undefined ? ' ' : indent
71- const eol = newline === undefined ? '\n' : newline
72- const content = ( JSON . stringify ( manifest , null , format ) + '\n' )
73- . replace ( / \n / g, eol )
74- fs . writeFileSync ( this . npm . localPrefix + '/package.json' , content )
75- if ( warn )
76- log . warn ( 'set-script' , `Script "${ args [ 0 ] } " was overwritten` )
117+ } = manifest
118+
119+ const content = ( JSON . stringify ( manifest , null , indent ) + '\n' )
120+ . replace ( / \n / g, newline )
121+ fs . writeFileSync ( resolve ( path , 'package.json' ) , content )
122+
123+ return warn
77124 }
78125}
79126module . exports = SetScript
0 commit comments