Skip to content

Commit 6c09206

Browse files
authored
Add support for Yarn v3 (#168)
1 parent 1e956b9 commit 6c09206

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/CommandLineOptions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface MultiProjectOptions {
99
inferTsconfig: boolean
1010
progressBar: boolean
1111
yarnWorkspaces: boolean
12+
yarnBerryWorkspaces: boolean
1213
cwd: string
1314
output: string
1415
indexedProjects: Set<string>
@@ -35,6 +36,11 @@ export function mainCommand(
3536
.command('index')
3637
.option('--cwd <path>', 'the working directory', process.cwd())
3738
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false)
39+
.option(
40+
'--yarn-berry-workspaces',
41+
'whether to index all yarn v3 workspaces',
42+
false
43+
)
3844
.option(
3945
'--infer-tsconfig',
4046
"whether to infer the tsconfig.json file, if it's missing",

src/main.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ for (const snapshotDirectory of snapshotDirectories) {
5151
inferTsconfig,
5252
output,
5353
yarnWorkspaces: Boolean(packageJson.workspaces),
54+
yarnBerryWorkspaces: false,
5455
progressBar: false,
5556
indexedProjects: new Set(),
5657
})

src/main.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export function indexCommand(
3232
): void {
3333
if (options.yarnWorkspaces) {
3434
projects.push(...listYarnWorkspaces(options.cwd))
35+
} else if (options.yarnBerryWorkspaces) {
36+
projects.push(...listYarnBerryWorkspaces(options.cwd))
3537
} else if (projects.length === 0) {
3638
projects.push(options.cwd)
3739
}
@@ -198,6 +200,28 @@ function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions {
198200
return options
199201
}
200202

203+
function listYarnBerryWorkspaces(directory: string): string[] {
204+
const result: string[] = []
205+
const lines = child_process
206+
.execSync('yarn workspaces list --json', {
207+
cwd: directory,
208+
encoding: 'utf-8',
209+
})
210+
.split('\n')
211+
for (const line of lines) {
212+
if (!line) {
213+
continue
214+
}
215+
const location = 'location'
216+
const json = JSON.parse(line)
217+
if (json[location] !== undefined) {
218+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
219+
result.push(path.join(directory, json[location]))
220+
}
221+
}
222+
return result
223+
}
224+
201225
function listYarnWorkspaces(directory: string): string[] {
202226
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
203227
const json = JSON.parse(

0 commit comments

Comments
 (0)