-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathpostinstall.mjs
More file actions
193 lines (168 loc) · 5.65 KB
/
postinstall.mjs
File metadata and controls
193 lines (168 loc) · 5.65 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// postinstall.mjs
// Runs after npm install to verify platform binary is available
import { readFileSync, readdirSync, rmSync } from "node:fs";
import { createRequire } from "node:module";
import { homedir } from "node:os";
import { join } from "node:path";
import {
getPlatformPackageCandidates,
getBinaryPath,
resolvePlatformPackageBaseName,
} from "./bin/platform.js";
import { detectPlatformBinaryMismatch } from "./bin/version-mismatch.js";
const require = createRequire(import.meta.url);
const MIN_OPENCODE_VERSION = "1.4.0";
const OPENCODE_PLUGIN_PACKAGES = ["oh-my-opencode", "oh-my-openagent"];
/**
* Parse version string into numeric parts
* @param {string} version
* @returns {number[]}
*/
function parseVersion(version) {
return version
.replace(/^v/, "")
.split("-")[0]
.split(".")
.map((part) => Number.parseInt(part, 10) || 0);
}
/**
* Compare two version strings
* @param {string} current
* @param {string} minimum
* @returns {boolean} true if current >= minimum
*/
function compareVersions(current, minimum) {
const currentParts = parseVersion(current);
const minimumParts = parseVersion(minimum);
const length = Math.max(currentParts.length, minimumParts.length);
for (let index = 0; index < length; index++) {
const currentPart = currentParts[index] ?? 0;
const minimumPart = minimumParts[index] ?? 0;
if (currentPart > minimumPart) return true;
if (currentPart < minimumPart) return false;
}
return true;
}
/**
* Check if opencode version meets minimum requirement
* @returns {{ok: boolean, version: string | null}}
*/
function checkOpenCodeVersion() {
try {
const result = require("child_process").execSync("opencode --version", {
encoding: "utf-8",
stdio: ["pipe", "pipe", "ignore"],
});
const version = result.trim();
const ok = compareVersions(version, MIN_OPENCODE_VERSION);
return { ok, version };
} catch {
return { ok: true, version: null };
}
}
/**
* Detect libc family on Linux
*/
function getLibcFamily() {
if (process.platform !== "linux") {
return undefined;
}
try {
const detectLibc = require("detect-libc");
return detectLibc.familySync();
} catch {
return null;
}
}
function readMainPackageJson() {
try {
return JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"));
} catch {
return null;
}
}
function getPackageBaseName() {
const packageJson = readMainPackageJson();
return resolvePlatformPackageBaseName(packageJson?.name || "oh-my-opencode");
}
function getMainPackageVersion() {
const packageJson = readMainPackageJson();
return packageJson?.version ?? null;
}
function invalidateOpenCodePluginCache() {
const cacheDir = join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "opencode");
const parentDirs = [cacheDir, join(cacheDir, "packages")];
const prefixes = OPENCODE_PLUGIN_PACKAGES.map((packageName) => `${packageName}@`);
for (const parentDir of parentDirs) {
try {
for (const entry of readdirSync(parentDir, { withFileTypes: true })) {
if (entry.isDirectory() && prefixes.some((prefix) => entry.name.startsWith(prefix))) {
rmSync(join(parentDir, entry.name), { recursive: true, force: true });
}
}
} catch {
// Cache invalidation is best-effort; postinstall should not fail package installs.
}
}
}
function readPlatformPackageVersion(pkg) {
try {
const platformPackageJsonPath = require.resolve(`${pkg}/package.json`);
const packageJson = JSON.parse(readFileSync(platformPackageJsonPath, "utf8"));
return packageJson.version ?? null;
} catch {
return null;
}
}
function main() {
const { platform, arch } = process;
const libcFamily = getLibcFamily();
const packageBaseName = getPackageBaseName();
invalidateOpenCodePluginCache();
// Check opencode version requirement
const versionCheck = checkOpenCodeVersion();
if (versionCheck.version && !versionCheck.ok) {
console.warn(`⚠ oh-my-opencode requires OpenCode >= ${MIN_OPENCODE_VERSION}`);
console.warn(` Detected: ${versionCheck.version}`);
console.warn(` Please update OpenCode to avoid compatibility issues.`);
}
try {
const packageCandidates = getPlatformPackageCandidates({
platform,
arch,
libcFamily,
packageBaseName,
});
const resolvedPackage = packageCandidates.find((pkg) => {
try {
require.resolve(getBinaryPath(pkg, platform));
return true;
} catch {
return false;
}
});
if (!resolvedPackage) {
throw new Error(
`No platform binary package installed. Tried: ${packageCandidates.join(", ")}`
);
}
const mismatch = detectPlatformBinaryMismatch({
mainVersion: getMainPackageVersion(),
platformVersion: readPlatformPackageVersion(resolvedPackage),
platformPackage: resolvedPackage,
});
if (mismatch) {
console.warn(`⚠ oh-my-opencode platform binary version mismatch detected`);
console.warn(` ${packageBaseName}: ${mismatch.mainVersion}`);
console.warn(` ${mismatch.platformPackage}: ${mismatch.platformVersion}`);
console.warn(` The startup banner may show the stale version until the platform binary is updated.`);
console.warn(` Fix: npm install -g ${packageBaseName}@${mismatch.mainVersion} ${mismatch.platformPackage}@${mismatch.mainVersion}`);
}
console.log(`✓ oh-my-opencode binary installed for ${platform}-${arch} (${resolvedPackage})`);
} catch (error) {
console.warn(`⚠ oh-my-opencode: ${error.message}`);
console.warn(` The CLI may not work on this platform.`);
// Don't fail installation - let user try anyway
}
}
main();