Skip to content

Commit aa6fe8e

Browse files
authored
Merge pull request #204 from cnblogs/replace-existssync
refactor: replace fs.existsSync with fs.stat
2 parents 910d7f9 + 4e986b8 commit aa6fe8e

File tree

12 files changed

+40
-24
lines changed

12 files changed

+40
-24
lines changed

src/cmd/blog-export/download.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { promisify } from 'util'
1111
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
1212
import AdmZip from 'adm-zip'
1313
import { setCtx } from '@/ctx/global-ctx'
14+
import { fsUtil } from '@/infra/fs/fsUtil'
1415

1516
export async function downloadBlogExport(treeItem?: BlogExportRecordTreeItem) {
1617
if (!(treeItem instanceof BlogExportRecordTreeItem)) return
@@ -24,7 +25,7 @@ export async function downloadBlogExport(treeItem?: BlogExportRecordTreeItem) {
2425
const nonZipFilePath = path.join(targetDir, treeItem.record.fileName)
2526
const zipFilePath = nonZipFilePath + '.zip'
2627
const downloadStream = BlogExportApi.download(blogId, exportId)
27-
const isFileExist = fs.existsSync(zipFilePath)
28+
const isFileExist = await fsUtil.exists(zipFilePath)
2829

2930
await extTreeViews.blogExport.reveal(treeItem, { expand: true })
3031

src/cmd/blog-export/open-local.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Alert } from '@/infra/alert'
66
import { DownloadedExportStore } from '@/service/downloaded-export.store'
77
import { BlogExportProvider } from '@/tree-view/provider/blog-export-provider'
88
import { BlogExportRecordsStore } from '@/service/blog-export/blog-export-records.store'
9+
import { fsUtil } from '@/infra/fs/fsUtil'
910

1011
const defaultOptions = { confirmUnzip: true }
1112

@@ -51,7 +52,7 @@ export async function openLocalExport(opts: Partial<typeof defaultOptions> = def
5152
}
5253
const dbFileName = path.basename(dbFilePath)
5354

54-
if (!fs.existsSync(dbFilePath)) return void Alert.warn('文件不存在')
55+
if (!(await fsUtil.exists(dbFilePath))) return void Alert.warn('文件不存在')
5556

5657
const treeProvider = BlogExportProvider.optionalInstance
5758
const dbFileSize = (await promisify(fs.stat)(dbFilePath)).size

src/cmd/post-cat/update-post-cat-treeview.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs'
21
import { ProgressLocation, window, Uri, workspace } from 'vscode'
32
import { PostCat } from '@/model/post-cat'
43
import { PostCatService } from '@/service/post/post-cat'
@@ -9,6 +8,7 @@ import { Alert } from '@/infra/alert'
98
import { PostCatTreeItem } from '@/tree-view/model/post-category-tree-item'
109
import { extTreeViews } from '@/tree-view/tree-view-register'
1110
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
11+
import { fsUtil } from '@/infra/fs/fsUtil'
1212

1313
export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) {
1414
let category: PostCat
@@ -39,8 +39,8 @@ export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) {
3939
// 如果选择了createLocalPostFileWithCategory模式且本地有该目录,则重命名该目录
4040
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
4141
const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory()
42-
const uri = Uri.joinPath(workspaceUri, category.title).fsPath
43-
const isFileExist = fs.existsSync(uri)
42+
const path = Uri.joinPath(workspaceUri, category.title).fsPath
43+
const isFileExist = await fsUtil.exists(path)
4444
if (shouldCreateLocalPostFileWithCategory && isFileExist) {
4545
const oldUri = Uri.joinPath(workspaceUri, category.title)
4646
const newUri = Uri.joinPath(workspaceUri, addDto.title)

src/cmd/post-list/modify-post-setting.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { PostService } from '@/service/post/post'
55
import { PostFileMapManager } from '@/service/post/post-file-map'
66
import { revealPostListItem } from '@/service/post/post-list-view'
77
import { PostCfgPanel } from '@/service/post/post-cfg-panel'
8-
import fs from 'fs'
98
import { LocalPost } from '@/service/local-post'
109
import { saveFilePendingChanges } from '@/infra/save-file-pending-changes'
1110
import { postDataProvider } from '@/tree-view/provider/post-data-provider'
1211
import { PostTreeItem } from '@/tree-view/model/post-tree-item'
1312
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
13+
import { fsUtil } from '@/infra/fs/fsUtil'
1414

1515
export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
1616
let post: Post | undefined
@@ -43,7 +43,7 @@ export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
4343
postCategoryDataProvider.onPostUpdated({ refreshPost: false, postIds: [id] })
4444
},
4545
beforeUpdate: async post => {
46-
if (localFilePath !== undefined && fs.existsSync(localFilePath)) {
46+
if (localFilePath !== undefined && (await fsUtil.exists(localFilePath))) {
4747
await saveFilePendingChanges(localFilePath)
4848
post.postBody = await new LocalPost(localFilePath).readAllText()
4949
}

src/cmd/post-list/open-post-in-vscode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs'
21
import path from 'path'
32
import { FileSystemError, Uri, workspace } from 'vscode'
43
import { Post } from '@/model/post'
@@ -10,6 +9,7 @@ import { PostCatService } from '@/service/post/post-cat'
109
import sanitizeFileName from 'sanitize-filename'
1110
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
1211
import { PostCatCfg } from '@/ctx/cfg/post-cat'
12+
import { fsUtil } from '@/infra/fs/fsUtil'
1313

1414
export async function buildLocalPostFileUri(post: Post, includePostId = false): Promise<Uri> {
1515
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
@@ -39,7 +39,7 @@ export async function buildLocalPostFileUri(post: Post, includePostId = false):
3939
export async function openPostInVscode(postId: number, forceUpdateLocalPostFile = false): Promise<Uri | false> {
4040
let mappedPostFilePath = PostFileMapManager.getFilePath(postId)
4141

42-
const isFileExist = mappedPostFilePath !== undefined && fs.existsSync(mappedPostFilePath)
42+
const isFileExist = mappedPostFilePath !== undefined && (await fsUtil.exists(mappedPostFilePath))
4343
if (mappedPostFilePath !== undefined && isFileExist && !forceUpdateLocalPostFile) {
4444
await openPostFile(mappedPostFilePath)
4545
return Uri.file(mappedPostFilePath)
@@ -58,7 +58,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile
5858

5959
// 博文尚未关联到本地文件的情况
6060
// 本地存在和博文同名的文件, 询问用户是要覆盖还是同时保留两者
61-
if (mappedPostFilePath === undefined && fs.existsSync(fileUri.fsPath)) {
61+
if (mappedPostFilePath === undefined && (await fsUtil.exists(fileUri.fsPath))) {
6262
const opt = ['保留本地文件并以博文 ID 为文件名新建另一个文件', '覆盖本地文件']
6363
const selected = await Alert.info(
6464
`无法建立博文与本地文件的关联, 文件名冲突`,

src/cmd/post-list/post-pull-all.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { PostService } from '@/service/post/post'
22
import { Alert } from '@/infra/alert'
33
import { PostFileMapManager } from '@/service/post/post-file-map'
4-
import fs from 'fs'
54
import { basename } from 'path'
65
import { ProgressLocation, Uri, window, workspace } from 'vscode'
76
import { buildLocalPostFileUri } from '@/cmd/post-list/open-post-in-vscode'
87
import { UserService } from '@/service/user-info'
8+
import { fsUtil } from '@/infra/fs/fsUtil'
99

1010
enum ConflictStrategy {
1111
ask,
@@ -67,7 +67,7 @@ export async function postPullAll() {
6767
const path = PostFileMapManager.getFilePath(post.id)
6868

6969
// 本地没有博文或关联到的文件不存在
70-
if (path === undefined || !fs.existsSync(path)) {
70+
if (path === undefined || !(await fsUtil.exists(path))) {
7171
const uri = await buildLocalPostFileUri(post, false)
7272
const buf = Buffer.from(post.postBody)
7373
await workspace.fs.writeFile(uri, buf)

src/cmd/post-list/post-pull.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import path from 'path'
88
import { revealPostListItem } from '@/service/post/post-list-view'
99
import { PostTreeItem } from '@/tree-view/model/post-tree-item'
1010
import { MarkdownCfg } from '@/ctx/cfg/markdown'
11-
import fs from 'fs'
11+
import { fsUtil } from '@/infra/fs/fsUtil'
1212

1313
export async function postPull(input: Post | PostTreeItem | Uri | undefined | null) {
1414
const ctxList: CmdCtx[] = []
@@ -52,7 +52,7 @@ const parsePostInput = (input: InputType): input is Post => input instanceof Pos
5252
async function handlePostInput(post: Post, contexts: CmdCtx[]) {
5353
const path = PostFileMapManager.getFilePath(post.id)
5454
// 本地没有博文或关联到的文件不存在
55-
if (path === undefined || !fs.existsSync(path)) {
55+
if (path === undefined || !(await fsUtil.exists(path))) {
5656
const uri = await buildLocalPostFileUri(post, false)
5757
await workspace.fs.writeFile(uri, Buffer.from(post.postBody))
5858
await PostFileMapManager.updateOrCreate(post.id, uri.path)

src/cmd/post-list/upload-post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import { PostTreeItem } from '@/tree-view/model/post-tree-item'
1515
import { MarkdownCfg } from '@/ctx/cfg/markdown'
1616
import { PostListView } from '@/cmd/post-list/post-list-view'
1717
import { LocalPost } from '@/service/local-post'
18-
import fs from 'fs'
1918
import { extractImg } from '@/service/extract-img/extract-img'
2019
import { dirname } from 'path'
2120
import { Workspace } from '@/cmd/workspace'
21+
import { fsUtil } from '@/infra/fs/fsUtil'
2222

2323
async function parseFileUri(fileUri?: Uri) {
2424
if (fileUri !== undefined && fileUri.scheme !== 'file') return undefined
@@ -64,7 +64,7 @@ export async function saveLocalPost(localPost: LocalPost) {
6464
beforeUpdate: async postToSave => {
6565
await saveFilePendingChanges(localPost.filePath)
6666

67-
if (!fs.existsSync(localPost.filePath)) {
67+
if (!(await fsUtil.exists(localPost.filePath))) {
6868
void Alert.warn('本地文件已删除, 无法新建博文')
6969
return false
7070
}

src/infra/fs/fsUtil.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Uri, workspace } from 'vscode'
2+
3+
export namespace fsUtil {
4+
export async function exists(path: string) {
5+
try {
6+
await workspace.fs.stat(Uri.file(path))
7+
return true
8+
} catch (e) {
9+
return false
10+
}
11+
}
12+
}

src/service/downloaded-export.store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DownloadedBlogExport } from '@/model/blog-export'
2-
import fs from 'fs'
32
import { take } from 'lodash-es'
43
import { LocalState } from '@/ctx/local-state'
4+
import { fsUtil } from '@/infra/fs/fsUtil'
55

66
const listKey = 'downloadExports'
77
const metadataKey = 'downloadedExport-'
@@ -30,8 +30,8 @@ export namespace DownloadedExportStore {
3030

3131
if (prune) {
3232
const prunedItems: DownloadedBlogExport[] = []
33-
items = items.filter(x => {
34-
const isExist = fs.existsSync(x.filePath)
33+
items = items.filter(async x => {
34+
const isExist = await fsUtil.exists(x.filePath)
3535
if (!isExist) {
3636
prunedItems.push(x)
3737
return false
@@ -69,7 +69,7 @@ export namespace DownloadedExportStore {
6969
let item = LocalState.getState(key) as DownloadedBlogExport | undefined
7070

7171
if (prune && item !== undefined) {
72-
const isExist = fs.existsSync(item.filePath)
72+
const isExist = await fsUtil.exists(item.filePath)
7373
if (!isExist) {
7474
item = undefined
7575
await updateExport(id, undefined)

0 commit comments

Comments
 (0)