|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 4 | +import { mkdirSync, readFileSync, rmSync, statSync } from "node:fs"; |
| 5 | +import { join } from "node:path"; |
| 6 | + |
| 7 | +const REPO_ROOT = join(import.meta.dirname, ".."); |
| 8 | +const OUTPUT = join(REPO_ROOT, "dist", "hyperframes-plugin.zip"); |
| 9 | +// Codex reports its upload limit in decimal MB. |
| 10 | +const MAX_UPLOAD_BYTES = 100 * 1_000_000; |
| 11 | +const pluginManifest = JSON.parse(readFileSync(join(REPO_ROOT, ".codex-plugin", "plugin.json"))); |
| 12 | +const assetPaths = [ |
| 13 | + ...new Set( |
| 14 | + Object.values(pluginManifest.interface) |
| 15 | + .filter((value) => typeof value === "string") |
| 16 | + .map((value) => value.replace(/^\.\//, "")) |
| 17 | + .filter((value) => value.startsWith("assets/")), |
| 18 | + ), |
| 19 | +].sort(); |
| 20 | +if (assetPaths.some((assetPath) => assetPath.split("/").includes(".."))) { |
| 21 | + throw new Error("Plugin manifest contains an unsafe asset path."); |
| 22 | +} |
| 23 | +const PLUGIN_PATHS = [".codex-plugin", ...assetPaths, "skills"]; |
| 24 | + |
| 25 | +for (const pluginPath of PLUGIN_PATHS) { |
| 26 | + execFileSync("git", ["cat-file", "-e", `HEAD:${pluginPath}`], { |
| 27 | + cwd: REPO_ROOT, |
| 28 | + stdio: "inherit", |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +const dirtyCheck = spawnSync("git", ["diff", "--quiet", "HEAD", "--", ...PLUGIN_PATHS], { |
| 33 | + cwd: REPO_ROOT, |
| 34 | +}); |
| 35 | +if (dirtyCheck.error || (dirtyCheck.status !== 0 && dirtyCheck.status !== 1)) { |
| 36 | + throw dirtyCheck.error ?? new Error("Unable to inspect the plugin working tree state."); |
| 37 | +} |
| 38 | +if (dirtyCheck.status === 1) { |
| 39 | + console.warn("Warning: packaging committed HEAD; uncommitted plugin changes are excluded."); |
| 40 | +} |
| 41 | + |
| 42 | +mkdirSync(join(REPO_ROOT, "dist"), { recursive: true }); |
| 43 | + |
| 44 | +execFileSync( |
| 45 | + "git", |
| 46 | + [ |
| 47 | + "archive", |
| 48 | + "--format=zip", |
| 49 | + "--prefix=hyperframes/", |
| 50 | + "--output", |
| 51 | + OUTPUT, |
| 52 | + "HEAD", |
| 53 | + "--", |
| 54 | + ...PLUGIN_PATHS, |
| 55 | + ], |
| 56 | + { cwd: REPO_ROOT, stdio: "inherit" }, |
| 57 | +); |
| 58 | + |
| 59 | +const bytes = statSync(OUTPUT).size; |
| 60 | +if (bytes > MAX_UPLOAD_BYTES) { |
| 61 | + rmSync(OUTPUT); |
| 62 | + throw new Error( |
| 63 | + `Codex plugin archive is ${(bytes / 1_000_000).toFixed(1)} MB; the upload limit is 100 MB.`, |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +console.log(`Wrote ${OUTPUT} (${(bytes / 1_000_000).toFixed(1)} MB).`); |
0 commit comments