diff --git a/src/CommandLineOptions.ts b/src/CommandLineOptions.ts index f9ec50b9..d57e2db6 100644 --- a/src/CommandLineOptions.ts +++ b/src/CommandLineOptions.ts @@ -9,6 +9,7 @@ export interface MultiProjectOptions { inferTsconfig: boolean progressBar: boolean yarnWorkspaces: boolean + yarnBerryWorkspaces: boolean cwd: string output: string indexedProjects: Set @@ -35,6 +36,11 @@ export function mainCommand( .command('index') .option('--cwd ', 'the working directory', process.cwd()) .option('--yarn-workspaces', 'whether to index all yarn workspaces', false) + .option( + '--yarn-berry-workspaces', + 'whether to index all yarn v3 workspaces', + false + ) .option( '--infer-tsconfig', "whether to infer the tsconfig.json file, if it's missing", diff --git a/src/main.test.ts b/src/main.test.ts index ad183276..c0a2a528 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -51,6 +51,7 @@ for (const snapshotDirectory of snapshotDirectories) { inferTsconfig, output, yarnWorkspaces: Boolean(packageJson.workspaces), + yarnBerryWorkspaces: false, progressBar: false, indexedProjects: new Set(), }) diff --git a/src/main.ts b/src/main.ts index 4a37248f..50bbe8e4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,6 +32,8 @@ export function indexCommand( ): void { if (options.yarnWorkspaces) { projects.push(...listYarnWorkspaces(options.cwd)) + } else if (options.yarnBerryWorkspaces) { + projects.push(...listYarnBerryWorkspaces(options.cwd)) } else if (projects.length === 0) { projects.push(options.cwd) } @@ -198,6 +200,28 @@ function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions { return options } +function listYarnBerryWorkspaces(directory: string): string[] { + const result: string[] = [] + const lines = child_process + .execSync('yarn workspaces list --json', { + cwd: directory, + encoding: 'utf-8', + }) + .split('\n') + for (const line of lines) { + if (!line) { + continue + } + const location = 'location' + const json = JSON.parse(line) + if (json[location] !== undefined) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access + result.push(path.join(directory, json[location])) + } + } + return result +} + function listYarnWorkspaces(directory: string): string[] { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const json = JSON.parse(