diff --git a/out/auto-reload.d.ts b/out/auto-reload.d.ts index 9586b23..f45a9bf 100644 --- a/out/auto-reload.d.ts +++ b/out/auto-reload.d.ts @@ -1,3 +1,11 @@ -export declare const AutoReloader: { - router: import("express-serve-static-core").Router; +import { Router } from 'express'; +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 {}; diff --git a/out/auto-reload.js b/out/auto-reload.js index d51d8fa..7349f63 100644 --- a/out/auto-reload.js +++ b/out/auto-reload.js @@ -4,26 +4,35 @@ exports.AutoReloader = void 0; const express_1 = require("express"); const _1 = require("."); const uuid_1 = require("uuid"); -const router = express_1.Router(); -if (!_1.DefaultConfig.isProduction) { - let uuid = uuid_1.v4(); - let updateTimeout = undefined; - Promise.resolve().then(() => require("node-watch")).then((watch) => { - watch.default('public', { recursive: true }, () => { - if (updateTimeout !== undefined) - clearTimeout(updateTimeout); - updateTimeout = setTimeout(() => { - uuid = uuid_1.v4(); - }, 200); - }); - }).catch((err) => { _1.Logger.error(err); }); - router.get("/auto-reload/client.js", (req, res) => { - _1.Logger.debug(req.url, req.originalUrl, req.baseUrl); - res.setHeader('Content-Type', "application/javascript"); - // language=JavaScript - res.send(` +function getRouter(opts) { + 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 updateTimeout = undefined; + Promise.resolve().then(() => require("node-watch")).then((watch) => { + for (const frontendDir of config.frontendDirs) { + watch.default(frontendDir, { recursive: true }, () => { + if (updateTimeout !== undefined) + clearTimeout(updateTimeout); + updateTimeout = setTimeout(() => { + uuid = uuid_1.v4(); + }, 200); + }); + } + }).catch((err) => { _1.Logger.error(err); }); + // /auto-reload/client.js + router.get(_1.urlJoin(config.subPath, config.jsName), (req, res) => { + _1.Logger.debug(req.url, req.originalUrl, req.baseUrl); + res.setHeader('Content-Type', "application/javascript"); + // language=JavaScript + res.send(` 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 = function(res) { return res.json() @@ -50,12 +59,15 @@ if (!_1.DefaultConfig.isProduction) { } }, 3000); `.replace(/\t/g, "")); - }); - router.get("/auto-reload", (req, res) => { - req.noHttpLogging = true; - res.json({ uuid }); - }); + }); + // /auto-reload + router.get(_1.urlJoin(config.subPath), (req, res) => { + req.noHttpLogging = true; + res.json({ uuid }); + }); + } + return router; } exports.AutoReloader = { - router, + getRouter, }; diff --git a/src/auto-reload.ts b/src/auto-reload.ts index 5ec6b12..66b4f75 100644 --- a/src/auto-reload.ts +++ b/src/auto-reload.ts @@ -3,26 +3,54 @@ import {DefaultConfig, Logger, urlJoin} from '.'; import {v4} from 'uuid'; import Timeout = NodeJS.Timeout; -const router = Router(); -if (!DefaultConfig.isProduction) { - let uuid = v4(); - let updateTimeout: Timeout|undefined = undefined; - import("node-watch").then((watch) => { - watch.default('public', {recursive: true}, () => { - if (updateTimeout !== undefined) clearTimeout(updateTimeout); - updateTimeout = setTimeout(() => { - uuid = v4(); - }, 200); - }); - }).catch((err) => { Logger.error(err); }); +export type ReloaderConfig = { + // 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, +} - router.get("/auto-reload/client.js", (req, res) => { - Logger.debug(req.url, req.originalUrl, req.baseUrl); - res.setHeader('Content-Type', "application/javascript"); - // language=JavaScript - res.send(` +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 updateTimeout: Timeout|undefined = undefined; + import("node-watch").then((watch) => { + for (const frontendDir of config.frontendDirs) { + watch.default(frontendDir, {recursive: true}, () => { + if (updateTimeout !== undefined) clearTimeout(updateTimeout); + updateTimeout = setTimeout(() => { + uuid = v4(); + }, 200); + }); + } + }).catch((err) => { Logger.error(err); }); + + // /auto-reload/client.js + router.get(urlJoin(config.subPath, config.jsName), (req, res) => { + Logger.debug(req.url, req.originalUrl, req.baseUrl); + res.setHeader('Content-Type', "application/javascript"); + // language=JavaScript + res.send(` 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 = function(res) { return res.json() @@ -49,14 +77,20 @@ if (!DefaultConfig.isProduction) { } }, 3000); `.replace(/\t/g, "")); - }); + }); - router.get("/auto-reload", (req, res) => { - req.noHttpLogging = true; - res.json({uuid}); - }); + // /auto-reload + router.get(urlJoin(config.subPath), (req, res) => { + req.noHttpLogging = true; + res.json({uuid}); + }); + } + + return router; } + + export const AutoReloader = { - router, + getRouter, };