fix: remove hardcoded /home/jchery paths, use __OPENCODE_DIR__ template token resolved at install time

Command markdown files now use __OPENCODE_DIR__ placeholder instead of
hardcoded user path. Both postinstall.js and install.sh perform template
replacement when copying files to ~/.config/opencode/, making CI portable
across any user/machine/container.
This commit is contained in:
CI
2026-05-29 16:08:46 +00:00
parent 940b85bfae
commit 7a20784c87
13 changed files with 39 additions and 21 deletions
+22 -7
View File
@@ -20,7 +20,7 @@ function isGlobalInstall() {
return false;
}
function copyFile(src, dest, force) {
function copyFile(src, dest, force, templateVars) {
if (!fs.existsSync(src)) return { copied: 0, skipped: 0 };
const dir = path.dirname(dest);
@@ -28,17 +28,27 @@ function copyFile(src, dest, force) {
if (fs.existsSync(dest) && !force) {
try {
const srcContent = fs.readFileSync(src, "utf8");
const srcContent = applyTemplate(fs.readFileSync(src, "utf8"), templateVars);
const destContent = fs.readFileSync(dest, "utf8");
if (srcContent === destContent) return { copied: 0, skipped: 1 };
} catch {}
return { copied: 0, skipped: 1 };
}
fs.copyFileSync(src, dest);
const content = applyTemplate(fs.readFileSync(src, "utf8"), templateVars);
fs.writeFileSync(dest, content, "utf8");
return { copied: 1, skipped: 0 };
}
function applyTemplate(content, vars) {
if (!vars) return content;
let result = content;
for (const [key, value] of Object.entries(vars)) {
result = result.replaceAll(key, value);
}
return result;
}
function install() {
const pkgDir = getPackageDir();
if (!pkgDir) {
@@ -58,6 +68,10 @@ function install() {
return;
}
const templateVars = {
__OPENCODE_DIR__: OPENCODE_DIR,
};
let copied = 0;
let skipped = 0;
@@ -68,7 +82,7 @@ function install() {
return f.startsWith(pattern);
});
for (const entry of entries) {
const result = copyFile(path.join(srcDir, entry), path.join(destDir, entry), false);
const result = copyFile(path.join(srcDir, entry), path.join(destDir, entry), false, templateVars);
copied += result.copied;
skipped += result.skipped;
}
@@ -88,7 +102,7 @@ function install() {
const versionFile = path.join(opencodeDir, "ci", "VERSION");
if (fs.existsSync(versionFile)) {
const result = copyFile(versionFile, path.join(OPENCODE_DIR, "ci", "VERSION"), false);
const result = copyFile(versionFile, path.join(OPENCODE_DIR, "ci", "VERSION"), false, templateVars);
copied += result.copied;
skipped += result.skipped;
}
@@ -98,11 +112,12 @@ function install() {
if (fs.existsSync(ciJsonPath)) {
if (!fs.existsSync(targetJsonPath)) {
fs.copyFileSync(ciJsonPath, targetJsonPath);
const content = applyTemplate(fs.readFileSync(ciJsonPath, "utf8"), templateVars);
fs.writeFileSync(targetJsonPath, content, "utf8");
} else {
try {
const existing = JSON.parse(fs.readFileSync(targetJsonPath, "utf8"));
const ciJson = JSON.parse(fs.readFileSync(ciJsonPath, "utf8"));
const ciJson = JSON.parse(applyTemplate(fs.readFileSync(ciJsonPath, "utf8"), templateVars));
existing.permission = existing.permission || {};
existing.permission.read = existing.permission.read || {};
existing.permission.external_directory = existing.permission.external_directory || {};