Skip to content

Commit

Permalink
feat(core): add support for pnpm workspaces
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
aparajita authored and JamieMason committed Aug 2, 2020
1 parent f2cac6a commit a6112ec
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/README_CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package.json files are resolved in this order of precendence:
1. If `--source`
[glob patterns](https://github.com/isaacs/node-glob#glob-primer) are
provided, use those.
1. If using [Pnpm Workspaces](https://pnpm.js.org/en/workspaces),
read `packages` from `pnpm-workspace.yaml` in the root of the current project.
1. If using [Yarn Workspaces](https://yarnpkg.com/lang/en/docs/workspaces/),
read `workspaces` from `./package.json`.
1. If using [Lerna](https://lerna.js.org/), read `packages` from `./lerna.json`.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "syncpack",
"description": "Manage multiple package.json files, such as in Lerna Monorepos and Yarn Workspaces",
"description": "Manage multiple package.json files, such as in Lerna Monorepos and Yarn/Pnpm Workspaces",
"version": "5.0.3",
"author": "Jamie Mason <[email protected]> (https://github.com/JamieMason)",
"bin": {
Expand All @@ -14,12 +14,14 @@
"bugs": "https://github.com/JamieMason/syncpack/issues",
"contributors": [
"Alex Hayton (https://github.com/AlexHayton)",
"Aparajita Fishman (https://github.com/aparajita)",
"Jamie Mason (https://github.com/JamieMason)",
"Luis Vieira (https://github.com/luisvieiragmr)",
"Marais Rossouw (https://github.com/maraisr)",
"Matt Sprague (https://github.com/uforic)"
],
"dependencies": {
"@manypkg/get-packages": "1.1.0",
"chalk": "4.1.0",
"commander": "6.0.0",
"fs-extra": "9.0.1",
Expand Down Expand Up @@ -63,6 +65,7 @@
"package",
"package-json",
"packages",
"pnpm",
"semver",
"workspace",
"yarn"
Expand Down
8 changes: 5 additions & 3 deletions src/bin-fix-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ program.on('--help', () => {
console.log(chalk`
Resolving Packages:
1. If {yellow --source} globs are provided, use those.
2. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
3. If using Lerna, read {yellow packages} from {yellow lerna.json}.
4. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
2. If using Pnpm Workspaces, read {yellow packages} from {yellow pnpm-workspace.yaml} in the root of the project.
3. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
4. If using Lerna, read {yellow packages} from {yellow lerna.json}.
5. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
Examples:
{dim # uses defaults for resolving packages}
Expand All @@ -40,6 +41,7 @@ Reference:
globs {blue.underline https://github.com/isaacs/node-glob#glob-primer}
lerna.json {blue.underline https://github.com/lerna/lerna#lernajson}
Yarn Workspaces {blue.underline https://yarnpkg.com/lang/en/docs/workspaces}
Pnpm Workspaces {blue.underline https://pnpm.js.org/en/workspaces}
`);
});

Expand Down
8 changes: 5 additions & 3 deletions src/bin-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ Examples:
Resolving Packages:
1. If {yellow --source} globs are provided, use those.
2. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
3. If using Lerna, read {yellow packages} from {yellow lerna.json}.
4. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
2. If using Pnpm Workspaces, read {yellow packages} from {yellow pnpm-workspace.yaml} in the root of the project.
3. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
4. If using Lerna, read {yellow packages} from {yellow lerna.json}.
5. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
Reference:
globs {blue.underline https://github.com/isaacs/node-glob#glob-primer}
lerna.json {blue.underline https://github.com/lerna/lerna#lernajson}
Yarn Workspaces {blue.underline https://yarnpkg.com/lang/en/docs/workspaces}
Pnpm Workspaces {blue.underline https://pnpm.js.org/en/workspaces}
`);
});

Expand Down
8 changes: 5 additions & 3 deletions src/bin-list-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ Examples:
Resolving Packages:
1. If {yellow --source} globs are provided, use those.
2. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
3. If using Lerna, read {yellow packages} from {yellow lerna.json}.
4. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
2. If using Pnpm Workspaces, read {yellow packages} from {yellow pnpm-workspace.yaml} in the root of the project.
3. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
4. If using Lerna, read {yellow packages} from {yellow lerna.json}.
5. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
Reference:
globs {blue.underline https://github.com/isaacs/node-glob#glob-primer}
lerna.json {blue.underline https://github.com/lerna/lerna#lernajson}
Yarn Workspaces {blue.underline https://yarnpkg.com/lang/en/docs/workspaces}
Pnpm Workspaces {blue.underline https://pnpm.js.org/en/workspaces}
`);
});

Expand Down
8 changes: 5 additions & 3 deletions src/bin-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ Examples:
Resolving Packages:
1. If {yellow --source} globs are provided, use those.
2. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
3. If using Lerna, read {yellow packages} from {yellow lerna.json}.
4. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
2. If using Pnpm Workspaces, read {yellow packages} from {yellow pnpm-workspace.yaml} in the root of the project.
3. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
4. If using Lerna, read {yellow packages} from {yellow lerna.json}.
5. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
Reference:
globs {blue.underline https://github.com/isaacs/node-glob#glob-primer}
lerna.json {blue.underline https://github.com/lerna/lerna#lernajson}
Yarn Workspaces {blue.underline https://yarnpkg.com/lang/en/docs/workspaces}
Pnpm Workspaces {blue.underline https://pnpm.js.org/en/workspaces}
`);
});

Expand Down
8 changes: 5 additions & 3 deletions src/bin-set-semver-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ Supported Ranges:
Resolving Packages:
1. If {yellow --source} globs are provided, use those.
2. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
3. If using Lerna, read {yellow packages} from {yellow lerna.json}.
4. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
2. If using Pnpm Workspaces, read {yellow packages} from {yellow pnpm-workspace.yaml} in the root of the project.
3. If using Yarn Workspaces, read {yellow workspaces} from {yellow package.json}.
4. If using Lerna, read {yellow packages} from {yellow lerna.json}.
5. Default to {yellow "package.json"} and {yellow "packages/*/package.json"}.
Reference:
globs {blue.underline https://github.com/isaacs/node-glob#glob-primer}
lerna.json {blue.underline https://github.com/lerna/lerna#lernajson}
Yarn Workspaces {blue.underline https://yarnpkg.com/lang/en/docs/workspaces}
Pnpm Workspaces {blue.underline https://pnpm.js.org/en/workspaces}
`);
});

Expand Down
24 changes: 24 additions & 0 deletions src/commands/lib/get-wrappers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,28 @@ describe('getWrappers', () => {
]);
});
});

describe('pnpm workspaces', () => {
afterEach(() => {
mock.restore();
});

beforeEach(() => {
mock({
'package.json': JSON.stringify({ name: 'root' }),
'pnpm-workspace.yaml': ['packages:', ' - "./*"'].join('\n'),
'a/package.json': JSON.stringify({ name: 'package-a' }),
'b/package.json': JSON.stringify({ name: 'package-b' }),
});
});

it('should resolve correctly', () => {
const program = { sources: [] };
expect(getWrappers(program)).toEqual([
{ contents: { name: 'root' }, filePath: `${process.cwd()}/package.json` },
getShape('package-a', 'a/package.json'),
getShape('package-b', 'b/package.json'),
]);
});
});
});
18 changes: 17 additions & 1 deletion src/commands/lib/get-wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getPackagesSync, Package } from '@manypkg/get-packages';
import { readJsonSync } from 'fs-extra';
import { sync } from 'glob';
import { join, resolve } from 'path';
Expand Down Expand Up @@ -36,6 +37,18 @@ const getPatternsFromConfig = (fileName: string, propName: string): string[] | n
: null;
};

