File size: 8,715 Bytes
d4f7e1d | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | #!/usr/bin/env node
/**
* Release script for pi-mono
*
* Usage:
* node scripts/release.mjs <major|minor|patch>
* node scripts/release.mjs <x.y.z>
*
* Steps:
* 1. Check for uncommitted changes
* 2. Verify every public workspace package is registered on npm
* 3. Bump version via npm run version:xxx or set an explicit version
* 4. Update CHANGELOG.md files: [Unreleased] -> [version] - date
* 5. Regenerate release artifacts
* 6. Run checks and tests
* 7. Commit and tag the release
* 8. Add new [Unreleased] section to changelogs
* 9. Commit next-cycle changelog updates
* 10. Push main and the tag to trigger CI publication and verified pi.dev announcement
*/
import { execSync, spawnSync } from "node:child_process";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { findPackageDirectories } from "./package-workspaces.mjs";
import { getPublicWorkspacePackages } from "./release-packages.mjs";
const RELEASE_TARGET = process.argv[2];
const BUMP_TYPES = new Set(["major", "minor", "patch"]);
const SEMVER_RE = /^\d+\.\d+\.\d+$/;
if (!RELEASE_TARGET || (!BUMP_TYPES.has(RELEASE_TARGET) && !SEMVER_RE.test(RELEASE_TARGET))) {
console.error("Usage: node scripts/release.mjs <major|minor|patch|x.y.z>");
process.exit(1);
}
function run(cmd, options = {}) {
console.log(`$ ${cmd}`);
try {
return execSync(cmd, { encoding: "utf-8", stdio: options.silent ? "pipe" : "inherit", ...options });
} catch (e) {
if (!options.ignoreError) {
console.error(`Command failed: ${cmd}`);
process.exit(1);
}
return null;
}
}
function getVersion() {
const pkg = JSON.parse(readFileSync("packages/ai/package.json", "utf-8"));
return pkg.version;
}
function assertPackagesAreRegisteredWithNpm() {
const packageNames = getPublicWorkspacePackages().map((pkg) => pkg.name);
const unregisteredPackages = [];
console.log("Checking npm package registration...");
for (const packageName of packageNames) {
const result = spawnSync(process.platform === "win32" ? "npm.cmd" : "npm", ["view", packageName, "version", "--json"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
});
if (result.status === 0 && result.stdout.trim()) {
console.log(` ${packageName}`);
continue;
}
const output = [result.stdout, result.stderr, result.error?.message].filter(Boolean).join("\n");
if (output.includes("E404") || output.includes("404 Not Found")) {
unregisteredPackages.push(packageName);
continue;
}
throw new Error(output ? `Failed to query npm registration for ${packageName}\n${output}` : `Failed to query npm registration for ${packageName}`);
}
if (unregisteredPackages.length > 0) {
throw new Error(`The following public workspace packages are not registered on npm:\n${unregisteredPackages.map((packageName) => ` ${packageName}`).join("\n")}\nRegister them before running a release.`);
}
console.log(" All public workspace packages are registered on npm\n");
}
function compareVersions(a, b) {
const aParts = a.split(".").map(Number);
const bParts = b.split(".").map(Number);
for (let i = 0; i < 3; i++) {
const diff = (aParts[i] || 0) - (bParts[i] || 0);
if (diff !== 0) {
return diff;
}
}
return 0;
}
function shellQuote(value) {
return `'${value.replace(/'/g, `'\\''`)}'`;
}
function removeStaleWorkspaceLockEntries() {
const workspaceVersions = new Map(
getPublicWorkspacePackages().map((pkg) => [pkg.name, pkg.version]),
);
const lockPath = "package-lock.json";
const lock = JSON.parse(readFileSync(lockPath, "utf8"));
let removed = 0;
for (const [path, pkg] of Object.entries(lock.packages)) {
if (!path.startsWith("packages/") || pkg.link === true) {
continue;
}
for (const [name, version] of workspaceVersions) {
if (path.endsWith(`/node_modules/${name}`) && pkg.version !== version) {
delete lock.packages[path];
removed++;
break;
}
}
}
if (removed > 0) {
writeFileSync(lockPath, `${JSON.stringify(lock, null, "\t")}\n`);
console.log(`Removed ${removed} stale workspace package lock ${removed === 1 ? "entry" : "entries"}.`);
}
}
function stageChangedFiles() {
const output = run("git ls-files -m -o -d --exclude-standard", { silent: true });
const paths = [...new Set((output || "").split("\n").map((line) => line.trim()).filter(Boolean))];
if (paths.length === 0) {
return;
}
run(`git add -- ${paths.map(shellQuote).join(" ")}`);
}
function bumpOrSetVersion(target) {
const currentVersion = getVersion();
if (BUMP_TYPES.has(target)) {
console.log(`Bumping version (${target})...`);
run(`npm run version:${target}`);
} else {
if (compareVersions(target, currentVersion) <= 0) {
console.error(`Error: explicit version ${target} must be greater than current version ${currentVersion}.`);
process.exit(1);
}
console.log(`Setting explicit version (${target})...`);
run(`npm version ${target} --workspaces --no-git-tag-version --no-workspaces-update && node scripts/sync-versions.js && npm install --package-lock-only --ignore-scripts`);
}
// npm version can temporarily install the previous workspace versions before
// sync-versions updates inter-package ranges. Remove those stale lock entries,
// refresh the lockfile, then hydrate from the final dependency graph.
removeStaleWorkspaceLockEntries();
run("npm install --package-lock-only --ignore-scripts");
run("npm ci --ignore-scripts");
return getVersion();
}
function getChangelogs() {
return findPackageDirectories()
.map((directory) => join(directory, "CHANGELOG.md"))
.filter((path) => existsSync(path));
}
function updateChangelogsForRelease(version) {
const date = new Date().toISOString().split("T")[0];
const changelogs = getChangelogs();
for (const changelog of changelogs) {
const content = readFileSync(changelog, "utf-8");
if (!content.includes("## [Unreleased]")) {
console.log(` Skipping ${changelog}: no [Unreleased] section`);
continue;
}
const updated = content.replace(
"## [Unreleased]",
`## [${version}] - ${date}`
);
writeFileSync(changelog, updated);
console.log(` Updated ${changelog}`);
}
}
function addUnreleasedSection() {
const changelogs = getChangelogs();
const unreleasedSection = "## [Unreleased]\n\n";
for (const changelog of changelogs) {
const content = readFileSync(changelog, "utf-8");
// Insert after "# Changelog\n\n"
const updated = content.replace(
/^(# Changelog\n\n)/,
`$1${unreleasedSection}`
);
writeFileSync(changelog, updated);
console.log(` Added [Unreleased] to ${changelog}`);
}
}
// Main flow
console.log("\n=== Release Script ===\n");
// 1. Check for uncommitted changes
console.log("Checking for uncommitted changes...");
const status = run("git status --porcelain", { silent: true });
if (status && status.trim()) {
console.error("Error: Uncommitted changes detected. Commit or stash first.");
console.error(status);
process.exit(1);
}
console.log(" Working directory clean\n");
// 2. Verify npm package registration before modifying the worktree.
assertPackagesAreRegisteredWithNpm();
// 3. Bump or set version
const version = bumpOrSetVersion(RELEASE_TARGET);
console.log(` New version: ${version}\n`);
// 4. Update changelogs
console.log("Updating CHANGELOG.md files...");
updateChangelogsForRelease(version);
console.log();
// 5. Regenerate release artifacts
console.log("Regenerating release artifacts...");
run("npm run generate:models");
run("npm run check:model-data");
run("npm run shrinkwrap:coding-agent");
run("npm run install-lock:coding-agent");
console.log();
// 6. Run checks and tests
console.log("Running checks...");
run("npm run check");
console.log();
console.log("Building packages for tests...");
run("npm run build:offline");
console.log();
console.log("Running tests...");
run("./test.sh");
console.log();
console.log("Checking the packed coding-agent consumer install...");
run("npm run check:package-install");
console.log();
// 7. Commit and tag
console.log("Committing and tagging...");
stageChangedFiles();
run(`git commit -m "Release v${version}"`);
run(`git tag v${version}`);
console.log();
// 8. Add new [Unreleased] sections
console.log("Adding [Unreleased] sections for next cycle...");
addUnreleasedSection();
console.log();
// 9. Commit
console.log("Committing changelog updates...");
stageChangedFiles();
run(`git commit -m "Add [Unreleased] section for next cycle"`);
console.log();
// 10. Push
console.log("Pushing to remote...");
run("git push origin main");
run(`git push origin v${version}`);
console.log();
console.log(`=== Prepared release v${version}; CI publication and pi.dev announcement start after the tag push ===`);
|