-
Notifications
You must be signed in to change notification settings - Fork 70
/
cli.js
executable file
·99 lines (85 loc) · 2.52 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs');
const servor = require('./servor.js');
const openBrowser = require('./utils/openBrowser.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;
if (args[0] && args[0].startsWith('gh:')) {
const repo = args[0].replace('gh:', '');
const dest = repo.split('/')[1];
if (!fs.existsSync(dest)) {
try {
require('child_process').execSync(
`git clone https://github.com/${repo}`,
{ stdio: 'ignore' }
);
} catch (e) {
console.log(
`\n ⚠️ Could not clone from https://github.com/${repo}\n`
);
process.exit();
}
}
args[0] = dest;
}
if (~process.argv.indexOf('--editor')) {
try {
require('child_process').execSync(`code ${args[0] || '.'}`);
} catch (e) {
console.log(`\n ⚠️ Could not open code editor for ${args[0] || '.'}`);
}
}
// Generate ssl certificates
if (~process.argv.indexOf('--secure')) {
admin && certify();
admin && process.platform === 'darwin' && process.setuid(501);
try {
credentials = readCredentials();
} catch (e) {
certify();
try {
credentials = readCredentials();
} catch (e) {
console.log(
'\n ⚠️ There was a problem generating ssl credentials. Try removing `--secure`\n'
);
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'),
module: !!~process.argv.indexOf('--module'),
static: !!~process.argv.indexOf('--static'),
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') && openBrowser(url);
})();