detect-monorepo 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -0
- package/dist/index.d.mts +19 -0
- package/dist/index.mjs +54 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -0
- package/src/detect-monorepo.test.ts +206 -0
- package/src/detect-monorepo.ts +65 -0
- package/src/index.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Thijs Koerselman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# detect-monorepo
|
|
2
|
+
|
|
3
|
+
A tiny, zero-dependency helper that walks upward from a starting directory to
|
|
4
|
+
decide whether it sits inside a JS/TS monorepo workspace.
|
|
5
|
+
|
|
6
|
+
It recognises:
|
|
7
|
+
|
|
8
|
+
- pnpm workspaces (`pnpm-workspace.yaml`)
|
|
9
|
+
- npm, yarn, and bun workspaces (`workspaces` field in `package.json`)
|
|
10
|
+
- Rush (`rush.json`)
|
|
11
|
+
|
|
12
|
+
It is intended for tools that need a cheap pre-check before loading heavier
|
|
13
|
+
monorepo tooling — for example,
|
|
14
|
+
[`isolate-package`](https://github.com/0x80/isolate-package) is only useful
|
|
15
|
+
inside a monorepo, so consumers like
|
|
16
|
+
[`firebase-tools-with-isolate`](https://github.com/0x80/firebase-tools-with-isolate)
|
|
17
|
+
can call `detectMonorepo` first and only pay the cost of loading
|
|
18
|
+
`isolate-package` when a workspace is actually detected.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
pnpm add detect-monorepo
|
|
24
|
+
# or
|
|
25
|
+
npm install detect-monorepo
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { detectMonorepo } from "detect-monorepo";
|
|
32
|
+
|
|
33
|
+
const info = detectMonorepo();
|
|
34
|
+
// or pass an explicit start directory:
|
|
35
|
+
const info = detectMonorepo("/path/to/some/package");
|
|
36
|
+
|
|
37
|
+
if (info) {
|
|
38
|
+
console.log(`Monorepo detected at ${info.rootDir} (kind: ${info.kind})`);
|
|
39
|
+
} else {
|
|
40
|
+
console.log("Not inside a monorepo");
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The function returns either `null` or:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
type MonorepoInfo = {
|
|
48
|
+
/** Absolute path to the monorepo workspace root. */
|
|
49
|
+
rootDir: string;
|
|
50
|
+
/** Which workspace marker was found. "workspaces" covers npm/yarn/bun. */
|
|
51
|
+
kind: "pnpm" | "workspaces" | "rush";
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Behaviour
|
|
56
|
+
|
|
57
|
+
- Walks upward from `startDir` (default: `process.cwd()`) up to four levels
|
|
58
|
+
(the start directory plus three parents), stopping at the filesystem root
|
|
59
|
+
if reached earlier.
|
|
60
|
+
- Returns the first match found while walking upward.
|
|
61
|
+
- A `package.json` that cannot be parsed, or one whose `workspaces` field
|
|
62
|
+
isn't an array or a `{ packages: string[] }` object, is treated as "no
|
|
63
|
+
workspace marker here" — the walk continues upward.
|
|
64
|
+
- Requires Node.js 20 or newer.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT © Thijs Koerselman
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/detect-monorepo.d.ts
|
|
2
|
+
type MonorepoInfo = {
|
|
3
|
+
/** Absolute path to the monorepo workspace root. */rootDir: string; /** Which workspace marker was found. "workspaces" covers npm/yarn/bun. */
|
|
4
|
+
kind: "pnpm" | "workspaces" | "rush";
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Walk upward from `startDir` looking for a monorepo workspace root. Returns
|
|
8
|
+
* null if none is found within `MAX_DEPTH` levels (startDir itself plus three
|
|
9
|
+
* parents) or before reaching the filesystem root.
|
|
10
|
+
*
|
|
11
|
+
* Supported markers:
|
|
12
|
+
* - `pnpm-workspace.yaml`
|
|
13
|
+
* - `package.json` containing a `workspaces` field (npm, yarn, bun)
|
|
14
|
+
* - `rush.json`
|
|
15
|
+
*/
|
|
16
|
+
declare function detectMonorepo(startDir?: string): MonorepoInfo | null;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { type MonorepoInfo, detectMonorepo };
|
|
19
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
//#region src/detect-monorepo.ts
|
|
4
|
+
const MAX_DEPTH = 4;
|
|
5
|
+
/**
|
|
6
|
+
* Walk upward from `startDir` looking for a monorepo workspace root. Returns
|
|
7
|
+
* null if none is found within `MAX_DEPTH` levels (startDir itself plus three
|
|
8
|
+
* parents) or before reaching the filesystem root.
|
|
9
|
+
*
|
|
10
|
+
* Supported markers:
|
|
11
|
+
* - `pnpm-workspace.yaml`
|
|
12
|
+
* - `package.json` containing a `workspaces` field (npm, yarn, bun)
|
|
13
|
+
* - `rush.json`
|
|
14
|
+
*/
|
|
15
|
+
function detectMonorepo(startDir = process.cwd()) {
|
|
16
|
+
let current = path.resolve(startDir);
|
|
17
|
+
for (let i = 0; i < MAX_DEPTH; i++) {
|
|
18
|
+
if (fs.existsSync(path.join(current, "pnpm-workspace.yaml"))) return {
|
|
19
|
+
rootDir: current,
|
|
20
|
+
kind: "pnpm"
|
|
21
|
+
};
|
|
22
|
+
if (fs.existsSync(path.join(current, "rush.json"))) return {
|
|
23
|
+
rootDir: current,
|
|
24
|
+
kind: "rush"
|
|
25
|
+
};
|
|
26
|
+
const pkgPath = path.join(current, "package.json");
|
|
27
|
+
if (fs.existsSync(pkgPath)) try {
|
|
28
|
+
if (hasWorkspacesField(JSON.parse(fs.readFileSync(pkgPath, "utf8")).workspaces)) return {
|
|
29
|
+
rootDir: current,
|
|
30
|
+
kind: "workspaces"
|
|
31
|
+
};
|
|
32
|
+
} catch {}
|
|
33
|
+
const parent = path.dirname(current);
|
|
34
|
+
if (parent === current) return null;
|
|
35
|
+
current = parent;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Accepts either an array of globs or a Yarn-style object with a `packages`
|
|
41
|
+
* array. Anything else is treated as not a workspace root.
|
|
42
|
+
*/
|
|
43
|
+
function hasWorkspacesField(value) {
|
|
44
|
+
if (Array.isArray(value)) return true;
|
|
45
|
+
if (typeof value === "object" && value !== null) {
|
|
46
|
+
const packages = value.packages;
|
|
47
|
+
return Array.isArray(packages);
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { detectMonorepo };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/detect-monorepo.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\n\nexport type MonorepoInfo = {\n /** Absolute path to the monorepo workspace root. */\n rootDir: string;\n /** Which workspace marker was found. \"workspaces\" covers npm/yarn/bun. */\n kind: \"pnpm\" | \"workspaces\" | \"rush\";\n};\n\nconst MAX_DEPTH = 4;\n\n/**\n * Walk upward from `startDir` looking for a monorepo workspace root. Returns\n * null if none is found within `MAX_DEPTH` levels (startDir itself plus three\n * parents) or before reaching the filesystem root.\n *\n * Supported markers:\n * - `pnpm-workspace.yaml`\n * - `package.json` containing a `workspaces` field (npm, yarn, bun)\n * - `rush.json`\n */\nexport function detectMonorepo(\n startDir: string = process.cwd(),\n): MonorepoInfo | null {\n let current = path.resolve(startDir);\n for (let i = 0; i < MAX_DEPTH; i++) {\n if (fs.existsSync(path.join(current, \"pnpm-workspace.yaml\"))) {\n return { rootDir: current, kind: \"pnpm\" };\n }\n if (fs.existsSync(path.join(current, \"rush.json\"))) {\n return { rootDir: current, kind: \"rush\" };\n }\n const pkgPath = path.join(current, \"package.json\");\n if (fs.existsSync(pkgPath)) {\n try {\n const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf8\")) as {\n workspaces?: unknown;\n };\n if (hasWorkspacesField(pkg.workspaces)) {\n return { rootDir: current, kind: \"workspaces\" };\n }\n } catch {\n /** Malformed or unreadable package.json — ignore and continue upward. */\n }\n }\n const parent = path.dirname(current);\n if (parent === current) return null;\n current = parent;\n }\n return null;\n}\n\n/**\n * Accepts either an array of globs or a Yarn-style object with a `packages`\n * array. Anything else is treated as not a workspace root.\n */\nfunction hasWorkspacesField(value: unknown): boolean {\n if (Array.isArray(value)) return true;\n if (typeof value === \"object\" && value !== null) {\n const packages = (value as { packages?: unknown }).packages;\n return Array.isArray(packages);\n }\n return false;\n}\n"],"mappings":";;;AAUA,MAAM,YAAY;;;;;;;;;;;AAYlB,SAAgB,eACd,WAAmB,QAAQ,KAAK,EACX;CACrB,IAAI,UAAU,KAAK,QAAQ,SAAS;AACpC,MAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAAK;AAClC,MAAI,GAAG,WAAW,KAAK,KAAK,SAAS,sBAAsB,CAAC,CAC1D,QAAO;GAAE,SAAS;GAAS,MAAM;GAAQ;AAE3C,MAAI,GAAG,WAAW,KAAK,KAAK,SAAS,YAAY,CAAC,CAChD,QAAO;GAAE,SAAS;GAAS,MAAM;GAAQ;EAE3C,MAAM,UAAU,KAAK,KAAK,SAAS,eAAe;AAClD,MAAI,GAAG,WAAW,QAAQ,CACxB,KAAI;AAIF,OAAI,mBAHQ,KAAK,MAAM,GAAG,aAAa,SAAS,OAAO,CAAC,CAG7B,WAAW,CACpC,QAAO;IAAE,SAAS;IAAS,MAAM;IAAc;UAE3C;EAIV,MAAM,SAAS,KAAK,QAAQ,QAAQ;AACpC,MAAI,WAAW,QAAS,QAAO;AAC/B,YAAU;;AAEZ,QAAO;;;;;;AAOT,SAAS,mBAAmB,OAAyB;AACnD,KAAI,MAAM,QAAQ,MAAM,CAAE,QAAO;AACjC,KAAI,OAAO,UAAU,YAAY,UAAU,MAAM;EAC/C,MAAM,WAAY,MAAiC;AACnD,SAAO,MAAM,QAAQ,SAAS;;AAEhC,QAAO"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "detect-monorepo",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Detect whether a directory sits inside a JS/TS monorepo workspace (pnpm, npm/yarn/bun workspaces, or rush).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bun",
|
|
7
|
+
"detect",
|
|
8
|
+
"monorepo",
|
|
9
|
+
"npm",
|
|
10
|
+
"pnpm",
|
|
11
|
+
"rush",
|
|
12
|
+
"workspace",
|
|
13
|
+
"workspaces",
|
|
14
|
+
"yarn"
|
|
15
|
+
],
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/0x80/detect-monorepo/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Thijs Koerselman",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/0x80/detect-monorepo.git"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./dist/index.mjs",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@codecompose/typescript-config": "^3.2.0",
|
|
39
|
+
"@types/node": "^25.5.2",
|
|
40
|
+
"husky": "^9.1.7",
|
|
41
|
+
"lint-staged": "^16.4.0",
|
|
42
|
+
"oxfmt": "^0.44.0",
|
|
43
|
+
"oxlint": "^1.59.0",
|
|
44
|
+
"oxlint-tsgolint": "^0.20.0",
|
|
45
|
+
"tsdown": "^0.21.7",
|
|
46
|
+
"typescript": "6.0.2",
|
|
47
|
+
"vitest": "^4.1.3"
|
|
48
|
+
},
|
|
49
|
+
"lint-staged": {
|
|
50
|
+
"*.{ts,tsx,js,mjs,cjs}": [
|
|
51
|
+
"oxfmt",
|
|
52
|
+
"oxlint -c .oxlintrc.json"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=20.0.0"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsdown",
|
|
60
|
+
"dev": "tsdown --watch",
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"format": "oxfmt",
|
|
63
|
+
"lint": "oxlint -c .oxlintrc.json --type-aware",
|
|
64
|
+
"check-format": "oxfmt --check",
|
|
65
|
+
"check-types": "tsc --noEmit"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
5
|
+
import { detectMonorepo } from "./detect-monorepo";
|
|
6
|
+
|
|
7
|
+
describe("detectMonorepo", () => {
|
|
8
|
+
let tmpRoot: string;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "detect-monorepo-"));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
fs.rmSync(tmpRoot, { recursive: true, force: true });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("detects a pnpm workspace via pnpm-workspace.yaml", () => {
|
|
19
|
+
fs.writeFileSync(
|
|
20
|
+
path.join(tmpRoot, "pnpm-workspace.yaml"),
|
|
21
|
+
"packages:\n - 'packages/*'\n",
|
|
22
|
+
);
|
|
23
|
+
fs.writeFileSync(
|
|
24
|
+
path.join(tmpRoot, "package.json"),
|
|
25
|
+
JSON.stringify({ name: "root", version: "1.0.0" }),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const result = detectMonorepo(tmpRoot);
|
|
29
|
+
|
|
30
|
+
expect(result).toEqual({
|
|
31
|
+
rootDir: tmpRoot,
|
|
32
|
+
kind: "pnpm",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("detects a workspaces array in package.json (npm/yarn/bun)", () => {
|
|
37
|
+
fs.writeFileSync(
|
|
38
|
+
path.join(tmpRoot, "package.json"),
|
|
39
|
+
JSON.stringify({
|
|
40
|
+
name: "root",
|
|
41
|
+
version: "1.0.0",
|
|
42
|
+
workspaces: ["packages/*"],
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const result = detectMonorepo(tmpRoot);
|
|
47
|
+
|
|
48
|
+
expect(result).toEqual({
|
|
49
|
+
rootDir: tmpRoot,
|
|
50
|
+
kind: "workspaces",
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("detects a workspaces object form in package.json (yarn nohoist)", () => {
|
|
55
|
+
fs.writeFileSync(
|
|
56
|
+
path.join(tmpRoot, "package.json"),
|
|
57
|
+
JSON.stringify({
|
|
58
|
+
name: "root",
|
|
59
|
+
version: "1.0.0",
|
|
60
|
+
workspaces: { packages: ["packages/*"], nohoist: [] },
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const result = detectMonorepo(tmpRoot);
|
|
65
|
+
|
|
66
|
+
expect(result).toEqual({
|
|
67
|
+
rootDir: tmpRoot,
|
|
68
|
+
kind: "workspaces",
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("detects a rush workspace via rush.json", () => {
|
|
73
|
+
fs.writeFileSync(path.join(tmpRoot, "rush.json"), "{}");
|
|
74
|
+
|
|
75
|
+
const result = detectMonorepo(tmpRoot);
|
|
76
|
+
|
|
77
|
+
expect(result).toEqual({
|
|
78
|
+
rootDir: tmpRoot,
|
|
79
|
+
kind: "rush",
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("returns null for a standalone package with no workspace markers", () => {
|
|
84
|
+
fs.writeFileSync(
|
|
85
|
+
path.join(tmpRoot, "package.json"),
|
|
86
|
+
JSON.stringify({ name: "standalone", version: "1.0.0" }),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const result = detectMonorepo(tmpRoot);
|
|
90
|
+
|
|
91
|
+
expect(result).toBeNull();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("finds a marker two levels up from the start directory", () => {
|
|
95
|
+
fs.writeFileSync(
|
|
96
|
+
path.join(tmpRoot, "pnpm-workspace.yaml"),
|
|
97
|
+
"packages:\n - 'packages/*'\n",
|
|
98
|
+
);
|
|
99
|
+
const nested = path.join(tmpRoot, "packages", "api");
|
|
100
|
+
fs.mkdirSync(nested, { recursive: true });
|
|
101
|
+
fs.writeFileSync(
|
|
102
|
+
path.join(nested, "package.json"),
|
|
103
|
+
JSON.stringify({ name: "api", version: "1.0.0" }),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const result = detectMonorepo(nested);
|
|
107
|
+
|
|
108
|
+
expect(result).toEqual({
|
|
109
|
+
rootDir: tmpRoot,
|
|
110
|
+
kind: "pnpm",
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("stops searching after MAX_DEPTH (4) levels", () => {
|
|
115
|
+
fs.writeFileSync(
|
|
116
|
+
path.join(tmpRoot, "pnpm-workspace.yaml"),
|
|
117
|
+
"packages:\n - 'apps/*/functions/src'\n",
|
|
118
|
+
);
|
|
119
|
+
const tooDeep = path.join(tmpRoot, "apps", "firebase", "functions", "src");
|
|
120
|
+
fs.mkdirSync(tooDeep, { recursive: true });
|
|
121
|
+
|
|
122
|
+
const result = detectMonorepo(tooDeep);
|
|
123
|
+
|
|
124
|
+
expect(result).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("finds a marker exactly at MAX_DEPTH (3 levels up)", () => {
|
|
128
|
+
fs.writeFileSync(
|
|
129
|
+
path.join(tmpRoot, "pnpm-workspace.yaml"),
|
|
130
|
+
"packages:\n - 'apps/*/functions'\n",
|
|
131
|
+
);
|
|
132
|
+
const deep = path.join(tmpRoot, "apps", "firebase", "functions");
|
|
133
|
+
fs.mkdirSync(deep, { recursive: true });
|
|
134
|
+
|
|
135
|
+
const result = detectMonorepo(deep);
|
|
136
|
+
|
|
137
|
+
expect(result).toEqual({
|
|
138
|
+
rootDir: tmpRoot,
|
|
139
|
+
kind: "pnpm",
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("ignores a malformed package.json and continues upward", () => {
|
|
144
|
+
fs.writeFileSync(
|
|
145
|
+
path.join(tmpRoot, "pnpm-workspace.yaml"),
|
|
146
|
+
"packages:\n - 'packages/*'\n",
|
|
147
|
+
);
|
|
148
|
+
const nested = path.join(tmpRoot, "packages", "api");
|
|
149
|
+
fs.mkdirSync(nested, { recursive: true });
|
|
150
|
+
fs.writeFileSync(path.join(nested, "package.json"), "{ not valid json");
|
|
151
|
+
|
|
152
|
+
const result = detectMonorepo(nested);
|
|
153
|
+
|
|
154
|
+
expect(result).toEqual({
|
|
155
|
+
rootDir: tmpRoot,
|
|
156
|
+
kind: "pnpm",
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("ignores an unexpected workspaces shape", () => {
|
|
161
|
+
fs.writeFileSync(
|
|
162
|
+
path.join(tmpRoot, "package.json"),
|
|
163
|
+
JSON.stringify({
|
|
164
|
+
name: "root",
|
|
165
|
+
version: "1.0.0",
|
|
166
|
+
workspaces: "packages/*",
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const result = detectMonorepo(tmpRoot);
|
|
171
|
+
|
|
172
|
+
expect(result).toBeNull();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("ignores a workspaces object without a packages array", () => {
|
|
176
|
+
fs.writeFileSync(
|
|
177
|
+
path.join(tmpRoot, "package.json"),
|
|
178
|
+
JSON.stringify({
|
|
179
|
+
name: "root",
|
|
180
|
+
version: "1.0.0",
|
|
181
|
+
workspaces: { nohoist: [] },
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const result = detectMonorepo(tmpRoot);
|
|
186
|
+
|
|
187
|
+
expect(result).toBeNull();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("does not match a package.json without a workspaces field", () => {
|
|
191
|
+
const nested = path.join(tmpRoot, "subdir");
|
|
192
|
+
fs.mkdirSync(nested);
|
|
193
|
+
fs.writeFileSync(
|
|
194
|
+
path.join(tmpRoot, "package.json"),
|
|
195
|
+
JSON.stringify({ name: "root", version: "1.0.0" }),
|
|
196
|
+
);
|
|
197
|
+
fs.writeFileSync(
|
|
198
|
+
path.join(nested, "package.json"),
|
|
199
|
+
JSON.stringify({ name: "child", version: "1.0.0" }),
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const result = detectMonorepo(nested);
|
|
203
|
+
|
|
204
|
+
expect(result).toBeNull();
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export type MonorepoInfo = {
|
|
5
|
+
/** Absolute path to the monorepo workspace root. */
|
|
6
|
+
rootDir: string;
|
|
7
|
+
/** Which workspace marker was found. "workspaces" covers npm/yarn/bun. */
|
|
8
|
+
kind: "pnpm" | "workspaces" | "rush";
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const MAX_DEPTH = 4;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Walk upward from `startDir` looking for a monorepo workspace root. Returns
|
|
15
|
+
* null if none is found within `MAX_DEPTH` levels (startDir itself plus three
|
|
16
|
+
* parents) or before reaching the filesystem root.
|
|
17
|
+
*
|
|
18
|
+
* Supported markers:
|
|
19
|
+
* - `pnpm-workspace.yaml`
|
|
20
|
+
* - `package.json` containing a `workspaces` field (npm, yarn, bun)
|
|
21
|
+
* - `rush.json`
|
|
22
|
+
*/
|
|
23
|
+
export function detectMonorepo(
|
|
24
|
+
startDir: string = process.cwd(),
|
|
25
|
+
): MonorepoInfo | null {
|
|
26
|
+
let current = path.resolve(startDir);
|
|
27
|
+
for (let i = 0; i < MAX_DEPTH; i++) {
|
|
28
|
+
if (fs.existsSync(path.join(current, "pnpm-workspace.yaml"))) {
|
|
29
|
+
return { rootDir: current, kind: "pnpm" };
|
|
30
|
+
}
|
|
31
|
+
if (fs.existsSync(path.join(current, "rush.json"))) {
|
|
32
|
+
return { rootDir: current, kind: "rush" };
|
|
33
|
+
}
|
|
34
|
+
const pkgPath = path.join(current, "package.json");
|
|
35
|
+
if (fs.existsSync(pkgPath)) {
|
|
36
|
+
try {
|
|
37
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")) as {
|
|
38
|
+
workspaces?: unknown;
|
|
39
|
+
};
|
|
40
|
+
if (hasWorkspacesField(pkg.workspaces)) {
|
|
41
|
+
return { rootDir: current, kind: "workspaces" };
|
|
42
|
+
}
|
|
43
|
+
} catch {
|
|
44
|
+
/** Malformed or unreadable package.json — ignore and continue upward. */
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const parent = path.dirname(current);
|
|
48
|
+
if (parent === current) return null;
|
|
49
|
+
current = parent;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Accepts either an array of globs or a Yarn-style object with a `packages`
|
|
56
|
+
* array. Anything else is treated as not a workspace root.
|
|
57
|
+
*/
|
|
58
|
+
function hasWorkspacesField(value: unknown): boolean {
|
|
59
|
+
if (Array.isArray(value)) return true;
|
|
60
|
+
if (typeof value === "object" && value !== null) {
|
|
61
|
+
const packages = (value as { packages?: unknown }).packages;
|
|
62
|
+
return Array.isArray(packages);
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
package/src/index.ts
ADDED