-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpublish-package.mjs
More file actions
62 lines (54 loc) · 2.27 KB
/
Copy pathpublish-package.mjs
File metadata and controls
62 lines (54 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
/**
* Publishes one packages/* library to npm with a dist-based manifest, without
* disturbing the source-first package.json the monorepo relies on for dev
* (Vite/vitest/tsc all resolve these packages via "main": "./src/index.ts").
*
* npm has no built-in way to swap main/types/exports at publish time. That's
* a Yarn Berry feature, not npm's: verified empirically, `npm pack` ignores
* `publishConfig.main/types/exports`. So this script does it by hand: build
* dist/, temporarily rewrite package.json to point at dist/, publish, then
* always restore the original package.json, even on failure.
*
* Usage:
* node scripts/publish-package.mjs posecode-parser # dry run
* node scripts/publish-package.mjs posecode-parser --live # real publish
*
* Requires `npm login` first (this script does not handle auth).
*/
import { execFileSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, "..");
const [name, ...rest] = process.argv.slice(2);
const live = rest.includes("--live");
if (!name) {
console.error("Usage: node scripts/publish-package.mjs <package-name> [--live]");
process.exit(1);
}
const pkgDir = resolve(repoRoot, "packages", name);
const pkgJsonPath = resolve(pkgDir, "package.json");
const original = readFileSync(pkgJsonPath, "utf8");
const pkg = JSON.parse(original);
function run(cmd, args) {
console.log(`$ ${cmd} ${args.join(" ")}`);
execFileSync(cmd, args, { cwd: pkgDir, stdio: "inherit" });
}
try {
console.log(`\n== Building ${name} ==`);
run("npm", ["run", "build"]);
const published = {
...pkg,
main: "./dist/index.js",
types: "./dist/index.d.ts",
exports: { ".": { types: "./dist/index.d.ts", default: "./dist/index.js" } },
};
writeFileSync(pkgJsonPath, JSON.stringify(published, null, 2) + "\n");
console.log(`\n== ${live ? "Publishing" : "Dry-run publishing"} ${name} ==`);
run("npm", ["publish", "--access", "public", ...(live ? [] : ["--dry-run"])]);
} finally {
writeFileSync(pkgJsonPath, original);
console.log(`\n== Restored ${name}/package.json to its source-first form ==`);
}