@anizoptera/publish-clean 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +151 -216
  2. package/dist/cli.js +449 -294
  3. package/package.json +3 -8
package/dist/cli.js CHANGED
@@ -1,12 +1,184 @@
1
1
  #!/usr/bin/env node
2
2
  import { execFileSync } from "node:child_process";
3
- import { readFileSync, statSync } from "node:fs";
4
- import { copyFile, mkdir, mkdtemp, readdir, rm, stat, writeFile } from "node:fs/promises";
3
+ import { readFileSync } from "node:fs";
4
+ import { copyFile, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
5
5
  import { tmpdir } from "node:os";
6
6
  import path from "node:path";
7
7
  import { parseArgs } from "node:util";
8
- //#region src/cli.ts
9
- const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.catalogs.patchedDependencies.private.stylelint.trustedDependencies.wireit.devDependencies.overrides.resolutions.ava.c8.jest.mocha.nyc.tap.vitest.babel.biome.browserslist.eslintConfig.eslintIgnore.prettier.remarkConfig.xo.commitlint.husky.lint-staged.simple-git-hooks.clean-publish.publish-clean.semantic-release.packageManager.pnpm.volta.workspaces.nx.turbo".split(".")), CONSUMER_SCRIPTS = /* @__PURE__ */ new Set([
8
+ import { gunzipSync, gzipSync } from "node:zlib";
9
+ //#region src/error.ts
10
+ /**
11
+ * The one failure type every module here throws, so `cli.ts` can tell a refusal this tool
12
+ * decided on — which is already phrased for the operator and needs no decoration — from a
13
+ * crash, whose message would otherwise reach them raw.
14
+ */
15
+ var PublishCleanError = class extends Error {
16
+ constructor(message, options) {
17
+ super(message, options), this.name = "PublishCleanError";
18
+ }
19
+ };
20
+ //#endregion
21
+ //#region src/json.ts
22
+ function isObject(value) {
23
+ return typeof value == "object" && !!value && !Array.isArray(value);
24
+ }
25
+ /**
26
+ * The form written into the published tarball: npm's own two-space indentation with a trailing
27
+ * newline, and key order left exactly as the packer emitted it. Preserving order is what makes
28
+ * the artifact byte-reproducible across runs, since cleaning only ever deletes keys.
29
+ */
30
+ function stringifyJson(value) {
31
+ return `${JSON.stringify(value, null, 2)}\n`;
32
+ }
33
+ /**
34
+ * The form used only to compare two manifests for equality. Key order carries no meaning to a
35
+ * consumer, so comparing the written form would report a difference where none exists.
36
+ */
37
+ function stableJson(value) {
38
+ return JSON.stringify(stableValue(value));
39
+ }
40
+ function stableValue(value) {
41
+ return Array.isArray(value) ? value.map(stableValue) : isObject(value) ? Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, child]) => [key, stableValue(child)])) : value;
42
+ }
43
+ //#endregion
44
+ //#region src/artifact.ts
45
+ /**
46
+ * What the published tarball may carry, and what it must. Every rule here judges a file list —
47
+ * the paths `tar` reads out of the archive, with the `package/` prefix already stripped — never
48
+ * a directory on disk, because that list is what a consumer's installer unpacks and it needs no
49
+ * filesystem to exist.
50
+ *
51
+ * Ambient inputs arrive as parameters and never by reaching for them — no process, filesystem or
52
+ * argv here. `cli.ts` owns the effects.
53
+ */
54
+ /**
55
+ * Content that must never reach a registry. `^` means the package root.
56
+ *
57
+ * Case-insensitive throughout, because the filesystems most packages are built on are
58
+ * too. On macOS and Windows `Server.PEM` and `server.pem` are the same file, so a
59
+ * case-sensitive guard refuses one and publishes the other, which is worse than no guard:
60
+ * it reads as coverage.
61
+ *
62
+ * Private keys are matched by extension and, separately, by exact filename. SSH keys carry
63
+ * no extension at all, and `id_rsa` is the most common private-key filename there is. The
64
+ * exact-name form deliberately does not match `id_rsa.pub`, which is public by design and
65
+ * legitimate to ship.
66
+ */
67
+ const CRITICAL_PATTERNS = [
68
+ /(?:^|\/)node_modules(?:\/|$)/i,
69
+ /(?:^|\/)\.git(?:\/|$)/i,
70
+ /(?:^|\/)\.env(?:\.|$)/i,
71
+ /(?:^|\/)\.npmrc$/i,
72
+ /\.(?:pem|key|p12|pfx|p8|ppk|jks|keystore)$/i,
73
+ /(?:^|\/)id_(?:rsa|dsa|ecdsa|ed25519)$/i
74
+ ], SUSPICIOUS_PATTERNS = [
75
+ /(?:^|\/)(?:test|tests|__tests__|__snapshots__|coverage)(?:\/|$)/,
76
+ /(?:^|\/)\.github(?:\/|$)/,
77
+ /(?:^|\/)tsconfig[^/]*\.json$/,
78
+ /(?:^|\/)(?:pnpm-lock\.yaml|bun\.lock|package-lock\.json)$/,
79
+ /\.(?:test|spec)\.[cm]?[jt]sx?$/
80
+ ];
81
+ function validatePackedFiles(files, skipSuspicious) {
82
+ let critical = files.filter((file) => CRITICAL_PATTERNS.some((pattern) => pattern.test(file)));
83
+ if (critical.length > 0) throw new PublishCleanError(`Critical files must not be published:\n${critical.join("\n")}`);
84
+ if (skipSuspicious) return;
85
+ let suspicious = files.filter((file) => SUSPICIOUS_PATTERNS.some((pattern) => pattern.test(file)));
86
+ if (suspicious.length > 0) throw new PublishCleanError(`Suspicious files in package artifact:\n${suspicious.join("\n")}`);
87
+ }
88
+ /**
89
+ * Refuses a rewritten tarball whose file set is not the packed one, entry for entry.
90
+ *
91
+ * Replacing a member is supposed to change one member, and the rewriter copies every other
92
+ * entry as opaque bytes — so this can only fail through a defect in that code, which is exactly
93
+ * why it is worth asserting on the step that cannot be taken back. Nothing else would notice a
94
+ * dropped file: the leak checks only ask what is present, and a package missing a file installs
95
+ * fine and then fails at import, on a version the registry keeps forever.
96
+ *
97
+ * Both lists are read out of the archives by `tar`, an instrument that cannot share a mistake
98
+ * with the walk being checked.
99
+ */
100
+ function assertSameEntries(packed, published) {
101
+ let before = new Set(packed), after = new Set(published), changed = [...packed.filter((file) => !after.has(file)).map((file) => `- ${file}`), ...published.filter((file) => !before.has(file)).map((file) => `+ ${file}`)];
102
+ if (changed.length > 0) throw new PublishCleanError(`Rewriting the tarball manifest changed its file set:\n${changed.join("\n")}`);
103
+ }
104
+ /**
105
+ * Walks a manifest value of any shape and collects the strings that name a file inside
106
+ * the package, so the caller can prove each one exists in the tarball.
107
+ *
108
+ * `mode` exists because the two families of path-bearing fields disagree about what a
109
+ * bare string means, and reading one by the other's rules produces wrong answers in both
110
+ * directions:
111
+ *
112
+ * - `"every-string"` for `main`, `module`, `types`, `typings`, `bin` and `typesVersions`,
113
+ * where every string is a path into this package.
114
+ * - `"relative-only"` for `exports`, `imports`, `sideEffects` and the object form of
115
+ * `browser`, where a string may just as well be an external package name, a condition
116
+ * target or a glob. Only a `./` or `../` prefix marks it as a path here. Collecting the
117
+ * rest would report a missing file for something that was never a file.
118
+ *
119
+ * Booleans are skipped rather than ignored by accident: `sideEffects: false` and
120
+ * `exports` condition values legitimately hold them.
121
+ */
122
+ function collectDeclaredPaths(value, out, mode) {
123
+ if (typeof value == "string") {
124
+ (mode === "every-string" || value.startsWith("./") || value.startsWith("../")) && out.push(value);
125
+ return;
126
+ }
127
+ if (typeof value != "boolean") {
128
+ if (Array.isArray(value)) {
129
+ for (let item of value) collectDeclaredPaths(item, out, mode);
130
+ return;
131
+ }
132
+ if (isObject(value)) for (let item of Object.values(value)) collectDeclaredPaths(item, out, mode);
133
+ }
134
+ }
135
+ function normalizeDeclaredPath(declared) {
136
+ let withoutDot = declared.startsWith("./") ? declared.slice(2) : declared;
137
+ if (!withoutDot || path.posix.isAbsolute(withoutDot)) return null;
138
+ let normalized = path.posix.normalize(withoutDot);
139
+ return normalized === "." || normalized === ".." || normalized.startsWith("../") ? null : normalized;
140
+ }
141
+ /**
142
+ * Refuses a manifest whose declared entry points are not in the tarball.
143
+ *
144
+ * A path containing `*` is skipped: `exports` subpath patterns and `typesVersions` globs stand
145
+ * for a set of files, and resolving them here would mean reimplementing npm's own matcher to
146
+ * answer a question npm answers at install time.
147
+ */
148
+ function assertDeclaredFiles(pkg, published) {
149
+ let declared = [];
150
+ for (let field of [
151
+ "main",
152
+ "module",
153
+ "types",
154
+ "typings",
155
+ "bin"
156
+ ]) collectDeclaredPaths(pkg[field], declared, "every-string");
157
+ typeof pkg.browser == "string" ? collectDeclaredPaths(pkg.browser, declared, "every-string") : collectDeclaredPaths(pkg.browser, declared, "relative-only");
158
+ for (let field of [
159
+ "exports",
160
+ "imports",
161
+ "sideEffects"
162
+ ]) collectDeclaredPaths(pkg[field], declared, "relative-only");
163
+ collectDeclaredPaths(pkg.typesVersions, declared, "every-string");
164
+ let resolved = declared.map((declaredPath) => ({
165
+ path: declaredPath,
166
+ normalized: normalizeDeclaredPath(declaredPath)
167
+ })), invalid = resolved.filter((item) => item.normalized === null);
168
+ if (invalid.length > 0) throw new PublishCleanError(`Manifest declares invalid package paths:\n${invalid.map((item) => item.path).join("\n")}`);
169
+ let shipped = new Set(published), missing = resolved.filter((item) => !item.path.includes("*") && !shipped.has(String(item.normalized)));
170
+ if (missing.length > 0) throw new PublishCleanError(`Manifest declares files missing from packed artifact:\n${missing.map((item) => item.path).join("\n")}`);
171
+ }
172
+ //#endregion
173
+ //#region src/manifest.ts
174
+ /**
175
+ * What the published `package.json` carries: which fields survive cleaning, which are refused,
176
+ * and what the author may add to either list.
177
+ *
178
+ * Ambient inputs arrive as parameters and never by reaching for them — no process, filesystem or
179
+ * argv here — so every rule is one input to one output. `cli.ts` owns the effects.
180
+ */
181
+ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.catalogs.patchedDependencies.private.files.stylelint.trustedDependencies.wireit.devDependencies.overrides.resolutions.ava.c8.jest.mocha.nyc.tap.vitest.babel.biome.browserslist.eslintConfig.eslintIgnore.prettier.remarkConfig.xo.commitlint.husky.lint-staged.simple-git-hooks.clean-publish.publish-clean.semantic-release.packageManager.pnpm.volta.workspaces.nx.turbo".split(".")), CONSUMER_SCRIPTS = /* @__PURE__ */ new Set([
10
182
  "preinstall",
