Skip to content

Commit

Permalink
Watch config file for changes: live reload option, resetStarted
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed May 4, 2022
1 parent 59138f4 commit 76b2b8a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -13,6 +14,7 @@ export default defineConfig({
plugins: [terser()]
},
plugins: [
json(),
typescript({
exclude: ["src/tests"]
}),
Expand Down
49 changes: 34 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -23,6 +25,13 @@ enum ExitCode {
success = 0,
failure = -1
}
let watcher: FSWatcher | undefined;

const createWatcherOnce = async (path: string): Promise<FSWatcher> => {
await watcher?.close();
watcher = chok.watch(path, {});
return watcher;
};

const main = async (): Promise<ExitCode> => {
let stopping = false;
Expand Down Expand Up @@ -57,6 +66,11 @@ const main = async (): Promise<ExitCode> => {
}
}
}
if (settings.watchConfigFile) {
(await createWatcherOnce(configFilePath!)).on("change", () => {
startMain();
});
}

let results;
if (!stopping) {
Expand Down Expand Up @@ -163,19 +177,24 @@ const main = async (): Promise<ExitCode> => {
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();
6 changes: 5 additions & 1 deletion src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ const longestLengthValue = (obj: Record<string, string>): 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 extends keyof typeof logMessages>(key: Key) => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
24 changes: 23 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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==
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 76b2b8a

Please sign in to comment.