refactor(config): unify config path resolution across app and launcher

Share config discovery logic between main and launcher so XDG/home and SubMiner/subminer precedence stay consistent. Add regression tests for resolution order and keep config path/show behavior stable.
This commit is contained in:
2026-02-19 01:06:26 -08:00
parent 9384d67b8e
commit 58f28b7b55
9 changed files with 250 additions and 72 deletions

View File

@@ -3,6 +3,7 @@ import path from 'node:path';
import os from 'node:os';
import { Command } from 'commander';
import { parse as parseJsonc } from 'jsonc-parser';
import { resolveConfigFilePath } from '../src/config/path-resolution.js';
import type {
LogLevel,
YoutubeSubgenMode,
@@ -27,12 +28,17 @@ import {
inferWhisperLanguage,
} from './util.js';
function resolveLauncherMainConfigPath(): string {
return resolveConfigFilePath({
xdgConfigHome: process.env.XDG_CONFIG_HOME,
homeDir: os.homedir(),
existsSync: fs.existsSync,
});
}
export function loadLauncherYoutubeSubgenConfig(): LauncherYoutubeSubgenConfig {
const configDir = path.join(os.homedir(), '.config', 'SubMiner');
const jsoncPath = path.join(configDir, 'config.jsonc');
const jsonPath = path.join(configDir, 'config.json');
const configPath = fs.existsSync(jsoncPath) ? jsoncPath : fs.existsSync(jsonPath) ? jsonPath : '';
if (!configPath) return {};
const configPath = resolveLauncherMainConfigPath();
if (!fs.existsSync(configPath)) return {};
try {
const data = fs.readFileSync(configPath, 'utf8');
@@ -118,11 +124,8 @@ export function loadLauncherYoutubeSubgenConfig(): LauncherYoutubeSubgenConfig {
}
export function loadLauncherJellyfinConfig(): LauncherJellyfinConfig {
const configDir = path.join(os.homedir(), '.config', 'SubMiner');
const jsoncPath = path.join(configDir, 'config.jsonc');
const jsonPath = path.join(configDir, 'config.json');
const configPath = fs.existsSync(jsoncPath) ? jsoncPath : fs.existsSync(jsonPath) ? jsonPath : '';
if (!configPath) return {};
const configPath = resolveLauncherMainConfigPath();
if (!fs.existsSync(configPath)) return {};
try {
const data = fs.readFileSync(configPath, 'utf8');