@@ -31,9 +31,9 @@ export function indexCommand(
31
31
options : MultiProjectOptions
32
32
) : void {
33
33
if ( options . yarnWorkspaces ) {
34
- projects . push ( ...listYarnWorkspaces ( options . cwd ) )
34
+ projects . push ( ...listYarnWorkspaces ( options . cwd , 'tryYarn1' ) )
35
35
} else if ( options . yarnBerryWorkspaces ) {
36
- projects . push ( ...listYarnBerryWorkspaces ( options . cwd ) )
36
+ projects . push ( ...listYarnWorkspaces ( options . cwd , 'yarn2Plus' ) )
37
37
} else if ( projects . length === 0 ) {
38
38
projects . push ( options . cwd )
39
39
}
@@ -200,51 +200,57 @@ function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions {
200
200
return options
201
201
}
202
202
203
- function listYarnBerryWorkspaces ( directory : string ) : string [ ] {
204
- const result : string [ ] = [ ]
205
- const lines = child_process
206
- . execSync ( 'yarn workspaces list --json' , {
203
+ function listYarnWorkspaces (
204
+ directory : string ,
205
+ yarnVersion : 'tryYarn1' | 'yarn2Plus'
206
+ ) : string [ ] {
207
+ const runYarn = ( cmd : string ) : string =>
208
+ child_process . execSync ( cmd , {
207
209
cwd : directory ,
208
210
encoding : 'utf-8' ,
209
211
maxBuffer : 1024 * 1024 * 5 , // 5MB
210
212
} )
211
- . split ( '\n' )
212
- for ( const line of lines ) {
213
- if ( ! line ) {
214
- continue
213
+ const result : string [ ] = [ ]
214
+ const yarn1WorkspaceInfo = ( ) : void => {
215
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
216
+ const json = JSON . parse (
217
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
218
+ JSON . parse ( runYarn ( 'yarn --silent --json workspaces info' ) ) . data
219
+ )
220
+ for ( const key of Object . keys ( json ) ) {
221
+ const location = 'location'
222
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
223
+ if ( json [ key ] [ location ] !== undefined ) {
224
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
225
+ result . push ( path . join ( directory , json [ key ] [ location ] ) )
226
+ }
215
227
}
216
- const location = 'location'
217
- const json = JSON . parse ( line )
218
- if ( json [ location ] !== undefined ) {
219
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
220
- result. push ( path . join ( directory , json [ location ] ) )
228
+ }
229
+ const yarn2PlusWorkspaceInfo = ( ) : void => {
230
+ const jsonLines = runYarn ( 'yarn --json workspaces list' ) . split (
231
+ / \r ? \n | \r | \n / g
232
+ )
233
+ for ( let line of jsonLines ) {
234
+ line = line . trim ( )
235
+ if ( line . length === 0 ) {
236
+ continue
237
+ }
238
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
239
+ const json = JSON . parse ( line )
240
+ if ( 'location' in json ) {
241
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
242
+ result . push ( path . join ( directory , json . location ) )
243
+ }
221
244
}
222
245
}
223
- return result
224
- }
225
-
226
- function listYarnWorkspaces ( directory : string ) : string [ ] {
227
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
228
- const json = JSON . parse (
229
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
230
- JSON . parse (
231
- child_process . execSync ( 'yarn --silent --json workspaces info' , {
232
- cwd : directory ,
233
- encoding : 'utf-8' ,
234
- maxBuffer : 1024 * 1024 * 5 , // 5MB
235
- } )
236
- ) . data
237
- )
238
-
239
- const result : string [ ] = [ ]
240
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
241
- for ( const key of Object . keys ( json ) ) {
242
- const location = 'location'
243
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
244
- if ( json [ key ] [ location ] !== undefined ) {
245
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
246
- result . push ( path . join ( directory , json [ key ] [ location ] ) )
246
+ if ( yarnVersion === 'tryYarn1' ) {
247
+ try {
248
+ yarn2PlusWorkspaceInfo ( )
249
+ } catch {
250
+ yarn1WorkspaceInfo ( )
247
251
}
252
+ } else {
253
+ yarn2PlusWorkspaceInfo ( )
248
254
}
249
255
return result
250
256
}
0 commit comments