-
Notifications
You must be signed in to change notification settings - Fork 72
/
bin.js
85 lines (83 loc) · 2.87 KB
/
bin.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
import { spawn } from "child_process";
import path from "path";
import fs from "fs";
import { getDirname} from "cross-dirname";
const __dirname = getDirname();
const debug = process.argv.includes('debug') || process.argv.includes('--inspect');
async function findElectronExecutable() {
const relativePaths = [
'node_modules/electron/dist/electron',
'../electron/dist/electron'
];
for (const relativePath of relativePaths) {
const fullPath = path.resolve(__dirname, relativePath);
const executable = process.platform === 'win32' ? `${fullPath}.exe` : fullPath;
try {
await fs.promises.access(executable, fs.constants.F_OK);
return executable;
} catch (error) { }
}
// Check environment variable
const environmentPath = process.env.ELECTRON_PATH;
if (environmentPath) {
try {
await fs.promises.access(environmentPath, fs.constants.F_OK);
return environmentPath;
}
catch (error) { }
}
// Check global NPM installation directory
const npmGlobalPrefix = process.env.npm_global_prefix;
if (npmGlobalPrefix) {
const globalExecutable = path.resolve(npmGlobalPrefix, 'electron/electron');
const globalExecutableWithExtension = process.platform === 'win32' ? `${globalExecutable}.exe` : globalExecutable;
try {
await fs.promises.access(globalExecutableWithExtension, fs.constants.F_OK);
return globalExecutableWithExtension;
}
catch (error) { }
}
// Default return if not found
return null;
}
findElectronExecutable().then(electronPath => {
if (electronPath) {
const params = []
if(debug) {
params.push(...[
'--inspect',
'--enable-logging=stderr',
'--trace-warnings',
'--remote-debugging-port=9222'
])
}
params.push(path.join(__dirname, 'www/nodejs/dist/main.js'));
const opts = debug ? {} : {
detached: true,
stdio: 'ignore',
};
const child = spawn(electronPath, params, opts);
if(debug) {
child.stdout.on('data', (data) => {
process.stdout.write(data);
});
child.stderr.on('data', (data) => {
process.stderr.write(data);
});
child.on('error', (error) => {
console.error(error);
});
child.once('close', (code) => {
console.log('exitcode: ' + code);
process.exit(code);
});
return;
} else {
child.unref();
}
} else {
console.error('Electron executable not found. Use \'npm i [email protected]\' to install it.');
}
process.exit(0);
}).catch(console.error);