-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-server.js
102 lines (88 loc) · 2.84 KB
/
dev-server.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
'use strict';
import _ from 'lodash';
import minimist from 'minimist';
import path from 'path';
import reload from 'reload';
import express from 'express';
import http from 'http';
import chokidar from 'chokidar';
import fs from 'fs';
import watchers from './src/devserver/watchers.js';
import machinery from './src/machinery.js'
import reloadInjector from './src/devserver/reload-injector.js';
import readConfig from './src/read_config.js';
// Development server - uses reload for http and runs prepply
const PORT = 8080;
function usage(){
console.log(`
usage:
$ node dev-server.js <options>
where <options> are
--indir <dir> :: input site directory of .md etc.
--outdir <dir> :: output site directory of html
--static <dir> :: [optional] path to additional static assets
--config <file> :: config yml file to use (default = <indir>/config.yml)
--noclean :: don't clean output dir (default = false, clean output dir)
--nowatch :: don't watch for changes
`);
}
function argsValid(args, config){
return _.every(['indir', 'outdir'], p => _.has(args, p) || _.has(config, p));
}
function setDefaults(args){
const defaults = {
config: `${args.indir}/config.yml`,
noclean: false,
nowatch: false
};
return _.assign(defaults, args);
}
async function run(args, config){
console.log('Running initial site prep...');
await machinery.run(args);
const reloadServer = await startServer(args);
if(args.nowatch){
return console.log('--nowatch specified, skipping fs change watching.');
}
watchers.configure(args, reloadServer, config.layoutsdir);
}
function startServer(args){
console.log('Starting http server...');
const app = express();
app.use(function(req, res, next) {
if (req.path.replace(/\/$/, '').match(/\/[-\w]+$/)) {
const file = `${args.outdir}${req.path.replace(/\/$/, '')}.html`;
return fs.exists(file, exists => {
console.log(`${req.url} => ${req.url}.html`);
if (exists) req.url += '.html';
next();
});
}
next();
});
app.use(reloadInjector);
app.use(express.static(args.outdir));
if(args.static){
app.use(express.static(args.static));
}
const server = http.createServer(app);
const reloadServer = reload(app, { verbose: true}); //reload pages when stuff changes on disk
server.listen(8080, () => {
console.log(`Web server listening on port ${PORT}`);
});
return reloadServer;
}
const inputArgs = minimist(process.argv.slice(2));
const config = readConfig(path.resolve(inputArgs.config));
if(!argsValid(inputArgs, config)){
usage();
process.exit(1);
}
const args = setDefaults(inputArgs);
args.indir = path.resolve(args.indir || config.indir);
args.outdir = path.resolve(args.outdir || config.outdir);
args.config = path.resolve(args.config);
console.log(args);
run(args, config).then(() => {
console.log('dev-server done.');
});