Skip to content

Commit

Permalink
Fix start directory for getPackages (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk authored Mar 15, 2024
1 parent 52d5dcb commit 3c9641c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-readers-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/get-packages": patch
---

Fixed the error for getPackages when given a non-root directory
111 changes: 66 additions & 45 deletions packages/get-packages/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fixturez from "fixturez";
import path from 'node:path';
import { getPackages, getPackagesSync } from "./";

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

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

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

expect(allPackages.packages[0].packageJson.name).toEqual(
"yarn-workspace-base-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"yarn-workspace-base-pkg-b"
);
expect(allPackages.tool.type).toEqual("yarn");
if (allPackages.packages === null) {
return expect(allPackages.packages).not.toBeNull();
}

expect(allPackages.packages[0].packageJson.name).toEqual(
"yarn-workspace-base-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"yarn-workspace-base-pkg-b"
);
expect(allPackages.tool.type).toEqual("yarn");
}
});

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

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

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

expect(allPackages.packages[0].packageJson.name).toEqual(
"bolt-workspace-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"bolt-workspace-pkg-b"
);
expect(allPackages.tool.type).toEqual("bolt");
if (allPackages.packages === null) {
return expect(allPackages.packages).not.toBeNull();
}

expect(allPackages.packages[0].packageJson.name).toEqual(
"bolt-workspace-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"bolt-workspace-pkg-b"
);
expect(allPackages.tool.type).toEqual("bolt");
}
});

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

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

expect(allPackages.packages[0].packageJson.name).toEqual(
"pnpm-workspace-base-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"pnpm-workspace-base-pkg-b"
);
expect(allPackages.tool.type).toEqual("pnpm");
if (allPackages.packages === null) {
return expect(allPackages.packages).not.toBeNull();
}

expect(allPackages.packages[0].packageJson.name).toEqual(
"pnpm-workspace-base-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"pnpm-workspace-base-pkg-b"
);
expect(allPackages.tool.type).toEqual("pnpm");
}
});

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

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

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

expect(allPackages.packages[0].packageJson.name).toEqual(
"lerna-workspace-base-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"lerna-workspace-base-pkg-b"
);
expect(allPackages.packages).toHaveLength(2);
expect(allPackages.tool.type).toEqual("lerna");
if (allPackages.packages === null) {
return expect(allPackages.packages).not.toBeNull();
}

expect(allPackages.packages[0].packageJson.name).toEqual(
"lerna-workspace-base-pkg-a"
);
expect(allPackages.packages[1].packageJson.name).toEqual(
"lerna-workspace-base-pkg-b"
);
expect(allPackages.packages).toHaveLength(2);
expect(allPackages.tool.type).toEqual("lerna");
}
});

it("should resolve workspaces for lerna without explicit packages config", async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/get-packages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getPackages(
options?: GetPackagesOptions
): Promise<Packages> {
const monorepoRoot: MonorepoRoot = await findRoot(dir, options);
const packages: Packages = await monorepoRoot.tool.getPackages(dir);
const packages: Packages = await monorepoRoot.tool.getPackages(monorepoRoot.rootDir);

validatePackages(packages);

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

validatePackages(packages);

Expand Down

0 comments on commit 3c9641c

Please sign in to comment.