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

### Index a TypeScript project using pnpm workspaces

Navigate to the project root, containing `package.json`.

```sh
pnpm install

scip-typescript index --pnpm-workspaces
```

### Indexing in CI

Add the following run steps to your CI pipeline:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "node ./node_modules/typescript/bin/tsc -b .",
"test": "uvu -r ts-node/register --ignore dist",
"update-snapshots": "uvu -r ts-node/register --ignore dist --update-snapshots",
"prepare": "cd snapshots && yarn && cd input/multi-project && yarn"
"prepare": "cd snapshots && yarn && cd input/multi-project && yarn && cd ../pnpm-workspaces && pnpm install"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -54,6 +54,7 @@
"eslint": "^7.32.0",
"eslint-plugin-unicorn": "^21.0.0",
"eslint-plugin-unused-imports": "^2.0.0",
"pnpm": "7.20.0",
"prettier": "2.8.1",
"ts-node": "^10.7.0",
"typescript-eslint": "0.0.1-alpha.0",
Expand Down
13 changes: 13 additions & 0 deletions snapshots/input/pnpm-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "pnpm-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": "pnpm@7.20.0"
}
11 changes: 11 additions & 0 deletions snapshots/input/pnpm-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/pnpm-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/pnpm-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/pnpm-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/pnpm-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()
}
11 changes: 11 additions & 0 deletions snapshots/input/pnpm-workspaces/packages/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"baseUrl": "./src",
"sourceRoot": "src",
"outDir": "dist"
},
"include": ["src/*"],
"references": [{ "path": "../a" }]
}

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions snapshots/input/pnpm-workspaces/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions snapshots/input/pnpm-workspaces/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
24 changes: 24 additions & 0 deletions snapshots/input/pnpm-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/pnpm-workspaces/packages/a/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function a(): string {
// definition @example/a 1.0.0 src/`a.ts`/
//documentation ```ts\nmodule "a.ts"\n```
// ^ definition @example/a 1.0.0 src/`a.ts`/a().
// documentation ```ts\nfunction a(): string\n```
return ''
}

13 changes: 13 additions & 0 deletions snapshots/output/pnpm-workspaces/packages/b/src/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { a } from '@example/a'
// definition @example/b 1.0.0 src/`b.ts`/
//documentation ```ts\nmodule "b.ts"\n```
// ^ 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().
// documentation ```ts\nfunction b(): string\n```
return a()
// ^ reference @example/a 1.0.0 src/`a.ts`/a().
}

26 changes: 13 additions & 13 deletions snapshots/output/react/src/LoaderInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
// definition react-example 1.0.0 src/`LoaderInput.tsx`/
//documentation ```ts\nmodule "LoaderInput.tsx"\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/

/** Takes loading prop, input component as child */
interface Props {
Expand All @@ -15,15 +15,15 @@
children: React.ReactNode
// ^^^^^^^^ definition react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
// documentation ```ts\n(property) children: ReactNode\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/ReactNode#
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/ReactNode#
}

export const LoaderInput: React.FunctionComponent<Props> = ({
// ^^^^^^^^^^^ definition react-example 1.0.0 src/`LoaderInput.tsx`/LoaderInput.
// documentation ```ts\nvar LoaderInput: FunctionComponent<Props>\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#
loading,
// ^^^^^^^ definition local 3
Expand All @@ -36,23 +36,23 @@
// ^^^^^^^^ reference local 7
}) => (
<div className="hello">
// ^^^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#div.
// ^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/HTMLAttributes#className.
// ^^^ reference @types/react 17.0.52 `index.d.ts`/global/JSX/IntrinsicElements#div.
// ^^^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/HTMLAttributes#className.
{children}
// ^^^^^^^^ reference local 4
{loading && <p>spinner</p>}
// ^^^^^^^ reference local 3
// ^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#p.
// ^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#p.
// ^ reference @types/react 17.0.52 `index.d.ts`/global/JSX/IntrinsicElements#p.
// ^ reference @types/react 17.0.52 `index.d.ts`/global/JSX/IntrinsicElements#p.
</div>
// ^^^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#div.
// ^^^ reference @types/react 17.0.52 `index.d.ts`/global/JSX/IntrinsicElements#div.
)

