Files
Genarrative/scripts/run-bash-script.mjs

56 lines
1.2 KiB
JavaScript

import {existsSync} from 'node:fs';
import {spawn} from 'node:child_process';
const [, , scriptPath, ...scriptArgs] = process.argv;
if (!scriptPath) {
console.error('[run-bash-script] missing script path.');
process.exit(1);
}
function resolveBashCommand() {
if (process.env.GENARRATIVE_BASH) {
return process.env.GENARRATIVE_BASH;
}
if (process.platform !== 'win32') {
return 'bash';
}
const candidates = [
'C:\\Program Files\\Git\\bin\\bash.exe',
'C:\\Program Files\\Git\\usr\\bin\\bash.exe',
'C:\\msys64\\usr\\bin\\bash.exe',
];
const matched = candidates.find((candidate) => existsSync(candidate));
if (matched) {
return matched;
}
return 'bash';
}
const bashCommand = resolveBashCommand();
const child = spawn(bashCommand, [scriptPath, ...scriptArgs], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit',
shell: false,
});
child.on('error', (error) => {
console.error(`[run-bash-script] failed to start bash: ${error.message}`);
process.exit(1);
});
child.on('exit', (code, signal) => {
if (signal) {
console.error(`[run-bash-script] bash exited by signal: ${signal}`);
process.exit(1);
}
process.exit(code ?? 0);
});