Skip to content

Commit 3c9641c

Browse files
Fix start directory for getPackages (#201)
1 parent 52d5dcb commit 3c9641c

File tree

3 files changed

+73
-47
lines changed

3 files changed

+73
-47
lines changed

.changeset/breezy-readers-chew.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@manypkg/get-packages": patch
3+
---
4+
5+
Fixed the error for getPackages when given a non-root directory

packages/get-packages/src/index.test.ts

+66-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fixturez from "fixturez";
2+
import path from 'node:path';
23
import { getPackages, getPackagesSync } from "./";
34

45
const f = fixturez(__dirname);
@@ -7,19 +8,24 @@ type GetPackages = typeof getPackages | typeof getPackagesSync;
78

89
let runTests = (getPackages: GetPackages) => {
910
it("should resolve workspaces for yarn", async () => {
10-
const allPackages = await getPackages(f.copy("yarn-workspace-base"));
11+
const dir = f.copy("yarn-workspace-base");
1112

12-
if (allPackages.packages === null) {
13-
return expect(allPackages.packages).not.toBeNull();
14-
}
13+
// Test for both root and subdirectories
14+
for (const location of ['.', 'packages', 'packages/pkg-a']) {
15+
const allPackages = await getPackages(path.join(dir, location));
1516

16-
expect(allPackages.packages[0].packageJson.name).toEqual(
17-
"yarn-workspace-base-pkg-a"
18-
);
19-
expect(allPackages.packages[1].packageJson.name).toEqual(
20-
"yarn-workspace-base-pkg-b"
21-
);
22-
expect(allPackages.tool.type).toEqual("yarn");
17+
if (allPackages.packages === null) {
18+
return expect(allPackages.packages).not.toBeNull();
19+
}
20+
21+
expect(allPackages.packages[0].packageJson.name).toEqual(
22+
"yarn-workspace-base-pkg-a"
23+
);
24+
expect(allPackages.packages[1].packageJson.name).toEqual(
25+
"yarn-workspace-base-pkg-b"
26+
);
27+
expect(allPackages.tool.type).toEqual("yarn");
28+
}
2329
});
2430

2531
it("should resolve yarn workspaces if the yarn option is passed and packages field is used", async () => {
@@ -38,35 +44,45 @@ let runTests = (getPackages: GetPackages) => {
3844
});
3945

4046
it("should resolve workspaces for bolt", async () => {
41-
const allPackages = await getPackages(f.copy("bolt-workspace"));
47+
const dir = f.copy("bolt-workspace");
4248

43-
if (allPackages.packages === null) {
44-
return expect(allPackages.packages).not.toBeNull();
45-
}
49+
// Test for both root and subdirectories
50+
for (const location of ['.', 'packages', 'packages/pkg-b']) {
51+
const allPackages = await getPackages(path.join(dir, location));
4652

47-
expect(allPackages.packages[0].packageJson.name).toEqual(
48-
"bolt-workspace-pkg-a"
49-
);
50-
expect(allPackages.packages[1].packageJson.name).toEqual(
51-
"bolt-workspace-pkg-b"
52-
);
53-
expect(allPackages.tool.type).toEqual("bolt");
53+
if (allPackages.packages === null) {
54+
return expect(allPackages.packages).not.toBeNull();
55+
}
56+
57+
expect(allPackages.packages[0].packageJson.name).toEqual(
58+
"bolt-workspace-pkg-a"
59+
);
60+
expect(allPackages.packages[1].packageJson.name).toEqual(
61+
"bolt-workspace-pkg-b"
62+
);
63+
expect(allPackages.tool.type).toEqual("bolt");
64+
}
5465
});
5566

5667
it("should resolve workspaces for pnpm", async () => {
57-
const allPackages = await getPackages(f.copy("pnpm-workspace-base"));
68+
const dir = f.copy("pnpm-workspace-base");
5869

59-
if (allPackages.packages === null) {
60-
return expect(allPackages.packages).not.toBeNull();
61-
}
70+
// Test for both root and subdirectories
71+
for (const location of ['.', 'packages', 'packages/pkg-a']) {
72+
const allPackages = await getPackages(path.join(dir, location));
6273

63-
expect(allPackages.packages[0].packageJson.name).toEqual(
64-
"pnpm-workspace-base-pkg-a"
65-
);
66-
expect(allPackages.packages[1].packageJson.name).toEqual(
67-
"pnpm-workspace-base-pkg-b"
68-
);
69-
expect(allPackages.tool.type).toEqual("pnpm");
74+
if (allPackages.packages === null) {
75+
return expect(allPackages.packages).not.toBeNull();
76+
}
77+
78+
expect(allPackages.packages[0].packageJson.name).toEqual(
79+
"pnpm-workspace-base-pkg-a"
80+
);
81+
expect(allPackages.packages[1].packageJson.name).toEqual(
82+
"pnpm-workspace-base-pkg-b"
83+
);
84+
expect(allPackages.tool.type).toEqual("pnpm");
85+
}
7086
});
7187

7288
it("should resolve workspace for pnpm with exclude rules", async () => {
@@ -85,20 +101,25 @@ let runTests = (getPackages: GetPackages) => {
85101
});
86102

87103
it("should resolve workspaces for lerna", async () => {
88-
const allPackages = await getPackages(f.copy("lerna-workspace-base"));
104+
const dir = f.copy("lerna-workspace-base");
89105

90-
if (allPackages.packages === null) {
91-
return expect(allPackages.packages).not.toBeNull();
92-
}
106+
// Test for both root and subdirectories
107+
for (const location of ['.', 'packages', 'packages/pkg-b']) {
108+
const allPackages = await getPackages(path.join(dir, location));
93109

94-
expect(allPackages.packages[0].packageJson.name).toEqual(
95-
"lerna-workspace-base-pkg-a"
96-
);
97-
expect(allPackages.packages[1].packageJson.name).toEqual(
98-
"lerna-workspace-base-pkg-b"
99-
);
100-
expect(allPackages.packages).toHaveLength(2);
101-
expect(allPackages.tool.type).toEqual("lerna");
110+
if (allPackages.packages === null) {
111+
return expect(allPackages.packages).not.toBeNull();
112+
}
113+
114+
expect(allPackages.packages[0].packageJson.name).toEqual(
115+
"lerna-workspace-base-pkg-a"
116+
);
117+
expect(allPackages.packages[1].packageJson.name).toEqual(
118+
"lerna-workspace-base-pkg-b"
119+
);
120+
expect(allPackages.packages).toHaveLength(2);
121+
expect(allPackages.tool.type).toEqual("lerna");
122+
}
102123
});
103124

104125
it("should resolve workspaces for lerna without explicit packages config", async () => {

packages/get-packages/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function getPackages(
3737
options?: GetPackagesOptions
3838
): Promise<Packages> {
3939
const monorepoRoot: MonorepoRoot = await findRoot(dir, options);
40-
const packages: Packages = await monorepoRoot.tool.getPackages(dir);
40+
const packages: Packages = await monorepoRoot.tool.getPackages(monorepoRoot.rootDir);
4141

4242
validatePackages(packages);
4343

@@ -52,7 +52,7 @@ export function getPackagesSync(
5252
options?: GetPackagesOptions
5353
): Packages {
5454
const monorepoRoot: MonorepoRoot = findRootSync(dir, options);
55-
const packages: Packages = monorepoRoot.tool.getPackagesSync(dir);
55+
const packages: Packages = monorepoRoot.tool.getPackagesSync(monorepoRoot.rootDir);
5656

5757
validatePackages(packages);
5858

0 commit comments

Comments
 (0)