diff --git a/src/cmd/workspace.ts b/src/cmd/workspace.ts index 1169177b..8ca35bc7 100644 --- a/src/cmd/workspace.ts +++ b/src/cmd/workspace.ts @@ -36,6 +36,7 @@ export namespace Workspace { if (firstUri === undefined) return await WorkspaceCfg.setWorkspaceUri(firstUri) + void Alert.info(`工作空间成功修改为: "${WorkspaceCfg.getWorkspaceUri().fsPath}"`) } } diff --git a/src/ctx/cfg/workspace.ts b/src/ctx/cfg/workspace.ts index b21e3a32..a2c8a8c2 100644 --- a/src/ctx/cfg/workspace.ts +++ b/src/ctx/cfg/workspace.ts @@ -1,22 +1,29 @@ import { PlatformCfg } from '@/ctx/cfg/platform' import getPlatformCfg = PlatformCfg.getPlatformCfg import os from 'os' -import fs from 'fs' -import { ConfigurationTarget, Uri } from 'vscode' +import { ConfigurationTarget, Uri, workspace } from 'vscode' +import { Alert } from '@/infra/alert' export namespace WorkspaceCfg { export function getWorkspaceUri() { const path = getPlatformCfg().get('workspace') ?? '~/Documents/Cnblogs' const absPath = path.replace('~', os.homedir()) - return Uri.file(absPath) } - export function setWorkspaceUri(uri: Uri) { + export async function setWorkspaceUri(uri: Uri): Promise { const fsPath = uri.fsPath - if (fs.existsSync(fsPath) || uri.scheme !== 'file') throw Error('Invalid Uri') + + if (uri.scheme !== 'file') throw Error(`Invalid Uri: ${uri.path}`) + + try { + await workspace.fs.stat(uri) + } catch (e) { + void Alert.err(`Invalid Uri: ${uri.path}`) + throw e + } const cfgTarget = ConfigurationTarget.Global - return getPlatformCfg()?.update('workspace', fsPath, cfgTarget) + await getPlatformCfg()?.update('workspace', fsPath, cfgTarget) } }