-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnwdc.js
53 lines (45 loc) · 1.38 KB
/
nwdc.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
#!/usr/bin/env node
// Run NW.js
const { exec, spawn } = require('child_process');
const path = require('path');
const appDir = process.argv.length > 2 ? process.argv[2] : '.';
let appManifest;
try {
appManifest = require(path.resolve(appDir, 'package.json'));
} catch (error) {
console.log(`Error reading package.json: ${error}`);
process.exit(1);
}
const nwBin = require('hasbin').sync('nw')
? 'nw'
: (appManifest.dependencies && Object.keys(appManifest.dependencies).includes('nw'))
|| (appManifest.devDependencies && Object.keys(appManifest.devDependencies).includes('nw'))
? 'npx nw'
: null;
if (!nwBin) {
console.log(`NW.js must be in %PATH% or included as a dependency in ${appDir}/package.json`);
process.exit(1);
}
// Start NW.js process
const proc = exec(`${nwBin} ${appDir}`);
// Kill app on CTRL+C
process.on('SIGINT', () => {
spawn('taskkill', ['/pid', proc.pid, '/f', '/t']);
});
// Keeps this wrapper process running
let keepAlive;
let quit = false;
function keepAliveCallback(){
if(!quit) keepAlive = setTimeout(keepAliveCallback, 1000);
}
keepAliveCallback();
// Relay stdout and stderr from process until it exits
proc.stdout.on("data", function(data){
process.stdout.write(data);
});
proc.stderr.on("data", function(data){
process.stderr.write(data);
});
proc.on("exit", function(){
quit = true;
});