-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
70 lines (57 loc) · 1.85 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
59
60
61
62
63
64
65
66
67
68
69
70
const path = require('path')
const fs = require('fs')
const { execSync, spawnSync } = require('child_process')
const configExample = require('./config.example.json')
const pkill = require('./pkill')
async function run(isForce = false) {
console.log('👀 detect platform:', process.platform)
if (process.platform !== 'win32' && process.platform !== 'linux') {
console.error('❌ os system is not supported:', process.platform)
process.exit(1)
}
const folder = path.dirname(process.execPath)
let execPath = process.execPath
if (
execPath.endsWith('nodejs')
|| execPath.endsWith('node')
|| execPath.endsWith('nodejs.exe')
|| execPath.endsWith('node.exe')
) {
if (process.platform === 'win32') {
execPath = 'dev-dns-win.exe'
} else if (process.platform === 'linux') {
execPath = 'dev-dns-linux'
}
}
const exe = path.basename(execPath)
const targetExec = path.join(folder, exe)
if (isForce) {
console.log('🔥 force kill old service')
await pkill.pkill({ exe })
}
const config = path.join(folder, 'config.json')
if (fs.existsSync(config)) {
console.log('🔧 skip config file, already exist:', config)
} else {
console.log('🔧 setup default config file:', config)
fs.writeFileSync(config, JSON.stringify(configExample, null, 2))
}
console.log('🌐 default primary dns:', configExample.dns.primary)
console.log('🚀 launch process:', targetExec)
const cwd = folder
if (process.platform === 'win32') {
execSync(`Start-Process -WindowStyle hidden -FilePath ${targetExec} -WorkingDirectory ${cwd}`, {
cwd, stdio: 'ignore', shell: 'powershell.exe',
})
} else if (process.platform === 'linux') {
execSync(`./${exe} /snapshot/dev_dns/server.js &`,
{
cwd,
stdio: 'ignore',
detached: true,
})
}
}
module.exports = {
run,
}