11
183
  "install",
12
184
  "postinstall",
@@ -33,7 +205,6 @@ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.cata
33
205
  "cpu",
34
206
  "engines",
35
207
  "exports",
36
- "files",
37
208
  "imports",
38
209
  "libc",
39
210
  "license",
@@ -73,20 +244,37 @@ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.cata
73
244
  "styleModule",
74
245
  "svelte",
75
246
  "unpkg"
76
- ]);
247
+ ]), PUBLISH_ADVISORY = "publish-clean packs with pnpm: it resolves workspace: and catalog: specs from the packing package's own node_modules, so a Bun workspace works as-is and a Yarn one needs a pnpm-workspace.yaml plus one pnpm install. npm packs those specs unresolved.";
248
+ function stripManifest(pkg, extraDevFields) {
249
+ let stripped = { ...pkg };
250
+ for (let field of [...DEV_FIELDS, ...extraDevFields]) delete stripped[field];
251
+ let scripts = isObject(pkg.scripts) ? pkg.scripts : null;
252
+ if (scripts) {
253
+ let kept = {};
254
+ for (let [name, value] of Object.entries(scripts)) CONSUMER_SCRIPTS.has(name) && typeof value == "string" && (kept[name] = value);
255
+ Object.keys(kept).length > 0 ? stripped.scripts = kept : delete stripped.scripts;
256
+ }
257
+ return stripped;
258
+ }
77
259
  /**
78
- * Names the fields that survived cleaning without anyone recognising them.
260
+ * Pins the published manifest to a registry, when one was chosen.
79
261
  *
80
- * The strip list can only ever describe tools that existed when it was written, so every
81
- * new `package.json` key some tool invents ships to consumers until a human notices. This
82
- * cannot be fixed by keeping only recognised fields: that would silently drop a key some
83
- * consumer genuinely resolves, and the breakage would surface in a stranger's project
84
- * rather than here. So the field ships, and the maintainer is told it did, with the exact
85
- * config that would drop it next time.
262
+ * Written into `publishConfig` rather than passed only on the command line, because that is
263
+ * where npm looks when someone later republishes the same tarball by hand — the artifact then
264
+ * carries its own destination instead of depending on whoever runs the command. Any other
265
+ * `publishConfig` keys the author set are consumer-facing and survive untouched.
86
266
  *
87
- * Reported on stderr because it is advice about the package, not an error in it. Nothing
88
- * about the published artifact depends on whether anyone reads this.
267
+ * Returns the manifest unchanged when no registry was chosen, so the caller has no branch and
268
+ * cannot forget one.
89
269
  */
