From f490e2679720da045f0a6b0448e098ad09a50f27 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sun, 1 Feb 2026 21:19:06 +0330 Subject: [PATCH 1/4] init --- packages/cli/index.ts | 54 +++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/packages/cli/index.ts b/packages/cli/index.ts index 470763e1..cec2e023 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -126,10 +126,10 @@ const main = defineCommand({ const paths = args._.length > 0 ? await glob(args._, { - expandDirectories: false, - onlyDirectories: true, - absolute: true, - }) + expandDirectories: false, + onlyDirectories: true, + absolute: true, + }) : [process.cwd()]; const templates = await glob(args.template || [], { @@ -140,7 +140,7 @@ const main = defineCommand({ const formData = new FormData(); - const isCompact = !!args.compact; + let isCompact = !!args.compact; let packMethod: PackMethod = "npm"; if (args.pnpm) { @@ -162,8 +162,8 @@ const main = defineCommand({ (args.packageManager as string) .split(",") .filter((s) => s.trim()) as Array< - "npm" | "bun" | "pnpm" | "yarn" - >, + "npm" | "bun" | "pnpm" | "yarn" + >, ), ]; const packageManagers = ["npm", "bun", "pnpm", "yarn"]; @@ -234,7 +234,6 @@ const main = defineCommand({ } const { sha } = await checkResponse.json(); - const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha; const deps: Map = new Map(); // pkg.pr.new versions of the package const realDeps: Map | null = isPeerDepsEnabled @@ -249,6 +248,11 @@ const main = defineCommand({ templates: [], }; + const packageInfos: Array<{ + packageName: string; + pJson: PackageJson; + }> = []; + for (const p of paths) { const pJsonPath = path.resolve(p, "package.json"); const pJson = await readPackageJson(pJsonPath); @@ -264,15 +268,35 @@ const main = defineCommand({ continue; } - if (isCompact) { - await verifyCompactMode(pJson.name); + const packageName = pJson.name; + packageInfos.push({ packageName, pJson }); + } + + if (isCompact) { + for (const { packageName } of packageInfos) { + try { + await verifyCompactMode(packageName); + } catch (error) { + const reason = + error instanceof Error ? error.message : String(error); + console.warn( + `Package ${packageName} cannot use --compact (${reason}). Falling back to non-compact URLs for this run.`, + ); + isCompact = false; + break; + } } + } + + const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha; + + for (const { packageName, pJson } of packageInfos) { const longDepUrl = new URL( - `/${owner}/${repo}/${pJson.name}@${formattedSha}`, + `/${owner}/${repo}/${packageName}@${formattedSha}`, apiUrl, ).href; - deps.set(pJson.name, longDepUrl); - realDeps?.set(pJson.name, pJson.version ?? longDepUrl); + deps.set(packageName, longDepUrl); + realDeps?.set(packageName, pJson.version ?? longDepUrl); const controller = new AbortController(); try { @@ -296,12 +320,12 @@ const main = defineCommand({ controller.abort(); const jsonUrl = isCompact - ? new URL(`/${pJson.name}@${formattedSha}`, apiUrl).href + ? new URL(`/${packageName}@${formattedSha}`, apiUrl).href : longDepUrl; // Collect package metadata outputMetadata.packages.push({ - name: pJson.name, + name: packageName, url: jsonUrl, shasum: "", // will be filled later }); From a62fb6193ac7200abbdee8535f68318b9431a207 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sun, 1 Feb 2026 21:21:55 +0330 Subject: [PATCH 2/4] prettier --- packages/cli/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/cli/index.ts b/packages/cli/index.ts index cec2e023..586b3d26 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -126,10 +126,10 @@ const main = defineCommand({ const paths = args._.length > 0 ? await glob(args._, { - expandDirectories: false, - onlyDirectories: true, - absolute: true, - }) + expandDirectories: false, + onlyDirectories: true, + absolute: true, + }) : [process.cwd()]; const templates = await glob(args.template || [], { @@ -162,8 +162,8 @@ const main = defineCommand({ (args.packageManager as string) .split(",") .filter((s) => s.trim()) as Array< - "npm" | "bun" | "pnpm" | "yarn" - >, + "npm" | "bun" | "pnpm" | "yarn" + >, ), ]; const packageManagers = ["npm", "bun", "pnpm", "yarn"]; From c646d5da9da4a5d0de9605ad8f9bb26e13695fe0 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sat, 7 Feb 2026 16:08:02 +0330 Subject: [PATCH 3/4] update --- packages/cli/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/cli/index.ts b/packages/cli/index.ts index 586b3d26..c723e45b 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -58,7 +58,12 @@ const main = defineCommand({ compact: { type: "boolean", description: - "compact urls. The shortest form of urls like pkg.pr.new/tinybench@a832a55)", + "compact urls (default). The shortest form of urls like pkg.pr.new/tinybench@a832a55)", + }, + "no-compact": { + type: "boolean", + description: "disable compact urls and always use long form", + default: false, }, peerDeps: { type: "boolean", @@ -140,7 +145,7 @@ const main = defineCommand({ const formData = new FormData(); - let isCompact = !!args.compact; + let isCompact = args["no-compact"] ? false : args.compact !== false; let packMethod: PackMethod = "npm"; if (args.pnpm) { From 143be26095e37d981388e3831ef9ad8f99e4cf84 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sat, 7 Feb 2026 18:31:10 +0330 Subject: [PATCH 4/4] update --- README.md | 8 ++++---- packages/cli/index.ts | 7 +------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8c91fdf0..19e0ee17 100644 --- a/README.md +++ b/README.md @@ -150,15 +150,15 @@ npx pkg-pr-new publish './packages/A' --template './examples/*' By default, pkg.pr.new will generate a template called "default" which includes each built package in the dependencies. This can be disabled with `--no-template`. -For shorter urls, `--compact` can be useful: +Compact URLs are the default (and will fall back to long form if npm metadata is unavailable). To force long-form URLs, use `--no-compact`: ```sh -npx pkg-pr-new publish --compact './packages/A' './packages/B' +npx pkg-pr-new publish --no-compact './packages/A' './packages/B' ``` -> `--compact` requires your package to be a valid (published) package on npm with a specified `repository` field in the package.json! See [this](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#repository). pkg.pr.new is case sensitive, if the GitHub owner is `PuruVJ`, the package.json `repository` field should not have `puruvj`. +> Compact URLs rely on your package being published on npm with a valid `repository` field in `package.json`. See [this](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#repository). pkg.pr.new is case sensitive, if the GitHub owner is `PuruVJ`, the package.json `repository` field should not have `puruvj`. -With `--compact`: +With default compact URLs: ```sh npm i https://pkg.pr.new/tinybench@a832a55 diff --git a/packages/cli/index.ts b/packages/cli/index.ts index c723e45b..22c0350a 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -60,11 +60,6 @@ const main = defineCommand({ description: "compact urls (default). The shortest form of urls like pkg.pr.new/tinybench@a832a55)", }, - "no-compact": { - type: "boolean", - description: "disable compact urls and always use long form", - default: false, - }, peerDeps: { type: "boolean", description: @@ -145,7 +140,7 @@ const main = defineCommand({ const formData = new FormData(); - let isCompact = args["no-compact"] ? false : args.compact !== false; + let isCompact = args.compact !== false; let packMethod: PackMethod = "npm"; if (args.pnpm) {