Skip to content

Commit

Permalink
Create @manypkg/tools and refactor find-root and get-packages (#…
Browse files Browse the repository at this point in the history
…151)

* Create sample core package

* Initial round of feedback

* eliminate index-based zip

* Add BoltTool

* Add PnpmTool

* Add Yarn and None tools, update findroot unit tests

* Implement sync methods for all Tools, fixing findRootSync tests

* wip: 90% of get-packages converted to use Tools

* revert back to original snaps

* Small tool fixes

* add latest content

* Rename NoneTool to SinglePackageTool, it is never the root of a monorepo

* Swap references to SinglePackageTool

* Remove errant comment

* Implement core type changes for Packages and Package

* Suggested feedback

* Mass prettier update after pull from main

* additional PR feedback

* errant console.log

* Type fixes

* Rename manypkg/core -> manypkg/tools

* fix unit tests, snapshots

* snapshot updates

* Fixed the `PackagesWithConfig` type

* Adjust checks implementations after changes

* Support both dir and relativeDir

* pr feedback

* unused importrs

* Fix prettier config, add changeset

* Introduce `getRootWs` test helper

* Exports only tools from `/tools` and handle default order in `/find-root`

* Fix suggested path.join

* Make root tool last

* Split up changesets

* Cleanup `/find-root`

* Update .changeset/frozen-yogurt.md

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Fix some test and type issues, feedback

* Refactor all try-catch blocks to assert positive instead of negative

* Revert change to jest default test timeout

* Update packages/tools/package.json

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update packages/get-packages/package.json

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
elliot-nelson and Andarist committed Jan 23, 2023
1 parent 45b396d commit a01efc9
Show file tree
Hide file tree
Showing 33 changed files with 1,253 additions and 473 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-days-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/find-root": major
---

The `find-root` package now returns a new `MonorepoRoot` interface, instead of a string. This interface provides a `rootDir` for the discovered monorepo, and a `tool` object, which is an object using the new `Tool` interface provided by `@manypkg/tools`.
5 changes: 5 additions & 0 deletions .changeset/frozen-yogurt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/tools": minor
---

Introduces a new `Tool` API that provides key functions related to a specific implementation of a monorepo, like `isMonorepoRoot` and `getPackages`. Existing tool implementations in manypkg have been converted to use this new interface.
5 changes: 5 additions & 0 deletions .changeset/upstream-animal-park.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/get-packages": major
---

The `get-packages` package now returns a slightly different structure. The old `tool` string has been replaced with a `tool` object, using the new `Tool` interface provided by `@manypkg/tools`. Each `Package` now contains both the absolute directory and relative directory path. Last, the `root` package has been renamed `rootPackage` and is optional, to support monorepos that do not contain a root package.
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ module.exports = {
proseWrap: "never",

// Disabling search dirs and pointing directly at plugins to keep configuration explicit
pluginSearchDirs: false,
pluginSearchDirs: [],
plugins: [require("prettier-plugin-packagejson")],
};
10 changes: 4 additions & 6 deletions packages/cli/src/checks/INCORRECT_REPOSITORY_FIELD.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import parseGithubUrl from "parse-github-url";
import path from "path";
import normalizePath from "normalize-path";
import { Package } from "@manypkg/get-packages";

