-
Notifications
You must be signed in to change notification settings - Fork 1
/
serve.js
executable file
·124 lines (101 loc) · 2.53 KB
/
serve.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
'use strict'
/**
* Dependencies
*/
const path = require('path');
const http = require('http');
const Express = require('express');
const reload = require('reload');
const chokidar = require('chokidar');
const args = require(`${__dirname}/util/args`).args;
const cnsl = require(`${__dirname}/util/console`);
const resolve = require(`${__dirname}/util/resolve`);
const alerts = resolve('config/alerts');
const global = resolve('config/global');
/**
* Constants
*/
const PORT = process.env.PORT || '7000';
const DIST = path.join(global.base, global.dist);
const GLOBS = [
`${DIST}/**/*.html`,
`${DIST}/**/*.css`,
`${DIST}/**/*.js`
];
/**
* Our Chokidar Watcher
*
* @type {Source} https://github.com/paulmillr/chokidar
*/
const watcher = chokidar.watch(GLOBS, global.chokidar);
/**
* The Express App
*
* @type {Source} https://expressjs.com/
*/
let APP = new Express();
APP.set('port', PORT); // set the port
/**
* Application request handler
*/
APP.get('/*', (request, resolve, next) => {
let req = request.params[0];
// Allow the reload script to pass
if (req === 'reload/reload.js') {
next();
} else {
// If it is a sub-directory request, use the directory's index.html
req = (req.substr(-1) === '/') ? `${req}index.html` : req;
// if the req is blank, use the index file, if it does not have an extension request append .html
let page = (req === '') ? 'index.html' : (req.includes('.')) ? req : `${req}.html`;
resolve.sendFile(`${DIST}/${page}`);
}
});
/**
* Create the http server
*/
let server = http.createServer(APP);
/**
* Main script, turns the server on to listen to the app
*
* @param {Object} app The express app
*/
const main = async (app = APP) => {
// Set the port to listen on
server.listen(app.get('port'), () => {
let port = app.get('port');
cnsl.notify(`${alerts.info} Serving ${alerts.str.path(DIST)} to ${alerts.str.url('http://localhost:' + port)}`);
});
};
/**
* Runner for the serve script
*
* @param {Object} app The express app
*/
const run = async (app = APP) => {
if (args.watch) {
try {
let reloadReturned = await reload(app);
main();
watcher.on('change', () => {
reloadReturned.reload();
cnsl.watching(`Serve reloading`);
});
cnsl.watching(`Serve watching ${alerts.str.ext(GLOBS.join(', '))}`);
} catch (err) {
cnsl.error(`Serve (run): ${err}`);
}
} else {
main()
}
};
/**
* Export our methods
*
* @type {Object}
*/
module.exports = {
main: main,
run: run
};