-
Notifications
You must be signed in to change notification settings - Fork 18
/
mqp.js
105 lines (95 loc) · 2.91 KB
/
mqp.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const chalk = require('chalk');
const fs = require('fs');
const daemon = require('daemon');
const path = require('path');
const update = require('./update');
const tail = require('file-tail');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
const notifier = updateNotifier({
pkg,
updateCheckInterval: 0,
});
if (notifier.update) {
console.log(`Update available ${chalk.dim(notifier.update.current)}${chalk.reset(' → ')}${chalk.green(notifier.update.latest)}`);
}
function getRunningPid(callback) {
fs.readFile(`${__dirname}/pidfile`, {
encoding: 'utf-8',
}, (err, pid) => {
if (err) {
return callback(err);
}
try {
process.kill(parseInt(pid, 10), 0);
callback(null, parseInt(pid, 10));
} catch (e) {
callback(e);
}
});
}
switch (process.argv[2]) {
case 'start':
getRunningPid((err, pid) => {
if (!err) {
console.log('Musiqpad is already running!');
} else {
console.log('\nStarting musiqpad');
console.log(` "${chalk.yellow.bold('npm stop')}" to stop the musiqpad server`);
console.log(` "${chalk.yellow.bold('npm run log')}" to view server output`);
console.log(` "${chalk.yellow.bold('npm restart')}" to restart musiqpad`);
// Spawn a new musiqpad daemon process, might need some more settings but I'm waiting for the new config storage for that.
daemon.daemon(`${__dirname}/start.js`, '--daemon', {
stdout: fs.openSync(path.join(process.cwd(), 'log.txt'), 'a'),
stderr: fs.openSync(path.join(process.cwd(), 'log.txt'), 'a'),
});
}
});
break;
case 'stop':
getRunningPid((err, pid) => {
if (!err) {
process.kill(pid, 'SIGTERM');
console.log('Stopping musiqpad!');
} else {
console.log('Musiqpad is already stopped.');
}
});
break;
case 'restart':
getRunningPid((err, pid) => {
if (!err) {
process.kill(pid, 'SIGTERM');
console.log('\nRestarting musiqpad');
daemon.daemon(`${__dirname}/start.js`, '--daemon', {
stdout: fs.openSync(path.join(process.cwd(), 'log.txt'), 'a'),
stderr: fs.openSync(path.join(process.cwd(), 'log.txt'), 'a'),
});
} else {
console.log('musiqpad could not be restarted, as a running instance could not be found.');
}
});
break;
case 'log': {
console.log('Type Ctrl-C to exit');
const ft = tail.startTailing('./log.txt');
ft.on('line', line => {
console.log(line);
});
break;
}
case 'update':
getRunningPid((err, pid) => {
if (!err) {
process.kill(pid, 'SIGTERM');
console.log('Stopping musiqpad!');
}
update();
});
break;
default:
console.log('Welcome to musiqpad!');
console.log('Usage: npm run {start|stop|restart|log|update}');
// TODO: Add infos for each cmd
break;
}