Skip to content

Commit 9f5d08a

Browse files
fix: Fix --yarn-workspace with Yarn 3. (#181)
1 parent 5c08bb9 commit 9f5d08a

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ Navigate to the project root, containing `package.json`.
4040
```sh
4141
yarn install
4242

43-
scip-typescript index --yarn-workspaces # For Yarn v2
44-
scip-typescript index --yarn-berry-workspaces # For Yarn v3 (Berry)
43+
scip-typescript index --yarn-workspaces
4544
```
4645

4746
### Indexing in CI

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: 45 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,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

Comments
 (0)