iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / worker / stack-commits.jsmain at aacbc74

// stack-commits.js — WHICH VERSIONS OF ITERATE THIS CLOUD HAS: the commit of the stack in its deploy
// image, and every commit behind it. container/pack-stack.sh writes them to
// container/stack/COMMITS, and the build puts that file's text in the Worker (vite.config.ts).
//
// With this list deployments.js says on the card, before anybody presses anything, what the run's
// own check would refuse. The why is beside that check: container/run.sh `not_unknown_stack`.
//
// A Worker with no list (`vite dev`, a build with no packed stack) says nothing on a card: the run
// still checks.

/** The list's text. The build writes it in place of the name; with no build there is no such
 *  name, and no list. */
function built() {
  try {
    return String(__STACK_COMMITS__ || "");
  } catch {
    return "";
  }
}

/** A list in place of the build's (`useCommits`). */
let given = null;
let read = { text: "", commits: commitsOf("") };

/** The pretend Worker, which has no image, and a test: this list's text in place of the build's.
 *  null: the build's again. */
export function useCommits(text) {
  given = text;
}

/** `{ own, all }` from a list's text: this Cloud's own commit (the first line), and every commit
 *  it has. */
export function commitsOf(text) {
  const all = text.split("\n").filter((line) => /^[0-9a-f]{40}$/.test(line));
  return { own: all[0] || null, all: new Set(all) };
}

/** This Cloud's commits: `own` is null when it has no list. */
export function stackCommits() {
  const text = given || built();
  if (text !== read.text) read = { text, commits: commitsOf(text) };
  return read.commits;
}