-
Notifications
You must be signed in to change notification settings - Fork 125
/
server.js
108 lines (95 loc) · 2.83 KB
/
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
103
104
105
106
107
108
import express from "express";
import { readdirSync, watch } from "node:fs";
import { resolve } from "node:path";
import { spawn, spawnSync } from "node:child_process";
const PORT = process.env.PORT ?? 8000;
process.env.PORT = PORT;
const HOSTNAME = process.env.HOSTNAME ?? `localhost`;
process.env.HOSTNAME = HOSTNAME;
const testing = process.argv.includes(`--test`);
const debug = process.argv.includes(`--debug`);
const npm = process.platform === `win32` ? `npm.cmd` : `npm`;
// Set up the core server
const app = express();
app.use((req, res, next) => {
res.setHeader("Surrogate-Control", "no-store");
res.setHeader(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate"
);
res.setHeader("Expires", "0");
next();
});
app.set("etag", false);
app.use((req, res, next) => {
if (!testing || (testing && debug)) {
console.log(`[${new Date().toISOString()}] ${req.url}`);
}
next();
});
// static routes
app.use(`/`, (req, res, next) => {
const { url } = req;
if (url === `/`) {
if (testing) return res.redirect(`/tests/integration`);
return res.redirect(`/demo`);
}
if (
url.includes(`/dist/drag-drop-touch.esm.min.js`) &&
(!testing || (testing && debug))
) {
return res.redirect(url.replace(`esm.min.js`, `debug.esm.js`));
}
next();
});
app.use(`/`, express.static(`.`));
app.use((req, res) => {
if (req.query.preview) {
res.status(404).send(`Preview not found`);
} else {
res.status(404).send(`${req.url} not found`);
}
});
// Run the server, and trigger a client bundle rebuild every time script.js changes.
app.listen(PORT, () => {
// Generate the server address notice
const msg = `= Server running on http://${HOSTNAME}:${PORT} =`;
const line = `=`.repeat(msg.length);
const mid = `=${` `.repeat(msg.length - 2)}=`;
console.log([``, line, mid, msg, mid, line, ``].join(`\n`));
// are we running tests?
if (testing) {
const runner = spawn(npm, [`run`, `test:integration`], {
stdio: `inherit`,
});
runner.on(`close`, () => process.exit());
runner.on(`error`, () => process.exit(1));
}
// we're not, run in watch mode
else {
try {
watchForRebuild();
} catch (e) {
console.error(e);
}
}
});
/**
* There's a few files we want to watch in order to rebuild the browser bundle.
*/
function watchForRebuild() {
let rebuilding = false;
async function rebuild() {
if (rebuilding) return;
rebuilding = true;
console.log(`rebuilding`);
const start = Date.now();
spawnSync(npm, [`run`, `build`], { stdio: `inherit` });
console.log(`Build took ${Date.now() - start}ms`), 8;
setTimeout(() => (rebuilding = false), 500);
}
function watchList(list) {
list.forEach((filename) => watch(resolve(filename), () => rebuild()));
}
watchList(readdirSync(`./ts`).map((v) => `./ts/${v}`));
}