From a507376596b8a9073f57efd3dab4750e2bcbaeba Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 24 Jun 2025 23:45:34 +0200 Subject: [PATCH] Modify Xcode script to be reliable on recompiles --- mac/scripts/build-web-frontend.sh | 6 +++ web/build-native.js | 62 ++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/mac/scripts/build-web-frontend.sh b/mac/scripts/build-web-frontend.sh index 9450022c..1619ea13 100755 --- a/mac/scripts/build-web-frontend.sh +++ b/mac/scripts/build-web-frontend.sh @@ -98,6 +98,12 @@ cd "${WEB_DIR}" echo "Cleaning build artifacts..." rm -rf dist public/bundle public/output.css native +# Clean native modules to ensure fresh build +echo "Cleaning native modules for fresh build..." +rm -rf node_modules/@serialport +rm -rf node_modules/node-pty +rm -rf node_modules/authenticate-pam + # Install dependencies echo "Installing dependencies..." # For Xcode builds, ensure C++20 standard for native modules diff --git a/web/build-native.js b/web/build-native.js index c654fc36..1b7f853e 100755 --- a/web/build-native.js +++ b/web/build-native.js @@ -254,10 +254,9 @@ function patchNodePty() { } } - // Only patch if not already patched + // Always re-patch to ensure compatibility with current build if (alreadyPatched) { - console.log('✓ node-pty already patched for SEA, skipping patch step'); - return; + console.log('⚠️ node-pty appears to be patched, but re-patching to ensure compatibility...'); } console.log('Patching node-pty for SEA build...'); @@ -345,20 +344,42 @@ exports.default = pty; if (fs.existsSync(unixTerminalFile)) { let content = fs.readFileSync(unixTerminalFile, 'utf8'); - // Check if already patched (contains our SEA comment) + // Always re-patch to ensure consistency if (content.includes('// For SEA, use spawn-helper from environment')) { - console.log('unixTerminal.js already patched, skipping...'); - } else { - // Find where helperPath is defined - const helperPathMatch = content.match(/var helperPath[^;]*;/); - if (!helperPathMatch) { - console.log('Warning: Could not find helperPath declaration in unixTerminal.js'); - } else { - // Find the position right after var helperPath; - const insertPosition = content.indexOf(helperPathMatch[0]) + helperPathMatch[0].length; + console.log('Re-patching unixTerminal.js for consistency...'); + // Find where helperPath is originally declared + const helperPathDecl = content.match(/var helperPath[^;]*;/); + if (helperPathDecl) { + // Replace everything from the declaration to the next var/const/let/function + const startPos = content.indexOf(helperPathDecl[0]); + const afterDecl = startPos + helperPathDecl[0].length; - // Insert our patch - const helperPathPatch = ` + // Find the next major declaration or class definition + const nextDeclMatch = content.substring(afterDecl).match(/^(var|const|let|function|class|\nvar|\nconst|\nlet|\nfunction|\nclass)/m); + let endPos; + if (nextDeclMatch) { + endPos = afterDecl + nextDeclMatch.index; + } else { + // Fallback: just remove up to DEFAULT_FILE declaration which we know exists + const defaultFilePos = content.indexOf('var DEFAULT_FILE'); + endPos = defaultFilePos > startPos ? defaultFilePos : content.length; + } + + // Restore to just the original declaration + content = content.substring(0, afterDecl) + '\n' + content.substring(endPos); + } + } + + // Find where helperPath is defined + const helperPathMatch = content.match(/var helperPath[^;]*;/); + if (!helperPathMatch) { + console.log('Warning: Could not find helperPath declaration in unixTerminal.js'); + } else { + // Find the position right after var helperPath; + const insertPosition = content.indexOf(helperPathMatch[0]) + helperPathMatch[0].length; + + // Insert our patch + const helperPathPatch = ` // For SEA, use spawn-helper from environment or next to executable if (process.env.NODE_PTY_SPAWN_HELPER_PATH) { helperPath = process.env.NODE_PTY_SPAWN_HELPER_PATH; @@ -376,12 +397,11 @@ if (process.env.NODE_PTY_SPAWN_HELPER_PATH) { helperPath = helperPath.replace('node_modules.asar', 'node_modules.asar.unpacked'); } }`; - - // Insert the patch after the helperPath declaration - content = content.substring(0, insertPosition) + helperPathPatch + content.substring(insertPosition); - - fs.writeFileSync(unixTerminalFile, content); - } + + // Insert the patch after the helperPath declaration + content = content.substring(0, insertPosition) + helperPathPatch + content.substring(insertPosition); + + fs.writeFileSync(unixTerminalFile, content); } }