|
| 1 | +#!/usr/bin/env node |
| 2 | +// Called by the changesets action in release.yml as the `version` |
| 3 | +// command. It consumes pending changesets, then applies release-time |
| 4 | +// references that must be committed with the Version Packages PR. |
| 5 | +// |
| 6 | +// Changesets owns package versions and changelogs, including private |
| 7 | +// packages (`privatePackages.version` is enabled in .changeset/config.json). |
| 8 | +// The linux-x64 image context is derivative of @cloudflare/computerd, not a |
| 9 | +// changeset target, so this script copies computerd's version into that |
| 10 | +// package.json and updates Dockerfile/docs image pins to the same version. |
| 11 | + |
| 12 | +import { execFileSync } from "node:child_process"; |
| 13 | +import { readdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 14 | +import { join } from "node:path"; |
| 15 | + |
| 16 | +const IMAGE_TAG_RE = |
| 17 | + /(ghcr\.io\/cloudflare\/computer-computerd-linux-x64:)[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?/g; |
| 18 | +const TEXT_FILE_EXTENSIONS = new Set([ |
| 19 | + "", |
| 20 | + ".Dockerfile", |
| 21 | + ".js", |
| 22 | + ".json", |
| 23 | + ".md", |
| 24 | + ".mjs", |
| 25 | + ".ts", |
| 26 | + ".yaml", |
| 27 | + ".yml", |
| 28 | +]); |
| 29 | +const IGNORED_DIRS = new Set([".git", ".devbox", ".venv", "artifacts", "dist", "node_modules"]); |
| 30 | +const IGNORED_FILES = new Set([ |
| 31 | + "package-lock.json", |
| 32 | + "CHANGELOG.md", |
| 33 | + ".github/changeset-version.mjs", |
| 34 | +]); |
| 35 | + |
| 36 | +function run(cmd, args) { |
| 37 | + execFileSync(cmd, args, { stdio: "inherit" }); |
| 38 | +} |
| 39 | + |
| 40 | +function readJson(path) { |
| 41 | + return JSON.parse(readFileSync(path, "utf8")); |
| 42 | +} |
| 43 | + |
| 44 | +function writeJson(path, value) { |
| 45 | + writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`); |
| 46 | +} |
| 47 | + |
| 48 | +function* walk(dir) { |
| 49 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 50 | + if (entry.name.startsWith(".") && entry.name !== ".github") continue; |
| 51 | + |
| 52 | + const path = join(dir, entry.name); |
| 53 | + if (entry.isDirectory()) { |
| 54 | + if (!IGNORED_DIRS.has(entry.name)) yield* walk(path); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + if (!entry.isFile()) continue; |
| 59 | + if (IGNORED_FILES.has(path) || IGNORED_FILES.has(entry.name)) continue; |
| 60 | + if (entry.name === "Dockerfile" || entry.name.startsWith("Dockerfile.")) { |
| 61 | + yield path; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + const suffix = entry.name.includes(".") ? entry.name.slice(entry.name.lastIndexOf(".")) : ""; |
| 66 | + if (TEXT_FILE_EXTENSIONS.has(suffix)) yield path; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function syncDerivedImagePackage(computerdVersion) { |
| 71 | + const path = "packages/computer-computerd-linux-x64/package.json"; |
| 72 | + const pkg = readJson(path); |
| 73 | + pkg.version = computerdVersion; |
| 74 | + pkg.private = true; |
| 75 | + writeJson(path, pkg); |
| 76 | + console.log(`${path}: derivative image package version → ${computerdVersion}`); |
| 77 | +} |
| 78 | + |
| 79 | +function updateImageReferences(computerdVersion) { |
| 80 | + let updatedFiles = 0; |
| 81 | + let replacements = 0; |
| 82 | + |
| 83 | + for (const file of walk(".")) { |
| 84 | + const before = readFileSync(file, "utf8"); |
| 85 | + if (!IMAGE_TAG_RE.test(before)) { |
| 86 | + IMAGE_TAG_RE.lastIndex = 0; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + IMAGE_TAG_RE.lastIndex = 0; |
| 91 | + const after = before.replace(IMAGE_TAG_RE, `$1${computerdVersion}`); |
| 92 | + if (after !== before) { |
| 93 | + const count = before.match(IMAGE_TAG_RE)?.length ?? 0; |
| 94 | + writeFileSync(file, after); |
| 95 | + updatedFiles += 1; |
| 96 | + replacements += count; |
| 97 | + console.log(`${file}: computerd image tag → ${computerdVersion}`); |
| 98 | + } |
| 99 | + IMAGE_TAG_RE.lastIndex = 0; |
| 100 | + } |
| 101 | + |
| 102 | + console.log( |
| 103 | + `updated ${replacements} computerd image tag reference(s) across ${updatedFiles} file(s)`, |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +run("npx", ["changeset", "version"]); |
| 108 | + |
| 109 | +const { version: computerdVersion } = readJson("packages/computerd/package.json"); |
| 110 | +syncDerivedImagePackage(computerdVersion); |
| 111 | +updateImageReferences(computerdVersion); |
| 112 | + |
| 113 | +// changeset version doesn't update package-lock.json (changesets/changesets#421). |
| 114 | +run("npm", ["install", "--package-lock-only"]); |
0 commit comments