-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin-debug.js
76 lines (70 loc) · 2.11 KB
/
bin-debug.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
#!/usr/bin/env node
const { spawn } = require('child_process');
const path = require('path'), fs = require('fs');
async function findElectronExecutable() {
const relativePaths = [
'node_modules/electron/dist/electron',
'www/nodejs-project/node_modules/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) {
// File not found
}
}
// 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) {
// File not found
}
}
// Default return if not found
return null;
}
findElectronExecutable().then(electronPath => {
if (electronPath) {
console.log(electronPath)
const child = spawn(electronPath, [
'--inspect',
'--enable-logging=stderr',
'--trace-warnings',
'--remote-debugging-port=9222',
path.join(__dirname, 'main.js')
]);
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);
});
console.log(electronPath)
} else {
console.error('Electron executable not found. Use \'npm i electron@9.1.2\' to install it.')
process.exit(0);
}
}).catch(console.error);