From af45726a15a6e2e4484a66bc044d2e4e120a7cc5 Mon Sep 17 00:00:00 2001 From: apple <245524539+apples-kksk@users.noreply.github.com> Date: Sat, 9 May 2026 17:51:27 +0800 Subject: [PATCH] Close open settings files before download overwrite --- src/service/file.service.ts | 31 ++++++++++++++++++++ src/sync.ts | 2 ++ test/service/fileService/fileService.test.ts | 21 +++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/service/file.service.ts b/src/service/file.service.ts index 288064da..b9a0c1da 100644 --- a/src/service/file.service.ts +++ b/src/service/file.service.ts @@ -3,6 +3,7 @@ import * as fs from "fs-extra"; import * as path from "path"; import * as recursiveRead from "recursive-readdir"; +import * as vscode from "vscode"; import { CustomConfig } from "../models/customConfig.model"; export class File { @@ -95,6 +96,23 @@ export class FileService { } } + public static async CloseOpenFile(filePath: string): Promise { + const matchingEditors = vscode.window.visibleTextEditors.filter(editor => { + return FileService.IsSamePath(editor.document.uri.fsPath, filePath); + }); + + for (const editor of matchingEditors) { + await vscode.window.showTextDocument( + editor.document, + editor.viewColumn, + false + ); + await vscode.commands.executeCommand( + "workbench.action.closeActiveEditor" + ); + } + } + public static async ListFiles( directory: string, customSettings: CustomConfig @@ -237,4 +255,17 @@ export class FileService { public static ConcatPath(...filePaths: string[]): string { return filePaths.join(path.sep); } + + private static IsSamePath(firstPath: string, secondPath: string): boolean { + const first = FileService.NormalizePath(firstPath); + const second = FileService.NormalizePath(secondPath); + return first === second; + } + + private static NormalizePath(filePath: string): string { + const normalizedPath = path.normalize(path.resolve(filePath)); + return process.platform === "win32" + ? normalizedPath.toLowerCase() + : normalizedPath; + } } diff --git a/src/sync.ts b/src/sync.ts index 3e926481..c4fa7f4a 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -671,6 +671,8 @@ export class Sync { } } + await FileService.CloseOpenFile(filePath); + actionList.push( FileService.WriteFile(filePath, content) .then(() => { diff --git a/test/service/fileService/fileService.test.ts b/test/service/fileService/fileService.test.ts index ac6dfcd2..b8322265 100644 --- a/test/service/fileService/fileService.test.ts +++ b/test/service/fileService/fileService.test.ts @@ -1,4 +1,8 @@ import { expect } from "chai"; +import * as fs from "fs-extra"; +import * as os from "os"; +import * as path from "path"; +import * as vscode from "vscode"; import { File, FileService } from "../../../src/service/file.service"; @@ -33,4 +37,21 @@ describe("FileService", () => { ); expect(actual).to.be.equals("/User/path/to/hoge/piyo/hoge.txt"); }); + + it("should close an open editor for a file path", async () => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "settings-sync-")); + const filePath = path.join(directory, "settings.json"); + fs.writeFileSync(filePath, "{}"); + + const document = await vscode.workspace.openTextDocument(filePath); + await vscode.window.showTextDocument(document); + + await FileService.CloseOpenFile(filePath); + + const openEditor = vscode.window.visibleTextEditors.find(editor => { + return editor.document.uri.fsPath === filePath; + }); + + expect(openEditor).to.be.equals(undefined); + }); });