42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
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;
|
|
}
|