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