/** * Publish the components to toboggan's npm registry (@pebble/). * * 1. bun run build — fresh dist/. * 2. bun test — the full board, size budgets included; nothing ships red. * 3. Assemble dist/publish//: the PREBUILT package (bundle + the * declarations its index reaches, scripts/declarations.ts + css), never * the TS source — the library's promise is "no * compiler needed". The package.json is generated here from * packages//package.json (name/version/description are the source * of truth) with exports aimed at the built files. * 4. Pre-check each packument: a 200 already carrying the version aborts * (versions are immutable; bump the package's version first). * 5. bun publish each, from inside its publish dir so its .npmrc applies * (bun 1.4: bun publish from elsewhere did not authenticate). * * `bun run publish tablet toaster` publishes only the packages named; with no * names, every package. A named package whose version is already published * still aborts: the guard catches a forgotten bump. * * Token: $TOBOGGAN_TOKEN, a Packages: pull and push token (settings → Personal * tokens). The trailing slash on the registry URL is load-bearing. */ import { Glob } from "bun"; import { shipped } from "./declarations"; const ROOT = `${import.meta.dir}/..`; const REGISTRY = "https://toboggan.cloud/npm/"; const run = (cmd: string[], cwd: string = ROOT): void => { const proc = Bun.spawnSync(cmd, { cwd, stdout: "inherit", stderr: "inherit" }); if (proc.exitCode !== 0) throw new Error(`${cmd.join(" ")} failed (${proc.exitCode})`); }; const token = process.env.TOBOGGAN_TOKEN; if (!token) throw new Error("TOBOGGAN_TOKEN is not set — publish needs a Packages: pull and push token (settings → Personal tokens)."); run(["bun", "run", "build"]); run(["bun", "test"]); const npmrc = `@pebble:registry=${REGISTRY}\n//toboggan.cloud/npm/:_authToken=${token}\n`; const only = process.argv.slice(2); for (const pkgPath of new Glob("packages/*/package.json").scanSync(ROOT)) { const name = pkgPath.split("/")[1]; if (only.length > 0 && !only.includes(name)) continue; const meta = (await Bun.file(`${ROOT}/${pkgPath}`).json()) as { name: string; version: string; description?: string; dependencies?: Record; peerDependencies?: Record; }; const declared = Object.keys({ ...meta.dependencies, ...meta.peerDependencies }); const declarations = shipped(`${ROOT}/dist/types`, name, declared); const out = `${ROOT}/dist/publish/${name}`; // The publishable face: built files only. run(["rm", "-rf", out]); run(["mkdir", "-p", out]); run(["cp", `${ROOT}/dist/${name}.js`, `${ROOT}/dist/${name}.js.map`, `${ROOT}/dist/${name}.css`, out]); for (const file of declarations) { run(["mkdir", "-p", `${out}/types/${name}/${file.split("/").slice(0, -1).join("/")}`]); run(["cp", `${ROOT}/dist/types/${name}/${file}`, `${out}/types/${name}/${file}`]); } await Bun.write(`${out}/index.d.ts`, `export * from "./types/${name}/index";\n`); await Bun.write( `${out}/package.json`, `${JSON.stringify( { name: meta.name, version: meta.version, description: meta.description, type: "module", dependencies: meta.dependencies, peerDependencies: meta.peerDependencies, exports: { ".": { types: "./index.d.ts", default: `./${name}.js` }, [`./${name}.css`]: `./${name}.css`, }, files: [`${name}.js`, `${name}.js.map`, `${name}.css`, "index.d.ts", `types/${name}/**`], publishConfig: { access: "public" }, }, null, 2, )}\n`, ); await Bun.write(`${out}/.npmrc`, npmrc); try { const res = await fetch(`${REGISTRY}${meta.name}`, { headers: { authorization: `Bearer ${token}` } }); if (res.status === 200) { const packument = (await res.json()) as { versions?: Record }; if (packument.versions?.[meta.version] !== undefined) throw new Error(`${meta.name}@${meta.version} is already published — versions are immutable. Bump "version" in ${pkgPath}, then publish again.`); } else if (res.status === 404) { console.log(`${meta.name}: new package — publishing ${meta.version}`); } else if (res.status === 401 || res.status === 403) { throw new Error(`packument pre-check: ${res.status} — the token cannot read ${meta.name}; a publish needs a Packages: pull and push token.`); } run(["bun", "publish"], out); console.log(`published ${meta.name}@${meta.version}`); } finally { // The token never outlives the publish. const { rm } = await import("node:fs/promises"); await rm(`${out}/.npmrc`, { force: true }).catch(() => {}); } }