270
+ function withRegistry(pkg, registry) {
271
+ if (registry === null) return pkg;
272
+ let publishConfig = isObject(pkg.publishConfig) ? { ...pkg.publishConfig } : {};
273
+ return publishConfig.registry = registry, {
274
+ ...pkg,
275
+ publishConfig
276
+ };
277
+ }
90
278
  /**
91
279
  * Refuses a published manifest that lost a field the source declared and consumers read.
92
280
  *
@@ -96,8 +284,8 @@ const DEV_FIELDS = /* @__PURE__ */ new Set(/* @__PURE__ */ "$schema.catalog.cata
96
284
  * `exports` makes the package unimportable, and neither leaves any trace in the artifact
97
285
  * to notice later. The registry keeps the version forever either way.
98
286
  *
99
- * Compared against the *final* tarball rather than the cleaned directory, so a loss
100
- * introduced by the re-pack is caught as well as one introduced by stripping.
287
+ * Read out of the published tarball rather than from the cleaned value in memory, so a loss
288
+ * introduced while rewriting the archive is caught as well as one introduced by stripping.
101
289
  *
102
290
  * `devFields` entries are exempt: removing those was the request. `scripts` is exempt
103
291
  * because it is rewritten rather than removed, down to the install lifecycle hooks a
@@ -112,50 +300,211 @@ function assertNoLostConsumerFields(sourcePkg, finalPkg, removedOnRequest) {
112
300
  ]), lost = Object.keys(sourcePkg).filter((field) => (RUNTIME_MANIFEST_FIELDS.has(field) || REGISTRY_MANIFEST_FIELDS.has(field)) && !expected.has(field) && !(field in finalPkg));
113
301
  if (lost.length > 0) throw new PublishCleanError(`The published manifest is missing fields the source declared and consumers read:\n${lost.join("\n")}`);
114
302
  }
