Skip to content

Commit 66d5d20

Browse files
cleanup: Consolidate code a bit.
1 parent 6320950 commit 66d5d20

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
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: 26 additions & 39 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,52 +200,30 @@ 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(directory: string, yarnVersion: 'tryYarn1' | 'yarn2Plus'): string[] {
227204
const runYarn = (cmd: string): string =>
228205
child_process.execSync(cmd, {
229206
cwd: directory,
230207
encoding: 'utf-8',
231208
maxBuffer: 1024 * 1024 * 5, // 5MB
232209
})
233210
const result: string[] = []
234-
try {
235-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
236-
const json = JSON.parse(
237-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
238-
JSON.parse(runYarn('yarn --silent --json workspaces info')).data
239-
)
240-
for (const key of Object.keys(json)) {
241-
const location = 'location'
242-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
243-
if (json[key][location] !== undefined) {
211+
const yarn1WorkspaceInfo = (): void => {
212+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
213+
const json = JSON.parse(
244214
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
245-
result.push(path.join(directory, json[key][location]))
215+
JSON.parse(runYarn('yarn --silent --json workspaces info')).data
216+
)
217+
for (const key of Object.keys(json)) {
218+
const location = 'location'
219+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
220+
if (json[key][location] !== undefined) {
221+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
222+
result.push(path.join(directory, json[key][location]))
223+
}
246224
}
247-
}
248-
} catch {
225+
};
226+
const yarn2PlusWorkspaceInfo = (): void => {
249227
const jsonLines = runYarn('yarn --json workspaces list').split(
250228
/\r?\n|\r|\n/g
251229
)
@@ -262,5 +240,14 @@ function listYarnWorkspaces(directory: string): string[] {
262240
}
263241
}
264242
}
243+
if (yarnVersion === 'tryYarn1') {
244+
try {
245+
yarn1WorkspaceInfo()
246+
} catch {
247+
yarn2PlusWorkspaceInfo()
248+
}
249+
} else {
250+
yarn2PlusWorkspaceInfo()
251+
}
265252
return result
266253
}

0 commit comments

Comments
 (0)