Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: .envのデフォルトエンジン情報を読み出す部分を共通化する #2334

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/backend/browser/contract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { type EngineInfo, envEngineInfoSchema } from "@/type/preload";
import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo";
import { type EngineInfo } from "@/type/preload";

const baseEngineInfo = envEngineInfoSchema
.array()
.parse(JSON.parse(import.meta.env.VITE_DEFAULT_ENGINE_INFOS))[0];
const baseEngineInfo = loadEnvEngineInfos()[0];

export const defaultEngine: EngineInfo = (() => {
const { protocol, hostname, port, pathname } = new URL(baseEngineInfo.host);
Expand Down
9 changes: 2 additions & 7 deletions src/backend/common/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import AsyncLock from "async-lock";
import {
AcceptTermsStatus,
ConfigType,
EngineId,
configSchema,
DefaultStyleId,
defaultHotkeySettings,
Expand All @@ -12,9 +11,9 @@ import {
HotkeyCombination,
VoiceId,
PresetKey,
envEngineInfoSchema,
} from "@/type/preload";
import { ensureNotNullish } from "@/helpers/errorHelper";
import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo";

const lockKey = "save";

Expand All @@ -40,11 +39,7 @@ const migrations: [string, (store: Record<string, unknown>) => unknown][] = [
if (import.meta.env.VITE_DEFAULT_ENGINE_INFOS == undefined) {
throw new Error("VITE_DEFAULT_ENGINE_INFOS == undefined");
}
const engineId = EngineId(
envEngineInfoSchema
.array()
.parse(JSON.parse(import.meta.env.VITE_DEFAULT_ENGINE_INFOS))[0].uuid,
);
const engineId = loadEnvEngineInfos()[0].uuid;
if (engineId == undefined)
throw new Error("VITE_DEFAULT_ENGINE_INFOS[0].uuid == undefined");
const prevDefaultStyleIds = config.defaultStyleIds as DefaultStyleId[];
Expand Down
8 changes: 2 additions & 6 deletions src/backend/electron/manager/engineInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@ import {
MinimumEngineManifestType,
EngineId,
minimumEngineManifestSchema,
envEngineInfoSchema,
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { BaseConfigManager } from "@/backend/common/ConfigManager";
import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo";

/**
* デフォルトエンジンの情報を取得する
*/
function fetchDefaultEngineInfos(defaultEngineDir: string): EngineInfo[] {
// TODO: envから直接ではなく、envに書いたengine_manifest.jsonから情報を得るようにする
const defaultEngineInfosEnv =
import.meta.env.VITE_DEFAULT_ENGINE_INFOS ?? "[]";

const envSchema = envEngineInfoSchema.array();
const engines = envSchema.parse(JSON.parse(defaultEngineInfosEnv));
const engines = loadEnvEngineInfos();

return engines.map((engineInfo) => {
const { protocol, hostname, port, pathname } = new URL(engineInfo.host);
Expand Down
28 changes: 28 additions & 0 deletions src/domain/defaultEngine/envEngineInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* デフォルトエンジンの環境変数関連のモジュール
*/

import { z } from "zod";

import { engineIdSchema } from "@/type/preload";

/** .envに書くデフォルトエンジン情報のスキーマ */
export const envEngineInfoSchema = z.object({
uuid: engineIdSchema,
host: z.string(),
name: z.string(),
executionEnabled: z.boolean(),
executionFilePath: z.string(),
executionArgs: z.array(z.string()),
path: z.string().optional(),
});
export type EnvEngineInfoType = z.infer<typeof envEngineInfoSchema>;

/** 環境変数を経由して.envを読み込む */
export function loadEnvEngineInfos(): EnvEngineInfoType[] {
const defaultEngineInfosEnv =
import.meta.env.VITE_DEFAULT_ENGINE_INFOS ?? "[]";

const envSchema = envEngineInfoSchema.array();
return envSchema.parse(JSON.parse(defaultEngineInfosEnv));
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* デフォルトエンジン関連のモジュール
* デフォルトエンジンの最新情報関連のモジュール
*/

import { z } from "zod";
Expand Down
11 changes: 0 additions & 11 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,6 @@ export const configSchema = z
.merge(rootMiscSettingSchema);
export type ConfigType = z.infer<typeof configSchema>;

export const envEngineInfoSchema = z.object({
uuid: engineIdSchema,
host: z.string(),
name: z.string(),
executionEnabled: z.boolean(),
executionFilePath: z.string(),
executionArgs: z.array(z.string()),
path: z.string().optional(),
});
export type EnvEngineInfoType = z.infer<typeof envEngineInfoSchema>;

// workaround. SystemError(https://nodejs.org/api/errors.html#class-systemerror)が2022/05/19時点ではNodeJSの型定義に記述されていないためこれを追加しています。
export class SystemError extends Error {
code?: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import path from "path";
import fs from "fs";
import { fetchDefaultEngineInfos } from "@/domain/defaultEngine";
import { fetchDefaultEngineInfos } from "@/domain/defaultEngine/latetDefaultEngine";

const currentDir = "tests/unit/domain/defaultEngine";

Expand Down
Loading