From 9954590008b994de42cb44f4498af81580ce36d6 Mon Sep 17 00:00:00 2001 From: Gorniaky Date: Thu, 15 Oct 2026 13:52:51 -0300 Subject: [PATCH] refactor: migrate workspace-related methods to FileSystem class --- src/commands/apps/backup.ts | 4 +- src/commands/apps/import.ts | 4 +- src/commands/team/backup.ts | 4 +- src/commands/team/import.ts | 4 +- src/core.ts | 33 ++------ src/utils/FileSystem.ts | 158 +++++++++++++++++++++--------------- 6 files changed, 104 insertions(+), 103 deletions(-) diff --git a/src/commands/apps/backup.ts b/src/commands/apps/backup.ts index b1476e5f..f597302f 100644 --- a/src/commands/apps/backup.ts +++ b/src/commands/apps/backup.ts @@ -21,11 +21,11 @@ export default class extends Command { } async run(task: TaskData, item: UserAppTreeItem) { - const workspaceAvailable = this.core.workspaceAvailable; + const workspaceAvailable = FileSystem.workspaceAvailable; let workspaceFolder: Uri | undefined; if (workspaceAvailable) workspaceFolder = await FileSystem.getWorkspaceFolder({ token: task.token }); if (!workspaceFolder) { - workspaceFolder = await this.core.getFolderDialog(task); + workspaceFolder = await FileSystem.getFolderDialog(task); if (!workspaceFolder) throw Error(t("no.folder.found")); } diff --git a/src/commands/apps/import.ts b/src/commands/apps/import.ts index 532236b2..52bdf864 100644 --- a/src/commands/apps/import.ts +++ b/src/commands/apps/import.ts @@ -21,11 +21,11 @@ export default class extends Command { } async run(task: TaskData, item: UserAppTreeItem) { - const workspaceAvailable = this.core.workspaceAvailable; + const workspaceAvailable = FileSystem.workspaceAvailable; let workspaceFolder: Uri | undefined; if (workspaceAvailable) workspaceFolder = await FileSystem.getWorkspaceFolder(); if (!workspaceFolder) { - workspaceFolder = await this.core.getFolderDialog(task); + workspaceFolder = await FileSystem.getFolderDialog(task); if (!workspaceFolder) throw Error(t("no.folder.found")); } diff --git a/src/commands/team/backup.ts b/src/commands/team/backup.ts index 91a8f062..84e26335 100644 --- a/src/commands/team/backup.ts +++ b/src/commands/team/backup.ts @@ -21,11 +21,11 @@ export default class extends Command { } async run(task: TaskData, item: TeamAppTreeItem) { - const workspaceAvailable = this.core.workspaceAvailable; + const workspaceAvailable = FileSystem.workspaceAvailable; let workspaceFolder: Uri | undefined; if (workspaceAvailable) workspaceFolder = await FileSystem.getWorkspaceFolder(); if (!workspaceFolder) { - workspaceFolder = await this.core.getFolderDialog(task); + workspaceFolder = await FileSystem.getFolderDialog(task); if (!workspaceFolder) throw Error(t("no.folder.found")); } diff --git a/src/commands/team/import.ts b/src/commands/team/import.ts index 998b0144..f5793d08 100644 --- a/src/commands/team/import.ts +++ b/src/commands/team/import.ts @@ -21,11 +21,11 @@ export default class extends Command { } async run(task: TaskData, item: TeamAppTreeItem) { - const workspaceAvailable = this.core.workspaceAvailable; + const workspaceAvailable = FileSystem.workspaceAvailable; let workspaceFolder: Uri | undefined; if (workspaceAvailable) workspaceFolder = await FileSystem.getWorkspaceFolder(); if (!workspaceFolder) { - workspaceFolder = await this.core.getFolderDialog(task); + workspaceFolder = await FileSystem.getFolderDialog(task); if (!workspaceFolder) throw Error(t("no.folder.found")); } diff --git a/src/core.ts b/src/core.ts index a5cf5fc2..a5b3f403 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,9 +1,8 @@ -import { t } from "@vscode/l10n"; import { EventEmitter } from "events"; import { normalize } from "path"; -import { commands, type Disposable, type ExtensionContext, type LogOutputChannel, type SecretStorage, window, workspace } from "vscode"; +import { commands, type Disposable, type ExtensionContext, type LogOutputChannel, type SecretStorage, workspace } from "vscode"; import { type ExtensionContextId } from "./@enum"; -import type { Events, IGlobalStateStorage, TaskData } from "./@types"; +import type { Events, IGlobalStateStorage } from "./@types"; import DiscloudAuthenticationProvider from "./authentication/provider"; import { commandsRegister } from "./commands"; import { loadEvents } from "./events"; @@ -86,19 +85,11 @@ export default class ExtensionCore extends EventEmitter implements Dispo return this.getLogOutputChannel("Discloud"); } - get singleWorkspaceFolder() { - const folders = workspace.workspaceFolders; - if (folders?.length === 1) return folders[0].uri; - } - - get workspaceAvailable() { - return Boolean(workspace.workspaceFolders?.length); - } - get workspaceIgnoreList() { + const config = this.config; return _workspaceIgnoreConfigKeys - .reduce((acc, config) => { - const data = this.config.get(config); + .reduce((acc, configName) => { + const data = config.get(configName); if (data) return acc.concat(normalize(data)); return acc; }, []) @@ -118,20 +109,6 @@ export default class ExtensionCore extends EventEmitter implements Dispo this.timers.dispose(); } - async getFolderDialog(task?: TaskData | null, title?: string, openLabel?: string) { - task?.progress.report({ increment: -1, message: t("folder.get.one") }); - - const uris = await window.showOpenDialog({ - canSelectFolders: true, - title, - openLabel, - }); - - if (uris?.length) task?.progress.report({ message: t("folder.selected") }); - - return uris?.at(0); - } - getLogOutputChannel(name: string) { return DiscloudLogOutputChannel.getInstance(this.context, name); } diff --git a/src/utils/FileSystem.ts b/src/utils/FileSystem.ts index 0f89809d..dbf52cd0 100644 --- a/src/utils/FileSystem.ts +++ b/src/utils/FileSystem.ts @@ -1,6 +1,7 @@ +import { t } from "@vscode/l10n"; import { dirname, join } from "path"; import { commands, env, FileType, Uri, window, workspace, type CancellationToken } from "vscode"; -import type { GetWorkspaceFolderOptions } from "../@types"; +import type { GetWorkspaceFolderOptions, TaskData } from "../@types"; import { BLOCKED_FILES } from "./constants"; import lazy from "./lazy"; @@ -23,6 +24,95 @@ export interface FileSystemOptions { } export default class FileSystem { + static get singleWorkspaceFolder() { + const folders = workspace.workspaceFolders; + if (folders?.length === 1) return folders[0].uri; + } + + static get workspaceAvailable() { + return Boolean(workspace.workspaceFolders?.length); + } + + static async getFolderDialog(task?: TaskData | null, title?: string, openLabel?: string) { + task?.progress.report({ increment: -1, message: t("folder.get.one") }); + + const uris = await window.showOpenDialog({ + canSelectFolders: true, + title, + openLabel, + }); + + if (uris?.length) task?.progress.report({ message: t("folder.selected") }); + + return uris?.at(0); + } + + static async findIgnoreFile(filename: string, ignoreList?: string | string[] | Set, token?: CancellationToken) { + if (!ignoreList) ignoreList = lazyDefaultBlockedFilesPattern(); + + if (ignoreList instanceof Set) ignoreList = Array.from(ignoreList); + + if (Array.isArray(ignoreList)) ignoreList = `{${ignoreList}}`; + + const files = await workspace.findFiles(join(_glob, filename), ignoreList, undefined, token); + + const result = new Set(); + + for (const file of files) { + const stat = await workspace.fs.stat(file); + + if (stat.type !== FileType.File || !stat.size) return []; + + const relativeFolder = dirname(workspace.asRelativePath(file, false)); + + const fileBuffer = await workspace.fs.readFile(file); + + const patterns = fileBuffer.toString() + .replace(_commentRegexp, _emptyString) + .split(_lineBreakRegexp); + + for (const pattern of patterns) { + result.add(join(relativeFolder, pattern)); + } + } + + return Array.from(result); + } + + static async getWorkspaceFolder(options?: GetWorkspaceFolderOptions | null): Promise { + options ??= {}; + + if (options.uri instanceof Uri) { + const folder = workspace.getWorkspaceFolder(options.uri); + if (folder) return folder.uri; + } + + const folders = workspace.workspaceFolders; + if (!folders?.length) return; + if (folders.length === _one) return folders[_zero].uri; + + options.allowReadSelectedPath ??= true; + + if (options.allowReadSelectedPath) { + const [filePath] = await FileSystem.readSelectedPath(false); + if (filePath && filePath !== _dot) + return workspace.getWorkspaceFolder(Uri.file(filePath))?.uri; + } + + if (options.silent || options.token?.isCancellationRequested) return; + + const picked = await window.showWorkspaceFolderPick(); + if (picked) return picked.uri; + } + + static readSelectedPath(relative?: true): Promise + static readSelectedPath(relative: boolean): Promise + static async readSelectedPath(relative: boolean = true) { + await commands.executeCommand(relative ? "copyRelativeFilePath" : "copyFilePath"); + const copied = await env.clipboard.readText(); + return copied.split(_lineBreakRegexp); + } + constructor(readonly options: FileSystemOptions = {}) { if (!options) options = {}; @@ -100,70 +190,4 @@ export default class FileSystem { this.patterns.add(file); } } - - static async findIgnoreFile(filename: string, ignoreList?: string | string[] | Set, token?: CancellationToken) { - if (!ignoreList) ignoreList = lazyDefaultBlockedFilesPattern(); - - if (ignoreList instanceof Set) ignoreList = Array.from(ignoreList); - - if (Array.isArray(ignoreList)) ignoreList = `{${ignoreList}}`; - - const files = await workspace.findFiles(join(_glob, filename), ignoreList, undefined, token); - - const result = new Set(); - - for (const file of files) { - const stat = await workspace.fs.stat(file); - - if (stat.type !== FileType.File || !stat.size) return []; - - const relativeFolder = dirname(workspace.asRelativePath(file, false)); - - const fileBuffer = await workspace.fs.readFile(file); - - const patterns = fileBuffer.toString() - .replace(_commentRegexp, _emptyString) - .split(_lineBreakRegexp); - - for (const pattern of patterns) { - result.add(join(relativeFolder, pattern)); - } - } - - return Array.from(result); - } - - static async getWorkspaceFolder(options?: GetWorkspaceFolderOptions | null): Promise { - options ??= {}; - - if (options.uri instanceof Uri) { - const folder = workspace.getWorkspaceFolder(options.uri); - if (folder) return folder.uri; - } - - const folders = workspace.workspaceFolders; - if (!folders?.length) return; - if (folders.length === _one) return folders[_zero].uri; - - options.allowReadSelectedPath ??= true; - - if (options.allowReadSelectedPath) { - const [filePath] = await FileSystem.readSelectedPath(false); - if (filePath && filePath !== _dot) - return workspace.getWorkspaceFolder(Uri.file(filePath))?.uri; - } - - if (options.silent || options.token?.isCancellationRequested) return; - - const picked = await window.showWorkspaceFolderPick(); - if (picked) return picked.uri; - } - - static readSelectedPath(relative?: true): Promise - static readSelectedPath(relative: boolean): Promise - static async readSelectedPath(relative: boolean = true) { - await commands.executeCommand(relative ? "copyRelativeFilePath" : "copyFilePath"); - const copied = await env.clipboard.readText(); - return copied.split(_lineBreakRegexp); - } }