UNPKG

2.73 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/*
4 * Documentative CLI
5 * (c) 2020 dragonwocky <thedragonring.bod@gmail.com>
6 * (https://dragonwocky.me/) under the MIT license
7 */
8
9const argv = require('minimist')(process.argv.slice(2));
10
11if (!argv['_'].length || argv.h || argv.help) {
12 const pkg = require('./package.json');
13 console.log(`
14documentative v${pkg.version}
15 by ${pkg.author}
16 licensed under the ${pkg.license} license
17 for more info, go to ${pkg.homepage}
18
19usage:
20
21 > documentative-build <inputdir> <outputdir>
22 e.g. documentative-build pages build
23
24 > documentative-serve <inputdir> [-p PORT]
25 e.g. documentative-serve pages -p 3000
26
27options:
28 --help, -h show this message
29 --port, -p set the HTTP server port used by documentative-serve
30 (default: 8080)
31
32** to configure the process, place configuration options into
33 <inputdir>/docs.json - check the docs for info on these options
34 `);
35} else if (process.argv[1].endsWith('/documentative-build')) {
36 const fs = require('fs'),
37 path = require('path'),
38 config = {
39 exclude: [],
40 ...(fs.existsSync(path.join(argv['_'][0], 'docs.json'))
41 ? JSON.parse(
42 fs.readFileSync(path.join(argv['_'][0], 'docs.json'), 'utf8')
43 )
44 : {})
45 };
46 config.exclude.unshift('docs.json');
47 require('./index.js')
48 .build(argv['_'][0], argv['_'][1], config)
49 .then(success => {
50 if (success)
51 console.log(
52 `documentative: successfully built ${argv['_'][0]} to ${argv['_'][1]}`
53 );
54 })
55 .catch(err => {
56 console.error(err);
57 process.exit(1);
58 });
59} else if (process.argv[1].endsWith('/documentative-serve')) {
60 const fs = require('fs'),
61 path = require('path'),
62 config = {
63 exclude: [],
64 ...(fs.existsSync(path.join(argv['_'][0], 'docs.json'))
65 ? JSON.parse(
66 fs.readFileSync(path.join(argv['_'][0], 'docs.json'), 'utf8')
67 )
68 : {})
69 },
70 port = ![null, undefined].includes(argv.p)
71 ? argv.p
72 : ![null, undefined].includes(argv.port)
73 ? argv.port
74 : 8080;
75 config.exclude.unshift('docs.json');
76 require('./index.js')
77 .serve(argv['_'][0], port, config)
78 .then(server => {
79 console.info(
80 `Serving HTTP on 0.0.0.0 port ${port} (http://localhost:${port}/)...`
81 );
82 server.on('request', (req, res) => {
83 console.info(
84 `- served ${req.url} to ${(req.headers['x-forwarded-for'] || '')
85 .split(',')
86 .pop() ||
87 req.connection.remoteAddress ||
88 req.socket.remoteAddress ||
89 req.connection.socket.remoteAddress}`
90 );
91 });
92 })
93 .catch(err => {
94 console.error(err);
95 process.exit(1);
96 });
97}