Skip to content

Commit 75eb42d

Browse files
cleanup: Consolidate code a bit.
1 parent 6320950 commit 75eb42d

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/CommandLineOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function mainCommand(
3838
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false)
3939
.option(
4040
'--yarn-berry-workspaces',
41-
'whether to index all yarn v3 workspaces',
41+
'(deprecated) use --yarn-workspaces instead',
4242
false
4343
)
4444
.option(

src/main.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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,38 +200,18 @@ 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', {
207-
cwd: directory,
208-
encoding: 'utf-8',
209-
maxBuffer: 1024 * 1024 * 5, // 5MB
210-
})
211-
.split('\n')
212-
for (const line of lines) {
213-
if (!line) {
214-
continue
215-
}
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]))
221-
}
222-
}
223-
return result
224-
}
225-
226-
function listYarnWorkspaces(directory: string): string[] {
203+
function listYarnWorkspaces(
204+
directory: string,
205+
yarnVersion: 'tryYarn1' | 'yarn2Plus'
206+
): string[] {
227207
const runYarn = (cmd: string): string =>
228208
child_process.execSync(cmd, {
229209
cwd: directory,
230210
encoding: 'utf-8',
231211
maxBuffer: 1024 * 1024 * 5, // 5MB
232212
})
233213
const result: string[] = []
234-
try {
214+
const yarn1WorkspaceInfo = (): void => {
235215
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
236216
const json = JSON.parse(
237217
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
@@ -245,7 +225,8 @@ function listYarnWorkspaces(directory: string): string[] {
245225
result.push(path.join(directory, json[key][location]))
246226
}
247227
}
248-
} catch {
228+
}
229+
const yarn2PlusWorkspaceInfo = (): void => {
249230
const jsonLines = runYarn('yarn --json workspaces list').split(
250231
/\r?\n|\r|\n/g
251232
)
@@ -262,5 +243,14 @@ function listYarnWorkspaces(directory: string): string[] {
262243
}
263244
}
264245
}
246+
if (yarnVersion === 'tryYarn1') {
247+
try {
248+
yarn1WorkspaceInfo()
249+
} catch {
250+
yarn2PlusWorkspaceInfo()
251+
}
252+
} else {
253+
yarn2PlusWorkspaceInfo()
254+
}
265255
return result
266256
}

0 commit comments

Comments
 (0)