Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export namespace Workspace {
if (firstUri === undefined) return

await WorkspaceCfg.setWorkspaceUri(firstUri)

void Alert.info(`工作空间成功修改为: "${WorkspaceCfg.getWorkspaceUri().fsPath}"`)
}
}
19 changes: 13 additions & 6 deletions src/ctx/cfg/workspace.ts
Original file line number Diff line number Diff line change
@@ -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<string>('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<void> {
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)
}
}