115
- function reportUnrecognizedFields(pkg, kept) {
303
+ /**
304
+ * Names the fields that survived cleaning without anyone recognising them.
305
+ *
306
+ * The strip list can only ever describe tools that existed when it was written, so every new
307
+ * `package.json` key some tool invents ships to consumers until a human notices. This cannot
308
+ * be fixed by keeping only recognised fields: that would silently drop a key some consumer
309
+ * genuinely resolves, and the breakage would surface in a stranger's project rather than here.
310
+ * So the field ships, and the maintainer is told it did, with the exact config that would drop
311
+ * it next time. It is advice about the package, not an error in it — nothing about the
312
+ * published artifact depends on whether anyone reads it.
313
+ *
314
+ * Returned rather than printed, so the message is this function's value and can be asserted
315
+ * whole. Writing to `console` from here would make that assertion a spy on a process-wide
316
+ * object, which two cases running side by side can clobber.
317
+ */
318
+ function unrecognizedFieldsReport(pkg, kept) {
116
319
  let acknowledged = new Set(kept), unrecognized = Object.keys(pkg).filter((field) => !RUNTIME_MANIFEST_FIELDS.has(field) && !REGISTRY_MANIFEST_FIELDS.has(field) && !acknowledged.has(field));
117
- unrecognized.length !== 0 && console.warn(`publish-clean: these manifest fields are not recognised and were published as-is:\n${unrecognized.map((field) => ` ${field}`).join("\n")}\nStrip the ones consumers do not read, and acknowledge the ones they do:\n "publish-clean": { "devFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }\n "publish-clean": { "keepFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }`);
320
+ return unrecognized.length === 0 ? null : `publish-clean: these manifest fields are not recognised and were published as-is:\n${unrecognized.map((field) => ` ${field}`).join("\n")}\nStrip the ones consumers do not read, and acknowledge the ones they do:\n "publish-clean": { "devFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }\n "publish-clean": { "keepFields": [${unrecognized.map((f) => `"${f}"`).join(", ")}] }`;
118
321
  }
119
322
  /**
120
- * Content that must never reach a registry. Matched against tarball paths with the
121
- * `package/` prefix already stripped, so `^` means the package root.
323
+ * Refuses a manifest that still carries a spec only a workspace can resolve. Published
324
+ * with one, the package is uninstallable for everyone, and the version cannot be taken
325
+ * back.
122
326
  *
123
- * Case-insensitive throughout, because the filesystems most packages are built on are
124
- * too. On macOS and Windows `Server.PEM` and `server.pem` are the same file, so a
125
- * case-sensitive guard refuses one and publishes the other, which is worse than no guard:
126
- * it reads as coverage.
327
+ * The caller passes an already-stripped manifest, so the dev-only members of DEP_FIELDS are
328
+ * absent by construction. They are still checked, because a guard whose correctness depends on
329
+ * the order it happens to be called in fails silently the day someone reorders it.
330
+ */
331
+ function assertNoMonorepoProtocols(pkg) {
332
+ let failures = [];
333
+ for (let field of DEP_FIELDS) {
334
+ let map = pkg[field];
335
+ if (isObject(map)) for (let [name, spec] of Object.entries(map)) typeof spec == "string" && MONOREPO_PROTOCOLS.some((prefix) => spec.includes(prefix)) && failures.push(`${field}.${name}: ${spec}`);
336
+ }
337
+ if (failures.length > 0) throw new PublishCleanError(`Packed manifest contains unresolved monorepo-only dependency specs:\n${failures.join("\n")}\n${PUBLISH_ADVISORY}`);
338
+ }
339
+ function assertPublicPackage(pkg) {
340
+ if (pkg.private === !0) throw new PublishCleanError("Refusing to publish a package with private: true.");
341
+ }
342
+ function assertFilesField(pkg, skip) {
343
+ if (!skip && (!Array.isArray(pkg.files) || pkg.files.length === 0)) throw new PublishCleanError("Package manifest must define a non-empty \"files\" array.");
344
+ }
345
+ function packageConfig(pkg) {
346
+ let config = pkg["publish-clean"];
347
+ return isObject(config) ? config : {};
348
+ }
349
+ function customDevFields(config) {
350
+ if (!Array.isArray(config.devFields)) return [];
351
+ let fields = config.devFields.filter((field) => typeof field == "string"), unsafe = fields.filter((field) => RUNTIME_MANIFEST_FIELDS.has(field));
352
+ if (unsafe.length > 0) throw new PublishCleanError(`publish-clean.devFields cannot remove runtime/public manifest fields:\n${unsafe.join("\n")}`);
353
+ return fields;
354
+ }
355
+ /**
356
+ * Fields the maintainer has confirmed belong in the published package, so they stop being
357
+ * reported as unrecognised.
127
358
  *
128
- * Private keys are matched by extension and, separately, by exact filename. SSH keys carry
129
- * no extension at all, and `id_rsa` is the most common private-key filename there is. The
130
- * exact-name form deliberately does not match `id_rsa.pub`, which is public by design and
131
- * legitimate to ship.
359
+ * Without this the report has only one resolution, `devFields`, which deletes the field.
360
+ * That is the wrong answer for every package whose ecosystem this tool has never heard of:
361
+ * a VS Code extension needs `contributes` and `publisher` in the artifact to function at
362
+ * all. Its author would face a warning on every release that can only be silenced by
363
+ * breaking the extension, and a warning nobody can act on trains everyone to ignore the
364
+ * next one, including a real leak.
132
365
  */
133
- const CRITICAL_PATTERNS = [
134
- /(?:^|\/)node_modules(?:\/|$)/i,
135
- /(?:^|\/)\.git(?:\/|$)/i,
136
- /(?:^|\/)\.env(?:\.|$)/i,
137
- /(?:^|\/)\.npmrc$/i,
138
- /\.(?:pem|key|p12|pfx|p8|ppk|jks|keystore)$/i,
139
- /(?:^|\/)id_(?:rsa|dsa|ecdsa|ed25519)$/i
140
- ], SUSPICIOUS_PATTERNS = [
141
- /(?:^|\/)(?:test|tests|__tests__|__snapshots__|coverage)(?:\/|$)/,
142
- /(?:^|\/)\.github(?:\/|$)/,
143
- /(?:^|\/)tsconfig[^/]*\.json$/,
144
- /(?:^|\/)(?:pnpm-lock\.yaml|bun\.lock|package-lock\.json)$/,
145
- /\.(?:test|spec)\.[cm]?[jt]sx?$/
146
- ], PUBLISH_ADVISORY = "publish-clean packs with pnpm: it resolves workspace: and catalog: specs from the packing package's own node_modules, so a Bun workspace works as-is and a Yarn one needs a pnpm-workspace.yaml plus one pnpm install. npm packs those specs unresolved.", MIN_TRUSTED_NPM_VERSION = [
366
+ function keptFields(config) {
367
+ return Array.isArray(config.keepFields) ? config.keepFields.filter((field) => typeof field == "string") : [];
368
+ }
369
+ //#endregion
370
+ //#region src/tarball.ts
371
+ /**
372
+ * Replaces the manifest inside a packed tarball without repacking it.
373
+ *
374
+ * The pipeline packs once, with pnpm, because that packer decides the file set. Cleaning the
375
+ * manifest by unpacking and packing again hands that decision to a second packer, which
376
+ * re-derives it from the very field being cleaned and silently disagrees; rewriting the one
377
+ * member that changed keeps the first packer's selection, and its normalisation (uid/gid 0,
378
+ * a fixed mtime, mode 644) that a naive `tar` invocation leaks the build machine's identity
379
+ * into.
380
+ *
381
+ * Every entry other than the manifest is copied as opaque bytes and never decoded, so entry
382
+ * shapes this code does not model — pax extended headers, ustar prefix splitting, GNU long
383
+ * names — survive untouched. Only walking is universal: an entry's length is always its
384
+ * header block plus its size rounded up to a block, whatever the type.
385
+ */
386
+ const MANIFEST_PATH = "package/package.json", PAX_TYPES = /* @__PURE__ */ new Set(["x", "g"]);
387
+ function field(block, offset, length) {
388
+ return block.toString("utf8", offset, offset + length).replace(/\0.*/s, "");
389
+ }
390
+ function entrySize(block) {
391
+ if ((block[124] ?? 0) & 128) throw new PublishCleanError("Tarball uses base-256 entry sizes, which this tool cannot rewrite.");
392
+ let size = Number.parseInt(field(block, 124, 12).trim(), 8);
393
+ if (!Number.isFinite(size) || size < 0) throw new PublishCleanError("Tarball entry has an unreadable size field.");
394
+ return size;
395
+ }
396
+ /**
397
+ * Rejects a pax header that renames another entry onto the manifest path.
398
+ *
399
+ * Without this the archive could extract a `package.json` that no check ever saw: the guards
400
+ * validate the entry rewritten here, while an extractor honours the pax override and writes
401
+ * a different one over it. The header's own name field is `PaxHeader`, so nothing about the
402
+ * substitution is visible from the walk alone.
403
+ */
404
+ function assertNoManifestOverride(payload) {
405
+ for (let record of payload.toString("utf8").split("\n")) if (/^\d+ path=/.test(record) && record.slice(record.indexOf("=") + 1) === MANIFEST_PATH) throw new PublishCleanError(`Tarball contains a pax header renaming an entry to ${MANIFEST_PATH}.`);
406
+ }
407
+ /** Rebuilds a header for a changed payload size, keeping every other field of the original. */
408
+ function reheader(original, size) {
409
+ let header = Buffer.from(original);
410
+ header.write(size.toString(8).padStart(11, "0") + "\0", 124, 12, "ascii"), header.write(" ".repeat(8), 148, 8, "ascii");
411
+ let sum = 0;
412
+ for (let byte of header) sum += byte;
413
+ return header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, 8, "ascii"), header;
414
+ }
415
+ /**
416
+ * Returns the gzipped tarball with `package/package.json` replaced by `manifest`.
417
+ *
418
+ * Refuses anything it cannot rewrite unambiguously. Duplicate manifest entries are a refusal
419
+ * rather than a last-one-wins guess, because extractors disagree about which survives and the
420
+ * published manifest would then depend on the extractor rather than on this tool.
421
+ */
422
+ function replaceTarballManifest(archive, manifest) {
423
+ let tar = gunzipSync(archive), parts = [], replaced = !1;
424
+ for (let offset = 0; offset < tar.length;) {
425
+ let header = tar.subarray(offset, offset + 512), name = field(header, 0, 100);
426
+ if (name === "") {
427
+ parts.push(tar.subarray(offset));
428
+ break;
429
+ }
430
+ let size = entrySize(header), end = offset + 512 + 512 * Math.ceil(size / 512), type = field(header, 156, 1);
431
+ if (type === "L") throw new PublishCleanError("Tarball uses GNU long-name entries, which this tool refuses.");
432
+ if (PAX_TYPES.has(type) && assertNoManifestOverride(tar.subarray(offset + 512, offset + 512 + size)), name === MANIFEST_PATH) {
433
+ if (replaced) throw new PublishCleanError(`Tarball contains ${MANIFEST_PATH} more than once.`);
434
+ if (type !== "0" && type !== "") throw new PublishCleanError(`${MANIFEST_PATH} is not a regular file in the tarball.`);
435
+ let body = Buffer.from(manifest, "utf8");
436
+ parts.push(reheader(header, body.length), body, Buffer.alloc(512 * Math.ceil(body.length / 512) - body.length)), replaced = !0;
437
+ } else parts.push(tar.subarray(offset, end));
438
+ offset = end;
439
+ }
440
+ if (!replaced) throw new PublishCleanError(`Tarball does not contain ${MANIFEST_PATH}.`);
441
+ return gzipSync(Buffer.concat(parts));
442
+ }
443
+ //#endregion
444
+ //#region src/trusted-publish.ts
445
+ /**
446
+ * Who npm will believe is publishing, and what the toolchain must be for it to sign anything.
447
+ *
448
+ * Ambient inputs arrive as parameters and never by reaching for them — no process, filesystem or
449
+ * argv here. `cli.ts` owns the effects.
450
+ */
451
+ /**
452
+ * The floors npm's trusted publishing puts on the toolchain, each formatted into its own error
453
+ * message so the version a reader is told to install is the version the comparison made.
454
+ *
455
+ * The Node floor is also the package's declared `engines.node`, since a publisher that cannot
456
+ * produce provenance cannot do this tool's job; a test asserts the two agree.
457
+ */
458
+ const MIN_TRUSTED_NPM_VERSION = [
147
459
  11,
148
460
  5,
149
461
  1
150
- ], TOOL_PROBE_TIMEOUT_MS = 1e4;
151
- var PublishCleanError = class extends Error {
152
- constructor(message, options) {
153
- super(message, options), this.name = "PublishCleanError";
462
+ ], MIN_TRUSTED_NODE_VERSION = [
463
+ 22,
464
+ 14,
465
+ 0
466
+ ];
467
+ function isAtLeast(actual, minimum) {
468
+ for (let index of [
469
+ 0,
470
+ 1,
471
+ 2
472
+ ]) {
473
+ if (actual[index] > minimum[index]) return !0;
474
+ if (actual[index] < minimum[index]) return !1;
154
475
  }
155
- };
156
- function isObject(value) {
157
- return typeof value == "object" && !!value && !Array.isArray(value);
476
+ return !0;
158
477
  }
478
+ function wantsTrustedPublish(pkg, publishArgs, env) {
479
+ return publishArgs.includes("--provenance") || isObject(pkg.publishConfig) && pkg.publishConfig.provenance === !0 ? !0 : env.GITHUB_ACTIONS === "true" && typeof env.ACTIONS_ID_TOKEN_REQUEST_URL == "string";
480
+ }
481
+ function repositoryUrl(pkg) {
482
+ return typeof pkg.repository == "string" ? pkg.repository : isObject(pkg.repository) && typeof pkg.repository.url == "string" ? pkg.repository.url : null;
483
+ }
484
+ function githubRepositorySlug(url) {
485
+ let withoutGitPrefix = url.startsWith("git+") ? url.slice(4) : url;
486
+ if (withoutGitPrefix.startsWith("git@github.com:")) {
487
+ let slug = withoutGitPrefix.slice(15);
488
+ return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
489
+ }
490
+ try {
491
+ let parsed = new URL(withoutGitPrefix);
492
+ if (parsed.hostname !== "github.com") return null;
493
+ let slug = parsed.pathname.replace(/^\/+/, "");
494
+ return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
495
+ } catch {
496
+ return null;
497
+ }
498
+ }
499
+ function assertRepositoryForTrustedPublish(pkg, publishArgs, env) {
500
+ if (!wantsTrustedPublish(pkg, publishArgs, env) || env.GITHUB_ACTIONS !== "true" || typeof env.GITHUB_REPOSITORY != "string") return;
501
+ let repoUrl = repositoryUrl(pkg);
502
+ if (!repoUrl) throw new PublishCleanError("GitHub trusted publishing requires package.json repository.url to match GITHUB_REPOSITORY.");
503
+ if (githubRepositorySlug(repoUrl) !== env.GITHUB_REPOSITORY) throw new PublishCleanError(`package.json repository.url must match GITHUB_REPOSITORY (${env.GITHUB_REPOSITORY}); found ${repoUrl}.`);
504
+ }
505
+ //#endregion
506
+ //#region src/cli.ts
507
+ const TOOL_PROBE_TIMEOUT_MS = 1e4;
159
508
  function readJson(file) {
160
509
  let parsed;
161
510
  try {
@@ -166,15 +515,6 @@ function readJson(file) {
166
515
  if (!isObject(parsed)) throw new PublishCleanError(`${file} must contain a JSON object.`);
167
516
  return parsed;
168
517
  }
169
- function stringifyJson(value) {
170
- return `${JSON.stringify(value, null, 2)}\n`;
171
- }
172
- function stableJson(value) {
173
- return JSON.stringify(stableValue(value));
174
- }
175
- function stableValue(value) {
176
- return Array.isArray(value) ? value.map(stableValue) : isObject(value) ? Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, child]) => [key, stableValue(child)])) : value;
177
- }
178
518
  function run(command, args, cwd) {
179
519
  return execFileSync(command, [...args], {
180
520
  cwd,
@@ -256,22 +596,20 @@ function npmVersion() {
256
596
  parts[2] ?? 0
257
597
  ];
258
598
  }
259
- function isAtLeast(actual, minimum) {
260
- for (let index of [
261
- 0,
262
- 1,
263
- 2
264
- ]) {
265
- if (actual[index] > minimum[index]) return !0;
266
- if (actual[index] < minimum[index]) return !1;
267
- }
268
- return !0;
269
- }
270
- function wantsTrustedPublish(pkg, publishArgs) {
271
- return publishArgs.includes("--provenance") || isObject(pkg.publishConfig) && pkg.publishConfig.provenance === !0 ? !0 : process.env.GITHUB_ACTIONS === "true" && typeof process.env.ACTIONS_ID_TOKEN_REQUEST_URL == "string";
599
+ /**
600
+ * The single place the ambient environment is read for publish decisions. Naming the three
601
+ * variables here keeps `process` out of the rules and makes the whole environmental surface of
602
+ * a publish visible at once, rather than spread across the guards that consult it.
603
+ */
604
+ function publishEnv() {
605
+ return {
606
+ ACTIONS_ID_TOKEN_REQUEST_URL: process.env.ACTIONS_ID_TOKEN_REQUEST_URL,
607
+ GITHUB_ACTIONS: process.env.GITHUB_ACTIONS,
608
+ GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY
609
+ };
272
610
  }
273
611
  function assertTrustedPublishingRuntime(pkg, publishArgs) {
274
- if (!wantsTrustedPublish(pkg, publishArgs)) return;
612
+ if (!wantsTrustedPublish(pkg, publishArgs, publishEnv())) return;
275
613
  let actual = npmVersion();
276
614
  if (!isAtLeast(actual, MIN_TRUSTED_NPM_VERSION)) throw new PublishCleanError(`Trusted npm publishing requires npm ${MIN_TRUSTED_NPM_VERSION.join(".")} or newer; found ${actual.join(".")}.`);
277
615
  let node = process.versions.node.split(".").map(Number);
@@ -279,11 +617,7 @@ function assertTrustedPublishingRuntime(pkg, publishArgs) {
279
617
  node[0] ?? 0,
280
618
  node[1] ?? 0,
281
619
  node[2] ?? 0
282
- ], [
283
- 22,
284
- 14,
285
- 0
286
- ])) throw new PublishCleanError(`Trusted npm publishing requires Node.js 22.14.0 or newer; found ${process.versions.node}.`);
620
+ ], MIN_TRUSTED_NODE_VERSION)) throw new PublishCleanError(`Trusted npm publishing requires Node.js ${MIN_TRUSTED_NODE_VERSION.join(".")} or newer; found ${process.versions.node}.`);
287
621
  }
