fix(cli/postinstall): avoid overwriting existing code shim

This commit is contained in:
James Peter
2025-09-25 15:43:26 +10:00
parent 7dd31f7179
commit 480640fa58
+39 -7
View File
@@ -54,6 +54,26 @@ function getCachedBinaryPath(version, targetTriple, isWindows) {
return join(cacheDir, `code-${targetTriple}${ext}`);
}
const CODE_SHIM_SIGNATURES = [
'@just-every/code',
'bin/coder.js',
'$(dirname "$0")/coder',
'%~dp0coder'
];
function shimContentsLookOurs(contents) {
return CODE_SHIM_SIGNATURES.some(sig => contents.includes(sig));
}
function looksLikeOurCodeShim(path) {
try {
const contents = readFileSync(path, 'utf8');
return shimContentsLookOurs(contents);
} catch {
return false;
}
}
function isWSL() {
if (platform() !== 'linux') return false;
try {
@@ -285,7 +305,7 @@ async function main() {
} catch {
contents = '';
}
const looksLikeOurs = contents.includes('@just-every/code') || contents.includes('bin/coder.js');
const looksLikeOurs = shimContentsLookOurs(contents);
if (!looksLikeOurs) {
console.warn('[notice] Found an existing `code` on PATH at:');
console.warn(` ${resolved}`);
@@ -647,7 +667,13 @@ async function main() {
const ourShim = globalBin ? join(globalBin, isWindows ? 'code.cmd' : 'code') : '';
const candidates = resolveAllOnPath();
const others = candidates.filter(p => p && (!ourShim || p !== ourShim));
const collision = others.length > 0;
const ourShimExists = ourShim && existsSync(ourShim);
const shimLooksOurs = ourShimExists && looksLikeOurCodeShim(ourShim);
const conflictPaths = [
...others,
...(ourShimExists && !shimLooksOurs ? [ourShim] : []),
];
const collision = conflictPaths.length > 0;
const ensureWrapper = (name, args) => {
if (!globalBin) return;
@@ -674,13 +700,19 @@ async function main() {
if (collision) {
console.error('⚠ Detected existing `code` on PATH:');
for (const p of others) console.error(` - ${p}`);
for (const p of conflictPaths) console.error(` - ${p}`);
if (globalBin) {
try {
if (existsSync(ourShim)) {
unlinkSync(ourShim);
console.error(`✓ Skipped global 'code' shim (removed ${ourShim})`);
skippedCmds.push({ name: 'code', reason: `existing: ${others[0]}` });
if (ourShimExists) {
if (shimLooksOurs && others.length > 0) {
unlinkSync(ourShim);
console.error(`✓ Removed global 'code' shim (ours) at ${ourShim}`);
const reason = others[0] || ourShim;
skippedCmds.push({ name: 'code', reason: `existing: ${reason}` });
} else if (!shimLooksOurs) {
console.error(`✓ Skipped global 'code' shim (different CLI at ${ourShim})`);
skippedCmds.push({ name: 'code', reason: `existing: ${ourShim}` });
}
}
} catch (e) {
console.error(`⚠ Could not remove npm shim '${ourShim}': ${e.message}`);