@@ -8,13 +8,22 @@ 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+   ] 
26+ 
1827  static  name  =  'init' 
1928  static  usage  =  [ 
2029    '[--force|-f|--yes|-y|--scope]' , 
@@ -46,11 +55,13 @@ class Init extends BaseCommand {
4655    const  pkg  =  await  rpj ( resolve ( this . npm . localPrefix ,  'package.json' ) ) 
4756    const  wPath  =  filterArg  =>  resolve ( this . npm . localPrefix ,  filterArg ) 
4857
58+     const  workspacesPaths  =  [ ] 
4959    // npm-exec style, runs in the context of each workspace filter 
5060    if  ( args . length )  { 
5161      for  ( const  filterArg  of  filters )  { 
5262        const  path  =  wPath ( filterArg ) 
5363        await  mkdirp ( path ) 
64+         workspacesPaths . push ( path ) 
5465        await  this . execCreate ( {  args,  path } ) 
5566        await  this . setWorkspace ( {  pkg,  workspacePath : path  } ) 
5667      } 
@@ -61,9 +72,13 @@ class Init extends BaseCommand {
6172    for  ( const  filterArg  of  filters )  { 
6273      const  path  =  wPath ( filterArg ) 
6374      await  mkdirp ( path ) 
75+       workspacesPaths . push ( path ) 
6476      await  this . template ( path ) 
6577      await  this . setWorkspace ( {  pkg,  workspacePath : path  } ) 
6678    } 
79+ 
80+     // reify packages once all workspaces have been initialized 
81+     await  this . update ( workspacesPaths ) 
6782  } 
6883
6984  async  execCreate  ( {  args,  path } )  { 
@@ -196,6 +211,34 @@ class Init extends BaseCommand {
196211
197212    await  pkgJson . save ( ) 
198213  } 
214+ 
215+   async  update  ( workspacesPaths )  { 
216+     // translate workspaces paths into an array containing workspaces names 
217+     const  workspaces  =  [ ] 
218+     for  ( const  path  of  workspacesPaths )  { 
219+       const  pkgPath  =  resolve ( path ,  'package.json' ) 
220+       const  {  name }  =  await  rpj ( pkgPath ) 
221+         . catch ( ( )  =>  ( { } ) ) 
222+ 
223+       if  ( name )  { 
224+         workspaces . push ( name ) 
225+       } 
226+     } 
227+ 
228+     const  { 
229+       config, 
230+       flatOptions, 
231+       localPrefix, 
232+     }  =  this . npm 
233+ 
234+     await  updateWorkspaces ( { 
235+       config, 
236+       flatOptions, 
237+       localPrefix, 
238+       npm : this . npm , 
239+       workspaces, 
240+     } ) 
241+   } 
199242} 
200243
201244module . exports  =  Init 
0 commit comments