-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrun.js
58 lines (47 loc) · 1.76 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { spawn } = require('child_process');
const os = require('os');
// Define the base name of your application
const appName = 'terminalcss-builder';
// Get the OS and architecture
const platform = os.platform(); // 'darwin', 'linux', 'win32', etc.
const arch = os.arch(); // 'x64', 'arm64', 'ia32' (for x86), etc.
// Map Node.js architecture and platform strings to your binary naming convention
const platformMap = {
'win32': 'windows',
'darwin': 'darwin',
'linux': 'linux'
};
const archMap = {
'x64': 'amd64',
'arm64': 'arm64',
'ia32': '386' // ia32 represents x86 in Node.js
};
// Construct the binary name based on the platform and architecture
let binaryName = `./builds/${appName}-${platformMap[platform]}-${archMap[arch]}`;
if (platform === 'win32') {
binaryName += '.exe';
}
// Add command line arguments to the binary command
const args = process.argv.slice(2).join(' '); // Skip the first two elements
// Spawn the binary process
const binaryProcess = spawn(binaryName, [args]);
// Forward stdout and stderr of the binary to the Node.js process
binaryProcess.stdout.on('data', (data) => {
process.stdout.write(data);
});
binaryProcess.stderr.on('data', (data) => {
process.stderr.write(data);
});
// Handle binary process exit
binaryProcess.on('close', (code) => {
console.log(`Binary process exited with code ${code || 0}.`);
process.exit(code);
});
// Function to terminate the binary process gracefully
const terminateBinaryProcess = () => {
console.log('Terminating binary process...');
binaryProcess.kill(); // Sends SIGTERM to the binary process
};
// Listen for termination signals
process.on('SIGINT', terminateBinaryProcess); // Ctrl+C signal
process.on('SIGTERM', terminateBinaryProcess); // Termination signal