const getPnpmPatterns = (): string[] | null => {
try {
const config = getPackagesSync(process.cwd());
const isNonEmptyArray = config && config.tool === 'pnpm' && config.packages.length > 0;
return isNonEmptyArray
? [config.root].concat(config.packages).map((pkg: Package) => join(pkg.dir, 'package.json'))
: null;
} catch (e) {
return null;
}
};

const hasCliPatterns = (program: Options): boolean => program.sources && program.sources.length > 0;
const getCliPatterns = (program: Options): Options['sources'] => program.sources;
const getYarnPatterns = (): string[] | null => getPatternsFromConfig('package.json', 'workspaces');
Expand All @@ -46,7 +59,10 @@ const reduceFlatArray = (all: string[], next: string[]): string[] => all.concat(
const createWrapper = (filePath: string): SourceWrapper => ({ contents: readJsonSync(filePath), filePath });

export const getWrappers = (program: Options): SourceWrapper[] =>
(hasCliPatterns(program) ? getCliPatterns(program) : getYarnPatterns() || getLernaPatterns() || getDefaultPatterns())
(hasCliPatterns(program)
? getCliPatterns(program)
: getYarnPatterns() || getPnpmPatterns() || getLernaPatterns() || getDefaultPatterns()
)
.map(resolvePattern)
.reduce(reduceFlatArray, [])
.map(createWrapper);
Loading

0 comments on commit a6112ec

Please sign in to comment.