Skip to content
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ pnpm install
scip-typescript index --pnpm-workspaces
```

### Index a TypeScript project using npm workspaces

Navigate to the project root, containing `package.json` with a `workspaces` field.

```sh
npm install

scip-typescript index --npm-workspaces
```

### Indexing in CI

Add the following run steps to your CI pipeline:
Expand Down
16 changes: 16 additions & 0 deletions snapshots/input/npm-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "npm-workspaces",
"version": "1.0.0",
"description": "Example TS/JS project",
"main": "src/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"private": true,
"packageManager": "npm@10.0.0",
"workspaces": [
"packages/*"
]
}
11 changes: 11 additions & 0 deletions snapshots/input/npm-workspaces/packages/a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@example/a",
"version": "1.0.0",
"description": "Example TS/JS project",
"main": "src/a.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
3 changes: 3 additions & 0 deletions snapshots/input/npm-workspaces/packages/a/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function a(): string {
return ''
}
9 changes: 9 additions & 0 deletions snapshots/input/npm-workspaces/packages/a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"baseUrl": ".",
"outDir": "dist"
},
"include": ["src/*"]
}
14 changes: 14 additions & 0 deletions snapshots/input/npm-workspaces/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@example/b",
"version": "1.0.0",
"description": "Example TS/JS project",
"main": "src/b.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@example/a": "1.0.0"
}
}
5 changes: 5 additions & 0 deletions snapshots/input/npm-workspaces/packages/b/src/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { a } from '@example/a'

export function b() {
return a()
}
14 changes: 14 additions & 0 deletions snapshots/input/npm-workspaces/packages/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"baseUrl": "./src",
"sourceRoot": "src",
"outDir": "dist",
"paths": {
"@example/a": ["../../a/src/a.ts"]
}
},
"include": ["src/*"],
"references": [{ "path": "../a" }]
}
24 changes: 24 additions & 0 deletions snapshots/input/npm-workspaces/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": "@sourcegraph/tsconfig",
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"allowJs": false,
"moduleResolution": "node",
"esModuleInterop": true,
"lib": ["esnext", "dom", "dom.iterable"],
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"noErrorTruncation": true,
"importHelpers": true,
"resolveJsonModule": true,
"composite": true,
"outDir": "out",
"rootDir": "."
},
"include": [],
"exclude": ["out", "node_modules", "dist"]
}
8 changes: 8 additions & 0 deletions snapshots/output/npm-workspaces/packages/a/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// language TypeScript
// < definition @example/a 1.0.0 src/`a.ts`/

export function a(): string {
// ^ definition @example/a 1.0.0 src/`a.ts`/a().
return ''
}

13 changes: 13 additions & 0 deletions snapshots/output/npm-workspaces/packages/b/src/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// language TypeScript
// < definition @example/b 1.0.0 src/`b.ts`/

import { a } from '@example/a'
// ^ reference @example/a 1.0.0 src/`a.ts`/a().
// ^^^^^^^^^^^^ reference @example/a 1.0.0 src/`a.ts`/

export function b() {
// ^ definition @example/b 1.0.0 src/`b.ts`/b().
return a()
// ^ reference @example/a 1.0.0 src/`a.ts`/a().
}

1 change: 1 addition & 0 deletions src/CommandLineOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ checkIndexParser([], {
checkIndexParser(['--cwd', 'qux'], { cwd: 'qux' })
checkIndexParser(['--yarn-workspaces'], { yarnWorkspaces: true })
checkIndexParser(['--pnpm-workspaces'], { pnpmWorkspaces: true })
checkIndexParser(['--npm-workspaces'], { npmWorkspaces: true })
checkIndexParser(['--infer-tsconfig'], { inferTsconfig: true })
checkIndexParser(['--no-progress-bar'], { progressBar: false })
checkIndexParser(['--progress-bar'], { progressBar: true })
Expand Down
2 changes: 2 additions & 0 deletions src/CommandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface MultiProjectOptions {
yarnWorkspaces: boolean
yarnBerryWorkspaces: boolean
pnpmWorkspaces: boolean
npmWorkspaces: boolean
globalCaches: boolean
maxFileByteSize?: string
maxFileByteSizeNumber?: number
Expand Down Expand Up @@ -54,6 +55,7 @@ export function mainCommand(
.command('index')
.option('--cwd <path>', 'the working directory', process.cwd())
.option('--pnpm-workspaces', 'whether to index all pnpm workspaces', false)
.option('--npm-workspaces', 'whether to index all npm workspaces', false)
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false)
.option(
'--yarn-berry-workspaces',
Expand Down
2 changes: 2 additions & 0 deletions src/FileIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('local prototype owners stay out of the global symbol table', () => {
yarnWorkspaces: false,
yarnBerryWorkspaces: false,
pnpmWorkspaces: false,
npmWorkspaces: false,
globalCaches: true,
indexedProjects: new Set(),
writeIndex: () => {},
Expand Down Expand Up @@ -88,6 +89,7 @@ test('lone surrogates in documentation and symbols survive serialization', () =>
yarnWorkspaces: false,
yarnBerryWorkspaces: false,
pnpmWorkspaces: false,
npmWorkspaces: false,
globalCaches: true,
indexedProjects: new Set(),
writeIndex: () => {},
Expand Down
1 change: 1 addition & 0 deletions src/ProjectIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ test('only deduplicates documents after successful emission', () => {
yarnWorkspaces: false,
yarnBerryWorkspaces: false,
pnpmWorkspaces: false,
npmWorkspaces: false,
globalCaches: false,
indexedProjects: new Set(),
writeIndex: () => {
Expand Down
1 change: 1 addition & 0 deletions src/Svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ test('no-global-caches keeps Svelte source information project-local', () => {
yarnWorkspaces: false,
yarnBerryWorkspaces: false,
pnpmWorkspaces: false,
npmWorkspaces: false,
globalCaches: false,
indexedProjects: new Set(),
writeIndex: index => documents.push(...index.documents),
Expand Down
4 changes: 3 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ for (const snapshotDirectory of snapshotDirectories) {
const tsconfigJsonPath = path.join(inputRoot, 'tsconfig.json')
const inferTsconfig = !fs.existsSync(tsconfigJsonPath)
const output = path.join(inputRoot, 'index.scip')
const isNpm = Boolean(packageJson.packageManager?.startsWith('npm@'))
indexCommand([], {
cwd: inputRoot,
inferTsconfig,
output,
yarnWorkspaces: Boolean(packageJson.workspaces),
yarnWorkspaces: Boolean(packageJson.workspaces) && !isNpm,
yarnBerryWorkspaces: false,
pnpmWorkspaces: Boolean(packageJson.packageManager?.includes('pnpm')),
npmWorkspaces: isNpm,
progressBar: false,
indexedProjects: new Set(),
globalCaches: true,
Expand Down
17 changes: 17 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export function indexCommand(
projects.push(...listYarnWorkspaces(options.cwd, 'yarn2Plus'))
} else if (options.pnpmWorkspaces) {
projects.push(...listPnpmWorkspaces(options.cwd))
} else if (options.npmWorkspaces) {
projects.push(...listNpmWorkspaces(options.cwd))
} else if (projects.length === 0) {
projects.push(options.cwd)
}
Expand Down Expand Up @@ -251,6 +253,21 @@ function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions {
return options
}

function listNpmWorkspaces(directory: string): string[] {
const { workspaces = [] } = JSON.parse(
fs.readFileSync(path.join(directory, 'package.json'), 'utf-8')
) as { workspaces?: string[] | { packages?: string[] } }
const patterns = Array.isArray(workspaces)
? workspaces
: (workspaces.packages ?? [])
return fs
.globSync(
patterns.map(pattern => path.posix.join(pattern, 'package.json')),
{ cwd: directory, exclude: name => name === 'node_modules' }
)
.map(packageJsonPath => path.join(directory, path.dirname(packageJsonPath)))
}

function listPnpmWorkspaces(directory: string): string[] {
/**
* Returns the list of projects formatted as:
Expand Down
Loading