Skip to content

Commit

Permalink
feat: support pnpm workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
MKruschke committed Apr 5, 2021
1 parent 0bd3d5e commit 0bfbff9
Show file tree
Hide file tree
Showing 97 changed files with 214 additions and 32 deletions.
51 changes: 25 additions & 26 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test
windows:
runs-on: windows-latest

strategy:
matrix:
node-version: [14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }} with windows
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test

# windows:
# runs-on: windows-latest

# strategy:
# matrix:
# node-version: [14.x]

# steps:
# - uses: actions/checkout@v2
# - name: Use Node.js ${{ matrix.node-version }} with windows
# uses: actions/setup-node@v1
# with:
# node-version: ${{ matrix.node-version }}
# - run: yarn
# - run: yarn test
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ tests
scripts
node_modules
test-run
test-scenarios
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# update-ts-references

If your repository is a multi package repository (via [lerna.js](https://lerna.js.org/) or [yarn workspaces](https://classic.yarnpkg.com/en/docs/workspaces/)) in combination with [project references](https://www.typescriptlang.org/docs/handbook/project-references.html) from TypeScript, this tool will be very helpful by reading dependencies out of the _package.json_ and applying them to the _tsconfig.json_ as references.
If your repository is a multi package repository (via [lerna.js](https://lerna.js.org/), [yarn workspaces](https://classic.yarnpkg.com/en/docs/workspaces/)) or [pnpm workspaces](https://pnpm.js.org/workspaces) in combination with [project references](https://www.typescriptlang.org/docs/handbook/project-references.html) from TypeScript, this tool will be very helpful by reading dependencies out of the _package.json_ and applying them to the _tsconfig.json_ as references.

There's a blog post that's a good introduction to it [Optimizing multi-package apps with TypeScript Project References](https://ebaytech.berlin/optimizing-multi-package-apps-with-typescript-project-references-d5c57a3b4440).

Expand Down Expand Up @@ -38,6 +38,9 @@ or you add it as dev dependency and include it in the `postinstall` script in th

## FAQ

> Why is my pnpm workspace alias not working?
_update-ts-references_ is currently not supporting [Referencing workspace packages through aliases](https://pnpm.js.org/workspaces#referencing-workspace-packages-through-aliases) yet. See issue #13

> Where are the comments from my tsconfig?
_update-ts-references_ is **not** able to preserve comments in tsconfig files when it is updating the references. If you need comments for the case like, explaining why compiler options are set, please move this part including comments into a second file and use the `extends` functionallity (see [here](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#tsconfig-bases)).
Expand Down
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "2.0.0",
"version": "2.1.0",
"bin": "src/index.js",
"scripts": {
"lint": "eslint src tests",
Expand All @@ -15,6 +15,8 @@
"dependencies": {
"@formatjs/cli": "^2.3.1",
"glob": "^7.1.6",
"js-yaml": "^4.0.0",
"minimatch": "^3.0.4",
"minimist": "^1.2.5",
"mkdirp": "^1.0.4",
"readline-sync": "^1.4.10"
Expand All @@ -38,6 +40,7 @@
"tool",
"util",
"yarn",
"pnpm",
"workspaces",
"lerna",
"monorepo",
Expand All @@ -50,5 +53,12 @@
"bugs": {
"url": "https://github.com/eBayClassifiedsGroup/update-ts-references/issues"
},
"homepage": "https://github.com/eBayClassifiedsGroup/update-ts-references"
"homepage": "https://github.com/eBayClassifiedsGroup/update-ts-references",
"jest": {
"verbose": true,
"modulePathIgnorePatterns": [
"<rootDir>/test-scenarios/",
"<rootDir>/test-run/"
]
}
}
2 changes: 1 addition & 1 deletion scripts/prepareTests.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
yarn link
rm -rf test-run
cp -R tests/scenarios test-run
cp -R test-scenarios test-run
find test-run -maxdepth 1 -type d \( ! -name 'test-run' \) -exec bash -c "cd {} && yarn && yarn link update-ts-references " \;
30 changes: 28 additions & 2 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const glob = require('glob');
const path = require('path');
const fs = require('fs');
const ts = require('typescript');
const yaml = require('js-yaml');
const minimatch = require('minimatch');
const readlineSync = require('readline-sync');
const assert = require('assert').strict;

Expand All @@ -17,8 +19,19 @@ const defaultOptions = {
};

const getAllPackageJsons = async (workspaces) => {
const ignoreGlobs = [];
const workspaceGlobs = [];

workspaces.forEach((workspaceGlob) => {
if (workspaceGlob.startsWith('!')) {
ignoreGlobs.push(workspaceGlob);
} else {
workspaceGlobs.push(workspaceGlob);
}
});

return Promise.all(
workspaces.map(
workspaceGlobs.map(
(workspace) =>
new Promise((resolve, reject) => {
glob(`${workspace}/${PACKAGE_JSON}`, (error, files) => {
Expand All @@ -39,7 +52,13 @@ const getAllPackageJsons = async (workspaces) => {
)
)
.then((allPackages) =>
allPackages.filter((packageName) => !packageName.includes('node_modules'))
allPackages.filter(
(packageName) =>
ignoreGlobs.reduce((prev, actualPattern) => {
if (!prev) return prev;
return minimatch(packageName, actualPattern);
}, true) && !packageName.includes('node_modules')
)
);
};

Expand Down Expand Up @@ -174,6 +193,13 @@ const execute = async ({ cwd, verbose, discardComments, check }) => {
workspaces = lernaJson.packages;
}

if (!workspaces && fs.existsSync(path.join(cwd, 'pnpm-workspace.yaml'))) {
const pnpmConfig = yaml.load(
fs.readFileSync(path.join(cwd, 'pnpm-workspace.yaml'))
);
workspaces = pnpmConfig.packages;
}

if (!workspaces) {
throw new Error(
'could not detect yarn workspaces or lerna in this repository'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions test-scenarios/pnpm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pnpm-workspace",
"version": "0.0.1",
"private": true,
"devDependencies": {
"typescript": "latest"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/pnpm/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
packages:
# all packages in subdirs of packages/ and components/
- 'workspace-a'
- 'workspace-b'
- 'shared/*'
- 'utils/**/'
# exclude packages that are inside test directories
- '!**/tests/**'
File renamed without changes.
4 changes: 4 additions & 0 deletions test-scenarios/pnpm/utils/foos/tests/test-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-a",
"version": "1.0.0"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws-nohoist/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
File renamed without changes.
10 changes: 10 additions & 0 deletions test-scenarios/yarn-ws/shared/workspace-c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-c",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"peerDependencies": {
"foo-a": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws/shared/workspace-c/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
7 changes: 7 additions & 0 deletions test-scenarios/yarn-ws/shared/workspace-d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-d",
"version": "1.0.0",
"dependencies": {
"workspace-c": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws/shared/workspace-d/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/yarn-ws/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
7 changes: 7 additions & 0 deletions test-scenarios/yarn-ws/utils/foos/foo-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo-a",
"version": "1.0.0",
"dependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws/utils/foos/foo-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
4 changes: 4 additions & 0 deletions test-scenarios/yarn-ws/utils/foos/foo-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo-b",
"version": "1.0.0"
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws/utils/foos/foo-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/yarn-ws/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws/workspace-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/yarn-ws/workspace-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
13 changes: 13 additions & 0 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const rootFolderYarnNohoist = path.join(
process.cwd(),
'test-run/yarn-ws-nohoist'
);
const rootFolderPnpm = path.join(process.cwd(), 'test-run/pnpm');
const rootFolderYarnCheck = path.join(process.cwd(), 'test-run/yarn-ws-check');
const rootFolderYarnCheckNoChanges = path.join(
process.cwd(),
Expand Down Expand Up @@ -164,6 +165,18 @@ test('Support yarn workspaces with noHoist', async () => {
});
});

test('Support pnpm workspaces', async () => {
await setup(rootFolderPnpm);

tsconfigs.forEach((tsconfig) => {
const [configPath, config] = tsconfig;

expect(
require(path.join(rootFolderPnpm, configPath, 'tsconfig.json'))
).toEqual(config);
});
});

test('Detect changes with the --check option', async () => {
let errorCode = 0;
try {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,11 @@ argparse@^1.0.7:
dependencies:
sprintf-js "~1.0.2"

argparse@^2.0.1:
version "2.0.1"
resolved "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

arr-diff@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520"
Expand Down Expand Up @@ -2644,6 +2649,13 @@ js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/js-yaml/-/js-yaml-4.0.0.tgz#f426bc0ff4b4051926cd588c71113183409a121f"
integrity sha512-pqon0s+4ScYUvX30wxQi3PogGFAlUyH0awepWvwkj4jD4v+ova3RiYw8bmA6x2rDrEaj8i/oWKoRxpVNW+Re8Q==
dependencies:
argparse "^2.0.1"

jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
Expand Down

0 comments on commit 0bfbff9

Please sign in to comment.