Skip to content

Commit 300713b

Browse files
committed
feat: add Windows-safe prebuildify runner for node-gyp
1 parent 3610e77 commit 300713b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

scripts/run-prebuildify.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env node
2+
// Windows-safe prebuildify runner that wraps child_process.spawn for node-gyp
3+
const os = require('os');
4+
const path = require('path');
5+
const cp = require('child_process');
6+
7+
const origSpawn = cp.spawn;
8+
cp.spawn = function patchedSpawn(command, args, options) {
9+
if (process.platform === 'win32') {
10+
// If prebuildify tries to spawn a .cmd (node-gyp.cmd), wrap via cmd.exe
11+
const isCmd = /\.cmd$/i.test(command) || /node-gyp(\.js)?$/i.test(command);
12+
if (isCmd) {
13+
const cmdExe = process.env.ComSpec || 'cmd.exe';
14+
const cmdline = [command].concat(args || []).join(' ');
15+
return origSpawn(cmdExe, ['/d', '/s', '/c', cmdline], Object.assign({ stdio: 'inherit' }, options, { shell: false }));
16+
}
17+
}
18+
return origSpawn(command, args, options);
19+
};
20+
21+
const prebuildify = require('prebuildify');
22+
23+
const opts = {
24+
napi: true,
25+
strip: true,
26+
cwd: process.cwd(),
27+
arch: process.env.PREBUILD_ARCH || os.arch(),
28+
platform: process.env.PREBUILD_PLATFORM || os.platform(),
29+
nodeGyp: process.env.PREBUILD_NODE_GYP || path.join(process.cwd(), 'node_modules', '.bin', process.platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp')
30+
};
31+
32+
prebuildify(opts, (err) => {
33+
if (err) {
34+
console.error(err.stack || err.message || String(err));
35+
process.exit(1);
36+
}
37+
console.log('prebuildify completed successfully');
38+
});
39+

0 commit comments

Comments
 (0)