Update README.md
[KisSync.git] / index.js
blob28f9db508cd2dcf0744325d8d2a65b647a57a743
1 #!/usr/bin/env node
3 const ver = process.version.match(/v(\d+)\.\d+\.\d+/);
5 if (parseInt(ver[1], 10) < 12) {
6     console.error(
7         `node.js ${process.version} is not supported. ` +
8         'CyTube requires node v12 or later.'
9     )
10     process.exit(1);
13 checkPlayerExists();
15 const args = parseArgs();
17 if (args.has('--daemonize')) {
18     fork();
19 } else {
20     try {
21         require('./lib/main');
22     } catch (err) {
23         console.error('FATAL: Failed to require() lib/main.js');
24         handleStartupError(err);
25     }
28 function fork() {
29     try {
30         console.log('Warning: --daemonize support is experimental.  Use with caution.');
32         const spawn = require('child_process').spawn;
33         const path = require('path');
34         const main = path.resolve(__dirname, 'lib', 'main.js');
36         const child = spawn(process.argv[0], [main], {
37             detached: true,
38             stdio: 'ignore' // TODO: support setting stdout/stderr logfile
39         });
41         child.unref();
42         console.log('Forked with PID ' + child.pid);
43     } catch (error) {
44         console.error('FATAL: Failed to fork lib/main.js');
45         handleStartupError(error);
46     }
49 function handleStartupError(err) {
50     if (/module version mismatch/i.test(err.message)) {
51         console.error('Module version mismatch, try running `npm rebuild` or ' +
52                       'removing the node_modules folder and re-running ' +
53                       '`npm install`');
54     } else {
55         console.error('Possible causes:\n' +
56                       '  * You haven\'t run `npm run build-server` to regenerate ' +
57                       'the runtime\n' +
58                       '  * You\'ve upgraded node/npm and haven\'t rebuilt dependencies ' +
59                       '(try `npm rebuild` or `rm -rf node_modules && npm install`)\n' +
60                       '  * A dependency failed to install correctly (check the output ' +
61                       'of `npm install` next time)');
62     }
64     console.error(err.stack);
65     process.exit(1);
68 function parseArgs() {
69     const args = new Map();
70     for (var i = 2; i < process.argv.length; i++) {
71         if (/^--/.test(process.argv[i])) {
72             var val;
73             if (i+1 < process.argv.length) val = process.argv[i+1];
74             else val = null;
76             args.set(process.argv[i], val);
77         }
78     }
80     return args;
83 function checkPlayerExists() {
84     const fs = require('fs');
85     const path = require('path');
87     const playerDotJs = path.join(__dirname, 'www', 'js', 'player.js');
89     if (!fs.existsSync(playerDotJs)) {
90         console.error(
91             'Missing video player: www/js/player.js.  This should have been ' +
92             'automatically generated by the postinstall step of ' +
93             '`npm install`, but you can manually regenerate it by running ' +
94             '`npm run build-player`'
95         );
96         process.exit(1);
97     }