UNPKG

2.61 kBJavaScriptView Raw
1const mime = require('mime-types');
2const path = require('path');
3const fs = require('fs');
4const { spawn } = require('child_process');
5const { promisify } = require('util');
6const pidusage = require('pidusage');
7const selfupdate = require('selfupdate');
8const package = require('./package.json');
9
10let processPath = path.join(__dirname, '/config/process.json');
11
12let pidFile = fs.existsSync(processPath) ? require('./config/process.json') : { ids: {} }
13
14module.exports.reload = () => {
15 Object.keys(require.cache).forEach(key => { delete require.cache[key] });
16}
17
18module.exports.start = () => {
19 if (pidFile.ids.master) {
20 // add a check for if the pid is truly found and is blaze
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
34module.exports.stop = () => {
35 // Only try if the pid is found and is blaze
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
50module.exports.restart = () => {
51 module.exports.stop();
52 module.exports.start();
53}
54
55module.exports.version = () => {
56 console.log('v' + package.version);
57}
58
59module.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
69module.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
83function savePID() {
84 fs.writeFileSync(processPath, JSON.stringify(pidFile));
85}
\No newline at end of file