Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ For templates (experimental):
pnpm exec pkg-pr-new publish './packages/A' --template './examples/*'
```

`catalog:` and `catalog:<name>` dependencies in a template's `package.json` are resolved from the nearest `pnpm-workspace.yaml`, so templates can use [pnpm catalogs](https://pnpm.io/catalogs).
`catalog:` and `catalog:<name>` dependencies in a template's `package.json` are resolved from the nearest `pnpm-workspace.yaml`, so templates can use [pnpm catalogs](https://pnpm.io/catalogs). Catalogs delegated to other files through [`include`](https://pnpm.io/config-includes) are resolved as well. Catalogs owned by a config dependency cannot be resolved and are left as-is.

**`--no-template`**

Expand All @@ -270,7 +270,7 @@ By default, pkg.pr.new will generate a template called "default" which includes
pkg.pr.new uses `npm pack --json` under the hood, in case you face issues, you can also use the `--pnpm`, `--yarn`, or `--bun` flag so it starts using `pnpm pack`, `yarn pack`, or `bun pm pack`. This is not necessary in most cases.

> [!IMPORTANT]
> If your workspace uses **pnpm catalogs** (the `catalog:` protocol in `package.json` dependencies, configured via `catalog` in `pnpm-workspace.yaml`), you **must** pass the `--pnpm` flag. `npm pack` does not understand the `catalog:` protocol and will fail to resolve those dependencies, whereas `pnpm pack` resolves them correctly.
> If your workspace uses **pnpm catalogs** (the `catalog:` protocol in `package.json` dependencies, configured via `catalog` in `pnpm-workspace.yaml`, directly or through [`include`](https://pnpm.io/config-includes)), you **must** pass the `--pnpm` flag. `npm pack` does not understand the `catalog:` protocol and will fail to resolve those dependencies, whereas `pnpm pack` resolves them correctly.
>
> ```sh
> pnpm exec pkg-pr-new publish --pnpm './packages/*'
Expand Down
186 changes: 185 additions & 1 deletion packages/cli/catalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { PackageJson } from "pkg-types";
import { resolveCatalogs } from "./catalog";

Expand All @@ -22,6 +22,12 @@ describe("resolveCatalogs", () => {
const writeWorkspace = (contents: string) =>
fs.writeFile(path.join(workspaceDir, "pnpm-workspace.yaml"), contents);

const writeIncludedFile = async (rel: string, contents: string) => {
const filePath = path.join(workspaceDir, rel);
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, contents);
};

it("resolves the default catalog", async () => {
await writeWorkspace("catalog:\n vite: ^6.0.0\n");
const pJson: PackageJson = { devDependencies: { vite: "catalog:" } };
Expand Down Expand Up @@ -97,4 +103,182 @@ describe("resolveCatalogs", () => {

expect(pJson.devDependencies).toEqual({ vite: "catalog:" });
});

it("resolves catalogs from files included through include", async () => {
await writeWorkspace(
[
"include:",
" ./config/catalog.yaml: [catalog]",
" ./config/named.yaml: [catalogs.react18, catalogs.tooling]",
"",
].join("\n"),
);
await writeIncludedFile(
"config/catalog.yaml",
"catalog:\n vite: ^6.0.0\n",
);
await writeIncludedFile(
"config/named.yaml",
[
"catalogs:",
" react18:",
" react: ^18.3.1",
" tooling:",
" typescript: ~5.7.0",
"",
].join("\n"),
);
const pJson: PackageJson = {
dependencies: { vite: "catalog:" },
devDependencies: {
react: "catalog:react18",
typescript: "catalog:tooling",
},
};

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "^6.0.0" });
expect(pJson.devDependencies).toEqual({
react: "^18.3.1",
typescript: "~5.7.0",
});
});

it("resolves catalogs from a '*' delegated file", async () => {
await writeWorkspace("include:\n ./config/all.yaml: '*'\n");
await writeIncludedFile(
"config/all.yaml",
[
"catalog:",
" vite: ^6.0.0",
"catalogs:",
" vue:",
" vue: ^3.5.0",
"",
].join("\n"),
);
const pJson: PackageJson = {
dependencies: { vite: "catalog:", vue: "catalog:vue" },
};

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "^6.0.0", vue: "^3.5.0" });
});

