Skip to content

Commit

Permalink
Added support for finding pnpm workspace packages (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Feb 2, 2020
1 parent 1c0b599 commit 0ed3f2b
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 119 deletions.
7 changes: 7 additions & 0 deletions .changeset/wet-falcons-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@manypkg/cli": minor
"find-workspaces-root": minor
"@manypkg/gatsby-source-workspace": minor
---

Added support for finding pnpm workspace packages.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
run: yarn

- name: Run Tests
run: yarn jest
run: yarn test

typescript:
name: TypeScript
Expand Down
4 changes: 4 additions & 0 deletions __fixtures__/basic-pnpm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@manypkg/basic-pnpm-fixture",
"version": "1.0.0"
}
4 changes: 4 additions & 0 deletions __fixtures__/basic-pnpm/packages/package-one/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@manypkg/basic-pnpm-fixture-pkg-one",
"version": "1.0.0"
}
Empty file.
2 changes: 2 additions & 0 deletions __fixtures__/basic-pnpm/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- "**"
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"@changesets/changelog-github": "^0.1.1",
"@changesets/cli": "^2.3.3",
"@changesets/changelog-github": "^0.2.1",
"@changesets/cli": "^2.5.0",
"@types/fs-extra": "^8.0.1",
"@types/jest": "^24.0.17",
"@types/semver": "^6.0.1",
"jest": "^24.8.0",
"jest-watch-typeahead": "^0.4.2",
"preconstruct": "^0.0.89",
"typescript": "^3.5.3"
},
"jest": {
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
},
"preconstruct": {
"packages": [
"packages/!(gatsby)*"
]
},
"prettier": {},
"scripts": {
"postinstall": "preconstruct dev && manypkg check",
"release": "yarn preconstruct build && yarn changeset publish",
"test-gatsby": "cd test-gatsby && gatsby develop"
"test-gatsby": "cd test-gatsby && gatsby develop",
"test": "jest"
}
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"find-up": "^4.1.0",
"find-workspaces-root": "^0.1.0",
"fs-extra": "^8.1.0",
"get-workspaces": "^0.5.0",
"get-workspaces": "^0.6.0",
"meow": "^5.0.0",
"p-limit": "^2.2.1",
"sembear": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function execCmd(args: string[]) {
let workspacesRoot = await findWorkspacesRoot(process.cwd());
let workspaces = (await getWorkspaces({
cwd: workspacesRoot,
tools: ["yarn", "bolt", "root"]
tools: ["yarn", "bolt", "pnpm", "root"]
}))!;
let highestExitCode = 0;
await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/script-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let keys: <Obj>(obj: Obj) => Array<keyof Obj> = Object.keys;
);
let workspaces = (await getWorkspaces({
cwd: workspacesRoot,
tools: ["yarn", "bolt", "root"]
tools: ["yarn", "bolt", "pnpm", "root"]
}))!;
let rootWorkspace: Workspace = {
config: await rootWorkspaceContentPromise,
Expand Down
5 changes: 2 additions & 3 deletions packages/find-workspaces-root/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.5.5",
"@types/fs-extra": "^8.0.0",
"@types/node": "^12.7.1",
"find-up": "^4.1.0",
"fs-extra": "^8.1.0"
"get-workspaces": "^0.6.0"
},
"devDependencies": {
"fixturez": "^1.1.0"
}
}
}
8 changes: 8 additions & 0 deletions packages/find-workspaces-root/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test("it returns the root of a monorepo", async () => {
expect(workspacesRoot).toBe(tmpPath);
});

test("it returns the root of a pnpm monorepo", async () => {
let tmpPath = f.copy("basic-pnpm");
let workspacesRoot = await findWorkspacesRoot(
path.join(tmpPath, "packages", "package-one", "src")
);
expect(workspacesRoot).toBe(tmpPath);
});

test("it returns the root of a single-package repo", async () => {
let tmpPath = f.copy("single-pkg");
let workspacesRoot = await findWorkspacesRoot(path.join(tmpPath, "src"));
Expand Down
37 changes: 21 additions & 16 deletions packages/find-workspaces-root/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import findUp from "find-up";
import path from "path";
import fs from "fs-extra";
import getWorkspaces from "get-workspaces";

export class NoPkgJsonFound extends Error {
directory: string;
Expand All @@ -12,16 +11,14 @@ export class NoPkgJsonFound extends Error {
}
}

export default async function findWorkspacesRoot(cwd: string): Promise<string> {
let firstPkgJsonDirectory: string | undefined;
let dir = await findUp(
const findWorkspacesUp = (options: {
cwd: string;
tools?: NonNullable<Parameters<typeof getWorkspaces>[0]>["tools"];
}) =>
findUp(
async directory => {
try {
let pkgJson = await fs.readJson(path.join(directory, "package.json"));
if (firstPkgJsonDirectory === undefined) {
firstPkgJsonDirectory = directory;
}
if (pkgJson.workspaces || pkgJson.bolt) {
if (await getWorkspaces({ cwd: directory, tools: options.tools })) {
return directory;
}
} catch (err) {
Expand All @@ -30,13 +27,21 @@ export default async function findWorkspacesRoot(cwd: string): Promise<string> {
}
}
},
{ cwd, type: "directory" }
{ cwd: options.cwd, type: "directory" }
);
if (firstPkgJsonDirectory === undefined) {
throw new NoPkgJsonFound(cwd);

export default async function findWorkspacesRoot(cwd: string): Promise<string> {
let workspacesRoot = await findWorkspacesUp({ cwd });

if (workspacesRoot) {
return workspacesRoot;
}
if (dir === undefined) {
return firstPkgJsonDirectory;

let singlePackageRoot = await findWorkspacesUp({ cwd, tools: ["root"] });

if (singlePackageRoot) {
return singlePackageRoot;
}
return dir;

throw new NoPkgJsonFound(cwd);
}
2 changes: 1 addition & 1 deletion packages/gatsby-source-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"find-workspaces-root": "^0.1.0",
"fs-extra": "^8.1.0",
"get-workspaces": "^0.5.0"
"get-workspaces": "^0.6.0"
},
"files": [
"gatsby-node.js",
Expand Down
Loading

0 comments on commit 0ed3f2b

Please sign in to comment.