mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract app logging runtime adapters
This commit is contained in:
@@ -16,7 +16,7 @@
|
|||||||
"docs:build": "vitepress build docs",
|
"docs:build": "vitepress build docs",
|
||||||
"docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort",
|
"docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort",
|
||||||
"test:config": "pnpm run build && node --test dist/config/config.test.js",
|
"test:config": "pnpm run build && node --test dist/config/config.test.js",
|
||||||
"test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js dist/core/services/runtime-options-manager-runtime-service.test.js dist/core/services/config-warning-runtime-service.test.js",
|
"test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js dist/core/services/runtime-options-manager-runtime-service.test.js dist/core/services/config-warning-runtime-service.test.js dist/core/services/app-logging-runtime-service.test.js",
|
||||||
"test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js",
|
"test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js",
|
||||||
"generate:config-example": "pnpm run build && node dist/generate-config-example.js",
|
"generate:config-example": "pnpm run build && node dist/generate-config-example.js",
|
||||||
"start": "pnpm run build && electron . --start",
|
"start": "pnpm run build && electron . --start",
|
||||||
|
|||||||
28
src/core/services/app-logging-runtime-service.test.ts
Normal file
28
src/core/services/app-logging-runtime-service.test.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { createAppLoggingRuntimeService } from "./app-logging-runtime-service";
|
||||||
|
|
||||||
|
test("createAppLoggingRuntimeService routes logs and formats config warnings", () => {
|
||||||
|
const lines: string[] = [];
|
||||||
|
const logger = {
|
||||||
|
log: (line: string) => lines.push(`log:${line}`),
|
||||||
|
warn: (line: string) => lines.push(`warn:${line}`),
|
||||||
|
error: (line: string) => lines.push(`error:${line}`),
|
||||||
|
};
|
||||||
|
|
||||||
|
const runtime = createAppLoggingRuntimeService(logger);
|
||||||
|
runtime.logInfo("hello");
|
||||||
|
runtime.logWarning("careful");
|
||||||
|
runtime.logNoRunningInstance();
|
||||||
|
runtime.logConfigWarning({
|
||||||
|
path: "x.y",
|
||||||
|
value: "bad",
|
||||||
|
fallback: "good",
|
||||||
|
message: "invalid",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(lines[0], "log:hello");
|
||||||
|
assert.equal(lines[1], "warn:careful");
|
||||||
|
assert.equal(lines[2], "error:No running instance. Use --start to launch the app.");
|
||||||
|
assert.match(lines[3], /^warn:\[config\] x\.y: invalid /);
|
||||||
|
});
|
||||||
28
src/core/services/app-logging-runtime-service.ts
Normal file
28
src/core/services/app-logging-runtime-service.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { ConfigValidationWarning } from "../../types";
|
||||||
|
import { logConfigWarningRuntimeService } from "./config-warning-runtime-service";
|
||||||
|
|
||||||
|
export interface AppLoggingRuntime {
|
||||||
|
logInfo: (message: string) => void;
|
||||||
|
logWarning: (message: string) => void;
|
||||||
|
logNoRunningInstance: () => void;
|
||||||
|
logConfigWarning: (warning: ConfigValidationWarning) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createAppLoggingRuntimeService(
|
||||||
|
logger: Pick<Console, "log" | "warn" | "error"> = console,
|
||||||
|
): AppLoggingRuntime {
|
||||||
|
return {
|
||||||
|
logInfo: (message) => {
|
||||||
|
logger.log(message);
|
||||||
|
},
|
||||||
|
logWarning: (message) => {
|
||||||
|
logger.warn(message);
|
||||||
|
},
|
||||||
|
logNoRunningInstance: () => {
|
||||||
|
logger.error("No running instance. Use --start to launch the app.");
|
||||||
|
},
|
||||||
|
logConfigWarning: (warning) => {
|
||||||
|
logConfigWarningRuntimeService(warning, (line) => logger.warn(line));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
15
src/main.ts
15
src/main.ts
@@ -206,7 +206,7 @@ import { runAppShutdownRuntimeService } from "./core/services/app-shutdown-runti
|
|||||||
import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service";
|
import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service";
|
||||||
import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service";
|
import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service";
|
||||||
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
|
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
|
||||||
import { logConfigWarningRuntimeService } from "./core/services/config-warning-runtime-service";
|
import { createAppLoggingRuntimeService } from "./core/services/app-logging-runtime-service";
|
||||||
import {
|
import {
|
||||||
runSubsyncManualFromIpcRuntimeService,
|
runSubsyncManualFromIpcRuntimeService,
|
||||||
triggerSubsyncFromConfigRuntimeService,
|
triggerSubsyncFromConfigRuntimeService,
|
||||||
@@ -236,6 +236,7 @@ const isDev =
|
|||||||
process.argv.includes("--dev") || process.argv.includes("--debug");
|
process.argv.includes("--dev") || process.argv.includes("--debug");
|
||||||
const texthookerService = new TexthookerService();
|
const texthookerService = new TexthookerService();
|
||||||
const subtitleWsService = new SubtitleWebSocketService();
|
const subtitleWsService = new SubtitleWebSocketService();
|
||||||
|
const appLogger = createAppLoggingRuntimeService();
|
||||||
|
|
||||||
function getDefaultSocketPath(): string {
|
function getDefaultSocketPath(): string {
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
@@ -473,9 +474,7 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
|||||||
parseArgs: (argv) => parseArgs(argv),
|
parseArgs: (argv) => parseArgs(argv),
|
||||||
handleCliCommand: (args, source) => handleCliCommand(args, source),
|
handleCliCommand: (args, source) => handleCliCommand(args, source),
|
||||||
printHelp: () => printHelp(DEFAULT_TEXTHOOKER_PORT),
|
printHelp: () => printHelp(DEFAULT_TEXTHOOKER_PORT),
|
||||||
logNoRunningInstance: () => {
|
logNoRunningInstance: () => appLogger.logNoRunningInstance(),
|
||||||
console.error("No running instance. Use --start to launch the app.");
|
|
||||||
},
|
|
||||||
onReady: async () => {
|
onReady: async () => {
|
||||||
await runAppReadyRuntimeService({
|
await runAppReadyRuntimeService({
|
||||||
loadSubtitlePosition: () => loadSubtitlePosition(),
|
loadSubtitlePosition: () => loadSubtitlePosition(),
|
||||||
@@ -533,9 +532,7 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
|||||||
},
|
},
|
||||||
getResolvedConfig: () => getResolvedConfig(),
|
getResolvedConfig: () => getResolvedConfig(),
|
||||||
getConfigWarnings: () => configService.getWarnings(),
|
getConfigWarnings: () => configService.getWarnings(),
|
||||||
logConfigWarning: (warning) => {
|
logConfigWarning: (warning) => appLogger.logConfigWarning(warning),
|
||||||
logConfigWarningRuntimeService(warning, (line) => console.warn(line));
|
|
||||||
},
|
|
||||||
initRuntimeOptionsManager: () => {
|
initRuntimeOptionsManager: () => {
|
||||||
runtimeOptionsManager = createRuntimeOptionsManagerRuntimeService({
|
runtimeOptionsManager = createRuntimeOptionsManagerRuntimeService({
|
||||||
getAnkiConfig: () => configService.getConfig().ankiConnect,
|
getAnkiConfig: () => configService.getConfig().ankiConnect,
|
||||||
@@ -559,9 +556,7 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
|||||||
startSubtitleWebsocket: (port) => {
|
startSubtitleWebsocket: (port) => {
|
||||||
subtitleWsService.start(port, () => currentSubText);
|
subtitleWsService.start(port, () => currentSubText);
|
||||||
},
|
},
|
||||||
log: (message) => {
|
log: (message) => appLogger.logInfo(message),
|
||||||
console.log(message);
|
|
||||||
},
|
|
||||||
createMecabTokenizerAndCheck: async () => {
|
createMecabTokenizerAndCheck: async () => {
|
||||||
mecabTokenizer = new MecabTokenizer();
|
mecabTokenizer = new MecabTokenizer();
|
||||||
await mecabTokenizer.checkAvailability();
|
await mecabTokenizer.checkAvailability();
|
||||||
|
|||||||
Reference in New Issue
Block a user