288
622
  function warnIfNonPnpmLifecycle() {
289
623
  let userAgent = process.env.npm_config_user_agent;
@@ -302,103 +636,16 @@ function assertCleanGit(packageDir, skip) {
302
636
  throw error instanceof PublishCleanError ? error : new PublishCleanError("Unable to verify source git status.", { cause: error });
303
637
  }
304
638
  }
305
- function assertPublicPackage(pkg) {
306
- if (pkg.private === !0) throw new PublishCleanError("Refusing to publish a package with private: true.");
307
- }
308
- function repositoryUrl(pkg) {
309
- return typeof pkg.repository == "string" ? pkg.repository : isObject(pkg.repository) && typeof pkg.repository.url == "string" ? pkg.repository.url : null;
310
- }
311
- function githubRepositorySlug(url) {
312
- let withoutGitPrefix = url.startsWith("git+") ? url.slice(4) : url;
313
- if (withoutGitPrefix.startsWith("git@github.com:")) {
314
- let slug = withoutGitPrefix.slice(15);
315
- return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
316
- }
317
- try {
318
- let parsed = new URL(withoutGitPrefix);
319
- if (parsed.hostname !== "github.com") return null;
320
- let slug = parsed.pathname.replace(/^\/+/, "");
321
- return slug.endsWith(".git") ? slug.slice(0, -4) : slug;
322
- } catch {
323
- return null;
324
- }
325
- }
326
- function assertRepositoryForTrustedPublish(pkg, publishArgs) {
327
- if (!wantsTrustedPublish(pkg, publishArgs) || process.env.GITHUB_ACTIONS !== "true" || typeof process.env.GITHUB_REPOSITORY != "string") return;
328
- let repoUrl = repositoryUrl(pkg);
329
- if (!repoUrl) throw new PublishCleanError("GitHub trusted publishing requires package.json repository.url to match GITHUB_REPOSITORY.");
330
- if (githubRepositorySlug(repoUrl) !== process.env.GITHUB_REPOSITORY) throw new PublishCleanError(`package.json repository.url must match GITHUB_REPOSITORY (${process.env.GITHUB_REPOSITORY}); found ${repoUrl}.`);
331
- }
332
- function stripManifest(pkg, extraDevFields) {
333
- let stripped = { ...pkg };
334
- for (let field of [...DEV_FIELDS, ...extraDevFields]) delete stripped[field];
335
- let scripts = isObject(pkg.scripts) ? pkg.scripts : null;
336
- if (scripts) {
337
- let kept = {};
338
- for (let [name, value] of Object.entries(scripts)) CONSUMER_SCRIPTS.has(name) && typeof value == "string" && (kept[name] = value);
339
- Object.keys(kept).length > 0 ? stripped.scripts = kept : delete stripped.scripts;
340
- }
341
- return stripped;
342
- }
343
639
  /**
344
- * Refuses a manifest that still carries a spec only a workspace can resolve. Published
345
- * with one, the package is uninstallable for everyone, and the version cannot be taken
346
- * back.
640
+ * The file list every content guard judges, read out of the archive by `tar` rather than from
641
+ * an extracted copy: this is the same enumeration a consumer's installer sees, and it needs no
642
+ * filesystem to exist.
347
643
  *
348
- * Both call sites pass an already-stripped manifest, so the dev-only members of
349
- * DEP_FIELDS are absent by construction. They are still checked, because a guard whose
350
- * correctness depends on the order it happens to be called in fails silently the day
351
- * someone reorders it.
644
+ * `tar tzf` prints one name per line, so a filename containing a newline would arrive as two
645
+ * entries. That is safe in the only direction that matters, because every guard here fails
646
+ * closed: splitting a name can only add entries to a leak scan that refuses on any match, and a
647
+ * declared path whose name was split is then absent and refused as missing.
352
648
  */
