Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dynaconf' into dynaconf
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheus65535 committed Aug 3, 2023
2 parents 21aef96 + 2b21d7b commit 1084db4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion frontend/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Bazarr configuration path, must be absolute path
# Vite will use this variable to find your bazarr's configuration file
VITE_BAZARR_CONFIG_FILE="../data/config/config.ini"
VITE_BAZARR_CONFIG_FILE="../data/config/config.yaml"

# Display update section in settings
VITE_CAN_UPDATE=true
Expand Down
36 changes: 11 additions & 25 deletions frontend/config/configReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,34 @@
/// <reference types="node" />

import { readFile } from "fs/promises";
import { get } from "lodash";
import YAML from "yaml";

class ConfigReader {
config?: string;
config: object;

constructor() {
this.config = undefined;
this.config = {};
}

async open(path: string) {
try {
this.config = await readFile(path, "utf8");
const rawConfig = await readFile(path, "utf8");
this.config = YAML.parse(rawConfig);
} catch (err) {
// We don't want to catch the error here, handle it on getValue method
}
}

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}]`));

if (targetSection.length === 0) {
throw new Error(`Cannot find [${sectionName}] section in config`);
}
const path = `${sectionName}.${fieldName}`;
const result = get(this.config, path);

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;
}
}
if (result === undefined) {
throw new Error(`Failed to find ${path} in the local config file`);
}

throw new Error(`Cannot find ${fieldName} in config`);
return result;
}
}

Expand Down
20 changes: 15 additions & 5 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@types/react-dom": "^18.2.0",
"@types/react-table": "^7.7.0",
"@vitejs/plugin-react": "^4.0.0",
"vitest": "^0.30.1",
"@vitest/coverage-c8": "^0.30.0",
"@vitest/ui": "^0.30.0",
"clsx": "^1.2.0",
Expand All @@ -62,7 +61,9 @@
"sass": "^1.62.0",
"typescript": "^5",
"vite": "^4.3.0",
"vite-plugin-checker": "^0.5.5"
"vite-plugin-checker": "^0.5.5",
"vitest": "^0.30.1",
"yaml": "^2.3.1"
},
"scripts": {
"start": "vite",
Expand Down

0 comments on commit 1084db4

Please sign in to comment.