forked from yoshuawuyts/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin.js
executable file
·56 lines (49 loc) · 1.15 KB
/
bin.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
#!/usr/bin/env node
const summary = require('server-summary');
const cliclopts = require('cliclopts');
const router = require('./docs-build');
const minimist = require('minimist');
const assert = require('assert');
const http = require('http');
const url = require('url');
const copts = cliclopts([
{
name: 'port',
abbr: 'p',
default: 1337,
help: 'port for the server to listen on'
}
]);
const argv = minimist(process.argv.slice(2), copts.options());
const cmd = argv._[0];
// help
if (argv.help || !cmd) {
console.log('Usage: command [options]');
copts.print();
process.exit();
}
// listen
if (cmd === 'listen' || cmd === 'server') {
server().listen(argv.port, summary);
}
// build
if (cmd === 'build') {
router.build(__dirname + '/build', function (err, res) {
if (err) {
console.log('error:', err);
process.exit(1);
}
process.exit();
});
}
// create a server
// null -> null
function server () {
return http.createServer(function (req, res) {
const pathname = url.parse(req.url).pathname;
router.match(pathname, function (err, body) {
assert.ifError(err);
res.end(body);
});
});
}