2022-06-01 02:42:03 +00:00
|
|
|
/* eslint-disable no-console */
|
2022-03-28 16:46:55 +00:00
|
|
|
/// <reference types="node" />
|
|
|
|
|
2022-03-17 04:01:22 +00:00
|
|
|
import { readFile } from "fs/promises";
|
|
|
|
|
2022-09-20 21:40:02 +00:00
|
|
|
class ConfigReader {
|
|
|
|
config?: string;
|
2022-03-17 04:01:22 +00:00
|
|
|
|
2022-09-20 21:40:02 +00:00
|
|
|
constructor() {
|
|
|
|
this.config = undefined;
|
|
|
|
}
|
2022-03-17 04:01:22 +00:00
|
|
|
|
2022-09-20 21:40:02 +00:00
|
|
|
async open(path: string) {
|
|
|
|
try {
|
|
|
|
this.config = await readFile(path, "utf8");
|
|
|
|
} catch (err) {
|
|
|
|
// We don't want to catch the error here, handle it on getValue method
|
|
|
|
}
|
2022-03-17 04:01:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 21:40:02 +00:00
|
|
|
getValue(sectionName: string, fieldName: string) {
|
|
|
|
if (!this.config) {
|
|
|
|
throw new Error("Cannot find config to read");
|
|
|
|
}
|
|
|
|
const targetSection = this.config
|
|
|
|
.split("\n\n")
|
|
|
|
.filter((section) => section.includes(`[${sectionName}]`));
|
2022-03-17 04:01:22 +00:00
|
|
|
|
2022-09-20 21:40:02 +00:00
|
|
|
if (targetSection.length === 0) {
|
|
|
|
throw new Error(`Cannot find [${sectionName}] section in config`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const section = targetSection[0];
|
|
|
|
|
|
|
|
for (const line of section.split("\n")) {
|
|
|
|
const matched = line.startsWith(fieldName);
|
|
|
|
if (matched) {
|
|
|
|
const results = line.split("=");
|
|
|
|
if (results.length === 2) {
|
|
|
|
const key = results[1].trim();
|
|
|
|
return key;
|
|
|
|
}
|
2022-03-17 04:01:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 21:40:02 +00:00
|
|
|
throw new Error(`Cannot find ${fieldName} in config`);
|
|
|
|
}
|
2022-03-17 04:01:22 +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);
|
|
|
|
|
2022-03-17 04:01:22 +00:00
|
|
|
if (env["VITE_API_KEY"] === undefined) {
|
|
|
|
try {
|
2022-09-20 21:40:02 +00:00
|
|
|
const apiKey = reader.getValue("auth", "apikey");
|
2022-03-17 04:01:22 +00:00
|
|
|
|
2022-06-01 02:42:03 +00:00
|
|
|
console.log(`Using API key: ${apiKey}`);
|
|
|
|
|
2022-03-17 04:01:22 +00:00
|
|
|
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");
|
2022-03-17 04:01:22 +00:00
|
|
|
|
2022-06-01 02:42:03 +00:00
|
|
|
const url = `http://127.0.0.1:${port}${baseUrl}`;
|
|
|
|
|
|
|
|
console.log(`Using backend url: ${url}`);
|
2022-03-17 04:01:22 +00:00
|
|
|
|
|
|
|
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})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|