it("resolves every catalog from a [catalogs] delegated file", async () => {
await writeWorkspace("include:\n ./config/all.yaml: [catalogs]\n");
await writeIncludedFile(
"config/all.yaml",
[
"catalog:",
" vite: ^6.0.0",
"catalogs:",
" react18:",
" react: ^18.3.1",
"",
].join("\n"),
);
const pJson: PackageJson = {
dependencies: { vite: "catalog:", react: "catalog:react18" },
};

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "^6.0.0", react: "^18.3.1" });
});

it("combines root catalogs with included catalogs", async () => {
await writeWorkspace(
[
"catalog:",
" vite: ^6.0.0",
"include:",
" ./config/named.yaml: [catalogs.react18]",
"",
].join("\n"),
);
await writeIncludedFile(
"config/named.yaml",
"catalogs:\n react18:\n react: ^18.3.1\n",
);
const pJson: PackageJson = {
dependencies: { vite: "catalog:", react: "catalog:react18" },
};

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "^6.0.0", react: "^18.3.1" });
});

it("ignores catalogs a file is not delegated", async () => {
await writeWorkspace("include:\n ./config/one.yaml: [catalogs.react18]\n");
await writeIncludedFile(
"config/one.yaml",
[
"catalogs:",
" react18:",
" react: ^18.3.1",
" vue:",
" vue: ^3.5.0",
"",
].join("\n"),
);
const pJson: PackageJson = {
dependencies: { react: "catalog:react18", vue: "catalog:vue" },
};

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({
react: "^18.3.1",
vue: "catalog:vue",
});
});

it("warns and leaves catalog: unresolved when owned by a config dependency", async () => {
await writeWorkspace("include:\n '@acme/pnpm-policy': [catalog]\n");
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const pJson: PackageJson = { dependencies: { vite: "catalog:" } };

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "catalog:" });
expect(warn).toHaveBeenCalledWith(
expect.stringContaining("@acme/pnpm-policy"),
);

warn.mockRestore();
});

it("does not warn about config dependencies without catalog usage", async () => {
await writeWorkspace(
"include:\n '@acme/pnpm-policy': [minimumReleaseAge]\n",
);
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const pJson: PackageJson = { dependencies: { vite: "^6.0.0" } };

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "^6.0.0" });
expect(warn).not.toHaveBeenCalled();

warn.mockRestore();
});

it("warns and continues when an included file cannot be read", async () => {
await writeWorkspace("include:\n ./config/missing.yaml: [catalog]\n");
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const pJson: PackageJson = { dependencies: { vite: "catalog:" } };

await resolveCatalogs(templateDir, pJson);

expect(pJson.dependencies).toEqual({ vite: "catalog:" });
expect(warn).toHaveBeenCalledWith(
expect.stringContaining("./config/missing.yaml"),
);

warn.mockRestore();
});
});
127 changes: 122 additions & 5 deletions packages/cli/catalog.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import fs from "node:fs/promises";
import path from "node:path";
import { parseYAML } from "confbox";
import { findNearestFile, type PackageJson } from "pkg-types";

type Catalog = Record<string, string>;
type Catalogs = Record<string, Catalog | undefined>;
type IncludeSettings = "*" | string[] | undefined;

interface PnpmWorkspace {
catalog?: Catalog;
catalogs?: Catalogs;
include?: Record<string, IncludeSettings>;
}

const CATALOG_PROTOCOL = "catalog:";
Expand All @@ -20,10 +23,11 @@ const dependencyFields = [
] as const;

