diff --git a/package.json b/package.json index 30e522a..6cd619a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "repository": { "url": "git+https://github.com/Digital-Alchemy-TS/core" }, - "version": "0.3.9", + "version": "0.3.10", "author": { "url": "https://github.com/zoe-codez", "name": "Zoe Codez" diff --git a/src/extensions/wiring.extension.ts b/src/extensions/wiring.extension.ts index eb37d0d..e7b2525 100644 --- a/src/extensions/wiring.extension.ts +++ b/src/extensions/wiring.extension.ts @@ -1,5 +1,6 @@ import { EventEmitter } from "events"; -import { exit } from "process"; +import minimist from "minimist"; +import { argv, exit } from "process"; import { ApplicationConfigurationOptions, @@ -11,6 +12,7 @@ import { DOWN, each, eachSeries, + findKey, GetApis, GetApisResult, LibraryConfigurationOptions, @@ -189,6 +191,11 @@ function CreateBoilerplate() { "Configuration property for cache provider, does not apply to memory caching", type: "string", }, + TRACE_CONFIG: { + default: false, + description: "Boot the app through configuration, then exit", + type: "boolean", + }, }, name: "boilerplate", // > 🐔 🥚 dependencies @@ -521,6 +528,12 @@ function BuildSortOrder< return out; } +const isTraceConfig = () => { + const keys = minimist(argv); + const target = findKey(Object.keys(keys), ["TRACE_CONFIG"]); + return !is.empty(target); +}; + let startup: Date; // # Lifecycle runners @@ -537,6 +550,8 @@ async function Bootstrap< ); } internal = new InternalDefinition(); + const isTrace = isTraceConfig(); + // const internal.boot = { application, completedLifecycleEvents: new Set(), diff --git a/src/helpers/config-environment-loader.helper.ts b/src/helpers/config-environment-loader.helper.ts index 1090c03..4ae7e1d 100644 --- a/src/helpers/config-environment-loader.helper.ts +++ b/src/helpers/config-environment-loader.helper.ts @@ -5,6 +5,7 @@ import { AbstractConfig, ConfigLoaderParams, ConfigLoaderReturn, + findKey, ModuleConfiguration, } from "./config.helper"; @@ -26,17 +27,7 @@ export async function ConfigLoaderEnvironment< const configPath = `${project}.${key}`; // Find an applicable switch - const flag = - // Find an exact match (if available) first - search.find((line) => switchKeys.includes(line)) || - // Do case insensitive searches - search.find((line) => { - const match = new RegExp( - `^${line.replaceAll(new RegExp("[-_]", "gi"), "[-_]?")}$`, - "gi", - ); - return switchKeys.some((item) => item.match(match)); - }); + const flag = findKey(search, switchKeys); if (flag) { const formattedFlag = switchKeys.find((key) => search.some((line) => @@ -66,17 +57,7 @@ export async function ConfigLoaderEnvironment< return; } // Find an environment variable - const environment = - // Find an exact match (if available) first - search.find((line) => environmentKeys.includes(line)) || - // Do case insensitive searches - search.find((line) => { - const match = new RegExp( - `^${line.replaceAll(new RegExp("[-_]", "gi"), "[-_]?")}$`, - "gi", - ); - return environmentKeys.some((item) => item.match(match)); - }); + const environment = findKey(search, environmentKeys); if (is.empty(environment)) { return; } diff --git a/src/helpers/config-file-loader.helper.ts b/src/helpers/config-file-loader.helper.ts index f087cb6..72eb087 100644 --- a/src/helpers/config-file-loader.helper.ts +++ b/src/helpers/config-file-loader.helper.ts @@ -18,16 +18,16 @@ const isWindows = platform === "win32"; export const SUPPORTED_CONFIG_EXTENSIONS = ["json", "ini", "yaml", "yml"]; function withExtensions(path: string): string[] { - return [path, ...SUPPORTED_CONFIG_EXTENSIONS.map((i) => `${path}.${i}`)]; + return [path, join(path, "config")].flatMap((path) => [ + path, + ...SUPPORTED_CONFIG_EXTENSIONS.map((i) => `${path}.${i}`), + ]); } export function configFilePaths(name = "digital-alchemy"): string[] { const out: string[] = []; if (!isWindows) { - out.push( - ...withExtensions(join(`/etc`, name, "config")), - ...withExtensions(join(`/etc`, `${name}`)), - ); + out.push(...withExtensions(join(`/etc`, `${name}`))); } let current = cwd(); let next: string; @@ -39,10 +39,7 @@ export function configFilePaths(name = "digital-alchemy"): string[] { } current = next; } - out.push( - ...withExtensions(join(homedir(), ".config", name)), - ...withExtensions(join(homedir(), ".config", name, "config")), - ); + out.push(...withExtensions(join(homedir(), ".config", name))); return out.filter( (filePath) => existsSync(filePath) && statSync(filePath).isFile(), ); diff --git a/src/helpers/config.helper.ts b/src/helpers/config.helper.ts index e8778fb..35ac360 100644 --- a/src/helpers/config.helper.ts +++ b/src/helpers/config.helper.ts @@ -162,3 +162,18 @@ export type ModuleConfiguration = { [key: string]: AnyConfig; }; export type OptionalModuleConfiguration = ModuleConfiguration | undefined; + +export function findKey(source: T[], find: T[]) { + return ( + // Find an exact match (if available) first + source.find((line) => find.includes(line)) || + // Do case insensitive searches + source.find((line) => { + const match = new RegExp( + `^${line.replaceAll(new RegExp("[-_]", "gi"), "[-_]?")}$`, + "gi", + ); + return find.some((item) => item.match(match)); + }) + ); +}