Skip to content

Commit

Permalink
Allow an async JS config, time when exited
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed Feb 28, 2022
1 parent ecd9350 commit 61ea5b1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
20 changes: 20 additions & 0 deletions config/config.example.async.js
Original file line number Diff line number Diff line change
@@ -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();
});
};
6 changes: 3 additions & 3 deletions config/config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <head>
custom_css:
"https://theme-park.dev/CSS/themes/adguard/organizr-dark.css"
"https://theme-park.dev/css/base/adguard/organizr.css"
},

// Custom JS example
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
18 changes: 15 additions & 3 deletions src/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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`);
}
Expand Down
11 changes: 10 additions & 1 deletion src/utils/parseUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
} 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;
Expand Down

0 comments on commit 61ea5b1

Please sign in to comment.