From 61ea5b1d402201917ed09cf5a97dffb28620adc4 Mon Sep 17 00:00:00 2001 From: Netfloex <38650595+Netfloex@users.noreply.github.com> Date: Mon, 28 Feb 2022 22:30:22 +0100 Subject: [PATCH] Allow an async JS config, time when exited --- config/config.example.async.js | 20 ++++++++++++++++++++ config/config.example.js | 6 +++--- src/index.ts | 4 ++-- src/utils/log.ts | 18 +++++++++++++++--- src/utils/parseUserConfig.ts | 11 ++++++++++- 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 config/config.example.async.js diff --git a/config/config.example.async.js b/config/config.example.async.js new file mode 100644 index 0000000..57d3beb --- /dev/null +++ b/config/config.example.async.js @@ -0,0 +1,20 @@ +const { request } = require("https"); + +const url = + "https://gist.githubusercontent.com/Netfloex/2fcf157353a573133b133b2623c32dd2/raw/90f06a7085a6f3f3ab967deb5cbc1196decfb6cb/async-config.js"; + +module.exports = () => { + let data = ""; + + return new Promise((res, rej) => { + const req = request(url, (response) => { + response.on("data", (chunk) => { + data = data + chunk.toString(); + }); + + response.on("end", () => res(JSON.parse(data))); + }); + req.on("error", rej); + req.end(); + }); +}; diff --git a/config/config.example.js b/config/config.example.js index 5547fed..d9e6c1e 100644 --- a/config/config.example.js +++ b/config/config.example.js @@ -15,10 +15,10 @@ module.exports = { // Custom CSS example adguard: { // adguard.example.com - proxy_pass: "http://adguard:80", + proxy_pass: "http://adguard", // This file gets downloaded and compressed, its then appended to the custom_css: - "https://theme-park.dev/CSS/themes/adguard/organizr-dark.css" + "https://theme-park.dev/css/base/adguard/organizr.css" }, // Custom JS example @@ -34,7 +34,7 @@ module.exports = { bitwarden: { proxy_pass: "http://bitwarden", custom_css: - "https://theme-park.dev/CSS/themes/bitwarden/organizr-dark.css", + "https://theme-park.dev/css/base/bitwarden/organizr.css", locations: { "/notifications/hub": { proxy_pass: "http://bitwarden:3012", diff --git a/src/index.ts b/src/index.ts index 833821a..ee7152b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -155,13 +155,13 @@ main() if (exitCode == ExitCode.success) { log.finished(started); } else { - log.exited(); + log.exited(started); process.exitCode = ExitCode.failure; } }) .catch((error) => { log.exception(); console.error(error); - log.exited(); + log.exited(started); process.exitCode = ExitCode.failure; }); diff --git a/src/utils/log.ts b/src/utils/log.ts index 5cad1f1..f44dfd8 100644 --- a/src/utils/log.ts +++ b/src/utils/log.ts @@ -38,11 +38,15 @@ class Log { } public finished(started: number) { - this.done(chalk`{green Done in ${(Date.now() - started) / 1000}s}`); + this.done( + chalk`{green Done} in {yellow ${(Date.now() - started) / 1000}s}` + ); } - public exited() { - this.error(chalk`{red Exited}`); + public exited(started: number) { + this.error( + chalk`{red Exited} in {yellow ${(Date.now() - started) / 1000}s}` + ); } public exception() { @@ -131,6 +135,14 @@ class Log { ); } + public configJSInvalidType(type: string, expected: string[]) { + this.error( + chalk`The JS file returned {dim ${type}}, expected ${expected + .map((str) => chalk`{dim ${str}}`) + .join(" or ")}` + ); + } + public configEmpty() { this.error(chalk`The config is empty`); } diff --git a/src/utils/parseUserConfig.ts b/src/utils/parseUserConfig.ts index b6e5bb3..b0e56b8 100644 --- a/src/utils/parseUserConfig.ts +++ b/src/utils/parseUserConfig.ts @@ -12,7 +12,16 @@ const parseUserConfig = async ( if (ext.match(/^\.js$/)) { try { - return require(configFilePath); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const data: unknown = require(configFilePath); + if (typeof data == "object" && data) { + return data as Record; + } else if (typeof data == "function") { + return await data(); + } else { + log.configJSInvalidType(typeof data, ["object", "function"]); + return false; + } } catch (error) { log.configJSError(error as Error); return false;