Added to configuration directory path resolution and dynamic config loader.
This commit is contained in:
42
src/index.js
42
src/index.js
@@ -1 +1,41 @@
|
|||||||
// Module entry point
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import { pathToFileURL } from "node:url";
|
||||||
|
|
||||||
|
export function getUserConfigDir() {
|
||||||
|
if (process.platform === "win32") {
|
||||||
|
return process.env.LOCALAPPDATA ?? path.join(os.homedir(), "AppData", "Local");
|
||||||
|
}
|
||||||
|
|
||||||
|
return process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadConfig(type) {
|
||||||
|
const configBaseDir = getUserConfigDir();
|
||||||
|
const configType = typeof type === "string" ? type.toLowerCase() : "";
|
||||||
|
let configFileName;
|
||||||
|
|
||||||
|
switch (configType) {
|
||||||
|
case "public":
|
||||||
|
case "p":
|
||||||
|
configFileName = "public-config.js";
|
||||||
|
break;
|
||||||
|
case "confidential":
|
||||||
|
case "c":
|
||||||
|
configFileName = "confidential-config.js";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
configFileName = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!configFileName) {
|
||||||
|
throw new Error(
|
||||||
|
`Invalid config type: ${type}. Expected "public"|"p" or "confidential"|"c".`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const configPath = path.join(configBaseDir, "sk-az-tools", configFileName);
|
||||||
|
const configUrl = pathToFileURL(configPath).href;
|
||||||
|
const { config } = await import(configUrl);
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user