|
| 1 | +// `moshcode upgrade` — update everything that has a newer version: moshcode |
| 2 | +// itself and every installed coding engine. Conductor pattern: we just re-run |
| 3 | +// each tool's own updater/installer (they fetch latest), never vendor them. |
| 4 | +import { ENGINES, engineStatus, resolveEngine, upgradeSpec, runCmd } from "./engines.mjs"; |
| 5 | + |
| 6 | +// Self-upgrade re-runs the moshcode installer's `update` path. Defaults to the |
| 7 | +// GitHub-hosted install.sh (always live); override with MOSHCODE_INSTALL_URL. |
| 8 | +const SELF_URL = process.env.MOSHCODE_INSTALL_URL |
| 9 | + || "https://raw.githubusercontent.com/moshcoder/moshcode/main/install.sh"; |
| 10 | + |
| 11 | +function selfSpec() { |
| 12 | + return { cmd: "sh", args: ["-c", `curl -fsSL ${SELF_URL} | sh -s -- update`] }; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Work out an upgrade plan from optional targets: |
| 17 | + * []/["all"] → moshcode + every *installed* engine |
| 18 | + * ["self"|"moshcode"] → moshcode only |
| 19 | + * ["engines"] → all installed engines (no self) |
| 20 | + * ["claude", …] → those engines (installs the ones not present yet) |
| 21 | + * Returns { self, items:[{key,label,spec,installed}], unknown:[] }. |
| 22 | + */ |
| 23 | +export function planUpgrade(targets = []) { |
| 24 | + const t = targets.map((x) => String(x).trim().toLowerCase()).filter(Boolean); |
| 25 | + const status = engineStatus(); |
| 26 | + const byKey = Object.fromEntries(status.map((e) => [e.key, e])); |
| 27 | + |
| 28 | + const wantsAll = t.length === 0 || t.includes("all"); |
| 29 | + const wantsSelf = wantsAll || t.includes("self") || t.includes("moshcode"); |
| 30 | + const wantsEngines = wantsAll || t.includes("engines"); |
| 31 | + |
| 32 | + const items = []; |
| 33 | + const unknown = []; |
| 34 | + const seen = new Set(); |
| 35 | + |
| 36 | + const add = (key) => { |
| 37 | + if (seen.has(key)) return; |
| 38 | + seen.add(key); |
| 39 | + const st = byKey[key]; |
| 40 | + items.push({ key, label: key, spec: upgradeSpec(ENGINES[key]), installed: st.installed }); |
| 41 | + }; |
| 42 | + |
| 43 | + if (wantsEngines) { |
| 44 | + for (const e of status) if (e.installed) add(e.key); |
| 45 | + } |
| 46 | + // Explicitly-named engines/aliases (upgrade even if not among "installed"). |
| 47 | + for (const tok of t) { |
| 48 | + if (["all", "self", "moshcode", "engines"].includes(tok)) continue; |
| 49 | + const resolved = resolveEngine(tok); |
| 50 | + if (resolved) add(resolved[0]); |
| 51 | + else unknown.push(tok); |
| 52 | + } |
| 53 | + |
| 54 | + return { self: wantsSelf, items, unknown }; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Run an upgrade plan sequentially, streaming each tool's own output. `io.log` |
| 59 | + * prints a status line, `io.rule` draws a divider around each hand-off (both |
| 60 | + * optional — default to plain console output). Returns a summary array. |
| 61 | + */ |
| 62 | +export async function runUpgrade(targets = [], io = {}) { |
| 63 | + const log = io.log || ((s) => console.log(s)); |
| 64 | + const rule = io.rule || (() => console.log("─".repeat(48))); |
| 65 | + const { self, items, unknown } = planUpgrade(targets); |
| 66 | + |
| 67 | + for (const u of unknown) log(`? skipping unknown engine "${u}"`); |
| 68 | + |
| 69 | + if (!self && items.length === 0) { |
| 70 | + if (!unknown.length) log("nothing to upgrade — no engines installed. install one: moshcode install claude"); |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + const results = []; |
| 75 | + const run = async (name, spec, note) => { |
| 76 | + log(`\n⬆ upgrading ${name}${note ? ` ${note}` : ""} — ${spec.cmd} ${spec.args.join(" ")}`); |
| 77 | + rule(); |
| 78 | + const r = await runCmd(spec.cmd, spec.args); |
| 79 | + rule(); |
| 80 | + const ok = r.ok && (r.code == null || r.code === 0); |
| 81 | + log(ok ? `✓ ${name} up to date` : `✗ ${name} upgrade failed${r.code != null ? ` (code ${r.code})` : r.error ? `: ${r.error.message || r.error}` : ""}`); |
| 82 | + results.push({ name, ok, code: r.code }); |
| 83 | + return ok; |
| 84 | + }; |
| 85 | + |
| 86 | + if (self) await run("moshcode", selfSpec(), "(self)"); |
| 87 | + for (const it of items) await run(it.label, it.spec, it.installed ? "" : "(installing — not present)"); |
| 88 | + |
| 89 | + const failed = results.filter((r) => !r.ok); |
| 90 | + log(`\n${failed.length ? "✗" : "✓"} upgraded ${results.length - failed.length}/${results.length}${failed.length ? ` — failed: ${failed.map((r) => r.name).join(", ")}` : "."} 🤘`); |
| 91 | + if (self) log("· restart moshcode to pick up its own new version."); |
| 92 | + return results; |
| 93 | +} |
0 commit comments