Skip to content

Commit e0e2224

Browse files
committed
fix: unable to set workspace
1 parent cdcd2d5 commit e0e2224

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/cmd/workspace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export namespace Workspace {
3636
if (firstUri === undefined) return
3737

3838
await WorkspaceCfg.setWorkspaceUri(firstUri)
39+
3940
void Alert.info(`工作空间成功修改为: "${WorkspaceCfg.getWorkspaceUri().fsPath}"`)
4041
}
4142
}

src/ctx/cfg/workspace.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import { PlatformCfg } from '@/ctx/cfg/platform'
22
import getPlatformCfg = PlatformCfg.getPlatformCfg
33
import os from 'os'
4-
import fs from 'fs'
5-
import { ConfigurationTarget, Uri } from 'vscode'
4+
import { ConfigurationTarget, Uri, workspace } from 'vscode'
5+
import { Alert } from '@/infra/alert'
66

77
export namespace WorkspaceCfg {
88
export function getWorkspaceUri() {
99
const path = getPlatformCfg().get<string>('workspace') ?? '~/Documents/Cnblogs'
1010
const absPath = path.replace('~', os.homedir())
11-
1211
return Uri.file(absPath)
1312
}
1413

15-
export function setWorkspaceUri(uri: Uri) {
14+
export async function setWorkspaceUri(uri: Uri): Promise<void> {
1615
const fsPath = uri.fsPath
17-
if (fs.existsSync(fsPath) || uri.scheme !== 'file') throw Error('Invalid Uri')
16+
17+
if (uri.scheme !== 'file') throw Error(`Invalid Uri: ${uri.path}`)
18+
19+
try {
20+
await workspace.fs.stat(uri)
21+
} catch (e) {
22+
void Alert.err(`Invalid Uri: ${uri.path}`)
23+
throw e
24+
}
1825

1926
const cfgTarget = ConfigurationTarget.Global
20-
return getPlatformCfg()?.update('workspace', fsPath, cfgTarget)
27+
await getPlatformCfg()?.update('workspace', fsPath, cfgTarget)
2128
}
2229
}

0 commit comments

Comments
 (0)