79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
import * as esbuild from 'esbuild';
|
|
import * as fs from 'node:fs';
|
|
import http from 'node:http';
|
|
|
|
const colors = {
|
|
reset: '\x1b[0m',
|
|
dim: '\x1b[2m',
|
|
bright: '\x1b[1m',
|
|
normal: '\x1b[22m',
|
|
red: '\x1b[31m',
|
|
green: '\x1b[32m',
|
|
yellow: '\x1b[33m',
|
|
}
|
|
|
|
const regex = new RegExp('mapsheets\..+\.svg');
|
|
|
|
const svgUseCacheBust = {
|
|
name: 'svgUseCacheBust',
|
|
setup(build) {
|
|
build.onStart(() => {
|
|
const version = Date.now();
|
|
console.log(`Adding cache buster ${version}`);
|
|
const file = fs.readFileSync('./src/scenario-side_show.svg', { encoding: 'utf-8' });
|
|
const newFile = file.replaceAll('%%VERSION%%', version);
|
|
fs.writeFileSync('./public/assets/images/scenario-side_show.svg', newFile);
|
|
const files = fs.readdirSync('./public/assets/images').filter(fn => regex.test(fn));
|
|
files.forEach(fn => fs.unlinkSync(`./public/assets/images/${fn}`));
|
|
fs.copyFileSync('./public/assets/images/mapsheets.svg', `./public/assets/images/mapsheets.${version}.svg`);
|
|
})
|
|
}
|
|
}
|
|
|
|
const ctx = await esbuild.context({
|
|
entryPoints: ['src/*.js'],
|
|
bundle: true,
|
|
outdir: 'public',
|
|
plugins: [svgUseCacheBust],
|
|
});
|
|
|
|
await ctx.watch();
|
|
|
|
const { host, port } = await ctx.serve({
|
|
servedir: 'public',
|
|
port: 3000,
|
|
onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
|
|
let statusColor = colors.red;
|
|
|
|
if (status >= 200 && status <= 299) {
|
|
statusColor = colors.green;
|
|
} else if (status >= 300 && status <= 399) {
|
|
statusColor = colors.yellow;
|
|
}
|
|
|
|
console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
|
|
},
|
|
});
|
|
|
|
http.createServer((req, res) => {
|
|
const options = {
|
|
hostname: host,
|
|
port: port,
|
|
path: req.url,
|
|
method: req.method,
|
|
headers: req.headers,
|
|
}
|
|
|
|
const proxyReq = http.request(options, proxyRes => {
|
|
for (const k in proxyRes.headers) {
|
|
res.setHeader(k, proxyRes.headers[k]);
|
|
}
|
|
|
|
res.writeHead(proxyRes.statusCode, { 'Cache-Control': 'no-cache, no-store, must-revalidate' });
|
|
|
|
proxyRes.pipe(res, { end: true });
|
|
})
|
|
|
|
req.pipe(proxyReq, { end: true });
|
|
}).listen(8080);
|