/** * Prepares a nightly build commit (nightly.yml): `node scripts/nightly-prepare.ts --date 20261130 * --build 42` sets the app version to the next patch's nightly, `0.1.56-nightly.20261130` (`.2`, * `.3`… when that tag already exists), in Cargo.toml, package.json and tauri.conf.json, points the * updater at the nightly channel's manifest, gives the MSI installer a numeric version, refreshes * Cargo.lock and the plugin API snapshot, and prints the version (also `version=` in * $GITHUB_OUTPUT). The next patch is the current version's when that is already released (its * `v` tag exists), else the current version itself. */ import { spawnSync } from "node:child_process"; import { appendFileSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { cargoVersion, edit, parseVersion, setCargoVersion, setJsonVersion, syncApiSnapshot, } from "./release-prepare.ts"; /** The nightly channel's updater manifest: the rolling `nightly` release (nightly.yml). */ export const NIGHTLY_ENDPOINT = "https://github.com/l0ng-ai/openidea/releases/download/nightly/latest.json"; /** * Bundle targets built for a nightly: every target except RPM, whose package version rejects * the `-` in SemVer prerelease strings (`0.1.57-nightly.20261201`) and whose Tauri config has * no separate version field. */ export const NIGHTLY_TARGETS = ["app", "dmg", "appimage", "deb", "msi", "nsis"]; function bump(version: string): string { const [major, minor, patch] = version.split(".").map(Number); return `${major}.${minor}.${patch! + 1}`; } /** The nightly's version: the first of `-nightly.`, `….2`, `….3` whose tag is not in `tags`. */ export function nightlyVersion(current: string, released: boolean, date: string, tags: Set): string { if (!/^\d{8}$/.test(date)) throw new Error(`expected a date like 20261130, got ${date}`); const base = `${released ? bump(parseVersion(current)) : parseVersion(current)}-nightly.${date}`; for (let n = 1; ; n++) { const version = n === 1 ? base : `${base}.${n}`; if (!tags.has(`v${version}`)) return version; } } /** * The MSI ProductVersion (`major.minor.patch.build`, numbers only): the last released version * with the build number, so a nightly sorts after that release and before the next. */ export function msiVersion(current: string, released: boolean, build: number): string { if (!Number.isInteger(build) || build < 0 || build > 65535) throw new Error(`build ${build} is not 0–65535`); const [major, minor, patch] = parseVersion(current).split(".").map(Number); const last = released || patch === 0 ? patch : patch! - 1; return `${major}.${minor}.${last}.${build}`; } /** tauri.conf.json for a nightly: its version, the nightly updater manifest, the MSI version and targets without RPM. */ export function setNightlyConfig(json: string, version: string, msi: string): string { const config = JSON.parse(setJsonVersion(json, version)) as { plugins?: { updater?: { endpoints?: string[] } }; bundle?: { targets?: string | string[]; windows?: { wix?: { version?: string } } }; }; if (!config.plugins?.updater) throw new Error("tauri.conf.json: no plugins.updater"); config.plugins.updater.endpoints = [NIGHTLY_ENDPOINT]; config.bundle ??= {}; config.bundle.targets = NIGHTLY_TARGETS; config.bundle.windows ??= {}; config.bundle.windows.wix = { ...config.bundle.windows.wix, version: msi }; return `${JSON.stringify(config, null, 2)}\n`; } function arg(name: string): string { const index = process.argv.indexOf(`--${name}`); const value = index >= 0 ? process.argv[index + 1] : undefined; if (!value) throw new Error(`missing --${name}`); return value; } function git(root: string, ...args: string[]): string { const result = spawnSync("git", args, { cwd: root, encoding: "utf8" }); if (result.status !== 0) throw new Error(`git ${args.join(" ")}: ${result.stderr}`); return result.stdout; } function main() { const root = fileURLToPath(new URL("..", import.meta.url)); const date = arg("date"); const build = Number(arg("build")); const current = cargoVersion(readFileSync(join(root, "Cargo.toml"), "utf8")); const tags = new Set(git(root, "tag", "--list", "v*").split("\n").filter(Boolean)); const released = tags.has(`v${current}`); const version = nightlyVersion(current, released, date, tags); const msi = msiVersion(current, released, build); edit(join(root, "Cargo.toml"), (text) => setCargoVersion(text, version)); edit(join(root, "package.json"), (text) => setJsonVersion(text, version)); edit(join(root, "src-tauri/tauri.conf.json"), (text) => setNightlyConfig(text, version, msi)); const cargo = spawnSync("cargo", ["update", "--workspace"], { cwd: root, stdio: "inherit" }); if (cargo.status !== 0) throw new Error("cargo update --workspace failed"); syncApiSnapshot(root); if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, `version=${version}\n`); console.log(version); } if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { try { main(); } catch (error) { console.error(`nightly-prepare: ${error instanceof Error ? error.message : String(error)}`); process.exit(1); } }