Add rcedit dependency and implement resolver function
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good

- Added `rcedit` as a dependency in `package.json` and `bun.lock`.
- Implemented a new function to resolve the path of the `rcedit` package in the `patch-electrobun-src.mjs` script, enhancing project compatibility.
- Updated the CLI handling to utilize the patched `rcedit` path if available, improving error handling and user experience.
This commit is contained in:
Tom Butcher 2026-08-02 00:19:38 +01:00
parent b59c525c22
commit b3d0da69d6
3 changed files with 87 additions and 16 deletions

View File

@ -35,6 +35,7 @@
"jimp": "^1.6.1", "jimp": "^1.6.1",
"png-to-ico": "^2.1.8", "png-to-ico": "^2.1.8",
"prop-types": "^15.8.1", "prop-types": "^15.8.1",
"rcedit": "^4.0.1",
"react": "^19.2.6", "react": "^19.2.6",
"react-dom": "^19.2.6", "react-dom": "^19.2.6",
"supertest": "^7.2.2", "supertest": "^7.2.2",

View File

@ -46,6 +46,7 @@
"jest": "^30.4.2", "jest": "^30.4.2",
"jimp": "^1.6.1", "jimp": "^1.6.1",
"png-to-ico": "^2.1.8", "png-to-ico": "^2.1.8",
"rcedit": "^4.0.1",
"prop-types": "^15.8.1", "prop-types": "^15.8.1",
"react": "^19.2.6", "react": "^19.2.6",
"react-dom": "^19.2.6", "react-dom": "^19.2.6",

View File

@ -127,6 +127,21 @@ export function getTemplate(name: string) {
`, `,
); );
const RCEDIT_RESOLVER_FN = `function resolveProjectRceditPkgPath(projectRoot) {
const candidates = [
join(projectRoot, "node_modules/rcedit/package.json"),
join(projectRoot, "node_modules/electrobun/node_modules/rcedit/package.json"),
];
for (const candidate of candidates) {
if (existsSync(candidate)) {
return candidate;
}
}
throw new Error(
"rcedit not found under " + projectRoot + ". Install rcedit in the project (bun add -d rcedit).",
);
}`;
const cliPath = path.join(electrobunDir, "src/cli/index.ts"); const cliPath = path.join(electrobunDir, "src/cli/index.ts");
let cliSource = readFileSync(cliPath, "utf8"); let cliSource = readFileSync(cliPath, "utf8");
if (!cliSource.includes("HOST_ARCH")) { if (!cliSource.includes("HOST_ARCH")) {
@ -180,26 +195,80 @@ if (!cliSource.includes("resolveProjectRceditPkgPath")) {
); );
cliSource = cliSource.replace( cliSource = cliSource.replace(
"function getPlatformPaths(", "function getPlatformPaths(",
`function resolveProjectRceditPkgPath(projectRoot) { `${RCEDIT_RESOLVER_FN}
const projectRcedit = join(projectRoot, "node_modules/rcedit/package.json");
if (existsSync(projectRcedit)) {
return projectRcedit;
}
const nestedRcedit = join(
projectRoot,
"node_modules/electrobun/node_modules/rcedit/package.json",
);
if (existsSync(nestedRcedit)) {
return nestedRcedit;
}
return require.resolve("rcedit/package.json");
}
function getPlatformPaths(`, function getPlatformPaths(`,
); );
writeFileSync(cliPath, cliSource); writeFileSync(cliPath, cliSource);
cliSource = readFileSync(cliPath, "utf8");
} else {
cliSource = cliSource.replace(
/function resolveProjectRceditPkgPath\(projectRoot\) \{[\s\S]*?\n\}/m,
RCEDIT_RESOLVER_FN,
);
writeFileSync(cliPath, cliSource);
}
const electrobunCjsPath = path.join(electrobunDir, "bin/electrobun.cjs");
let electrobunCjs = readFileSync(electrobunCjsPath, "utf8");
if (!electrobunCjs.includes("farmcontrol-use-patched-cli")) {
electrobunCjs = electrobunCjs.replace(
`async function main() {
try {
const args = process.argv.slice(2);
const cliPath = await ensureCliBinary();
// Replace this process with the actual CLI
const child = spawn(cliPath, args, {
stdio: 'inherit',
cwd: process.cwd()
});`,
`async function main() {
try {
const args = process.argv.slice(2);
const patchedCliPath = join(electrobunDir, 'src', 'cli', 'index.ts');
if (existsSync(patchedCliPath)) {
// farmcontrol-use-patched-cli: bundled electrobun.exe cannot resolve project rcedit
const bunBinary = process.env.BUN_INSTALL
? join(process.env.BUN_INSTALL, 'bin', 'bun' + binExt)
: 'bun';
const child = spawn(bunBinary, [patchedCliPath, ...args], {
stdio: 'inherit',
cwd: process.cwd(),
env: process.env,
shell: platform === 'win',
});
child.on('exit', (code) => {
process.exit(code || 0);
});
child.on('error', (error) => {
console.error('Failed to start electrobun patched CLI:', error.message);
process.exit(1);
});
return;
}
const cliPath = await ensureCliBinary();
// Replace this process with the actual CLI
const child = spawn(cliPath, args, {
stdio: 'inherit',
cwd: process.cwd()
});`,
);
writeFileSync(electrobunCjsPath, electrobunCjs);
}
const rceditSrc = path.join(rootDir, "node_modules/rcedit");
const rceditDest = path.join(electrobunDir, "node_modules/rcedit");
if (existsSync(rceditSrc)) {
mkdirSync(path.join(electrobunDir, "node_modules"), { recursive: true });
cpSync(rceditSrc, rceditDest, { recursive: true, force: true });
} }
const markerPath = path.join(electrobunDir, ".farmcontrol-electrobun-patched"); const markerPath = path.join(electrobunDir, ".farmcontrol-electrobun-patched");