@@ -10,7 +10,6 @@ function composePath(componentRootFolder: string) {
10
10
return path . join ( componentRootFolder , PACKAGE_JSON ) ;
11
11
}
12
12
function convertComponentsIdToValidPackageName ( registryPrefix : string , id : string ) : string {
13
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
14
13
return `${ registryPrefix } /${ id . replace ( / \/ / g, '.' ) } ` ;
15
14
}
16
15
function convertComponentsToValidPackageNames (
@@ -21,7 +20,6 @@ function convertComponentsToValidPackageNames(
21
20
if ( R . isEmpty ( bitDependencies ) || R . isNil ( bitDependencies ) ) return obj ;
22
21
Object . keys ( bitDependencies ) . forEach ( key => {
23
22
const name = convertComponentsIdToValidPackageName ( registryPrefix , key ) ;
24
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
25
23
obj [ name ] = bitDependencies [ key ] ;
26
24
} ) ;
27
25
return obj ;
@@ -51,7 +49,7 @@ export type PackageJsonProps = {
51
49
peerDependencies ?: Record < string , any > ;
52
50
license ?: string ;
53
51
scripts ?: Record < string , any > ;
54
- workspaces : string [ ] ;
52
+ workspaces ? : string [ ] ;
55
53
private ?: boolean ;
56
54
} ;
57
55
@@ -129,8 +127,7 @@ export default class PackageJson {
129
127
) ;
130
128
}
131
129
132
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
133
- static hasExisting ( componentRootFolder : string , throws ? = false ) : boolean {
130
+ static hasExisting ( componentRootFolder : string , throws = false ) : boolean {
134
131
const packageJsonPath = composePath ( componentRootFolder ) ;
135
132
const exists = fs . pathExistsSync ( packageJsonPath ) ;
136
133
if ( ! exists && throws ) {
@@ -148,7 +145,6 @@ export default class PackageJson {
148
145
}
149
146
150
147
static create ( componentRootFolder : string ) : PackageJson {
151
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
152
148
return new PackageJson ( componentRootFolder , { } ) ;
153
149
}
154
150
@@ -157,7 +153,6 @@ export default class PackageJson {
157
153
}
158
154
159
155
static fromPlainObject ( componentRootFolder : string , object : Record < string , any > ) {
160
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
161
156
return new PackageJson ( componentRootFolder , object ) ;
162
157
}
163
158
@@ -242,8 +237,11 @@ export default class PackageJson {
242
237
* Also, in case there is no package.json file in this project, it generates a new one with only the 'dependencies'
243
238
* attribute. Nothing more, nothing less.
244
239
*/
245
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
246
- static async addComponentsIntoExistingPackageJson ( rootDir : string , components : Array , registryPrefix : string ) {
240
+ static async addComponentsIntoExistingPackageJson (
241
+ rootDir : string ,
242
+ components : Record < string , any > ,
243
+ registryPrefix : string
244
+ ) {
247
245
const packageJson = ( await PackageJson . getPackageJson ( rootDir ) ) || { dependencies : { } } ;
248
246
packageJson . dependencies = Object . assign (
249
247
{ } ,
@@ -266,11 +264,12 @@ export default class PackageJson {
266
264
customImportPath : string | null | undefined
267
265
) {
268
266
const pkg = ( await PackageJson . getPackageJson ( rootDir ) ) || { } ;
269
- const workSpaces = pkg . workspaces || [ ] ;
267
+ const workSpaces = PackageJson . extractWorkspacesPackages ( pkg ) || [ ] ;
270
268
workSpaces . push ( dependenciesDirectory ) ;
271
269
workSpaces . push ( componentsDefaultDirectory ) ;
272
270
if ( customImportPath ) workSpaces . push ( customImportPath ) ;
273
- pkg . workspaces = R . uniq ( workSpaces ) ;
271
+ if ( ! pkg . workspaces ) pkg . workspaces = [ ] ;
272
+ this . updateWorkspacesPackages ( pkg , R . uniq ( workSpaces ) ) ;
274
273
pkg . private = ! ! pkg . workspaces ;
275
274
await PackageJson . saveRawObject ( rootDir , pkg ) ;
276
275
}
@@ -280,8 +279,10 @@ export default class PackageJson {
280
279
*/
281
280
static async removeComponentsFromWorkspaces ( rootDir : string , pathsTOoRemove : string [ ] ) {
282
281
const pkg = ( await PackageJson . getPackageJson ( rootDir ) ) || { } ;
283
- const workSpaces = pkg . workspaces || [ ] ;
284
- pkg . workspaces = workSpaces . filter ( folder => ! pathsTOoRemove . includes ( folder ) ) ;
282
+ const workspaces = this . extractWorkspacesPackages ( pkg ) ;
283
+ if ( ! workspaces ) return ;
284
+ const updatedWorkspaces = workspaces . filter ( folder => ! pathsTOoRemove . includes ( folder ) ) ;
285
+ this . updateWorkspacesPackages ( pkg , updatedWorkspaces ) ;
285
286
await PackageJson . saveRawObject ( rootDir , pkg ) ;
286
287
}
287
288
@@ -297,4 +298,44 @@ export default class PackageJson {
297
298
await PackageJson . saveRawObject ( rootDir , pkg ) ;
298
299
}
299
300
}
301
+
302
+ static extractWorkspacesPackages ( packageJson : { [ k : string ] : any } ) : string [ ] | null {
303
+ if ( ! packageJson . workspaces ) return null ;
304
+ this . throwForInvalidWorkspacesConfig ( packageJson ) ;
305
+ if ( Array . isArray ( packageJson . workspaces ) ) {
306
+ return packageJson . workspaces ;
307
+ }
308
+ if ( Array . isArray ( packageJson . workspaces . packages ) ) {
309
+ return packageJson . workspaces . packages ;
310
+ }
311
+ return null ;
312
+ }
313
+
314
+ static updateWorkspacesPackages ( packageJson , workspacesPackages ) : void {
315
+ if ( ! packageJson . workspaces ) return ;
316
+ this . throwForInvalidWorkspacesConfig ( packageJson ) ;
317
+ if ( Array . isArray ( packageJson . workspaces ) ) {
318
+ packageJson . workspaces = workspacesPackages ;
319
+ }
320
+ if ( Array . isArray ( packageJson . workspaces . packages ) ) {
321
+ packageJson . workspaces . packages = workspacesPackages ;
322
+ }
323
+ }
324
+
325
+ /**
326
+ * according to Yarn Git repo, the workspaces type configured as the following
327
+ * `workspaces?: Array<string> | WorkspacesConfig`
328
+ * and `WorkspacesConfig` is:
329
+ * `export type WorkspacesConfig = { packages?: Array<string>, nohoist?: Array<string> };`
330
+ * see https://github.com/yarnpkg/yarn/blob/master/src/types.js
331
+ */
332
+ static throwForInvalidWorkspacesConfig ( packageJson ) {
333
+ if ( ! packageJson . workspaces ) return ;
334
+ if (
335
+ typeof packageJson . workspaces !== 'object' ||
336
+ ( ! Array . isArray ( packageJson . workspaces ) && ! Array . isArray ( packageJson . workspaces . packages ) )
337
+ ) {
338
+ throw new Error ( 'workspaces property does not have the correct format, please refer to Yarn documentation' ) ;
339
+ }
340
+ }
300
341
}
0 commit comments