@@ -8,13 +8,21 @@ const libexec = require('libnpmexec')
88const mapWorkspaces = require ( '@npmcli/map-workspaces' )
99const PackageJson = require ( '@npmcli/package-json' )
1010const log = require ( '../utils/log-shim.js' )
11+ const updateWorkspaces = require ( '../workspaces/update-workspaces.js' )
1112
1213const getLocationMsg = require ( '../exec/get-workspace-location-msg.js' )
1314const BaseCommand = require ( '../base-command.js' )
1415
1516class Init extends BaseCommand {
1617 static description = 'Create a package.json file'
17- static params = [ 'yes' , 'force' , 'workspace' , 'workspaces' , 'include-workspace-root' ]
18+ static params = [
19+ 'yes' ,
20+ 'force' ,
21+ 'workspace' ,
22+ 'workspaces' ,
23+ 'workspaces-update' ,
24+ 'include-workspace-root' ,
25+ ]
1826 static name = 'init'
1927 static usage = [
2028 '[--force|-f|--yes|-y|--scope]' ,
@@ -46,11 +54,13 @@ class Init extends BaseCommand {
4654 const pkg = await rpj ( resolve ( this . npm . localPrefix , 'package.json' ) )
4755 const wPath = filterArg => resolve ( this . npm . localPrefix , filterArg )
4856
57+ const workspacesPaths = [ ]
4958 // npm-exec style, runs in the context of each workspace filter
5059 if ( args . length ) {
5160 for ( const filterArg of filters ) {
5261 const path = wPath ( filterArg )
5362 await mkdirp ( path )
63+ workspacesPaths . push ( path )
5464 await this . execCreate ( { args, path } )
5565 await this . setWorkspace ( { pkg, workspacePath : path } )
5666 }
@@ -61,9 +71,13 @@ class Init extends BaseCommand {
6171 for ( const filterArg of filters ) {
6272 const path = wPath ( filterArg )
6373 await mkdirp ( path )
74+ workspacesPaths . push ( path )
6475 await this . template ( path )
6576 await this . setWorkspace ( { pkg, workspacePath : path } )
6677 }
78+
79+ // reify packages once all workspaces have been initialized
80+ await this . update ( workspacesPaths )
6781 }
6882
6983 async execCreate ( { args, path } ) {
@@ -196,6 +210,31 @@ class Init extends BaseCommand {
196210
197211 await pkgJson . save ( )
198212 }
213+
214+ async update ( workspacesPaths ) {
215+ // translate workspaces paths into an array containing workspaces names
216+ const workspaces = [ ]
217+ for ( const path of workspacesPaths ) {
218+ const pkgPath = resolve ( path , 'package.json' )
219+ const { name } = await rpj ( pkgPath )
220+ . catch ( ( ) => ( { } ) )
221+ workspaces . push ( name )
222+ }
223+
224+ const {
225+ config,
226+ flatOptions,
227+ localPrefix
228+ } = this . npm
229+
230+ await updateWorkspaces ( {
231+ config,
232+ flatOptions,
233+ localPrefix,
234+ npm : this . npm ,
235+ workspaces,
236+ } )
237+ }
199238}
200239
201240module . exports = Init
0 commit comments