353
- function assertNoMonorepoProtocols(pkg) {
354
- let failures = [];
355
- for (let field of DEP_FIELDS) {
356
- let map = pkg[field];
357
- if (isObject(map)) for (let [name, spec] of Object.entries(map)) typeof spec == "string" && MONOREPO_PROTOCOLS.some((prefix) => spec.includes(prefix)) && failures.push(`${field}.${name}: ${spec}`);
358
- }
359
- if (failures.length > 0) throw new PublishCleanError(`Packed manifest contains unresolved monorepo-only dependency specs:\n${failures.join("\n")}\n${PUBLISH_ADVISORY}`);
360
- }
361
- function customDevFields(config) {
362
- if (!Array.isArray(config.devFields)) return [];
363
- let fields = config.devFields.filter((field) => typeof field == "string"), unsafe = fields.filter((field) => RUNTIME_MANIFEST_FIELDS.has(field));
364
- if (unsafe.length > 0) throw new PublishCleanError(`publish-clean.devFields cannot remove runtime/public manifest fields:\n${unsafe.join("\n")}`);
365
- return fields;
366
- }
367
- /**
368
- * Fields the maintainer has confirmed belong in the published package, so they stop being
369
- * reported as unrecognised.
370
- *
371
- * Without this the report has only one resolution, `devFields`, which deletes the field.
372
- * That is the wrong answer for every package whose ecosystem this tool has never heard of:
373
- * a VS Code extension needs `contributes` and `publisher` in the artifact to function at
374
- * all. Its author would face a warning on every release that can only be silenced by
375
- * breaking the extension, and a warning nobody can act on trains everyone to ignore the
376
- * next one, including a real leak.
377
- */
378
- function keptFields(config) {
379
- return Array.isArray(config.keepFields) ? config.keepFields.filter((field) => typeof field == "string") : [];
380
- }
381
- async function walkFiles(dir, root, files) {
382
- let entries = await readdir(dir, { withFileTypes: !0 });
383
- for (let entry of entries) {
384
- let absolute = path.join(dir, entry.name), relative = path.relative(root, absolute).replaceAll(path.sep, "/");
385
- if (entry.isDirectory()) {
386
- await walkFiles(absolute, root, files);
387
- continue;
388
- }
389
- entry.isFile() && files.push(relative);
390
- }
391
- }
392
- function assertFilesField(pkg, skip) {
393
- if (!skip && (!Array.isArray(pkg.files) || pkg.files.length === 0)) throw new PublishCleanError("Package manifest must define a non-empty \"files\" array.");
394
- }
395
- function validatePackedFiles(files, skipSuspicious) {
396
- let critical = files.filter((file) => CRITICAL_PATTERNS.some((pattern) => pattern.test(file)));
397
- if (critical.length > 0) throw new PublishCleanError(`Critical files must not be published:\n${critical.join("\n")}`);
398
- if (skipSuspicious) return;
399
- let suspicious = files.filter((file) => SUSPICIOUS_PATTERNS.some((pattern) => pattern.test(file)));
400
- if (suspicious.length > 0) throw new PublishCleanError(`Suspicious files in package artifact:\n${suspicious.join("\n")}`);
401
- }
402
649
  function listTarballFiles(tarball, cwd) {
403
650
  return run("tar", ["tzf", tarball], cwd).trim().split("\n").filter(Boolean).map((file) => file.replaceAll(path.sep, "/")).map((file) => file.startsWith("package/") ? file.slice(8) : file).filter((file) => file.length > 0 && !file.endsWith("/")).sort();
404
651
  }
