From ae0038462a0b22aa5e4d82035389f50d61a53a69 Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Tue, 27 Aug 2024 10:07:55 +0900 Subject: [PATCH] engine-infos alias poc --- .env.development | 4 +++ .env.production | 10 ------ .env.test | 10 ------ CONTRIBUTING.md | 33 ++++++++++--------- README.md | 4 +-- engine-infos/development.ts | 13 ++++++++ engine-infos/production.ts | 12 +++++++ engine-infos/test.ts | 12 +++++++ engine-infos/types.d.ts | 18 ++++++++++ playwright.config.ts | 19 ++--------- src/backend/browser/contract.ts | 9 ++--- src/backend/common/ConfigManager.ts | 14 ++------ src/backend/electron/manager/engineManager.ts | 11 ++----- src/type/preload.ts | 11 ------- src/vite-env.d.ts | 1 - tsconfig.json | 6 ++-- vite.config.mts | 21 ++++++++++-- 17 files changed, 111 insertions(+), 97 deletions(-) create mode 100644 .env.development create mode 100644 engine-infos/development.ts create mode 100644 engine-infos/production.ts create mode 100644 engine-infos/test.ts create mode 100644 engine-infos/types.d.ts diff --git a/.env.development b/.env.development new file mode 100644 index 0000000000..c0576f19fd --- /dev/null +++ b/.env.development @@ -0,0 +1,4 @@ +VITE_APP_NAME=voicevox +VITE_OFFICIAL_WEBSITE_URL=https://voicevox.hiroshiba.jp/ +VITE_LATEST_UPDATE_INFOS_URL=https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/data/updateInfos.json +VITE_GTM_CONTAINER_ID=GTM-DUMMY diff --git a/.env.production b/.env.production index 5b0dcb0498..c0576f19fd 100644 --- a/.env.production +++ b/.env.production @@ -1,14 +1,4 @@ VITE_APP_NAME=voicevox -VITE_DEFAULT_ENGINE_INFOS=`[ - { - "uuid": "074fc39e-678b-4c13-8916-ffca8d505d1d", - "name": "VOICEVOX Engine", - "executionEnabled": true, - "executionFilePath": "vv-engine/run.exe", - "executionArgs": [], - "host": "http://127.0.0.1:50021" - } -]` VITE_OFFICIAL_WEBSITE_URL=https://voicevox.hiroshiba.jp/ VITE_LATEST_UPDATE_INFOS_URL=https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/data/updateInfos.json VITE_GTM_CONTAINER_ID=GTM-DUMMY diff --git a/.env.test b/.env.test index dcf447f8b2..c0576f19fd 100644 --- a/.env.test +++ b/.env.test @@ -1,14 +1,4 @@ VITE_APP_NAME=voicevox -VITE_DEFAULT_ENGINE_INFOS=`[ - { - "uuid": "074fc39e-678b-4c13-8916-ffca8d505d1d", - "name": "VOICEVOX Engine", - "executionEnabled": true, - "executionFilePath": "../voicevox_engine/run.exe", - "executionArgs": [], - "host": "http://127.0.0.1:50021" - } -]` VITE_OFFICIAL_WEBSITE_URL=https://voicevox.hiroshiba.jp/ VITE_LATEST_UPDATE_INFOS_URL=https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/data/updateInfos.json VITE_GTM_CONTAINER_ID=GTM-DUMMY diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5e6dcfe71d..f18c02c076 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -125,21 +125,24 @@ git clone git@github.com:(個人のGitHubアカウント名)/voicevox.git ### 6. エンジンを指定する -- `.env.production`というファイルがありますので、コピーして、名前を`.env`にします。 -- ファイルをエディタでひらいて、`VITE_DEFAULT_ENGINE_INFOS`内の`executionFilePath`に手順1のフォルダ名をいれます。たとえば製品版をインストーラで導入し、インストール先を変更していない場合は、下記のように書き換えて保存します。 - -```ini -VITE_APP_NAME=voicevox -VITE_DEFAULT_ENGINE_INFOS=`[ - { - "uuid": "074fc39e-678b-4c13-8916-ffca8d505d1d", - "name": "VOICEVOX Engine", - "executionEnabled": true, - "executionFilePath": "C:/Users/(ユーザー名)/AppData/Local/Programs/VOICEVOX/vv-engine/run.exe", - "executionArgs": [], - "host": "http://127.0.0.1:50021" - } -]` +- `engine-infos/production.ts`というファイルがありますので、コピーして、名前を`engine-infos/development.ts`にします。 +- ファイルをエディタでひらいて、`executionFilePath`に手順1のフォルダ名をいれます。たとえば製品版をインストーラで導入し、インストール先を変更していない場合は、下記のように書き換えて保存します。 + +```ts +import type { DefaultEngineInfos } from "./types"; + +export default [ + { + uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d", + name: "VOICEVOX Engine", + executionEnabled: true, + executionFilePath: + "/Applications/VOICEVOX.app/Contents/Resources/vv-engine/run", + executionArgs: [], + host: "http://127.0.0.1:50021", + }, +] satisfies DefaultEngineInfos; + ``` - あなたがVOICEVOX製品版のインストール先を変更している場合は個別で指定します。たとえば、`D:\VOICEVOX0.14.1`に製品版をインストールしている場合は、下記のように書き換えて保存します。 diff --git a/README.md b/README.md index 84e7dc1a41..64221afce3 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ npm ci ### エンジンの準備 -`.env.production`をコピーして`.env`を作成し、`VITE_DEFAULT_ENGINE_INFOS`内の`executionFilePath`に +`engine-infos/production.ts`をコピーして`engine-infos/development.ts`を作成し、`executionFilePath`に [製品版 VOICEVOX](https://voicevox.hiroshiba.jp/) 内の`vv-engine/run.exe`を指定すれば動きます。 Windows でインストール先を変更していない場合は`C:/Users/(ユーザー名)/AppData/Local/Programs/VOICEVOX/vv-engine/run.exe`を指定してください。 @@ -66,7 +66,7 @@ VOICEVOX エディタの実行とは別にエンジン API のサーバを立て 代わりに`executionEnabled`を`false`にしてください。 これは製品版 VOICEVOX を起動している場合もあてはまります。 -エンジン API の宛先エンドポイントを変更する場合は`VITE_DEFAULT_ENGINE_INFOS`内の`host`を変更してください。 +エンジン API の宛先エンドポイントを変更する場合は`host`を変更してください。 ### Electron の実行 diff --git a/engine-infos/development.ts b/engine-infos/development.ts new file mode 100644 index 0000000000..551035eaab --- /dev/null +++ b/engine-infos/development.ts @@ -0,0 +1,13 @@ +import type { DefaultEngineInfos, EngineId } from "./types"; + +export default [ + { + uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d" as EngineId, + name: "VOICEVOX Engine", + executionEnabled: true, + executionFilePath: + "/Applications/VOICEVOX.app/Contents/Resources/vv-engine/run", + executionArgs: [], + host: "http://127.0.0.1:50021", + }, +] satisfies DefaultEngineInfos; diff --git a/engine-infos/production.ts b/engine-infos/production.ts new file mode 100644 index 0000000000..fae7779612 --- /dev/null +++ b/engine-infos/production.ts @@ -0,0 +1,12 @@ +import type { DefaultEngineInfos, EngineId } from "./types"; + +export default [ + { + uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d" as EngineId, + name: "VOICEVOX Engine", + executionEnabled: true, + executionFilePath: "vv-engine/run", + executionArgs: [], + host: "http://127.0.0.1:50021", + }, +] satisfies DefaultEngineInfos; diff --git a/engine-infos/test.ts b/engine-infos/test.ts new file mode 100644 index 0000000000..4a4f4bbdad --- /dev/null +++ b/engine-infos/test.ts @@ -0,0 +1,12 @@ +import type { DefaultEngineInfos, EngineId } from "./types"; + +export default [ + { + uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d" as EngineId, + name: "VOICEVOX Engine", + executionEnabled: true, + executionFilePath: "../voicevox_engine/run.exe", + executionArgs: [], + host: "http://127.0.0.1:50021", + }, +] satisfies DefaultEngineInfos; diff --git a/engine-infos/types.d.ts b/engine-infos/types.d.ts new file mode 100644 index 0000000000..8debc9c966 --- /dev/null +++ b/engine-infos/types.d.ts @@ -0,0 +1,18 @@ +import { BRAND } from "zod"; + +export type EngineId = string & BRAND<"EngineId">; + +export interface DefaultEngineInfo { + uuid: EngineId; + name: string; + path?: string; + executionEnabled: boolean; + executionFilePath: string; + executionArgs: string[]; + host: string; +} + +export type DefaultEngineInfos = DefaultEngineInfo[]; + +declare const engineInfos: DefaultEngineInfos; +export default engineInfos; diff --git a/playwright.config.ts b/playwright.config.ts index 8b0e6e768c..a4a3ed973b 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,7 +1,7 @@ import type { PlaywrightTestConfig, Project } from "@playwright/test"; -import { z } from "zod"; - import dotenv from "dotenv"; +import defaultEngineInfos from "./engine-infos/test"; + dotenv.config({ override: true }); let project: Project; @@ -14,20 +14,7 @@ if (isElectron) { } else if (isBrowser) { project = { name: "browser", testDir: "./tests/e2e/browser" }; - // エンジンの起動が必要 - const defaultEngineInfosEnv = process.env.VITE_DEFAULT_ENGINE_INFOS ?? "[]"; - const envSchema = z // FIXME: electron起動時のものと共通化したい - .object({ - host: z.string(), - executionFilePath: z.string(), - executionArgs: z.array(z.string()), - executionEnabled: z.boolean(), - }) - .passthrough() - .array(); - const engineInfos = envSchema.parse(JSON.parse(defaultEngineInfosEnv)); - - for (const info of engineInfos) { + for (const info of defaultEngineInfos) { if (!info.executionEnabled) { continue; } diff --git a/src/backend/browser/contract.ts b/src/backend/browser/contract.ts index 089b07280c..063106c687 100644 --- a/src/backend/browser/contract.ts +++ b/src/backend/browser/contract.ts @@ -1,11 +1,8 @@ -import { EngineInfo, envEngineInfoSchema } from "@/type/preload"; - -const baseEngineInfo = envEngineInfoSchema - .array() - .parse(JSON.parse(import.meta.env.VITE_DEFAULT_ENGINE_INFOS))[0]; +import engineInfos from "@engine-infos"; +import { EngineInfo } from "@/type/preload"; export const defaultEngine: EngineInfo = { - ...baseEngineInfo, + ...engineInfos[0], type: "path", // FIXME: ダミーで"path"にしているので、エンジンAPIのURLを設定できるようにし、type: "URL"にする isDefault: true, }; diff --git a/src/backend/common/ConfigManager.ts b/src/backend/common/ConfigManager.ts index c071b07a28..eac988e62e 100644 --- a/src/backend/common/ConfigManager.ts +++ b/src/backend/common/ConfigManager.ts @@ -1,5 +1,6 @@ import semver from "semver"; import AsyncLock from "async-lock"; +import engineInfos from "@engine-infos"; import { AcceptTermsStatus, ConfigType, @@ -12,7 +13,6 @@ import { HotkeyCombination, VoiceId, PresetKey, - envEngineInfoSchema, } from "@/type/preload"; import { ensureNotNullish } from "@/helpers/errorHelper"; @@ -36,17 +36,7 @@ const migrations: [string, (store: Record) => unknown][] = [ [ ">=0.14", (config) => { - // FIXME: できるならEngineManagerからEngineIDを取得したい - 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, - ); - if (engineId == undefined) - throw new Error("VITE_DEFAULT_ENGINE_INFOS[0].uuid == undefined"); + const engineId = EngineId(engineInfos[0].uuid); const prevDefaultStyleIds = config.defaultStyleIds as DefaultStyleId[]; config.defaultStyleIds = prevDefaultStyleIds.map((defaultStyle) => ({ engineId, diff --git a/src/backend/electron/manager/engineManager.ts b/src/backend/electron/manager/engineManager.ts index 6c32171316..da7c2a813d 100644 --- a/src/backend/electron/manager/engineManager.ts +++ b/src/backend/electron/manager/engineManager.ts @@ -7,6 +7,7 @@ import shlex from "shlex"; import { app, dialog } from "electron"; // FIXME: ここでelectronをimportするのは良くない import log from "electron-log/main"; +import engineInfos from "@engine-infos"; import { findAltPort, getPidFromPort, @@ -21,7 +22,6 @@ import { MinimumEngineManifestType, EngineId, minimumEngineManifestSchema, - envEngineInfoSchema, } from "@/type/preload"; import { AltPortInfos } from "@/store/type"; import { BaseConfigManager } from "@/backend/common/ConfigManager"; @@ -35,14 +35,7 @@ type EngineProcessContainer = { * デフォルトエンジンの情報を作成する */ function createDefaultEngineInfos(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)); - - return engines.map((engineInfo) => { + return engineInfos.map((engineInfo) => { return { ...engineInfo, isDefault: true, diff --git a/src/type/preload.ts b/src/type/preload.ts index 9b07278510..a02f9a8fc6 100644 --- a/src/type/preload.ts +++ b/src/type/preload.ts @@ -698,17 +698,6 @@ export const configSchema = z .merge(rootMiscSettingSchema); export type ConfigType = z.infer; -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; - // workaround. SystemError(https://nodejs.org/api/errors.html#class-systemerror)が2022/05/19時点ではNodeJSの型定義に記述されていないためこれを追加しています。 export class SystemError extends Error { code?: string | undefined; diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 2112976e79..2749bde64c 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -4,7 +4,6 @@ interface ImportMetaEnv { readonly VITE_7Z_BIN_NAME: string; readonly VITE_APP_NAME: string; readonly VITE_APP_VERSION: string; - readonly VITE_DEFAULT_ENGINE_INFOS: string; readonly VITE_OFFICIAL_WEBSITE_URL: string; readonly VITE_LATEST_UPDATE_INFOS_URL: string; readonly VITE_GTM_CONTAINER_ID: string; diff --git a/tsconfig.json b/tsconfig.json index fe3a92f753..616de860c3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,8 @@ "baseUrl": ".", "types": ["vitest/globals"], "paths": { - "@/*": ["src/*"] + "@/*": ["src/*"], + "@engine-infos": ["engine-infos/types.d.ts"] }, "lib": ["esnext", "dom", "dom.iterable"] }, @@ -26,7 +27,8 @@ "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx", - ".storybook/**/*.ts", + "engine-infos/*.ts", + ".storybook/**/*.ts" ], "exclude": ["node_modules"] } diff --git a/vite.config.mts b/vite.config.mts index c6bd3ab3ee..52cc5466e8 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -4,7 +4,6 @@ import { rm } from "fs/promises"; import { fileURLToPath } from "url"; import electron from "vite-plugin-electron"; -import tsconfigPaths from "vite-tsconfig-paths"; import vue from "@vitejs/plugin-vue"; import checker from "vite-plugin-checker"; import { nodePolyfills } from "vite-plugin-node-polyfills"; @@ -25,6 +24,11 @@ export default defineConfig((options) => { `"package.json"の"name":"${packageName}"は"VITE_APP_NAME":"${env.VITE_APP_NAME}"から始まっている必要があります`, ); } + const engineInfosPath = path.resolve( + __dirname, + "engine-infos", + `${options.mode}.ts`, + ); const shouldEmitSourcemap = ["development", "test"].includes(options.mode); process.env.VITE_7Z_BIN_NAME = (options.mode === "development" @@ -58,6 +62,7 @@ export default defineConfig((options) => { resolve: { alias: { "@": path.resolve(__dirname, "src/"), + "@engine-infos": engineInfosPath, }, }, test: { @@ -92,7 +97,12 @@ export default defineConfig((options) => { } }, vite: { - plugins: [tsconfigPaths({ root: __dirname })], + resolve: { + alias: { + "@": path.resolve(__dirname, "src/"), + "@engine-infos": engineInfosPath, + }, + }, build: { outDir: path.resolve(__dirname, "dist"), sourcemap, @@ -106,7 +116,12 @@ export default defineConfig((options) => { reload(); }, vite: { - plugins: [tsconfigPaths({ root: __dirname })], + resolve: { + alias: { + "@": path.resolve(__dirname, "src/"), + "@engine-infos": engineInfosPath, + }, + }, build: { outDir: path.resolve(__dirname, "dist"), sourcemap,