export const LoaderInput2: React.FunctionComponent<Props> = props => {
// ^^^^^^^^^^^^ definition react-example 1.0.0 src/`LoaderInput.tsx`/LoaderInput2.
// documentation ```ts\nvar LoaderInput2: FunctionComponent<Props>\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#
// ^^^^^ definition local 9
// documentation ```ts\n(parameter) props: PropsWithChildren<Props>\n```
Expand Down
12 changes: 6 additions & 6 deletions snapshots/output/react/src/MyTSXElement.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
// definition react-example 1.0.0 src/`MyTSXElement.tsx`/
//documentation ```ts\nmodule "MyTSXElement.tsx"\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/

export interface MyProps {}
// ^^^^^^^ definition react-example 1.0.0 src/`MyTSXElement.tsx`/MyProps#
Expand All @@ -11,8 +11,8 @@
export const MyTSXElement: React.FunctionComponent<MyProps> = ({}) => (<p></p>)
// ^^^^^^^^^^^^ definition react-example 1.0.0 src/`MyTSXElement.tsx`/MyTSXElement.
// documentation ```ts\nvar MyTSXElement: FunctionComponent<MyProps>\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/FunctionComponent#
// ^^^^^^^ reference react-example 1.0.0 src/`MyTSXElement.tsx`/MyProps#
// ^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#p.
// ^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#p.
// ^ reference @types/react 17.0.52 `index.d.ts`/global/JSX/IntrinsicElements#p.
// ^ reference @types/react 17.0.52 `index.d.ts`/global/JSX/IntrinsicElements#p.
8 changes: 4 additions & 4 deletions snapshots/output/react/src/UseMyTSXElement.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
// definition react-example 1.0.0 src/`UseMyTSXElement.tsx`/
//documentation ```ts\nmodule "UseMyTSXElement.tsx"\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/

import { MyProps, MyTSXElement } from "./MyTSXElement";
// ^^^^^^^ reference react-example 1.0.0 src/`MyTSXElement.tsx`/MyProps#
Expand All @@ -12,8 +12,8 @@
export const _: React.FunctionComponent<MyProps> =
// ^ definition react-example 1.0.0 src/`UseMyTSXElement.tsx`/_.
// documentation ```ts\nvar _: FunctionComponent<MyProps>\n```
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.52 `index.d.ts`/React/FunctionComponent#
// ^^^^^^^ reference react-example 1.0.0 src/`MyTSXElement.tsx`/MyProps#
({}) => (<MyTSXElement></MyTSXElement>)
// ^^^^^^^^^^^^ reference react-example 1.0.0 src/`MyTSXElement.tsx`/MyTSXElement.
Expand Down
1 change: 1 addition & 0 deletions src/CommandLineOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ checkIndexParser([], {

checkIndexParser(['--cwd', 'qux'], { cwd: 'qux' })
checkIndexParser(['--yarn-workspaces'], { yarnWorkspaces: true })
checkIndexParser(['--pnpm-workspaces'], { pnpmWorkspaces: 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 @@ -12,6 +12,7 @@ export interface MultiProjectOptions {
progressBar: boolean
yarnWorkspaces: boolean
yarnBerryWorkspaces: boolean
pnpmWorkspaces: boolean
globalCaches: boolean
cwd: string
output: string
Expand Down Expand Up @@ -47,6 +48,7 @@ export function mainCommand(
command
.command('index')
.option('--cwd <path>', 'the working directory', process.cwd())
.option('--pnpm-workspaces', 'whether to index all pnpm 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/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if (isUpdate && fs.existsSync(outputDirectory)) {
}
interface PackageJson {
workspaces: string[]
packageManager?: string
}
for (const snapshotDirectory of snapshotDirectories) {
// Uncomment below if you want to skip certain tests for local development.
Expand All @@ -56,6 +57,7 @@ for (const snapshotDirectory of snapshotDirectories) {
output,
yarnWorkspaces: Boolean(packageJson.workspaces),
yarnBerryWorkspaces: false,
pnpmWorkspaces: Boolean(packageJson.packageManager?.includes('pnpm')),
progressBar: false,
indexedProjects: new Set(),
globalCaches: true,
Expand Down
25 changes: 25 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
import * as child_process from 'child_process'
import * as fs from 'fs'
import { EOL } from 'os'
import * as path from 'path'
import * as url from 'url'

Expand Down Expand Up @@ -33,6 +34,8 @@ export function indexCommand(
projects.push(...listYarnWorkspaces(options.cwd, 'tryYarn1'))
} else if (options.yarnBerryWorkspaces) {
projects.push(...listYarnWorkspaces(options.cwd, 'yarn2Plus'))
} else if (options.pnpmWorkspaces) {
projects.push(...listPnpmWorkspaces(options.cwd))
} else if (projects.length === 0) {
projects.push(options.cwd)
}
Expand Down Expand Up @@ -211,6 +214,28 @@ function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions {
return options
}

function listPnpmWorkspaces(directory: string): string[] {
/**
* Returns the list of projects formatted as:
* '/Users/user/sourcegraph/client/web:@sourcegraph/web@1.10.1:PRIVATE',
*
* See https://pnpm.io/id/cli/list#--depth-number
*/
const output = child_process.execSync(
'pnpm ls -r --depth -1 --long --parseable',
{
cwd: directory,
encoding: 'utf-8',
maxBuffer: 1024 * 1024 * 5, // 5MB
}
)

return output
.split(EOL)
.filter(project => project.includes(':'))
.map(project => project.split(':')[0])
}

function listYarnWorkspaces(
directory: string,
yarnVersion: 'tryYarn1' | 'yarn2Plus'
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,11 @@ pluralize@^8.0.0:
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==

pnpm@7.20.0:
version "7.20.0"
resolved "https://registry.yarnpkg.com/pnpm/-/pnpm-7.20.0.tgz#fa5c9d347852e9ce1e98abe7f6636c3a78f710bb"
integrity sha512-klGp+P0sxw1f6OYwJtl0PcoYiIHVhMZdh9e2B8maUmzOGmAj0NL9jKaKPfIX4kKfyQcWeADm46Z+4CKtabZqSw==

prelude-ls@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
Expand Down