Files
Aperant/auto-claude-ui/scripts/postinstall.js
T
AndyMik90 41a507fe8b feat: auto-download prebuilt node-pty binaries for Windows
Eliminates the need for Visual Studio Build Tools on Windows by:

1. GitHub Actions workflow (.github/workflows/build-prebuilds.yml)
   - Builds node-pty for Windows x64 with correct Electron ABI
   - Uploads prebuilt binaries as release assets
   - Triggered on releases and manual dispatch

2. Smart postinstall script (auto-claude-ui/scripts/postinstall.js)
   - On Windows: tries to download prebuilts first
   - Falls back to electron-rebuild if prebuilts unavailable
   - Shows clear instructions if compilation fails

3. Download helper (auto-claude-ui/scripts/download-prebuilds.js)
   - Fetches prebuilt binaries from GitHub releases
   - Extracts and installs to node_modules/node-pty

Windows users can now run `npm install` without installing Visual Studio
Build Tools, as long as prebuilt binaries exist for their Electron version.

Fixes #8

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-18 23:53:06 +01:00

141 lines
4.2 KiB
JavaScript

#!/usr/bin/env node
/**
* Post-install script for Auto Claude UI
*
* On Windows:
* 1. Try to download prebuilt node-pty binaries from GitHub releases
* 2. Fall back to electron-rebuild if prebuilds aren't available
* 3. Show helpful error message if compilation fails
*
* On macOS/Linux:
* 1. Run electron-rebuild (compilers are typically available)
*/
const { spawn } = require('child_process');
const os = require('os');
const path = require('path');
const fs = require('fs');
const isWindows = os.platform() === 'win32';
const WINDOWS_BUILD_TOOLS_HELP = `
================================================================================
VISUAL STUDIO BUILD TOOLS REQUIRED
================================================================================
Prebuilt binaries weren't available for your Electron version, and compilation
requires Visual Studio Build Tools.
To install:
1. Download Visual Studio Build Tools 2022:
https://visualstudio.microsoft.com/visual-cpp-build-tools/
2. Run installer and select:
- "Desktop development with C++" workload
3. In "Individual Components", also select:
- "MSVC v143 - VS 2022 C++ x64/x86 Spectre-mitigated libs"
4. Restart your terminal and run: npm install
================================================================================
`;
/**
* Run electron-rebuild
*/
function runElectronRebuild() {
return new Promise((resolve, reject) => {
const npx = isWindows ? 'npx.cmd' : 'npx';
const child = spawn(npx, ['electron-rebuild'], {
stdio: 'inherit',
shell: isWindows,
cwd: path.join(__dirname, '..'),
});
child.on('close', (code) => {
if (code === 0) {
resolve({ success: true });
} else {
reject(new Error(`electron-rebuild exited with code ${code}`));
}
});
child.on('error', reject);
});
}
/**
* Check if node-pty is already built
*/
function isNodePtyBuilt() {
const buildDir = path.join(__dirname, '..', 'node_modules', 'node-pty', 'build', 'Release');
if (!fs.existsSync(buildDir)) return false;
// Check for the main .node file
const files = fs.readdirSync(buildDir);
return files.some((f) => f.endsWith('.node'));
}
/**
* Main postinstall logic
*/
async function main() {
console.log('[postinstall] Setting up native modules for Electron...\n');
// If node-pty is already built (e.g., from a previous successful install), skip
if (isNodePtyBuilt()) {
console.log('[postinstall] Native modules already built, skipping rebuild.');
return;
}
if (isWindows) {
// On Windows, try prebuilds first
console.log('[postinstall] Windows detected - checking for prebuilt binaries...\n');
try {
// Dynamic import to handle case where the script doesn't exist yet
const { downloadPrebuilds } = require('./download-prebuilds.js');
const result = await downloadPrebuilds();
if (result.success) {
console.log('\n[postinstall] Successfully installed prebuilt binaries!');
console.log('[postinstall] No Visual Studio Build Tools required.\n');
return;
}
console.log(`\n[postinstall] Prebuilds not available (${result.reason})`);
console.log('[postinstall] Falling back to electron-rebuild...\n');
} catch (err) {
console.log('[postinstall] Could not check for prebuilds:', err.message);
console.log('[postinstall] Falling back to electron-rebuild...\n');
}
}
// Run electron-rebuild
try {
console.log('[postinstall] Running electron-rebuild...\n');
await runElectronRebuild();
console.log('\n[postinstall] Native modules built successfully!');
} catch (error) {
console.error('\n[postinstall] Failed to build native modules.\n');
if (isWindows) {
console.error(WINDOWS_BUILD_TOOLS_HELP);
} else {
console.error('Error:', error.message);
console.error('\nYou may need to install build tools for your platform:');
console.error(' macOS: xcode-select --install');
console.error(' Linux: sudo apt-get install build-essential\n');
}
process.exit(1);
}
}
main().catch((err) => {
console.error('[postinstall] Unexpected error:', err);
process.exit(1);
});