Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

6.7 KB
Raw Blame
#!/usr/bin/env node
/**
 * Bump the workspace version everywhere and (optionally) create the release tag.
 *
 * Usage:
 *   node scripts/release.mjs <version>          # bump files only
 *   node scripts/release.mjs <version> --tag    # bump + commit (only bumped files) + tag v<version>
 *
 * --tag still creates the tag when the bump was already committed (nothing to
 * change), and refuses when v<version> exists locally or on origin.
 *
 * BEFORE running this for a stable release, update every version-bearing
 * document (D164 + D260, docs/spec/06-delivery/06-release-runbook.md section 4.1):
 *   - the pinned Pi packages supply the model catalog
 *     and committed with the release tag
 *   - packages/shared/src/changelog*.ts (one entry for <version> in every
 *     shipped locale, matching highlight counts) and its newest-first list in
 *     changelog.test.ts
 *   - the release line stated in README.md and README.zh-CN.md
 * GitHub auto-generated release bodies are web-only and are not a substitute.
 *
 * This script runs `scripts/check-release-docs.mjs <version>` after bumping and
 * refuses to tag while any surface disagrees. Prereleases skip that preflight
 * (changelog catalogs the next stable version, not `x.y.z-beta.*`); still run
 * `node scripts/check-release-docs.mjs x.y.z` against the stable version being
 * previewed. Use --skip-docs-check only for a deliberate non-release bump.
 *
 * Pushing the tag triggers .github/workflows/release.yml, which builds the
 * macOS / Windows / Linux installers and publishes them to a GitHub Release:
 *
 *   git push origin <branch> v<version>
 */
import { readdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import path from "node:path";

const root = fileURLToPath(new URL("..", import.meta.url));
const version = process.argv[2];
const doTag = process.argv.includes("--tag");
const skipDocsCheck = process.argv.includes("--skip-docs-check");
const isPrerelease = version?.includes("-") ?? false;

if (!version || !/^\d+\.\d+\.\d+(-[0-9A-Za-z.]+)?$/.test(version)) {
  console.error(
    "Usage: node scripts/release.mjs <version> [--tag] [--skip-docs-check]   e.g. 0.11.0 or 0.11.0-beta.1",
  );
  process.exit(1);
}

const changed = [];
function bumpPackageJson(relPath) {
  const file = path.join(root, relPath);
  if (!existsSync(file)) return;
  const pkg = JSON.parse(readFileSync(file, "utf8"));
  if (pkg.version === version) return;
  pkg.version = version;
  writeFileSync(file, JSON.stringify(pkg, null, 2) + "\n");
  changed.push(relPath);
}

bumpPackageJson("package.json");
// `docs` is a third workspace root (pnpm-workspace.yaml), not under apps/packages.
bumpPackageJson("docs/package.json");
for (const group of ["apps", "packages"]) {
  for (const dir of readdirSync(path.join(root, group), { withFileTypes: true })) {
    if (dir.isDirectory()) bumpPackageJson(path.join(group, dir.name, "package.json"));
  }
}

// Cargo workspace version ([workspace.package] in root Cargo.toml), lockfile
// entry, and the APP_VERSION constant surfaced in the About panel.
for (const [relPath, pattern] of [
  ["Cargo.toml", /(\[workspace\.package\][\s\S]*?\bversion = ")[^"]+(")/],
  ["Cargo.lock", /(name = "host-core"\nversion = ")[^"]+(")/],
  ["packages/shared/src/protocol.ts", /(export const APP_VERSION = ")[^"]+(")/],
]) {
  const file = path.join(root, relPath);
  const source = readFileSync(file, "utf8");
  const updated = source.replace(pattern, `$1${version}$2`);
  if (updated === source) continue;
  writeFileSync(file, updated);
  changed.push(relPath);
}

if (changed.length === 0) {
  console.log(`Everything is already at ${version}; nothing to do.`);
} else {
  console.log(`Bumped to ${version}:\n  ${changed.join("\n  ")}`);
}

// Version surfaces, the shipped-locale changelog, and the README release line must
// agree before a tag exists (D260). Bumping files is reversible; a tag is not.
if (!skipDocsCheck && !isPrerelease) {
  try {
    execFileSync(process.execPath, [path.join(root, "scripts/check-release-docs.mjs"), version], {
      cwd: root,
      stdio: "inherit",
    });
  } catch {
    console.error("\nRelease documentation check failed; not committing or tagging.");
    process.exit(1);
  }
} else if (isPrerelease && !skipDocsCheck) {
  console.log("Skipping stable release-document preflight for prerelease version.");
}

const git = (...args) => execFileSync("git", args, { cwd: root, encoding: "utf8" }).trim();
const tag = `v${version}`;
const branch = git("rev-parse", "--abbrev-ref", "HEAD");

/**
 * Where `tag` already exists: "locally", "on origin", or null. A local-only
 * check misses a tag that was pushed from another clone, so origin is queried
 * too when it is configured; a network failure only downgrades to a warning.
 */
function findExistingTag() {
  if (git("tag", "--list", tag)) return "locally";
  let hasOrigin = false;
  try {
    git("remote", "get-url", "origin");
    hasOrigin = true;
  } catch {
    // No remote named origin: nothing further to check.
  }
  if (!hasOrigin) return null;
  try {
    const remoteTags = execFileSync("git", ["ls-remote", "--tags", "origin", `refs/tags/${tag}`], {
      cwd: root,
      encoding: "utf8",
      timeout: 20_000,
      stdio: ["ignore", "pipe", "pipe"],
    }).trim();
    if (remoteTags) return "on origin";
  } catch (error) {
    const reason = (error.stderr || error.message || "").toString().trim().split("\n")[0];
    console.warn(
      `Warning: could not query origin for ${tag} (${reason || "unknown error"}); ` +
        "only local tags were checked.",
    );
  }
  return null;
}

if (doTag) {
  const existing = findExistingTag();
  if (existing) {
    console.error(`Tag ${tag} already exists ${existing}.`);
    process.exit(1);
  }
  if (changed.length > 0) {
    // Pathspec commit: only the bumped files, so unrelated staged work stays untouched.
    git("commit", "-m", `chore(release): ${tag}`, "--", ...changed);
  } else {
    // The bump was applied earlier; still tag, but only if HEAD carries it,
    // since release.yml compares the tag with apps/desktop/package.json at HEAD.
    const headVersion = JSON.parse(git("show", "HEAD:apps/desktop/package.json")).version;
    if (headVersion !== version) {
      console.error(
        `HEAD has apps/desktop/package.json at ${headVersion}, not ${version}; ` +
          "commit the bump before tagging.",
      );
      process.exit(1);
    }
    console.log(`Nothing to commit; tagging current HEAD as ${tag}.`);
  }
  git("tag", tag);
  console.log(`\nTagged ${tag}. Publish with:\n  git push origin ${branch} ${tag}`);
} else if (changed.length > 0) {
  console.log(`\nNext steps:\n  git commit -m "chore(release): ${tag}" -- ${changed.join(" ")}\n  git tag ${tag}\n  git push origin ${branch} ${tag}`);
}