forked from lukejacksonn/servor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·70 lines (58 loc) · 1.64 KB
/
cli.js
File metadata and controls
executable file
·70 lines (58 loc) · 1.64 KB
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
#!/usr/bin/env node
const fs = require('fs');
const servor = require('./servor.js');
const readCredentials = () => ({
cert: fs.readFileSync(__dirname + '/servor.crt'),
key: fs.readFileSync(__dirname + '/servor.key')
});
const certify = () =>
require('child_process').execSync(__dirname + '/certify.sh', {
cwd: __dirname
});
const open =
process.platform == 'darwin'
? 'open'
: process.platform == 'win32'
? 'start'
: 'xdg-open';
(async () => {
const args = process.argv.slice(2).filter(x => !~x.indexOf('--'));
const admin = process.getuid && process.getuid() === 0;
let credentials;
// Generate ssl certificates
if (~process.argv.indexOf('--secure')) {
admin && certify();
process.setuid(501);
try {
credentials = readCredentials();
} catch (e) {
certify();
try {
credentials = readCredentials();
} catch (e) {
console.log(
' ⚠️ There was a problem generating ssl credentials. Try removing `--secure`'
);
process.exit();
}
}
}
// Parse arguments from the command line
const { root, protocol, port, ips, url } = await servor({
root: args[0],
fallback: args[1],
port: args[2],
reload: ~process.argv.indexOf('--reload'),
credentials
});
// Output server details to the console
!~process.argv.indexOf('--silent') &&
console.log(`
🗂 Serving:\t${root}\n
🏡 Local:\t${url}
${ips.map(ip => `📡 Network:\t${protocol}://${ip}:${port}`).join('\n ')}
`);
// Browser the server index
~process.argv.indexOf('--browse') &&
require('child_process').execSync(`${open} ${url}`);
})();