diff --git a/README.md b/README.md index 473cafe..f54fb8e 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ cd ~/broke && claude `CLAUDE.md` tells it the privacy rules. Run `npm run dev` to see changes as they happen. On an online copy, `git push` publishes them. -**Get the latest broke:** run `npm run update`. On an online copy there's nothing to install: in your GitHub repo open Actions, Update broke, Run workflow, and Vercel rebuilds your site. Your changes stay either way. +**Get the latest broke:** it updates itself each time you start it (`broke`, or the install command), as long as you haven't changed its code. With your own changes, run `npm run update`. To turn auto-update off, start it with `BROKE_NO_AUTO_UPDATE=1`. On an online copy there's nothing to install: in your GitHub repo open Actions, Update broke, Run workflow, and Vercel rebuilds your site. Your changes stay either way. If you make something others would want, send a pull request. diff --git a/scripts/start.mjs b/scripts/start.mjs index b7dd2b5..de17a89 100755 --- a/scripts/start.mjs +++ b/scripts/start.mjs @@ -18,7 +18,27 @@ const npm = (...args) => spawnSync("npm", args, { stdio: "inherit", shell: win } // npm ci installs exactly what the repo pins and leaves package-lock.json as it is (so `npm run update` stays clean). // Checks for next itself, not node_modules: a stopped first install leaves a half-full node_modules. const nextBin = join("node_modules", ".bin", win ? "next.cmd" : "next"); -if (!existsSync(nextBin) && !npm("ci") && !npm("install")) process.exit(1); + +// Auto-update: each start brings in the newest broke when this copy has no changes of its own (a fast-forward only). +// Your own changes or commits, no git, no internet, or BROKE_NO_AUTO_UPDATE=1: it starts the copy you have. +const git = (...args) => spawnSync("git", args, { encoding: "utf8", shell: win }); +let lockChanged = false; +if (existsSync(".git") && !process.env.BROKE_NO_AUTO_UPDATE) { + if (git("status", "--porcelain").stdout.trim() === "M package-lock.json") git("checkout", "--", "package-lock.json"); // npm's rewrite, not yours + const clean = git("status", "--porcelain").stdout.trim() === ""; + const fetched = git("fetch", "--quiet", process.env.BROKE_REPO || "https://github.com/nextwork-projects/broke.git", "main").status === 0; + const head = git("rev-parse", "HEAD").stdout.trim(), latest = fetched ? git("rev-parse", "FETCH_HEAD").stdout.trim() : head; + if (latest !== head) { + const behindOnly = git("merge-base", "--is-ancestor", "HEAD", latest).status === 0; + if (clean && behindOnly && git("merge", "--quiet", "--ff-only", latest).status === 0) { + lockChanged = git("diff", "--quiet", head, latest, "--", "package-lock.json").status !== 0; + console.log("Updated broke to the newest version."); + } else if (!git("merge-base", "--is-ancestor", latest, "HEAD").status) { + // already has it (your copy is ahead): nothing to say + } else console.log("A new version of broke is out. You have your own changes, so run `npm run update` to bring it in."); + } +} +if ((lockChanged || !existsSync(nextBin)) && !npm("ci") && !npm("install")) process.exit(1); const env = {}; for (const f of [".env", ".env.local"]) {