Expand All @@ -15,7 +14,8 @@ type ErrorType = {
export default makeCheck<ErrorType>({
type: "all",
validate: (workspace, allWorkspaces, rootWorkspace, options) => {
let rootRepositoryField = (rootWorkspace.packageJson as any).repository;
let rootRepositoryField: unknown = (rootWorkspace?.packageJson as any)
?.repository;

if (typeof rootRepositoryField === "string") {
let result = parseGithubUrl(rootRepositoryField);
Expand Down Expand Up @@ -48,12 +48,10 @@ export default makeCheck<ErrorType>({
if (result.host === "github.com") {
correctRepositoryField = `${baseRepositoryUrl}/tree/${
options.defaultBranch
}/${normalizePath(
path.relative(rootWorkspace.dir, workspace.dir)
)}`;
}/${normalizePath(workspace.relativeDir)}`;
} else if (result.host === "dev.azure.com") {
correctRepositoryField = `${baseRepositoryUrl}?path=${normalizePath(
path.relative(rootWorkspace.dir, workspace.dir)
workspace.relativeDir
)}&version=GB${options.defaultBranch}&_a=contents`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/checks/INVALID_PACKAGE_NAME.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default makeCheck<ErrorType>({
print: (error) => {
if (!error.workspace.packageJson.name) {
return `The package at ${JSON.stringify(
error.workspace.dir
error.workspace.relativeDir
)} does not have a name`;
}
return `${
Expand Down
19 changes: 12 additions & 7 deletions packages/cli/src/checks/__tests__/EXTERNAL_MISMATCH.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import internalMismatch from "../EXTERNAL_MISMATCH";
import { getWS, getFakeWS } from "../../test-helpers";
import { getWS, getFakeWS, getRootWS } from "../../test-helpers";

let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();

it("should error if the ranges are valid and they are not equal", () => {
let ws = getWS();
Expand All @@ -27,14 +27,15 @@ it("should error if the ranges are valid and they are not equal", () => {
"mostCommonDependencyRange": "2.0.0",
"type": "EXTERNAL_MISMATCH",
"workspace": {
"dir": "some/fake/dir/pkg-1",
"dir": "fake/monorepo/packages/pkg-1",
"packageJson": {
"dependencies": {
"something": "1.0.0",
},
"name": "pkg-1",
"version": "1.0.0",
},
"relativeDir": "packages/pkg-1",
},
},
]
Expand Down Expand Up @@ -76,14 +77,15 @@ it("should error and return the correct mostCommonDependencyRange when the range
"mostCommonDependencyRange": "1.0.0",
"type": "EXTERNAL_MISMATCH",
"workspace": {
"dir": "some/fake/dir/pkg-2",
"dir": "fake/monorepo/packages/pkg-2",
"packageJson": {
"dependencies": {
"something": "2.0.0",
},
"name": "pkg-2",
"version": "1.0.0",
},
"relativeDir": "packages/pkg-2",
},
},
]
Expand Down Expand Up @@ -123,14 +125,15 @@ it("should error and return the correct mostCommonDependencyRange when the range
"mostCommonDependencyRange": "1.0.0",
"type": "EXTERNAL_MISMATCH",
"workspace": {
"dir": "some/fake/dir/pkg-1",
"dir": "fake/monorepo/packages/pkg-1",
"packageJson": {
"dependencies": {
"something": "2.0.0",
},
"name": "pkg-1",
"version": "1.0.0",
},
"relativeDir": "packages/pkg-1",
},
},
]
Expand Down Expand Up @@ -172,14 +175,15 @@ it("should error and return the correct mostCommonDependencyRange when the range
"mostCommonDependencyRange": "3.0.0",
"type": "EXTERNAL_MISMATCH",
"workspace": {
"dir": "some/fake/dir/pkg-1",
"dir": "fake/monorepo/packages/pkg-1",
"packageJson": {
"dependencies": {
"something": "1.0.0",
},
"name": "pkg-1",
"version": "1.0.0",
},
"relativeDir": "packages/pkg-1",
},
},
]
Expand All @@ -195,14 +199,15 @@ it("should error and return the correct mostCommonDependencyRange when the range
"mostCommonDependencyRange": "3.0.0",
"type": "EXTERNAL_MISMATCH",
"workspace": {
"dir": "some/fake/dir/pkg-2",
"dir": "fake/monorepo/packages/pkg-2",
"packageJson": {
"dependencies": {
"something": "2.0.0",
},
"name": "pkg-2",
"version": "1.0.0",
},
"relativeDir": "packages/pkg-2",
},
},
]
Expand Down
26 changes: 7 additions & 19 deletions packages/cli/src/checks/__tests__/INCORRECT_REPOSITORY_FIELD.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import path from "path";
import check from "../INCORRECT_REPOSITORY_FIELD";
import { getWS, getFakeWS, getFakeString } from "../../test-helpers";
import { getWS, getFakeWS, getFakeString, getRootWS } from "../../test-helpers";

describe("incorrect repository field", () => {
describe("github", () => {
it("should work", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();
let defaultBranch = `b${getFakeString(5)}`;

(rootWorkspace.packageJson as any).repository =
"https://github.com/Thinkmill/manypkg";
rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(workspace, ws, rootWorkspace, {
Expand All @@ -38,15 +36,13 @@ describe("incorrect repository field", () => {
});
it("should fix root in a different format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();
let defaultBranch = `b${getFakeString(5)}`;

(rootWorkspace.packageJson as any).repository =
"https://github.com/Thinkmill/manypkg.git";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace, {
Expand All @@ -71,15 +67,13 @@ describe("incorrect repository field", () => {
});
it("should do nothing if already in good format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();
let defaultBranch = `b${getFakeString(5)}`;

(rootWorkspace.packageJson as any).repository =
"https://github.com/Thinkmill/manypkg";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace, {
Expand All @@ -98,14 +92,12 @@ describe("incorrect repository field", () => {
describe("azure devops", () => {
it("should work", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();
let defaultBranch = `b${getFakeString(5)}`;

(rootWorkspace.packageJson as any).repository =
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg";
rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(workspace, ws, rootWorkspace, {
Expand All @@ -130,15 +122,13 @@ describe("incorrect repository field", () => {
});
it("should fix root in a different format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();
let defaultBranch = `b${getFakeString(5)}`;

(rootWorkspace.packageJson as any).repository =
"https://Thinkmill@dev.azure.com/Thinkmill/monorepos/_git/manypkg";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace, {
Expand All @@ -163,15 +153,13 @@ describe("incorrect repository field", () => {
});
it("should do nothing if already in good format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();
let defaultBranch = `b${getFakeString(5)}`;

(rootWorkspace.packageJson as any).repository =
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace, {
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/checks/__tests__/INTERNAL_MISMATCH.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import makeCheck, { ErrorType } from "../INTERNAL_MISMATCH";
import { getWS, getFakeWS } from "../../test-helpers";
import { getWS, getFakeWS, getRootWS } from "../../test-helpers";

let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();

describe("internal mismatch", () => {
it("should not error if internal version is compatible", () => {
Expand Down Expand Up @@ -62,22 +62,24 @@ describe("internal mismatch", () => {
{
"dependencyRange": "^0.1.0",
"dependencyWorkspace": {
"dir": "some/fake/dir/pkg-1",
"dir": "fake/monorepo/packages/pkg-1",
"packageJson": {
"name": "pkg-1",
"version": "1.0.0",
},
"relativeDir": "packages/pkg-1",
},
"type": "INTERNAL_MISMATCH",
"workspace": {
"dir": "some/fake/dir/depends-on-one",
"dir": "fake/monorepo/packages/depends-on-one",
"packageJson": {
"devDependencies": {
"pkg-1": "^0.1.0",
},
"name": "depends-on-one",
"version": "1.0.0",
},
"relativeDir": "packages/depends-on-one",
},
},
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import makeCheck from "../INVALID_DEV_AND_PEER_DEPENDENCY_RELATIONSHIP";
import { getWS, getFakeWS } from "../../test-helpers";
import { getWS, getFakeWS, getRootWS } from "../../test-helpers";

let rootWorkspace = getFakeWS("root");
let rootWorkspace = getRootWS();

describe("invalid dev and peer dependency", () => {
describe("internal dependencies", () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/cli/src/checks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type RootCheck<ErrorType> = {
validate: (
rootPackage: Package,
allPackages: Map<string, Package>,
rootWorkspace: Package,
options: Options
) => ErrorType[];
fix?: (
Expand All @@ -37,7 +36,6 @@ type RootCheckWithFix<ErrorType> = {
validate: (
rootPackage: Package,
allPackages: Map<string, Package>,
rootWorkspace: Package,
options: Options
) => ErrorType[];
fix: (
Expand All @@ -52,7 +50,7 @@ type AllCheck<ErrorType> = {
validate: (
workspace: Package,
allWorkspaces: Map<string, Package>,
rootWorkspace: Package,
rootWorkspace: Package | undefined,
options: Options
) => ErrorType[];
fix?: (
Expand All @@ -67,7 +65,7 @@ type AllCheckWithFix<ErrorType> = {
validate: (
workspace: Package,
allWorkspaces: Map<string, Package>,
rootWorkspace: Package,
rootWorkspace: Package | undefined,
options: Options
) => ErrorType[];
fix: (
Expand Down

0 comments on commit a01efc9

Please sign in to comment.