-
Notifications
You must be signed in to change notification settings - Fork 0
/
.cli-dev.js
156 lines (143 loc) · 3.35 KB
/
.cli-dev.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
/**
* Module dependencies.
*/
const http = require('http');
const path = require('path');
const broadcastemCore = require('./dist/index');
const fs = require('fs');
const os = require('os');
let server, interfaceTimer, addressCount;
/**
* Accept CLI arguments
*/
const yargs = require('yargs');
const argv = yargs
.options({
port: {
alias: 'p',
describe: 'the port to bind to on this machine',
default: process.env.PORT || 3000,
group: 'Configuration:',
type: 'number',
requiresArg: true,
},
list: {
alias: 'L',
describe:
'a list file which has the paths to the files you want to share',
group: 'Configuration:',
type: 'string',
requiresArg: true,
},
destination: {
alias: 'd',
describe: 'folder where incoming files will be saved',
default: path.resolve(
os.homedir(),
'Downloads',
'Broadcastem Received'
),
group: 'Configuration:',
type: 'string',
requiresArg: true,
},
'logging-level': {
alias: ['l', 'log'],
describe: `one of the following logging levels
0 - do not log anything
1 - log only errors (Response codes > 400)
2 - log all requests`,
default: 0,
group: 'Configuration:',
type: 'number',
requiresArg: true,
},
'core-version': {
alias: 'c',
describe: `print version of the core module and exit`,
type: 'boolean',
},
})
.check(argv => {
if (isNaN(argv.port)) throw new Error('Port should be a number');
else if (argv.port <= 0) throw new Error('Port should be positive');
else if (argv.list && !fs.existsSync(argv.list))
throw new Error(`Specified file ${argv.list} doesnt exist`);
else if (isNaN(argv.loggingLevel))
throw new Error('Logging level should be a number');
else if (argv.loggingLevel < 0 || argv.loggingLevel > 2)
throw new Error('Invalid logging level');
else if (!argv._.every(path => fs.existsSync(path)))
throw new Error(
'All the files to be shared must be present on disk'
);
return true;
})
.wrap(yargs.terminalWidth()).argv;
// Since we are taking file paths as the remaining input, pass it on to files property for easy reading
argv.files = argv._;
argv.restart = true;
if (argv.coreVersion) {
console.log(
require(path.resolve(
require.resolve('broadcastem-core'),
'..',
'package.json'
)).version
);
process.exit(0);
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
switch (error.code) {
case 'EACCES':
console.error(argv.port + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(argv.port + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Print the port to the console
*/
function onListening() {
console.log(`Listening on ${argv.port}`);
}
/**
* Start Express app from the CLI flags
*/
broadcastemCore(argv)
.then(app => {
/**
* Create HTTP server.
*/
server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(argv.port);
server.on('error', onError);
server.on('listening', onListening);
})
.catch(err => {
console.error(err);
process.exit(1);
})
.finally(() => {
process.on('SIGINT', function () {
if (server.listening) {
clearTimeout(interfaceTimer);
server.close();
}
console.log('Shutting down server');
process.exit();
});
});