2017-08-14 05:16:42 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-03-14 05:36:21 +00:00
|
|
|
const ver = process.version.match(/v(\d+)\.\d+\.\d+/);
|
|
|
|
|
2019-12-02 00:04:55 +00:00
|
|
|
if (parseInt(ver[1], 10) < 10) {
|
2018-03-14 05:36:21 +00:00
|
|
|
console.error(
|
|
|
|
`node.js ${process.version} is not supported. ` +
|
2019-12-02 00:04:55 +00:00
|
|
|
'CyTube requires node v10 or later.'
|
2018-03-14 05:36:21 +00:00
|
|
|
)
|
2016-08-05 02:00:20 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-04-06 03:39:49 +00:00
|
|
|
checkPlayerExists();
|
|
|
|
|
2017-08-14 05:16:42 +00:00
|
|
|
const args = parseArgs();
|
|
|
|
|
|
|
|
if (args.has('--daemonize')) {
|
|
|
|
fork();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
require('./lib/main');
|
|
|
|
} catch (err) {
|
|
|
|
console.error('FATAL: Failed to require() lib/main.js');
|
|
|
|
handleStartupError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fork() {
|
|
|
|
try {
|
|
|
|
console.log('Warning: --daemonize support is experimental. Use with caution.');
|
|
|
|
|
2018-03-14 05:36:21 +00:00
|
|
|
const spawn = require('child_process').spawn;
|
2017-08-14 05:16:42 +00:00
|
|
|
const path = require('path');
|
|
|
|
const main = path.resolve(__dirname, 'lib', 'main.js');
|
|
|
|
|
|
|
|
const child = spawn(process.argv[0], [main], {
|
|
|
|
detached: true,
|
|
|
|
stdio: 'ignore' // TODO: support setting stdout/stderr logfile
|
|
|
|
});
|
|
|
|
|
|
|
|
child.unref();
|
|
|
|
console.log('Forked with PID ' + child.pid);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('FATAL: Failed to fork lib/main.js');
|
|
|
|
handleStartupError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleStartupError(err) {
|
2015-12-22 01:34:22 +00:00
|
|
|
if (/module version mismatch/i.test(err.message)) {
|
2017-08-14 05:16:42 +00:00
|
|
|
console.error('Module version mismatch, try running `npm rebuild` or ' +
|
|
|
|
'removing the node_modules folder and re-running ' +
|
|
|
|
'`npm install`');
|
2015-12-22 01:34:22 +00:00
|
|
|
} else {
|
|
|
|
console.error('Possible causes:\n' +
|
|
|
|
' * You haven\'t run `npm run build-server` to regenerate ' +
|
|
|
|
'the runtime\n' +
|
|
|
|
' * You\'ve upgraded node/npm and haven\'t rebuilt dependencies ' +
|
|
|
|
'(try `npm rebuild` or `rm -rf node_modules && npm install`)\n' +
|
|
|
|
' * A dependency failed to install correctly (check the output ' +
|
|
|
|
'of `npm install` next time)');
|
|
|
|
}
|
2017-08-14 05:16:42 +00:00
|
|
|
|
2015-10-27 05:56:53 +00:00
|
|
|
console.error(err.stack);
|
2015-10-07 04:25:27 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2013-10-11 20:48:01 +00:00
|
|
|
|
2017-08-14 05:16:42 +00:00
|
|
|
function parseArgs() {
|
|
|
|
const args = new Map();
|
2018-03-14 05:36:21 +00:00
|
|
|
for (var i = 2; i < process.argv.length; i++) {
|
2017-08-14 05:16:42 +00:00
|
|
|
if (/^--/.test(process.argv[i])) {
|
2018-03-14 05:36:21 +00:00
|
|
|
var val;
|
2017-08-14 05:16:42 +00:00
|
|
|
if (i+1 < process.argv.length) val = process.argv[i+1];
|
|
|
|
else val = null;
|
2013-10-11 20:48:01 +00:00
|
|
|
|
2017-08-14 05:16:42 +00:00
|
|
|
args.set(process.argv[i], val);
|
2016-04-24 02:53:18 +00:00
|
|
|
}
|
2014-02-26 20:28:24 +00:00
|
|
|
}
|
2016-07-16 05:06:27 +00:00
|
|
|
|
2017-08-14 05:16:42 +00:00
|
|
|
return args;
|
2016-07-16 05:06:27 +00:00
|
|
|
}
|
2018-04-06 03:39:49 +00:00
|
|
|
|
|
|
|
function checkPlayerExists() {
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const playerDotJs = path.join(__dirname, 'www', 'js', 'player.js');
|
|
|
|
|
|
|
|
if (!fs.existsSync(playerDotJs)) {
|
|
|
|
console.error(
|
|
|
|
'Missing video player: www/js/player.js. This should have been ' +
|
|
|
|
'automatically generated by the postinstall step of ' +
|
|
|
|
'`npm install`, but you can manually regenerate it by running ' +
|
|
|
|
'`npm run build-player`'
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|