From 1888dbacc52832dbc507128856b85b0ab0ea5e17 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Fri, 5 Jan 2024 15:32:14 +0000 Subject: [PATCH] v0.1: Remove ngineer.ts --- README.md | 2 +- ngineer.ts | 96 ------------------------------------------------------ ngx.ts | 7 ++-- 3 files changed, 5 insertions(+), 100 deletions(-) delete mode 100644 ngineer.ts diff --git a/README.md b/README.md index 5d13ab6..168d48f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# ngineer +# ngx Opinionated nginx config generation in Deno. diff --git a/ngineer.ts b/ngineer.ts deleted file mode 100644 index c2371ee..0000000 --- a/ngineer.ts +++ /dev/null @@ -1,96 +0,0 @@ -type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile; - -class ConfigBlock { - value: string; - children: ConfigNode[]; - - constructor(value: string, children: ConfigNode[]) { - this.value = value; - this.children = children; - } - - build(): string { - let output = this.value; - output += " {\n "; - output += this.children - .map((child) => child.build().split("\n").join("\n ")) - .join("\n "); - output += "\n}"; - return output; - } -} - -class ConfigStatement { - value: string; - constructor(value: string) { - this.value = value; - } - - build(): string { - return this.value + ";"; - } -} - -class ConfigBreak { - build(): string { - return ""; - } -} - -export const br = new ConfigBreak(); - -class ConfigFile { - nodes: ConfigNode[]; - constructor(nodes: ConfigNode[]) { - this.nodes = nodes; - } - - build(): string { - return this.nodes.map((n) => n.build()).join("\n"); - } -} - -type LooseConfigNode = ConfigNode | string; - -function conformNode(looseNode: LooseConfigNode): ConfigNode { - if (typeof looseNode === "string") { - return new ConfigStatement(looseNode); - } - return looseNode; -} - -function conformNodes(looseNodes: LooseConfigNode[]): ConfigNode[] { - return looseNodes.map((node) => conformNode(node)); -} - -export function ng(value?: string, children?: LooseConfigNode[]): ConfigNode { - const hasValue = value !== undefined && value !== ""; - const hasChildren = children !== undefined; - - if (!hasValue && !hasChildren) { - return new ConfigBreak(); - } else if (hasValue && !hasChildren) { - return new ConfigStatement(value); - } else if (hasValue && hasChildren) { - return new ConfigBlock(value, conformNodes(children)); - } else if (!hasValue && hasChildren) { - return new ConfigFile(conformNodes(children)); - } - - throw new Error("unreachable"); -} - -export const listenv4v6 = (...extras: string[]) => - conformNodes([ - `listen 443 ${["ssl", "http2", ...extras].join(" ")}`, - `listen [::]:443 ${["ssl", "http2", ...extras].join(" ")}`, - ]); - -export const letsEncrypt = ( - domain: string, - liveDir = "/etc/letsencrypt/live" -) => - conformNodes([ - `ssl_certificate ${liveDir}/${domain}/fullchain.pem`, - `ssl_certificate_key ${liveDir}/${domain}/privkey.pem`, - ]); diff --git a/ngx.ts b/ngx.ts index 75f6dde..459d871 100644 --- a/ngx.ts +++ b/ngx.ts @@ -1,3 +1,5 @@ +const NGX_VERSION = "0.1"; + // #region Implementation type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile; @@ -106,8 +108,6 @@ export function ngx(value?: string, children?: LooseConfigNode[]): ConfigNode { throw new Error("unreachable"); } -export const br = new ConfigBreak(); - export const listen = (...extras: string[]) => conform([ `listen 443 ${["ssl", "http2", ...extras].join(" ")}`, @@ -123,7 +123,8 @@ export const letsEncrypt = ( `ssl_certificate_key ${liveDir}/${domain}/privkey.pem`, ]); +// the default export is both the ngx function and a namespace: export default Object.assign( (value?: string, children?: LooseConfigNode[]) => ngx(value, children), - { br, listen, letsEncrypt } + { NGX_VERSION, listen, letsEncrypt } );