Skip to content

Commit

Permalink
Config paths may be folders
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Apr 14, 2024
1 parent d8fb3bb commit 21d7ba2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 16 additions & 1 deletion src/extensions/wiring.extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from "events";
import { exit } from "process";
import minimist from "minimist";
import { argv, exit } from "process";

import {
ApplicationConfigurationOptions,
Expand All @@ -11,6 +12,7 @@ import {
DOWN,
each,
eachSeries,
findKey,
GetApis,
GetApisResult,
LibraryConfigurationOptions,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -537,6 +550,8 @@ async function Bootstrap<
);
}
internal = new InternalDefinition();
const isTrace = isTraceConfig();

Check warning on line 553 in src/extensions/wiring.extension.ts

View workflow job for this annotation

GitHub Actions / lint-and-build

'isTrace' is assigned a value but never used. Allowed unused vars must match /_|logger/u

Check failure on line 553 in src/extensions/wiring.extension.ts

View workflow job for this annotation

GitHub Actions / lint-and-build

'isTrace' is declared but its value is never read.

Check warning on line 553 in src/extensions/wiring.extension.ts

View workflow job for this annotation

GitHub Actions / publish

'isTrace' is assigned a value but never used. Allowed unused vars must match /_|logger/u

Check failure on line 553 in src/extensions/wiring.extension.ts

View workflow job for this annotation

GitHub Actions / publish

'isTrace' is declared but its value is never read.
// const
internal.boot = {
application,
completedLifecycleEvents: new Set(),
Expand Down
25 changes: 3 additions & 22 deletions src/helpers/config-environment-loader.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AbstractConfig,
ConfigLoaderParams,
ConfigLoaderReturn,
findKey,
ModuleConfiguration,
} from "./config.helper";

Expand All @@ -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) =>
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 6 additions & 9 deletions src/helpers/config-file-loader.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
);
Expand Down
15 changes: 15 additions & 0 deletions src/helpers/config.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,18 @@ export type ModuleConfiguration = {
[key: string]: AnyConfig;
};
export type OptionalModuleConfiguration = ModuleConfiguration | undefined;

export function findKey<T extends string>(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));
})
);
}

0 comments on commit 21d7ba2

Please sign in to comment.