@@ -8,13 +8,22 @@ const libexec = require('libnpmexec')
8
8
const mapWorkspaces = require ( '@npmcli/map-workspaces' )
9
9
const PackageJson = require ( '@npmcli/package-json' )
10
10
const log = require ( '../utils/log-shim.js' )
11
+ const updateWorkspaces = require ( '../workspaces/update-workspaces.js' )
11
12
12
13
const getLocationMsg = require ( '../exec/get-workspace-location-msg.js' )
13
14
const BaseCommand = require ( '../base-command.js' )
14
15
15
16
class Init extends BaseCommand {
16
17
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
+
18
27
static name = 'init'
19
28
static usage = [
20
29
'[--force|-f|--yes|-y|--scope]' ,
@@ -46,11 +55,13 @@ class Init extends BaseCommand {
46
55
const pkg = await rpj ( resolve ( this . npm . localPrefix , 'package.json' ) )
47
56
const wPath = filterArg => resolve ( this . npm . localPrefix , filterArg )
48
57
58
+ const workspacesPaths = [ ]
49
59
// npm-exec style, runs in the context of each workspace filter
50
60
if ( args . length ) {
51
61
for ( const filterArg of filters ) {
52
62
const path = wPath ( filterArg )
53
63
await mkdirp ( path )
64
+ workspacesPaths . push ( path )
54
65
await this . execCreate ( { args, path } )
55
66
await this . setWorkspace ( { pkg, workspacePath : path } )
56
67
}
@@ -61,9 +72,13 @@ class Init extends BaseCommand {
61
72
for ( const filterArg of filters ) {
62
73
const path = wPath ( filterArg )
63
74
await mkdirp ( path )
75
+ workspacesPaths . push ( path )
64
76
await this . template ( path )
65
77
await this . setWorkspace ( { pkg, workspacePath : path } )
66
78
}
79
+
80
+ // reify packages once all workspaces have been initialized
81
+ await this . update ( workspacesPaths )
67
82
}
68
83
69
84
async execCreate ( { args, path } ) {
@@ -196,6 +211,34 @@ class Init extends BaseCommand {
196
211
197
212
await pkgJson . save ( )
198
213
}
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
+ }
199
242
}
200
243
201
244
module . exports = Init
0 commit comments