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');

View File

@@ -1,6 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { resolveConfigFilePath } from '../src/config/path-resolution.js';
import type { Args } from './types.js';
import { log, fail } from './log.js';
import { commandExists, isYoutubeTarget, resolvePathMaybe, realpathMaybe } from './util.js';
@@ -97,18 +98,11 @@ function registerCleanup(args: Args): void {
}
function resolveMainConfigPath(): string {
const xdgConfigHome = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config');
const baseDirs = Array.from(new Set([xdgConfigHome, path.join(os.homedir(), '.config')]));
const appNames = ['SubMiner', 'subminer'];
for (const baseDir of baseDirs) {
for (const appName of appNames) {
const jsoncPath = path.join(baseDir, appName, 'config.jsonc');
if (fs.existsSync(jsoncPath)) return jsoncPath;
const jsonPath = path.join(baseDir, appName, 'config.json');
if (fs.existsSync(jsonPath)) return jsonPath;
}
}
return path.join(baseDirs[0], 'SubMiner', 'config.jsonc');
return resolveConfigFilePath({
xdgConfigHome: process.env.XDG_CONFIG_HOME,
homeDir: os.homedir(),
existsSync: fs.existsSync,
});
}
function runDoctor(args: Args, appPath: string | null, mpvSocketPath: string): never {