export async function resolveCatalogs(dir: string, pJson: PackageJson) {
const catalogs = await readCatalogs(dir);
const { catalogs, configDependencyOwners } = await readCatalogs(dir);
if (!catalogs) {
return;
}
let hasUnresolvedSpec = false;
for (const field of dependencyFields) {
const deps = pJson[field];
if (!deps) {
Expand All @@ -33,22 +37,135 @@ export async function resolveCatalogs(dir: string, pJson: PackageJson) {
const version = resolveCatalogSpec(catalogs, name, spec);
if (version) {
deps[name] = version;
} else if (spec.startsWith(CATALOG_PROTOCOL)) {
hasUnresolvedSpec = true;
}
}
}
if (hasUnresolvedSpec && configDependencyOwners.length > 0) {
console.warn(
`pkg-pr-new: cannot resolve 'catalog:' dependencies whose catalog is owned by the config dependenc${configDependencyOwners.length > 1 ? "ies" : "y"} ${configDependencyOwners.map((owner) => `'${owner}'`).join(", ")}. They are left as-is.`,
);
}
}

async function readCatalogs(dir: string): Promise<Catalogs | null> {
async function readCatalogs(dir: string): Promise<{
catalogs: Catalogs | null;
configDependencyOwners: string[];
}> {
const workspacePath = await findNearestFile("pnpm-workspace.yaml", {
startingFrom: dir,
}).catch(() => null);
if (!workspacePath) {
return null;
return { catalogs: null, configDependencyOwners: [] };
}
const { catalog, catalogs } =
const workspaceRoot = path.dirname(workspacePath);
const workspace =
parseYAML<PnpmWorkspace | null>(await fs.readFile(workspacePath, "utf8")) ??
{};
return { ...catalogs, default: catalog ?? catalogs?.default };
const catalogs: Catalogs = {
...workspace.catalogs,
default: workspace.catalog ?? workspace.catalogs?.default,
};
const configDependencyOwners = await mergeIncludedCatalogs(
workspace,
workspaceRoot,
catalogs,
);
return { catalogs, configDependencyOwners };
}

// pnpm-workspace.yaml can delegate settings to other files through `include`
// (https://pnpm.io/config-includes), so catalogs may live outside the root
// workspace file. Each catalog has exactly one owner, so merge only what each
// source is delegated. `include` itself is never valid inside an included
// file, hence no recursion.
async function mergeIncludedCatalogs(
workspace: PnpmWorkspace,
workspaceRoot: string,
catalogs: Catalogs,
): Promise<string[]> {
const configDependencyOwners: string[] = [];
for (const [source, settings] of Object.entries(workspace.include ?? {})) {
if (!ownsCatalogSettings(settings)) {
continue;
}
if (!source.startsWith("./") && !source.startsWith("../")) {
configDependencyOwners.push(source);
continue;
}
const included = await readIncludedWorkspace(source, workspaceRoot);
if (!included) {
continue;
}
for (const [name, catalog] of Object.entries(
extractDelegatedCatalogs(included, settings),
)) {
if (!catalogs[name]) {
catalogs[name] = catalog;
}
}
}
return configDependencyOwners;
}

async function readIncludedWorkspace(
source: string,
workspaceRoot: string,
): Promise<PnpmWorkspace | null> {
try {
const contents = await fs.readFile(
path.resolve(workspaceRoot, source),
"utf8",
);
return parseYAML<PnpmWorkspace | null>(contents) ?? {};
} catch (error) {
console.warn(
`pkg-pr-new: failed to read '${source}', included from ${path.join(workspaceRoot, "pnpm-workspace.yaml")}, while resolving catalogs: ${error instanceof Error ? error.message : String(error)}`,
);
return null;
}
}

function ownsCatalogSettings(settings: IncludeSettings) {
return (
settings === "*" ||
!!settings?.some(
(setting) =>
setting === "catalog" ||
setting === "catalogs" ||
setting.startsWith("catalogs."),
)
);
}

function extractDelegatedCatalogs(
workspace: PnpmWorkspace,
settings: IncludeSettings,
): Catalogs {
const extracted: Catalogs = {};
const defaultCatalog = workspace.catalog ?? workspace.catalogs?.default;
if (
defaultCatalog &&
(settings === "*" ||
settings?.includes("catalog") ||
settings?.includes("catalogs.default") ||
settings?.includes("catalogs"))
) {
extracted.default = defaultCatalog;
}
for (const [name, catalog] of Object.entries(workspace.catalogs ?? {})) {
if (
name !== "default" &&
catalog &&
(settings === "*" ||
settings?.includes("catalogs") ||
settings?.includes(`catalogs.${name}`))
) {
extracted[name] = catalog;
}
}
return extracted;
}

function resolveCatalogSpec(catalogs: Catalogs, name: string, spec: string) {
Expand Down
Loading