AutoReloader generalization
This commit is contained in:
12
out/auto-reload.d.ts
vendored
12
out/auto-reload.d.ts
vendored
@@ -1,3 +1,11 @@
|
|||||||
export declare const AutoReloader: {
|
import { Router } from 'express';
|
||||||
router: import("express-serve-static-core").Router;
|
export declare type ReloaderConfig = {
|
||||||
|
frontendDirs: string | string[];
|
||||||
|
subPath?: string;
|
||||||
|
jsName?: string;
|
||||||
};
|
};
|
||||||
|
declare function getRouter(opts: ReloaderConfig): Router;
|
||||||
|
export declare const AutoReloader: {
|
||||||
|
getRouter: typeof getRouter;
|
||||||
|
};
|
||||||
|
export {};
|
||||||
|
|||||||
@@ -4,26 +4,35 @@ exports.AutoReloader = void 0;
|
|||||||
const express_1 = require("express");
|
const express_1 = require("express");
|
||||||
const _1 = require(".");
|
const _1 = require(".");
|
||||||
const uuid_1 = require("uuid");
|
const uuid_1 = require("uuid");
|
||||||
const router = express_1.Router();
|
function getRouter(opts) {
|
||||||
if (!_1.DefaultConfig.isProduction) {
|
const config = {
|
||||||
|
frontendDirs: Array.isArray(opts.frontendDirs) ? opts.frontendDirs : [opts.frontendDirs],
|
||||||
|
subPath: opts.subPath || '/auto-reload',
|
||||||
|
jsName: opts.jsName || 'client.js',
|
||||||
|
};
|
||||||
|
const router = express_1.Router();
|
||||||
|
if (!_1.DefaultConfig.isProduction) {
|
||||||
let uuid = uuid_1.v4();
|
let uuid = uuid_1.v4();
|
||||||
let updateTimeout = undefined;
|
let updateTimeout = undefined;
|
||||||
Promise.resolve().then(() => require("node-watch")).then((watch) => {
|
Promise.resolve().then(() => require("node-watch")).then((watch) => {
|
||||||
watch.default('public', { recursive: true }, () => {
|
for (const frontendDir of config.frontendDirs) {
|
||||||
|
watch.default(frontendDir, { recursive: true }, () => {
|
||||||
if (updateTimeout !== undefined)
|
if (updateTimeout !== undefined)
|
||||||
clearTimeout(updateTimeout);
|
clearTimeout(updateTimeout);
|
||||||
updateTimeout = setTimeout(() => {
|
updateTimeout = setTimeout(() => {
|
||||||
uuid = uuid_1.v4();
|
uuid = uuid_1.v4();
|
||||||
}, 200);
|
}, 200);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}).catch((err) => { _1.Logger.error(err); });
|
}).catch((err) => { _1.Logger.error(err); });
|
||||||
router.get("/auto-reload/client.js", (req, res) => {
|
// /auto-reload/client.js
|
||||||
|
router.get(_1.urlJoin(config.subPath, config.jsName), (req, res) => {
|
||||||
_1.Logger.debug(req.url, req.originalUrl, req.baseUrl);
|
_1.Logger.debug(req.url, req.originalUrl, req.baseUrl);
|
||||||
res.setHeader('Content-Type', "application/javascript");
|
res.setHeader('Content-Type', "application/javascript");
|
||||||
// language=JavaScript
|
// language=JavaScript
|
||||||
res.send(`
|
res.send(`
|
||||||
const loc = window.location;
|
const loc = window.location;
|
||||||
const url = loc.protocol+'//'+loc.host+'${_1.urlJoin(req.baseUrl, "/auto-reload")}';
|
const url = loc.protocol+'//'+loc.host+'${_1.urlJoin(req.baseUrl, config.subPath)}';
|
||||||
// const parse = async res => (await res.json()).uuid;
|
// const parse = async res => (await res.json()).uuid;
|
||||||
const parse = function(res) {
|
const parse = function(res) {
|
||||||
return res.json()
|
return res.json()
|
||||||
@@ -51,11 +60,14 @@ if (!_1.DefaultConfig.isProduction) {
|
|||||||
}, 3000);
|
}, 3000);
|
||||||
`.replace(/\t/g, ""));
|
`.replace(/\t/g, ""));
|
||||||
});
|
});
|
||||||
router.get("/auto-reload", (req, res) => {
|
// /auto-reload
|
||||||
|
router.get(_1.urlJoin(config.subPath), (req, res) => {
|
||||||
req.noHttpLogging = true;
|
req.noHttpLogging = true;
|
||||||
res.json({ uuid });
|
res.json({ uuid });
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
return router;
|
||||||
}
|
}
|
||||||
exports.AutoReloader = {
|
exports.AutoReloader = {
|
||||||
router,
|
getRouter,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,26 +3,54 @@ import {DefaultConfig, Logger, urlJoin} from '.';
|
|||||||
import {v4} from 'uuid';
|
import {v4} from 'uuid';
|
||||||
import Timeout = NodeJS.Timeout;
|
import Timeout = NodeJS.Timeout;
|
||||||
|
|
||||||
const router = Router();
|
export type ReloaderConfig = {
|
||||||
if (!DefaultConfig.isProduction) {
|
// frontend directories to watch
|
||||||
|
frontendDirs: string|string[],
|
||||||
|
// auto reloader sub path (defaults to '/auto-reload')
|
||||||
|
subPath?: string,
|
||||||
|
// auto reloader js name (defaults to 'client.js')
|
||||||
|
jsName?: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
type IntReloaderConfig = {
|
||||||
|
// frontend directories to watch
|
||||||
|
frontendDirs: string[],
|
||||||
|
// auto reloader sub path (defaults to '/auto-reload')
|
||||||
|
subPath: string,
|
||||||
|
// auto reloader js name (defaults to 'client.js')
|
||||||
|
jsName: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRouter(opts: ReloaderConfig): Router {
|
||||||
|
const config: IntReloaderConfig = {
|
||||||
|
frontendDirs: Array.isArray(opts.frontendDirs) ? opts.frontendDirs : [opts.frontendDirs],
|
||||||
|
subPath: opts.subPath || '/auto-reload',
|
||||||
|
jsName: opts.jsName || 'client.js',
|
||||||
|
};
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
if (!DefaultConfig.isProduction) {
|
||||||
let uuid = v4();
|
let uuid = v4();
|
||||||
let updateTimeout: Timeout|undefined = undefined;
|
let updateTimeout: Timeout|undefined = undefined;
|
||||||
import("node-watch").then((watch) => {
|
import("node-watch").then((watch) => {
|
||||||
watch.default('public', {recursive: true}, () => {
|
for (const frontendDir of config.frontendDirs) {
|
||||||
|
watch.default(frontendDir, {recursive: true}, () => {
|
||||||
if (updateTimeout !== undefined) clearTimeout(updateTimeout);
|
if (updateTimeout !== undefined) clearTimeout(updateTimeout);
|
||||||
updateTimeout = setTimeout(() => {
|
updateTimeout = setTimeout(() => {
|
||||||
uuid = v4();
|
uuid = v4();
|
||||||
}, 200);
|
}, 200);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}).catch((err) => { Logger.error(err); });
|
}).catch((err) => { Logger.error(err); });
|
||||||
|
|
||||||
router.get("/auto-reload/client.js", (req, res) => {
|
// /auto-reload/client.js
|
||||||
|
router.get(urlJoin(config.subPath, config.jsName), (req, res) => {
|
||||||
Logger.debug(req.url, req.originalUrl, req.baseUrl);
|
Logger.debug(req.url, req.originalUrl, req.baseUrl);
|
||||||
res.setHeader('Content-Type', "application/javascript");
|
res.setHeader('Content-Type', "application/javascript");
|
||||||
// language=JavaScript
|
// language=JavaScript
|
||||||
res.send(`
|
res.send(`
|
||||||
const loc = window.location;
|
const loc = window.location;
|
||||||
const url = loc.protocol+'//'+loc.host+'${urlJoin(req.baseUrl, "/auto-reload")}';
|
const url = loc.protocol+'//'+loc.host+'${urlJoin(req.baseUrl, config.subPath)}';
|
||||||
// const parse = async res => (await res.json()).uuid;
|
// const parse = async res => (await res.json()).uuid;
|
||||||
const parse = function(res) {
|
const parse = function(res) {
|
||||||
return res.json()
|
return res.json()
|
||||||
@@ -51,12 +79,18 @@ if (!DefaultConfig.isProduction) {
|
|||||||
`.replace(/\t/g, ""));
|
`.replace(/\t/g, ""));
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/auto-reload", (req, res) => {
|
// /auto-reload
|
||||||
|
router.get(urlJoin(config.subPath), (req, res) => {
|
||||||
req.noHttpLogging = true;
|
req.noHttpLogging = true;
|
||||||
res.json({uuid});
|
res.json({uuid});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const AutoReloader = {
|
export const AutoReloader = {
|
||||||
router,
|
getRouter,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user