1 | const mime = require('mime-types');
|
2 | const path = require('path');
|
3 | const fs = require('fs');
|
4 | const { spawn } = require('child_process');
|
5 | const { promisify } = require('util');
|
6 | const pidusage = require('pidusage');
|
7 | const selfupdate = require('selfupdate');
|
8 | const package = require('./package.json');
|
9 |
|
10 | let processPath = path.join(__dirname, '/config/process.json');
|
11 |
|
12 | let pidFile = fs.existsSync(processPath) ? require('./config/process.json') : { ids: {} }
|
13 |
|
14 | module.exports.reload = () => {
|
15 | Object.keys(require.cache).forEach(key => { delete require.cache[key] });
|
16 | }
|
17 |
|
18 | module.exports.start = () => {
|
19 | if (pidFile.ids.master) {
|
20 |
|
21 | console.log('Blaze Web Server already running');
|
22 | } else {
|
23 | console.log('Starting Blaze Web Server');
|
24 | let daemon = spawn('node', ['start.js'], { detached: true });
|
25 | daemon.on('error', (err) => {
|
26 | console.error(err);
|
27 | });
|
28 | pidFile.ids.master = daemon.pid;
|
29 | savePID();
|
30 | process.exit();
|
31 | }
|
32 | }
|
33 |
|
34 | module.exports.stop = () => {
|
35 |
|
36 | if (!pidFile.ids.master) {
|
37 | console.log('Blaze Web Server is not running');
|
38 | } else {
|
39 | console.log('Stopping Blaze Web Server');
|
40 | try {
|
41 | process.kill(pidFile.ids.master, ["SIGTERM"]);
|
42 | } catch (error) {
|
43 | console.error(error);
|
44 | }
|
45 | }
|
46 | delete pidFile.ids.master;
|
47 | savePID();
|
48 | }
|
49 |
|
50 | module.exports.restart = () => {
|
51 | module.exports.stop();
|
52 | module.exports.start();
|
53 | }
|
54 |
|
55 | module.exports.version = () => {
|
56 | console.log('v' + package.version);
|
57 | }
|
58 |
|
59 | module.exports.update = async() => {
|
60 | let isUpdated = await promisify(selfupdate.isUpdated)(package);
|
61 | if (isUpdated) {
|
62 | console.log('Blaze Web Server is already up to date');
|
63 | } else {
|
64 | let newVersion = await promisify(selfupdate.update)(package);
|
65 | console.log('The package was updated to version: ' + newVersion);
|
66 | }
|
67 | }
|
68 |
|
69 | module.exports.help = (commands) => {
|
70 | console.log('blaze web server v' + package.version + '\n');
|
71 | console.log('Commands:');
|
72 | let strLength = 4 + commands.reduce(function(longestCommand, schema) {
|
73 | return Math.max(longestCommand, schema.command.length);
|
74 | }, 0);
|
75 |
|
76 | commands.forEach(
|
77 | schema => {
|
78 | console.log(' blaze ' + (schema.command.padEnd(strLength)) + schema.help);
|
79 | }
|
80 | );
|
81 | }
|
82 |
|
83 | function savePID() {
|
84 | fs.writeFileSync(processPath, JSON.stringify(pidFile));
|
85 | } |
\ | No newline at end of file |