@@ -411,157 +658,65 @@ function readTarballJson(tarball, file, cwd) {
411
658
  `package/${file}`
412
659
  ], cwd));
413
660
  } catch (cause) {
414
- throw new PublishCleanError(`Unable to read JSON from final npm tarball: ${file}`, { cause });
661
+ throw new PublishCleanError(`Unable to read JSON from ${path.basename(tarball)}: ${file}`, { cause });
415
662
  }
416
- if (!isObject(parsed)) throw new PublishCleanError(`Final npm tarball ${file} must contain a JSON object.`);
663
+ if (!isObject(parsed)) throw new PublishCleanError(`${file} in ${path.basename(tarball)} must contain a JSON object.`);
417
664
  return parsed;
418
665
  }
419
666
  /**
420
- * Locates the tarball a packer just produced by reading its destination directory.
667
+ * Locates the tarball `pnpm pack` just produced by reading its destination directory.
421
668
  *
422
- * Every pack here targets a directory this process created and owns exclusively, so
423
- * the result is discoverable without interpreting the packer's output at all. That
424
- * matters because `pack` runs the package's `prepare`/`prepack` lifecycle scripts and
425
- * forwards their stdout: any build tool that logs — most of them — lands ahead of the
426
- * `--json` payload and breaks a parse of that stream. Reading the directory is immune
427
- * to whatever a foreign package's scripts choose to print.
669
+ * The pack targets a directory this process created and owns exclusively, so the result is
670
+ * discoverable without interpreting the packer's output at all. That matters because `pack`
671
+ * runs the package's `prepare`/`prepack` lifecycle scripts and forwards their stdout: any
672
+ * build tool that logs — most of them — lands ahead of a `--json` payload and breaks a parse
673
+ * of that stream. Reading the directory is immune to whatever a foreign package's scripts
674
+ * choose to print.
428
675
  *
429
676
  * Exactly one tarball is the invariant, not a convenience: more than one means the
430
677
  * destination was not exclusive and the wrong artifact could be published.
431
678
  */
