From b2df0cc7d703dff928e7e5629320b2bb99850a68 Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Tue, 22 Oct 2024 14:59:58 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=E3=82=A2=E3=83=88=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=81=AA=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E3=82=92=E3=83=98=E3=83=AB=E3=83=91=E3=83=BC=E9=96=A2?= =?UTF-8?q?=E6=95=B0=E3=81=AB=E5=88=87=E3=82=8A=E5=87=BA=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/electronConfig.ts | 12 +++--------- src/helpers/fileHelper.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/backend/electron/electronConfig.ts b/src/backend/electron/electronConfig.ts index 0fc90bf23b..846ad637fd 100644 --- a/src/backend/electron/electronConfig.ts +++ b/src/backend/electron/electronConfig.ts @@ -1,9 +1,9 @@ import { join } from "path"; import fs from "fs"; import { app } from "electron"; -import { moveFile } from "move-file"; import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager"; import { ConfigType } from "@/type/preload"; +import { writeFileSafely } from "@/helpers/fileHelper"; export class ElectronConfigManager extends BaseConfigManager { protected getAppVersion() { @@ -23,16 +23,10 @@ export class ElectronConfigManager extends BaseConfigManager { } protected async save(config: ConfigType & Metadata) { - // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する - const temp_path = `${this.configPath}.tmp`; - await fs.promises.writeFile( - temp_path, + await writeFileSafely( + this.configPath, JSON.stringify(config, undefined, 2), ); - - await moveFile(temp_path, this.configPath, { - overwrite: true, - }); } private get configPath(): string { diff --git a/src/helpers/fileHelper.ts b/src/helpers/fileHelper.ts index d39640189c..d616c4628c 100644 --- a/src/helpers/fileHelper.ts +++ b/src/helpers/fileHelper.ts @@ -1,5 +1,20 @@ +import fs from "fs"; +import { moveFile } from "move-file"; import { ResultError } from "@/type/result"; +export async function writeFileSafely( + path: string, + data: string | NodeJS.ArrayBufferView, +) { + // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する + const temp_path = `${path}.tmp`; + await fs.promises.writeFile(temp_path, data); + + await moveFile(temp_path, path, { + overwrite: true, + }); +} + /** ファイル書き込み時のエラーメッセージを生成する */ // instanceof ResultErrorで生まれるResultErrorを受け取れるようにするため、anyを許容する // eslint-disable-next-line @typescript-eslint/no-explicit-any From ab6ad25a65ff0545e11613a28530d7afc334333d Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Tue, 22 Oct 2024 15:01:11 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E4=BF=9D=E5=AD=98=E3=82=92=E3=82=A2=E3=83=88=E3=83=9F?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=81=AA=E6=93=8D=E4=BD=9C=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/main.ts | 5 +++-- src/backend/electron/manager/RuntimeInfoManager.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 42a908ecca..e194853282 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -40,6 +40,7 @@ import { TextAsset, } from "@/type/preload"; import { themes } from "@/domain/theme"; +import { writeFileSafely } from "@/helpers/fileHelper"; type SingleInstanceLockData = { filePath: string | undefined; @@ -742,9 +743,9 @@ registerIpcMainHandle({ win.show(); }, - WRITE_FILE: (_, { filePath, buffer }) => { + WRITE_FILE: async (_, { filePath, buffer }) => { try { - fs.writeFileSync( + await writeFileSafely( filePath, new DataView(buffer instanceof Uint8Array ? buffer.buffer : buffer), ); diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index e3ec4340ca..579f9b2c75 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -3,11 +3,11 @@ * ランタイム情報には起動しているエンジンのURLなどが含まれる。 */ -import fs from "fs"; import AsyncLock from "async-lock"; import log from "electron-log/main"; import type { AltPortInfos } from "@/store/type"; import { EngineId, EngineInfo } from "@/type/preload"; +import { writeFileSafely } from "@/helpers/fileHelper"; /** * ランタイム情報書き出しに必要なEngineInfo @@ -99,7 +99,7 @@ export class RuntimeInfoManager { // ファイル書き出し try { - await fs.promises.writeFile( + await writeFileSafely( this.runtimeInfoPath, JSON.stringify(runtimeInfoFormatFor3rdParty), // FIXME: zod化する ); From ba2a62520362ca23a3020a0fca983053cc6332db Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Thu, 24 Oct 2024 14:41:07 +0900 Subject: [PATCH 3/5] =?UTF-8?q?writeFileSafely=E9=96=A2=E6=95=B0=E3=82=92s?= =?UTF-8?q?rc/backend/electron=E4=BB=A5=E4=B8=8B=E3=81=AB=E7=A7=BB?= =?UTF-8?q?=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/electronConfig.ts | 2 +- src/backend/electron/fileHelper.ts | 15 +++++++++++++++ src/backend/electron/main.ts | 2 +- .../electron/manager/RuntimeInfoManager.ts | 2 +- src/helpers/fileHelper.ts | 15 --------------- 5 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 src/backend/electron/fileHelper.ts diff --git a/src/backend/electron/electronConfig.ts b/src/backend/electron/electronConfig.ts index 846ad637fd..0b9d07c4dd 100644 --- a/src/backend/electron/electronConfig.ts +++ b/src/backend/electron/electronConfig.ts @@ -1,9 +1,9 @@ import { join } from "path"; import fs from "fs"; import { app } from "electron"; +import { writeFileSafely } from "./fileHelper"; import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager"; import { ConfigType } from "@/type/preload"; -import { writeFileSafely } from "@/helpers/fileHelper"; export class ElectronConfigManager extends BaseConfigManager { protected getAppVersion() { diff --git a/src/backend/electron/fileHelper.ts b/src/backend/electron/fileHelper.ts new file mode 100644 index 0000000000..f301d866da --- /dev/null +++ b/src/backend/electron/fileHelper.ts @@ -0,0 +1,15 @@ +import fs from "fs"; +import { moveFile } from "move-file"; + +export async function writeFileSafely( + path: string, + data: string | NodeJS.ArrayBufferView, +) { + // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する + const temp_path = `${path}.tmp`; + await fs.promises.writeFile(temp_path, data); + + await moveFile(temp_path, path, { + overwrite: true, + }); +} diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index e194853282..9448e27c55 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -28,6 +28,7 @@ import { RuntimeInfoManager } from "./manager/RuntimeInfoManager"; import { registerIpcMainHandle, ipcMainSendProxy, IpcMainHandle } from "./ipc"; import { getConfigManager } from "./electronConfig"; import { EngineAndVvppController } from "./engineAndVvppController"; +import { writeFileSafely } from "./fileHelper"; import { failure, success } from "@/type/result"; import { AssetTextFileNames } from "@/type/staticResources"; import { @@ -40,7 +41,6 @@ import { TextAsset, } from "@/type/preload"; import { themes } from "@/domain/theme"; -import { writeFileSafely } from "@/helpers/fileHelper"; type SingleInstanceLockData = { filePath: string | undefined; diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index 579f9b2c75..0c1d87b9ef 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -7,7 +7,7 @@ import AsyncLock from "async-lock"; import log from "electron-log/main"; import type { AltPortInfos } from "@/store/type"; import { EngineId, EngineInfo } from "@/type/preload"; -import { writeFileSafely } from "@/helpers/fileHelper"; +import { writeFileSafely } from "@/backend/electron/fileHelper"; /** * ランタイム情報書き出しに必要なEngineInfo diff --git a/src/helpers/fileHelper.ts b/src/helpers/fileHelper.ts index d616c4628c..d39640189c 100644 --- a/src/helpers/fileHelper.ts +++ b/src/helpers/fileHelper.ts @@ -1,20 +1,5 @@ -import fs from "fs"; -import { moveFile } from "move-file"; import { ResultError } from "@/type/result"; -export async function writeFileSafely( - path: string, - data: string | NodeJS.ArrayBufferView, -) { - // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する - const temp_path = `${path}.tmp`; - await fs.promises.writeFile(temp_path, data); - - await moveFile(temp_path, path, { - overwrite: true, - }); -} - /** ファイル書き込み時のエラーメッセージを生成する */ // instanceof ResultErrorで生まれるResultErrorを受け取れるようにするため、anyを許容する // eslint-disable-next-line @typescript-eslint/no-explicit-any From b7f595a9c9ed5d4356fc08e241ce36d3ab3a3ee5 Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Sat, 26 Oct 2024 09:33:44 +0900 Subject: [PATCH 4/5] =?UTF-8?q?writeFileSafely=E3=82=92=E5=90=8C=E6=9C=9F?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/electronConfig.ts | 5 +---- src/backend/electron/fileHelper.ts | 8 ++++---- src/backend/electron/main.ts | 4 ++-- src/backend/electron/manager/RuntimeInfoManager.ts | 2 +- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/backend/electron/electronConfig.ts b/src/backend/electron/electronConfig.ts index 0b9d07c4dd..59dbfba4a5 100644 --- a/src/backend/electron/electronConfig.ts +++ b/src/backend/electron/electronConfig.ts @@ -23,10 +23,7 @@ export class ElectronConfigManager extends BaseConfigManager { } protected async save(config: ConfigType & Metadata) { - await writeFileSafely( - this.configPath, - JSON.stringify(config, undefined, 2), - ); + writeFileSafely(this.configPath, JSON.stringify(config, undefined, 2)); } private get configPath(): string { diff --git a/src/backend/electron/fileHelper.ts b/src/backend/electron/fileHelper.ts index f301d866da..79d165fc5c 100644 --- a/src/backend/electron/fileHelper.ts +++ b/src/backend/electron/fileHelper.ts @@ -1,15 +1,15 @@ import fs from "fs"; -import { moveFile } from "move-file"; +import { moveFileSync } from "move-file"; -export async function writeFileSafely( +export function writeFileSafely( path: string, data: string | NodeJS.ArrayBufferView, ) { // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する const temp_path = `${path}.tmp`; - await fs.promises.writeFile(temp_path, data); + fs.writeFileSync(temp_path, data); - await moveFile(temp_path, path, { + moveFileSync(temp_path, path, { overwrite: true, }); } diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 9448e27c55..463262dd6f 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -743,9 +743,9 @@ registerIpcMainHandle({ win.show(); }, - WRITE_FILE: async (_, { filePath, buffer }) => { + WRITE_FILE: (_, { filePath, buffer }) => { try { - await writeFileSafely( + writeFileSafely( filePath, new DataView(buffer instanceof Uint8Array ? buffer.buffer : buffer), ); diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index 0c1d87b9ef..04683ea375 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -99,7 +99,7 @@ export class RuntimeInfoManager { // ファイル書き出し try { - await writeFileSafely( + writeFileSafely( this.runtimeInfoPath, JSON.stringify(runtimeInfoFormatFor3rdParty), // FIXME: zod化する ); From 548202ad4561b5a7aa898db77506527e481e306f Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Sat, 26 Oct 2024 09:37:21 +0900 Subject: [PATCH 5/5] =?UTF-8?q?writeFileSafely=E3=81=AE=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E5=90=8D=E3=81=AE=E3=83=95=E3=82=A9=E3=83=BC=E3=83=9E=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=81=A8=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/fileHelper.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/backend/electron/fileHelper.ts b/src/backend/electron/fileHelper.ts index 79d165fc5c..9d80ac0c44 100644 --- a/src/backend/electron/fileHelper.ts +++ b/src/backend/electron/fileHelper.ts @@ -1,15 +1,18 @@ import fs from "fs"; import { moveFileSync } from "move-file"; +/** + * 書き込みに失敗したときにファイルが消えないように、 + * tmpファイル書き込み後、保存先ファイルに上書きする + */ export function writeFileSafely( path: string, data: string | NodeJS.ArrayBufferView, ) { - // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する - const temp_path = `${path}.tmp`; - fs.writeFileSync(temp_path, data); + const tmpPath = `${path}.tmp`; + fs.writeFileSync(tmpPath, data); - moveFileSync(temp_path, path, { + moveFileSync(tmpPath, path, { overwrite: true, }); }