1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-26 17:47:20 +00:00
bazarr/frontend/config/configReader.ts

79 lines
1.9 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-console */
/// <reference types="node" />
import { readFile } from "fs/promises";
import { get } from "lodash";
import { parse } from "yaml";
2022-09-20 21:40:02 +00:00
class ConfigReader {
config: object;
2022-09-20 21:40:02 +00:00
constructor() {
this.config = {};
2022-09-20 21:40:02 +00:00
}
2022-09-20 21:40:02 +00:00
async open(path: string) {
try {
const rawConfig = await readFile(path, "utf8");
this.config = parse(rawConfig);
2022-09-20 21:40:02 +00:00
} catch (err) {
// We don't want to catch the error here, handle it on getValue method
}
}
2022-09-20 21:40:02 +00:00
getValue(sectionName: string, fieldName: string) {
const path = `${sectionName}.${fieldName}`;
const result = get(this.config, path);
2022-09-20 21:40:02 +00:00
if (result === undefined) {
throw new Error(`Failed to find ${path} in the local config file`);
}
return result;
2022-09-20 21:40:02 +00:00
}
}
export default async function overrideEnv(env: Record<string, string>) {
const configPath = env["VITE_BAZARR_CONFIG_FILE"];
if (configPath === undefined) {
return;
}
2022-09-20 21:40:02 +00:00
const reader = new ConfigReader();
await reader.open(configPath);
if (env["VITE_API_KEY"] === undefined) {
try {
2022-09-20 21:40:02 +00:00
const apiKey = reader.getValue("auth", "apikey");
console.log(`Using API key: ${apiKey}`);
env["VITE_API_KEY"] = apiKey;
process.env["VITE_API_KEY"] = apiKey;
} catch (err) {
throw new Error(
`No API key found, please run the backend first, (error: ${err.message})`,
);
}
}
if (env["VITE_PROXY_URL"] === undefined) {
try {
2022-09-20 21:40:02 +00:00
const port = reader.getValue("general", "port");
const baseUrl = reader.getValue("general", "base_url");
const url = `http://127.0.0.1:${port}${baseUrl}`;
console.log(`Using backend url: ${url}`);
env["VITE_PROXY_URL"] = url;
process.env["VITE_PROXY_URL"] = url;
} catch (err) {
throw new Error(
`No proxy url found, please run the backend first, (error: ${err.message})`,
);
}
}
}