Repository navigation
Expand file tree
/
Copy pathbin.js
More file actions
59 lines (53 loc) · 1.7 KB
/
Copy pathbin.js
File metadata and controls
59 lines (53 loc) · 1.7 KB
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
#!/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){
const child = spawn(electronPath, [path.join(__dirname, 'main.js')], {
detached: true,
stdio: 'ignore',
});
child.unref();
} else {
console.error('Electron executable not found. Use \'npm i electron@9.1.2\' to install it.')
}
process.exit(0);
}).catch(console.error);