-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
129 lines (119 loc) · 4.86 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const os = require('os');
const pkg = require('./package.json');
const Manager = require('./manager');
const Ora = require('ora');
const Table = require('cli-table3');
const prog = require('caporal');
prog
.version(pkg.version)
.command('start', 'Start cast-web-api as daemon')
.option('-H --hostname <host>', 'Hostname cast-web-api webserver should bind to')
.option('-p --port <port>', 'Port cast-web-api webserver should bind to')
.option('-d --debug <port>', 'Toggles debugging log messages')
.option('-a --autoConnect <port>', 'Cast devices auto connect on discovery, devices will reconnect regardless on address change.')
.option('-r --reconnectTimeout <port>', 'Cast devices trie to reconnect every x ms if it goes down')
.action((args, options, logger) => {
let spinner = Ora('Starting cast-web-api').start();
Manager.start(process.argv)
.then(value => {
let table = getTableHead();
value.forEach(({pid='-', name='-', pm2_env={status: '-'}, address='-'}) => {
table.push([pid, name, pm2_env.status, address, pm2_env.pm_out_log_path]);
});
spinner.succeed("Started service");
console.log(table.toString());
})
.catch(error => {
spinner.fail("Error starting cast-web-api:");
});
})
.command('stop', 'Stop the cast-web-api daemon')
.action((args, options, logger) => {
let spinner = Ora('Stopping cast-web-api').start();
Manager.stop()
.then(value => {
let table = getTableHead();
value.forEach(({pid='-', name='-', pm2_env={status: '-'}, address='-'}) => {
table.push([pid, name, pm2_env.status, address, pm2_env.pm_out_log_path]);
});
spinner.succeed("Stopped service");
console.log(table.toString());
})
.catch(error => {
spinner.fail("Error stopping cast-web-api:");
console.log(error);
});
})
.command('status', 'Check status of the cast-web-api daemon')
.alias('info')
.action((args, options, logger) => {
let spinner = Ora('Getting cast-web-api status').start();
Manager.status()
.then(value => {
let table = getTableHead();
value.forEach(({pid='-', name='-', pm2_env={status: '-', pm_out_log_path:'-'}, address='-'}) => {
table.push([pid, name, pm2_env.status, address, pm2_env.pm_out_log_path]);
});
spinner.succeed("cast-web-api status");
console.log(table.toString());
})
.catch(error => {
spinner.fail("Error getting status of cast-web-api:");
console.log(error);
});
})
.command('startup', 'Start the cast-web-api daemon on system startup')
.action((args, options, logger) => {
let spinner = Ora('Setting up startup: ').start();
Manager.startup()
.then(value => {
spinner.succeed("cast-web-api startup");
console.log(value);
})
.catch(error => {
if (error.error && error.error.message) {
spinner.fail(error.error.message);
} else {
spinner.fail("Error: ");
//console.error(error);
}
console.log(error.stdout);
});
})
.command('unstartup', 'Remove the cast-web-api daemon start on system startup')
.action((args, options, logger) => {
let spinner = Ora('Setting up unstartup: ').start();
Manager.unstartup()
.then(value => {
spinner.succeed("cast-web-api unstartup");
console.log(value);
})
.catch(error => {
if (error.error && error.error.message) {
spinner.fail(error.error.message);
} else {
spinner.fail("Error: ");
//console.error(error);
}
console.log(error.stdout);
});
})
.command('fix-perm', 'Changes permissions on /config to current user. Will be removed in next release!')
.action((args, options, logger) => {
let spinner = Ora('Fixing permissions: ').start();
Manager.fixPermission()
.then(value => {
spinner.succeed("/config permissions set");
console.log(value);
})
.catch(error => {
spinner.fail(error.error.message);
console.log(error.stdout);
});
});
prog.parse(process.argv);
function getTableHead() {
return new Table({
head: ['pid', 'name', 'status', 'address', 'log'],
});
}