From 76b2b8a1a56f49c72ba627e7baa21885c7337227 Mon Sep 17 00:00:00 2001 From: Netfloex <38650595+Netfloex@users.noreply.github.com> Date: Wed, 4 May 2022 22:30:29 +0200 Subject: [PATCH] Watch config file for changes: live reload option, resetStarted --- package.json | 2 ++ rollup.config.ts | 2 ++ src/index.ts | 49 ++++++++++++++++++++++++++++++------------- src/lib/logger.ts | 6 +++++- src/utils/settings.ts | 1 + yarn.lock | 24 ++++++++++++++++++++- 6 files changed, 67 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index f1c140e..e41c708 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "devDependencies": { "@rollup/plugin-commonjs": "^21.0.2", + "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.1.3", "@trivago/prettier-plugin-sort-imports": "^3.2.0", "@types/clean-css": "^4.2.5", @@ -32,6 +33,7 @@ "axios": "^0.26.0", "cert2json": "^1.0.12", "chalk": "4.1.2", + "chokidar": "^3.5.3", "clean-css": "^5.2.4", "eslint": "^8.10.0", "eslint-config-prettier": "^8.4.0", diff --git a/rollup.config.ts b/rollup.config.ts index 0de0208..aed4bd4 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -1,4 +1,5 @@ import commonjs from "@rollup/plugin-commonjs"; +import json from "@rollup/plugin-json"; import { nodeResolve } from "@rollup/plugin-node-resolve"; import { defineConfig } from "rollup"; import { terser } from "rollup-plugin-terser"; @@ -13,6 +14,7 @@ export default defineConfig({ plugins: [terser()] }, plugins: [ + json(), typescript({ exclude: ["src/tests"] }), diff --git a/src/index.ts b/src/index.ts index 8b16955..8a6cc2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,10 @@ +import chok from "chokidar"; +import type { FSWatcher } from "chokidar"; import { pathExists, readdir, remove } from "fs-extra"; import { join } from "path"; import { certbot } from "@lib/certbot"; -import { logger, started } from "@lib/logger"; +import { logger, resetStarted, started } from "@lib/logger"; import parseServers from "@lib/parseServers"; import validateConfig from "@lib/validateConfig"; import { @@ -23,6 +25,13 @@ enum ExitCode { success = 0, failure = -1 } +let watcher: FSWatcher | undefined; + +const createWatcherOnce = async (path: string): Promise => { + await watcher?.close(); + watcher = chok.watch(path, {}); + return watcher; +}; const main = async (): Promise => { let stopping = false; @@ -57,6 +66,11 @@ const main = async (): Promise => { } } } + if (settings.watchConfigFile) { + (await createWatcherOnce(configFilePath!)).on("change", () => { + startMain(); + }); + } let results; if (!stopping) { @@ -163,19 +177,24 @@ const main = async (): Promise => { return ExitCode.success; }; -logger.start(); -main() - .then((exitCode) => { - if (exitCode == ExitCode.success) { - logger.done({ started }); - } else { +export const startMain = (): void => { + resetStarted(); + logger.start(); + main() + .then((exitCode) => { + if (exitCode == ExitCode.success) { + logger.done({ started }); + } else { + logger.exited({ started }); + process.exitCode = ExitCode.failure; + } + }) + .catch((error) => { + logger.exception(); + console.error(error); logger.exited({ started }); process.exitCode = ExitCode.failure; - } - }) - .catch((error) => { - logger.exception(); - console.error(error); - logger.exited({ started }); - process.exitCode = ExitCode.failure; - }); + }); +}; + +startMain(); diff --git a/src/lib/logger.ts b/src/lib/logger.ts index fd312e6..b37c64a 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -59,7 +59,11 @@ const longestLengthValue = (obj: Record): number => const longestTagLength = longestLengthValue(TagList); const longestTypeLength = longestLengthValue(TypeList); -export const started = performance.now(); +export let started = performance.now(); + +export const resetStarted = (): void => { + started = performance.now(); +}; // eslint-disable-next-line @typescript-eslint/explicit-function-return-type const createLogFunction = (key: Key) => { diff --git a/src/utils/settings.ts b/src/utils/settings.ts index d47e72d..9d82418 100644 --- a/src/utils/settings.ts +++ b/src/utils/settings.ts @@ -48,6 +48,7 @@ const settings = { enableConfigMissingCerts: yn(env.ENABLE_CONFIG_MISSING_CERTS, { default: false }), + watchConfigFile: yn(env.WATCH_CONFIG_FILE, { default: false }), dhParamSize: parseIntDefault(env.DHPARAM_SIZE, 2048), disableCertbot: yn(env.DISABLE_CERTBOT, { default: false }), diff --git a/yarn.lock b/yarn.lock index 8924127..22c677c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -841,6 +841,13 @@ magic-string "^0.25.7" resolve "^1.17.0" +"@rollup/plugin-json@^4.1.0": + version "4.1.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-4.1.0.tgz#54e09867ae6963c593844d8bd7a9c718294496f3" + integrity sha512-yfLbTdNS6amI/2OpmbiBoW12vngr5NW2jCJVZSBEz+H5KfUJZ2M7sDjk0U6GOOdCWFVScShte29o9NezJ53TPw== + dependencies: + "@rollup/pluginutils" "^3.0.8" + "@rollup/plugin-node-resolve@^13.1.3": version "13.1.3" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-13.1.3.tgz#2ed277fb3ad98745424c1d2ba152484508a92d79" @@ -853,7 +860,7 @@ is-module "^1.0.0" resolve "^1.19.0" -"@rollup/pluginutils@^3.1.0": +"@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.1.0": version "3.1.0" resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b" integrity sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg== @@ -1544,6 +1551,21 @@ chokidar@^3.5.1: optionalDependencies: fsevents "~2.3.2" +chokidar@^3.5.3: + version "3.5.3" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" + integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== + dependencies: + anymatch "~3.1.2" + braces "~3.0.2" + glob-parent "~5.1.2" + is-binary-path "~2.1.0" + is-glob "~4.0.1" + normalize-path "~3.0.0" + readdirp "~3.6.0" + optionalDependencies: + fsevents "~2.3.2" + ci-info@^3.1.1: version "3.2.0" resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-3.2.0.tgz#2876cb948a498797b5236f0095bc057d0dca38b6"