* test: add Windows npm global install CI test (reproduces #262) This test packs the package and installs it globally with npm, then runs agent-browser --version. This reproduces the issue where npm-generated shims on Windows try to invoke /bin/sh which doesn't exist. The bin/agent-browser.js wrapper is added but not yet wired up, so this commit should fail CI to confirm the issue. * fix: Windows npm global install and npx support The shell script wrapper (bin/agent-browser) with #!/bin/sh shebang causes npm to generate Windows shims that try to invoke /bin/sh, which doesn't exist on Windows. This fix uses a hybrid approach: 1. Node.js wrapper (bin/agent-browser.js) as bin entry - Makes npx work on all platforms - ~100ms overhead (acceptable since npx has its own overhead) 2. postinstall patches bin entries for global installs - Windows: Overwrites .cmd/.ps1 shims to invoke .exe directly - Mac/Linux: Replaces symlink to point to native binary - Zero overhead for `npm i -g agent-browser` users on all platforms Also fixes PowerShell glob expansion in CI test. Fixes #262 * fix: Windows npm global install and npx support The shell script wrapper (bin/agent-browser) with #!/bin/sh shebang causes npm to generate Windows shims that try to invoke /bin/sh, which doesn't exist on Windows. This fix uses a hybrid approach: 1. Node.js wrapper (bin/agent-browser.js) as bin entry - Makes npx work on all platforms - ~100ms overhead (acceptable since npx has its own overhead) 2. postinstall patches bin entries for global installs - Windows: Overwrites .cmd/.ps1 shims to invoke .exe directly - Mac/Linux: Replaces symlink to point to native binary - Zero overhead for `npm i -g agent-browser` users on all platforms Also adds cross-platform CI tests for npm global install to catch regressions on all platforms (Ubuntu, macOS, Windows). Fixes #262 * test global install * remove dead code
93 lines
2.1 KiB
JavaScript
93 lines
2.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Cross-platform CLI wrapper for agent-browser
|
|
*
|
|
* This wrapper enables npx support on Windows where shell scripts don't work.
|
|
* For global installs, postinstall.js patches the shims to invoke the native
|
|
* binary directly (zero overhead).
|
|
*/
|
|
|
|
import { spawn } from 'child_process';
|
|
import { existsSync } from 'fs';
|
|
import { dirname, join } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { platform, arch } from 'os';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
// Map Node.js platform/arch to binary naming convention
|
|
function getBinaryName() {
|
|
const os = platform();
|
|
const cpuArch = arch();
|
|
|
|
let osKey;
|
|
switch (os) {
|
|
case 'darwin':
|
|
osKey = 'darwin';
|
|
break;
|
|
case 'linux':
|
|
osKey = 'linux';
|
|
break;
|
|
case 'win32':
|
|
osKey = 'win32';
|
|
break;
|
|
default:
|
|
return null;
|
|
}
|
|
|
|
let archKey;
|
|
switch (cpuArch) {
|
|
case 'x64':
|
|
case 'x86_64':
|
|
archKey = 'x64';
|
|
break;
|
|
case 'arm64':
|
|
case 'aarch64':
|
|
archKey = 'arm64';
|
|
break;
|
|
default:
|
|
return null;
|
|
}
|
|
|
|
const ext = os === 'win32' ? '.exe' : '';
|
|
return `agent-browser-${osKey}-${archKey}${ext}`;
|
|
}
|
|
|
|
function main() {
|
|
const binaryName = getBinaryName();
|
|
|
|
if (!binaryName) {
|
|
console.error(`Error: Unsupported platform: ${platform()}-${arch()}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const binaryPath = join(__dirname, binaryName);
|
|
|
|
if (!existsSync(binaryPath)) {
|
|
console.error(`Error: No binary found for ${platform()}-${arch()}`);
|
|
console.error(`Expected: ${binaryPath}`);
|
|
console.error('');
|
|
console.error('Run "npm run build:native" to build for your platform,');
|
|
console.error('or reinstall the package to trigger the postinstall download.');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Spawn the native binary with inherited stdio
|
|
const child = spawn(binaryPath, process.argv.slice(2), {
|
|
stdio: 'inherit',
|
|
windowsHide: false,
|
|
});
|
|
|
|
child.on('error', (err) => {
|
|
console.error(`Error executing binary: ${err.message}`);
|
|
process.exit(1);
|
|
});
|
|
|
|
child.on('close', (code) => {
|
|
process.exit(code ?? 0);
|
|
});
|
|
}
|
|
|
|
main();
|