@@ -31,9 +31,9 @@ export function indexCommand(
3131 options : MultiProjectOptions
3232) : void {
3333 if ( options . yarnWorkspaces ) {
34- projects . push ( ...listYarnWorkspaces ( options . cwd ) )
34+ projects . push ( ...listYarnWorkspaces ( options . cwd , 'tryYarn1' ) )
3535 } else if ( options . yarnBerryWorkspaces ) {
36- projects . push ( ...listYarnBerryWorkspaces ( options . cwd ) )
36+ projects . push ( ...listYarnWorkspaces ( options . cwd , 'yarn2Plus' ) )
3737 } else if ( projects . length === 0 ) {
3838 projects . push ( options . cwd )
3939 }
@@ -200,51 +200,57 @@ function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions {
200200 return options
201201}
202202
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 , {
207209 cwd : directory ,
208210 encoding : 'utf-8' ,
209211 maxBuffer : 1024 * 1024 * 5 , // 5MB
210212 } )
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+ }
215227 }
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+ }
221244 }
222245 }
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 ( )
247251 }
252+ } else {
253+ yarn2PlusWorkspaceInfo ( )
248254 }
249255 return result
250256}
0 commit comments