|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFileSync } from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | +const rootDir = path.resolve(scriptDir, ".."); |
| 9 | +const pkgPath = path.join(rootDir, "package.json"); |
| 10 | +const lockPath = path.join(rootDir, "package-lock.json"); |
| 11 | +const versionPath = path.join(rootDir, "src", "version.ts"); |
| 12 | + |
| 13 | +const bumpArg = process.argv[2] || process.env.BUMP || "patch"; |
| 14 | + |
| 15 | +const run = (cmd, args, options = {}) => |
| 16 | + execFileSync(cmd, args, { encoding: "utf8", ...options }).trim(); |
| 17 | + |
| 18 | +const snapshots = new Map(); |
| 19 | +const record = (filePath) => { |
| 20 | + if (fs.existsSync(filePath)) { |
| 21 | + snapshots.set(filePath, fs.readFileSync(filePath, "utf8")); |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +const restore = () => { |
| 26 | + for (const [filePath, contents] of snapshots.entries()) { |
| 27 | + fs.writeFileSync(filePath, contents, "utf8"); |
| 28 | + } |
| 29 | + for (const filePath of [pkgPath, lockPath, versionPath]) { |
| 30 | + if (!snapshots.has(filePath) && fs.existsSync(filePath)) { |
| 31 | + fs.unlinkSync(filePath); |
| 32 | + } |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +record(pkgPath); |
| 37 | +record(lockPath); |
| 38 | +record(versionPath); |
| 39 | + |
| 40 | +try { |
| 41 | + run("node", [path.join(rootDir, "scripts", "bump-version.mjs"), bumpArg], { |
| 42 | + cwd: rootDir, |
| 43 | + stdio: "inherit", |
| 44 | + }); |
| 45 | + run("npm", ["run", "build"], { cwd: rootDir, stdio: "inherit" }); |
| 46 | + run("npm", ["publish", "--ignore-scripts"], { cwd: rootDir, stdio: "inherit" }); |
| 47 | +} finally { |
| 48 | + restore(); |
| 49 | +} |
0 commit comments