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 470763e1..22c0350a 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -58,7 +58,7 @@ 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)", }, peerDeps: { type: "boolean", @@ -140,7 +140,7 @@ const main = defineCommand({ const formData = new FormData(); - const isCompact = !!args.compact; + let isCompact = args.compact !== false; let packMethod: PackMethod = "npm"; if (args.pnpm) { @@ -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 });