This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinexor.js
executable file
·70 lines (62 loc) · 2.21 KB
/
inexor.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
#!/usr/bin/env node
const process = require('process');
const fs = require('fs');
const yargs = require('yargs');
const child_process = require('child_process'); // eslint-disable-line security/detect-child-process
const wait_on = require('wait-on');
const path = require('path');
const inexor_path = require('@inexorgame/path');
const log = require('@inexorgame/logger')();
// Convert URL to command line parameters if necessary
if (process.argv.length == 3 && process.argv[2].startsWith('inexor:')) {
// Convert the URL starting with 'inexor:' to command line arguments
process.argv = process.argv.slice(0, 2).concat(process.argv[2].substr(7).split(' '));
}
let serverDir = path.join(__dirname, 'server');
log.debug(`The server dir is at ${serverDir}`);
const hostname = 'localhost' // This will comfort >90% of our users
const pid_path = path.join(inexor_path.pid_path, `flex.${hostname}.${inexor_path.DEFAULT_PORT}.pid`);
log.debug(`Checking wether the flex pid exists at ${pid_path} exists`)
if (!fs.existsSync(pid_path)) {
log.warn('Inexor Flex is not running! Starting Inexor Flex...');
// Starting Inexor Flex detached without stdio
let serverPath = path.join(serverDir, 'index.js');
log.debug(`Trying to start the server via ${serverPath}`)
const child = child_process.spawn('npm', ['start'],
{
shell: true, // otherwise it wont work on windows.
detached: true,
stdio: 'inherit',
cwd: __dirname // Ensures stuff to run correctly
}
);
child.unref();
}
// Wait until PID is available
wait_on({
resources: [ pid_path ],
delay: 0,
interval: 25,
window: 50,
timeout: 10000
}, function (err) {
if (err) {
log.error('Inexor Flex didn\'t came up:');
log.error(err);
} else {
if (process.argv.length >= 3 && process.argv[2].trim() == 'shell') {
const argv = yargs // eslint-disable-line no-unused-vars
.commandDir(path.join(serverDir, 'commands'))
.demandCommand(1)
.help()
.argv;
} else {
const argv = yargs // eslint-disable-line no-unused-vars
.commandDir(path.join(serverDir, 'commands/cli'))
.command('shell', 'Opens an interactive shell')
.demandCommand(1)
.help()
.argv;
}
}
});