From b3d0da69d6cef2ff62bba70bb706fd8bb4baa6de Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sun, 2 Aug 2026 00:19:38 +0100 Subject: [PATCH] Add rcedit dependency and implement resolver function - 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. --- bun.lock | 1 + package.json | 1 + scripts/patch-electrobun-src.mjs | 101 ++++++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/bun.lock b/bun.lock index 9311b1e..8ffe372 100644 --- a/bun.lock +++ b/bun.lock @@ -35,6 +35,7 @@ "jimp": "^1.6.1", "png-to-ico": "^2.1.8", "prop-types": "^15.8.1", + "rcedit": "^4.0.1", "react": "^19.2.6", "react-dom": "^19.2.6", "supertest": "^7.2.2", diff --git a/package.json b/package.json index ad85ba0..1047f0e 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "jest": "^30.4.2", "jimp": "^1.6.1", "png-to-ico": "^2.1.8", + "rcedit": "^4.0.1", "prop-types": "^15.8.1", "react": "^19.2.6", "react-dom": "^19.2.6", diff --git a/scripts/patch-electrobun-src.mjs b/scripts/patch-electrobun-src.mjs index 671c9c6..fcad615 100644 --- a/scripts/patch-electrobun-src.mjs +++ b/scripts/patch-electrobun-src.mjs @@ -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"); let cliSource = readFileSync(cliPath, "utf8"); if (!cliSource.includes("HOST_ARCH")) { @@ -180,26 +195,80 @@ if (!cliSource.includes("resolveProjectRceditPkgPath")) { ); cliSource = cliSource.replace( "function getPlatformPaths(", - `function resolveProjectRceditPkgPath(projectRoot) { - 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"); -} + `${RCEDIT_RESOLVER_FN} function getPlatformPaths(`, ); 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");