432
- async function soleTarball(packRoot, packer) {
679
+ async function soleTarball(packRoot) {
433
680
  let tarballs = (await readdir(packRoot)).filter((entry) => entry.endsWith(".tgz")), [tarball] = tarballs;
434
- if (tarballs.length !== 1 || !tarball) throw new PublishCleanError(`${packer} left ${tarballs.length} tarballs in ${packRoot}; expected exactly one.`);
681
+ if (tarballs.length !== 1 || !tarball) throw new PublishCleanError(`pnpm pack left ${tarballs.length} tarballs in ${packRoot}; expected exactly one.`);
435
682
  return path.join(packRoot, tarball);
436
683
  }
437
- function assertFinalTarballIncludesCleanedFiles(cleanedFiles, finalFiles) {
438
- let missing = cleanedFiles.filter((file) => !finalFiles.includes(file)).sort();
439
- if (missing.length !== 0) throw new PublishCleanError(`Final npm tarball dropped files from the cleaned package:\n${missing.join("\n")}`);
440
- }
441
- /**
442
- * Walks a manifest value of any shape and collects the strings that name a file inside
443
- * the package, so the caller can prove each one exists in the tarball.
444
- *
445
- * `mode` exists because the two families of path-bearing fields disagree about what a
446
- * bare string means, and reading one by the other's rules produces wrong answers in both
447
- * directions:
448
- *
449
- * - `"every-string"` for `main`, `module`, `types`, `typings`, `bin` and `typesVersions`,
450
- * where every string is a path into this package.
451
- * - `"relative-only"` for `exports`, `imports`, `sideEffects` and the object form of
452
- * `browser`, where a string may just as well be an external package name, a condition
453
- * target or a glob. Only a `./` or `../` prefix marks it as a path here. Collecting the
454
- * rest would report a missing file for something that was never a file.
455
- *
456
- * Booleans are skipped rather than ignored by accident: `sideEffects: false` and
457
- * `exports` condition values legitimately hold them.
458
- */
459
- function collectDeclaredPaths(value, out, mode) {
460
- if (typeof value == "string") {
461
- (mode === "every-string" || value.startsWith("./") || value.startsWith("../")) && out.push(value);
462
- return;
463
- }
464
- if (typeof value != "boolean") {
465
- if (Array.isArray(value)) {
466
- for (let item of value) collectDeclaredPaths(item, out, mode);
467
- return;
468
- }
469
- if (isObject(value)) for (let item of Object.values(value)) collectDeclaredPaths(item, out, mode);
470
- }
471
- }
472
- function normalizeDeclaredPath(declared) {
473
- let withoutDot = declared.startsWith("./") ? declared.slice(2) : declared;
474
- if (!withoutDot || path.posix.isAbsolute(withoutDot)) return null;
475
- let normalized = path.posix.normalize(withoutDot);
476
- return normalized === "." || normalized === ".." || normalized.startsWith("../") ? null : normalized;
477
- }
478
- function assertDeclaredFiles(pkg, packageDir) {
479
- let paths = [];
480
- for (let field of [
481
- "main",
482
- "module",
483
- "types",
484
- "typings",
485
- "bin"
486
- ]) collectDeclaredPaths(pkg[field], paths, "every-string");
487
- typeof pkg.browser == "string" ? collectDeclaredPaths(pkg.browser, paths, "every-string") : collectDeclaredPaths(pkg.browser, paths, "relative-only");
488
- for (let field of [
489
- "exports",
490
- "imports",
491
- "sideEffects"
492
- ]) collectDeclaredPaths(pkg[field], paths, "relative-only");
493
- collectDeclaredPaths(pkg.typesVersions, paths, "every-string");
494
- let normalized = paths.map((declared) => ({
495
- declared,
496
- normalized: normalizeDeclaredPath(declared)
497
- })), invalid = normalized.filter((item) => item.normalized === null);
498
- if (invalid.length > 0) throw new PublishCleanError(`Manifest declares invalid package paths:\n${invalid.map((item) => item.declared).join("\n")}`);
499
- let missing = normalized.filter((declared) => !declared.declared.includes("*") && !exists(path.join(packageDir, String(declared.normalized))));
500
- if (missing.length > 0) throw new PublishCleanError(`Manifest declares files missing from packed artifact:\n${missing.map((item) => item.declared).join("\n")}`);
501
- }
502
- function exists(file) {
503
- try {
504
- return !!statSync(file).isFile();
505
- } catch {
506
- return !1;
507
- }
508
- }
509
- function packageConfig(pkg) {
510
- let config = pkg["publish-clean"];
511
- return isObject(config) ? config : {};
512
- }
513
684
  async function packAndClean(packageDir, opts) {
514
685
  requireTool("pnpm"), requireTool("npm"), requireTool("tar"), warnIfNonPnpmLifecycle();
515
686
  let sourcePkg = readJson(path.join(packageDir, "package.json"));
516
687
  !opts.guardOnly && !opts.dryRun && assertTrustedPublishingRuntime(sourcePkg, opts.publishArgs);
517
688
  let config = packageConfig(sourcePkg), skipFileCheck = opts.skipFileCheck || config.skipFileCheck === !0, noGitChecks = opts.noGitChecks || config.noGitChecks === !0, registry = opts.registry ?? (typeof config.registry == "string" ? config.registry : null), extraDevFields = customDevFields(config), keepFields = keptFields(config);
518
689
  assertPublicPackage(sourcePkg), assertCleanGit(packageDir, noGitChecks), assertFilesField(sourcePkg, skipFileCheck);
519
- let root = await mkdtemp(path.join(tmpdir(), "publish-clean-")), keepRoot = !1;
690
+ let root = await mkdtemp(path.join(tmpdir(), "publish-clean-"));
520
691
  try {
521
692
  run("pnpm", [
522
693
  "pack",
523
694
  "--pack-destination",
524
695
  root
525
- ], packageDir), run("tar", [
526
- "xzf",
527
- await soleTarball(root, "pnpm pack"),
528
- "-C",
529
- root
530
696
  ], packageDir);
531
- let extracted = path.join(root, "package");
532
- if (!(await stat(extracted)).isDirectory()) throw new PublishCleanError("Extracted tarball did not contain package/ directory.");
533
- let files = [];
534
- await walkFiles(extracted, extracted, files), validatePackedFiles(files, skipFileCheck);
535
- let packedPkgPath = path.join(extracted, "package.json"), cleanedPkg = stripManifest(readJson(packedPkgPath), extraDevFields);
536
- if (registry) {
537
- let publishConfig = isObject(cleanedPkg.publishConfig) ? { ...cleanedPkg.publishConfig } : {};
538
- publishConfig.registry = registry, cleanedPkg.publishConfig = publishConfig;
539
- }
540
- assertNoMonorepoProtocols(cleanedPkg), reportUnrecognizedFields(cleanedPkg, keepFields), await writeFile(packedPkgPath, stringifyJson(cleanedPkg)), assertDeclaredFiles(cleanedPkg, extracted);
541
- let cleanedFiles = [];
542
- await walkFiles(extracted, extracted, cleanedFiles);
543
- let finalPackRoot = path.join(root, "npm-pack");
544
- await mkdir(finalPackRoot), run("npm", [
545
- "pack",
546
- "--ignore-scripts",
547
- "--pack-destination",
548
- finalPackRoot
549
- ], extracted);
550
- let finalTarball = await soleTarball(finalPackRoot, "npm pack"), finalFiles = listTarballFiles(finalTarball, extracted);
551
- validatePackedFiles(finalFiles, skipFileCheck), assertFinalTarballIncludesCleanedFiles(cleanedFiles, finalFiles);
552
- let finalPkg = readTarballJson(finalTarball, "package.json", extracted);
553
- if (assertNoMonorepoProtocols(finalPkg), assertNoLostConsumerFields(sourcePkg, finalPkg, extraDevFields), stableJson(finalPkg) !== stableJson(cleanedPkg)) throw new PublishCleanError("Final npm tarball manifest differs from the cleaned manifest.");
697
+ let tarball = await soleTarball(root), cleanedPkg = withRegistry(stripManifest(readTarballJson(tarball, "package.json", root), extraDevFields), registry), unrecognized = unrecognizedFieldsReport(cleanedPkg, keepFields);
698
+ unrecognized && console.warn(unrecognized);
699
+ let publishRoot = path.join(root, "publish");
700
+ await mkdir(publishRoot);
701
+ let finalTarball = path.join(publishRoot, path.basename(tarball));
702
+ await writeFile(finalTarball, replaceTarballManifest(await readFile(tarball), stringifyJson(cleanedPkg)));
703
+ let finalFiles = listTarballFiles(finalTarball, root);
704
+ assertSameEntries(listTarballFiles(tarball, root), finalFiles), validatePackedFiles(finalFiles, skipFileCheck), assertDeclaredFiles(cleanedPkg, finalFiles);
705
+ let shippedPkg = readTarballJson(finalTarball, "package.json", root);
706
+ if (assertNoMonorepoProtocols(shippedPkg), assertNoLostConsumerFields(sourcePkg, shippedPkg, extraDevFields), stableJson(shippedPkg) !== stableJson(cleanedPkg)) throw new PublishCleanError("Rewritten tarball manifest differs from the cleaned manifest.");
554
707
  if (opts.tarballOut !== null) {
555
708
  await mkdir(opts.tarballOut, { recursive: !0 });
556
709
  let kept = path.join(opts.tarballOut, path.basename(finalTarball));
557
710
  await copyFile(finalTarball, kept), console.log(`Final tarball kept at: ${kept}`);
558
711
  }
559
- if (opts.guardOnly || opts.dryRun) {
560
- if (!opts.dryRun) return;
561
- keepRoot = !0, console.log(`[dry-run] Extracted package at: ${extracted}`), console.log(`[dry-run] Final tarball at: ${finalTarball}`);
712
+ if (opts.dryRun) {
713
+ console.log(`[dry-run] ${finalFiles.length} files:`);
714
+ for (let file of finalFiles) console.log(` ${file}`);
715
+ console.log(`[dry-run] cleaned package.json:\n${stringifyJson(cleanedPkg)}`);
562
716
  return;
563
717
  }
564
- assertRepositoryForTrustedPublish(cleanedPkg, opts.publishArgs), runAttached("npm", registry ? [
718
+ if (opts.guardOnly) return;
719
+ assertRepositoryForTrustedPublish(cleanedPkg, opts.publishArgs, publishEnv()), runAttached("npm", registry ? [
565
720
  "publish",
566
721
  finalTarball,
567
722
  "--registry",
@@ -571,9 +726,9 @@ async function packAndClean(packageDir, opts) {
571
726
  "publish",
572
727
  finalTarball,
573
728
  ...opts.publishArgs
574
- ], extracted);
729
+ ], packageDir);
575
730
  } finally {
576
- keepRoot || await rm(root, {
731
+ await rm(root, {
577
732
  recursive: !0,
578
733
  force: !0
579
734
  });