49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
// SPDX-License-Identifier: MIT
|
|
|
|
import { readFile } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
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");
|
|
}
|
|
|
|
async function loadConfig(configFileName) {
|
|
if (typeof configFileName !== "string" || configFileName.trim() === "") {
|
|
throw new Error(
|
|
'Invalid config file name. Expected a non-empty string like "public-config.json" or "confidential-config.json".',
|
|
);
|
|
}
|
|
|
|
const config = {
|
|
tenantId: process.env.AZURE_TENANT_ID,
|
|
clientId: process.env.AZURE_CLIENT_ID,
|
|
};
|
|
|
|
const configPath = path.join(getUserConfigDir(), "sk-az-tools", configFileName);
|
|
return readFile(configPath, "utf8")
|
|
.then((configJson) => JSON.parse(configJson))
|
|
.catch((err) => {
|
|
if (err?.code === "ENOENT") {
|
|
return {};
|
|
}
|
|
throw err;
|
|
})
|
|
.then((json) => ({
|
|
tenantId: json.tenantId || config.tenantId,
|
|
clientId: json.clientId || config.clientId,
|
|
}));
|
|
}
|
|
|
|
export function loadPublicConfig() {
|
|
return loadConfig("public-config.json");
|
|
}
|
|
|
|
export function loadConfidentialConfig() {
|
|
return loadConfig("confidential-config.json");
|
|
}
|