Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ Navigate to the project root, containing `package.json`.
```sh
yarn install

scip-typescript index --yarn-workspaces # For Yarn v2
scip-typescript index --yarn-berry-workspaces # For Yarn v3 (Berry)
scip-typescript index --yarn-workspaces
```

### Indexing in CI
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function mainCommand(
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false)
.option(
'--yarn-berry-workspaces',
'whether to index all yarn v3 workspaces',
'(deprecated) use --yarn-workspaces instead',
false
)
.option(
Expand Down
84 changes: 45 additions & 39 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export function indexCommand(
options: MultiProjectOptions
): void {
if (options.yarnWorkspaces) {
projects.push(...listYarnWorkspaces(options.cwd))
projects.push(...listYarnWorkspaces(options.cwd, 'tryYarn1'))
} else if (options.yarnBerryWorkspaces) {
projects.push(...listYarnBerryWorkspaces(options.cwd))
projects.push(...listYarnWorkspaces(options.cwd, 'yarn2Plus'))
} else if (projects.length === 0) {
projects.push(options.cwd)
}
Expand Down Expand Up @@ -200,51 +200,57 @@ 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', {
function listYarnWorkspaces(
directory: string,
yarnVersion: 'tryYarn1' | 'yarn2Plus'
): string[] {
const runYarn = (cmd: string): string =>
child_process.execSync(cmd, {
cwd: directory,
encoding: 'utf-8',
maxBuffer: 1024 * 1024 * 5, // 5MB
})
.split('\n')
for (const line of lines) {
if (!line) {
continue
const result: string[] = []
const yarn1WorkspaceInfo = (): void => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = JSON.parse(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
JSON.parse(runYarn('yarn --silent --json workspaces info')).data
)
for (const key of Object.keys(json)) {
const location = 'location'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (json[key][location] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
result.push(path.join(directory, json[key][location]))
}
}
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]))
}
const yarn2PlusWorkspaceInfo = (): void => {
const jsonLines = runYarn('yarn --json workspaces list').split(
/\r?\n|\r|\n/g
)
for (let line of jsonLines) {
line = line.trim()
if (line.length === 0) {
continue
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = JSON.parse(line)
if ('location' in json) {
// eslint-disable-next-line @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(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
JSON.parse(
child_process.execSync('yarn --silent --json workspaces info', {
cwd: directory,
encoding: 'utf-8',
maxBuffer: 1024 * 1024 * 5, // 5MB
})
).data
)

const result: string[] = []
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
for (const key of Object.keys(json)) {
const location = 'location'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (json[key][location] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
result.push(path.join(directory, json[key][location]))
if (yarnVersion === 'tryYarn1') {
try {
yarn2PlusWorkspaceInfo()
} catch {
yarn1WorkspaceInfo()
}
} else {
yarn2PlusWorkspaceInfo()
}
return result
}