|
| 1 | +#!/usr/bin/env node |
| 2 | +// Sync the ObjectOS One release version to the @objectstack/cli version that |
| 3 | +// the bundled server depends on. |
| 4 | +// |
| 5 | +// Source of truth, in order of preference: |
| 6 | +// 1. node_modules/@objectstack/cli/package.json (resolved/installed version) |
| 7 | +// 2. apps/objectos/package.json `dependencies["@objectstack/cli"]` |
| 8 | +// with any leading ^/~/= stripped |
| 9 | +// |
| 10 | +// Targets updated (skipped if already up to date): |
| 11 | +// - apps/objectos-one/package.json "version" |
| 12 | +// - apps/objectos-one/src-tauri/tauri.conf.json "version" |
| 13 | +// - apps/objectos-one/src-tauri/Cargo.toml [package].version |
| 14 | +// |
| 15 | +// Flags: |
| 16 | +// --check Exit non-zero if any file would change. Doesn't write. |
| 17 | +// --quiet Suppress informational logs. |
| 18 | +// |
| 19 | +// Used by CI (.github/workflows/one.yml) and locally before tagging. |
| 20 | + |
| 21 | +import { readFileSync, writeFileSync, existsSync } from "node:fs"; |
| 22 | +import { dirname, join, resolve } from "node:path"; |
| 23 | +import { fileURLToPath } from "node:url"; |
| 24 | + |
| 25 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 26 | +const REPO_ROOT = resolve(__dirname, "..", "..", ".."); |
| 27 | + |
| 28 | +const argv = process.argv.slice(2); |
| 29 | +const CHECK_ONLY = argv.includes("--check"); |
| 30 | +const QUIET = argv.includes("--quiet"); |
| 31 | + |
| 32 | +const log = (...a) => { |
| 33 | + if (!QUIET) console.log("[sync-version]", ...a); |
| 34 | +}; |
| 35 | +const warn = (...a) => console.warn("[sync-version]", ...a); |
| 36 | + |
| 37 | +function readJson(path) { |
| 38 | + return JSON.parse(readFileSync(path, "utf8")); |
| 39 | +} |
| 40 | + |
| 41 | +function resolveCliVersion() { |
| 42 | + // Prefer the actually-resolved version from node_modules — what the bundle |
| 43 | + // will ship — but fall back to the declared dependency range if the |
| 44 | + // workspace hasn't been installed yet (e.g. fresh CI before pnpm install). |
| 45 | + const resolved = join( |
| 46 | + REPO_ROOT, |
| 47 | + "node_modules/@objectstack/cli/package.json" |
| 48 | + ); |
| 49 | + if (existsSync(resolved)) { |
| 50 | + const v = readJson(resolved).version; |
| 51 | + log(`resolved @objectstack/cli@${v} from node_modules`); |
| 52 | + return v; |
| 53 | + } |
| 54 | + |
| 55 | + const serverPkgPath = join(REPO_ROOT, "apps/objectos/package.json"); |
| 56 | + const serverPkg = readJson(serverPkgPath); |
| 57 | + const range = |
| 58 | + serverPkg.dependencies?.["@objectstack/cli"] ?? |
| 59 | + serverPkg.devDependencies?.["@objectstack/cli"]; |
| 60 | + if (!range) { |
| 61 | + throw new Error( |
| 62 | + `@objectstack/cli not found in ${serverPkgPath}; cannot derive version` |
| 63 | + ); |
| 64 | + } |
| 65 | + const v = range.replace(/^[\^~=v]+/, "").trim(); |
| 66 | + if (!/^\d+\.\d+\.\d+/.test(v)) { |
| 67 | + throw new Error( |
| 68 | + `cannot extract a concrete version from "${range}"; run pnpm install first` |
| 69 | + ); |
| 70 | + } |
| 71 | + log(`derived @objectstack/cli@${v} from ${serverPkgPath} (not installed)`); |
| 72 | + return v; |
| 73 | +} |
| 74 | + |
| 75 | +function updateJson(path, mutate) { |
| 76 | + const before = readFileSync(path, "utf8"); |
| 77 | + const data = JSON.parse(before); |
| 78 | + mutate(data); |
| 79 | + // Keep 2-space indent + trailing newline to match the rest of the repo |
| 80 | + // and avoid noisy diffs. |
| 81 | + const after = JSON.stringify(data, null, 2) + "\n"; |
| 82 | + if (after === before) return false; |
| 83 | + if (!CHECK_ONLY) writeFileSync(path, after); |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +function updateCargoTomlVersion(path, version) { |
| 88 | + const before = readFileSync(path, "utf8"); |
| 89 | + // Only replace the version line inside the first `[package]` table. |
| 90 | + // We don't pull in a TOML parser to avoid an extra dep for this one field. |
| 91 | + const pkgIdx = before.indexOf("[package]"); |
| 92 | + if (pkgIdx < 0) { |
| 93 | + throw new Error(`no [package] section in ${path}`); |
| 94 | + } |
| 95 | + const before_pkg = before.slice(0, pkgIdx); |
| 96 | + const rest = before.slice(pkgIdx); |
| 97 | + // Replace only the FIRST version = "..." after [package] (before the next [table]). |
| 98 | + const nextTableIdx = rest.indexOf("\n[", 1); |
| 99 | + const head = nextTableIdx < 0 ? rest : rest.slice(0, nextTableIdx); |
| 100 | + const tail = nextTableIdx < 0 ? "" : rest.slice(nextTableIdx); |
| 101 | + |
| 102 | + const versionRe = /^(version\s*=\s*)"([^"]*)"/m; |
| 103 | + const match = head.match(versionRe); |
| 104 | + if (!match) { |
| 105 | + throw new Error(`could not find version line under [package] in ${path}`); |
| 106 | + } |
| 107 | + if (match[2] === version) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + const replaced = head.replace(versionRe, `$1"${version}"`); |
| 111 | + const after = before_pkg + replaced + tail; |
| 112 | + if (!CHECK_ONLY) writeFileSync(path, after); |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +function main() { |
| 117 | + const version = resolveCliVersion(); |
| 118 | + |
| 119 | + const targets = [ |
| 120 | + { |
| 121 | + path: join(REPO_ROOT, "apps/objectos-one/package.json"), |
| 122 | + label: "apps/objectos-one/package.json", |
| 123 | + apply: (p) => |
| 124 | + updateJson(p, (data) => { |
| 125 | + data.version = version; |
| 126 | + }), |
| 127 | + }, |
| 128 | + { |
| 129 | + path: join(REPO_ROOT, "apps/objectos-one/src-tauri/tauri.conf.json"), |
| 130 | + label: "apps/objectos-one/src-tauri/tauri.conf.json", |
| 131 | + apply: (p) => |
| 132 | + updateJson(p, (data) => { |
| 133 | + data.version = version; |
| 134 | + }), |
| 135 | + }, |
| 136 | + { |
| 137 | + path: join(REPO_ROOT, "apps/objectos-one/src-tauri/Cargo.toml"), |
| 138 | + label: "apps/objectos-one/src-tauri/Cargo.toml", |
| 139 | + apply: (p) => updateCargoTomlVersion(p, version), |
| 140 | + }, |
| 141 | + ]; |
| 142 | + |
| 143 | + let changedAny = false; |
| 144 | + for (const t of targets) { |
| 145 | + if (!existsSync(t.path)) { |
| 146 | + warn(`skip: ${t.label} (not found)`); |
| 147 | + continue; |
| 148 | + } |
| 149 | + const changed = t.apply(t.path); |
| 150 | + if (changed) { |
| 151 | + changedAny = true; |
| 152 | + log(`${CHECK_ONLY ? "would update" : "updated"} ${t.label} → ${version}`); |
| 153 | + } else { |
| 154 | + log(`unchanged ${t.label} (already ${version})`); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (CHECK_ONLY && changedAny) { |
| 159 | + console.error( |
| 160 | + `[sync-version] FAIL: versions are out of sync with @objectstack/cli@${version}. ` + |
| 161 | + `Run \`pnpm --filter @objectos/one sync-version\` and commit the result.` |
| 162 | + ); |
| 163 | + process.exit(1); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +main(); |
0 commit comments