-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex-setup.mjs
More file actions
executable file
·105 lines (91 loc) · 2.56 KB
/
codex-setup.mjs
File metadata and controls
executable file
·105 lines (91 loc) · 2.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
import { readdirSync, statSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const args = process.argv.slice(2);
const skipNpm = args.includes("--skip-npm");
const skipCache = args.includes("--skip-cache");
if (!skipCache) {
run("node", ["scripts/codex-worktree-cache.mjs", "hydrate"]);
}
ensureNativeBuildDependencies();
if (!skipNpm) {
ensureNodeModules(".", "root");
ensureNodeModules("client", "client");
}
function ensureNativeBuildDependencies() {
if (process.platform !== "darwin") {
return;
}
if (!commandSucceeds("pkg-config", ["--version"])) {
installBrewPackage("pkgconf", "pkg-config");
}
if (!commandSucceeds("pkg-config", ["--exists", "x264"])) {
installBrewPackage("x264", "x264 pkg-config metadata");
}
}
function installBrewPackage(formula, label) {
if (truthy(process.env.SIMDECK_CODEX_SKIP_BREW)) {
throw new Error(
`Missing ${label}. Install it with \`brew install ${formula}\` or unset SIMDECK_CODEX_SKIP_BREW.`,
);
}
if (!commandSucceeds("brew", ["--version"])) {
throw new Error(
`Missing ${label}, and Homebrew is not available to install ${formula}.`,
);
}
run("brew", ["install", formula]);
}
function ensureNodeModules(prefix, label) {
const modulesPath =
prefix === "."
? resolve(ROOT, "node_modules")
: resolve(ROOT, prefix, "node_modules");
if (existsAndHasContent(modulesPath)) {
console.log(
`[setup] skip ${label} npm install; node_modules already exists`,
);
return;
}
const args = prefix === "." ? ["ci"] : ["ci", "--prefix", prefix];
run("npm", args);
}
function existsAndHasContent(path) {
try {
const stats = statSync(path);
if (stats.isDirectory()) {
return readdirSync(path).length > 0;
}
return stats.size > 0;
} catch {
return false;
}
}
function commandSucceeds(command, args) {
const result = spawnSync(command, args, {
cwd: ROOT,
stdio: "ignore",
env: process.env,
});
return result.status === 0;
}
function run(command, args) {
console.log(`\n$ ${[command, ...args].join(" ")}`);
const result = spawnSync(command, args, {
cwd: ROOT,
stdio: "inherit",
env: process.env,
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
process.exit(result.status ?? 1);
}
}
function truthy(value) {
return value === "1" || value === "true" || value === "yes";
}