Skip to content

Commit 57a64b6

Browse files
authored
refactor: post pull all (#282)
1 parent 172f3a2 commit 57a64b6

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

build.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,27 @@ async function buildExtension() {
3333
copyPlugin({
3434
src: 'src/assets',
3535
dest: `${OUT_DIR}/assets`,
36+
errorOnExist: false,
3637
}),
3738
copyPlugin({
3839
src: 'node_modules/@mapbox/node-pre-gyp',
3940
dest: `${OUT_DIR}/node_modules/@mapbox/node-pre-gyp`,
41+
errorOnExist: false,
4042
}),
4143
copyPlugin({
4244
src: 'node_modules/sequelize',
4345
dest: `${OUT_DIR}/node_modules/sequelize`,
46+
errorOnExist: false,
4447
}),
4548
copyPlugin({
4649
src: 'node_modules/@fluentui/font-icons-mdl2/fonts/',
4750
dest: `${OUT_DIR}/assets/fonts`,
51+
errorOnExist: false,
4852
}),
4953
copyPlugin({
5054
src: 'src/wasm/rs_bg.wasm',
5155
dest: `${OUT_DIR}/rs_bg.wasm`,
56+
errorOnExist: false,
5257
}),
5358
],
5459
}
@@ -72,6 +77,7 @@ async function buildUI(...apps) {
7277
copyPlugin({
7378
src: `${srcPath}${app}/index.html`,
7479
dest: `${outPath}${app}/index.html`,
80+
errorOnExist: false,
7581
}),
7682
],
7783
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
import { Uri } from 'vscode'
2-
import { Post } from '@/model/post'
32
import { Alert } from '@/infra/alert'
43
import { PostFileMapManager } from '@/service/post/post-file-map'
54
import { openPostFile } from './open-post-file'
6-
import sanitizeFileName from 'sanitize-filename'
7-
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
85
import { fsUtil } from '@/infra/fs/fsUtil'
96
import { postPull } from './post-pull'
107

11-
export function buildLocalPostFileUri(post: Post, appendToFileName = ''): Uri {
12-
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
13-
const ext = `${post.isMarkdown ? 'md' : 'html'}`
14-
let postTitle = sanitizeFileName(post.title)
15-
if (/\.\d+$/.test(postTitle)) postTitle += '_'
16-
return Uri.joinPath(workspaceUri, `${postTitle}${appendToFileName}.${post.id}.${ext}`)
17-
}
18-
198
export async function openPostInVscode(postId: number): Promise<Uri | undefined> {
209
const mappedPostFilePath = await getMappedPostFilePath(postId)
2110

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { Alert } from '@/infra/alert'
33
import { PostFileMapManager } from '@/service/post/post-file-map'
44
import { basename } from 'path'
55
import { ProgressLocation, Uri, window, workspace } from 'vscode'
6-
import { buildLocalPostFileUri } from '@/cmd/post-list/open-post-in-vscode'
76
import { UserService } from '@/service/user.service'
87
import { fsUtil } from '@/infra/fs/fsUtil'
9-
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
108

119
enum ConflictStrategy {
1210
ask,
@@ -65,22 +63,22 @@ export async function postPullAll() {
6563

6664
p.report({ message: `${post.title}` })
6765

68-
const path = PostFileMapManager.getFilePath(post.id)
66+
const fileUri = PostFileMapManager.ensurePostFileUri(post)
67+
if (fileUri == null) {
68+
console.warn('fileUri is null')
69+
return
70+
}
6971

70-
// 本地没有博文或关联到的文件不存在
71-
if (
72-
path === undefined ||
73-
!(await fsUtil.exists(path)) ||
74-
path.indexOf(WorkspaceCfg.getWorkspaceUri().fsPath) < 0
75-
) {
76-
const uri = buildLocalPostFileUri(post)
72+
if (!(await fsUtil.exists(fileUri.fsPath))) {
7773
const buf = Buffer.from(post.postBody)
78-
await workspace.fs.writeFile(uri, buf)
79-
await PostFileMapManager.updateOrCreate(post.id, uri.path)
74+
await workspace.fs.writeFile(fileUri, buf)
75+
await PostFileMapManager.updateOrCreate(post.id, fileUri.path)
76+
console.info(`Writing post ${post.id} to file '${fileUri.fsPath}'`)
8077
continue
8178
}
8279

83-
const fileName = basename(path)
80+
const fsPath = fileUri.fsPath
81+
const fileName = basename(fsPath)
8482

8583
// 存在冲突
8684
if (strategy === ConflictStrategy.ask) {
@@ -90,17 +88,17 @@ export async function postPullAll() {
9088
)
9189

9290
if (answer === '覆盖') {
93-
await overwriteFile(path, post.postBody)
91+
await overwriteFile(fsPath, post.postBody)
9492
} else if (answer === '退出') {
9593
break
9694
} else if (answer === '跳过所有冲突') {
9795
strategy = ConflictStrategy.skip
9896
} else if (answer === '覆盖全部') {
9997
strategy = ConflictStrategy.overwrite
100-
await overwriteFile(path, post.postBody)
98+
await overwriteFile(fsPath, post.postBody)
10199
} // answer eq undefined or '跳过', do nothing.
102100
} else if (strategy === ConflictStrategy.overwrite) {
103-
await overwriteFile(path, post.postBody)
101+
await overwriteFile(fsPath, post.postBody)
104102
} // strategy eq ConflictStrategy.skip, do nothing.
105103
}
106104

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Uri, window, workspace } from 'vscode'
22
import { Post } from '@/model/post'
33
import { PostFileMapManager } from '@/service/post/post-file-map'
4-
import { buildLocalPostFileUri } from '@/cmd/post-list/open-post-in-vscode'
54
import { PostService } from '@/service/post/post'
65
import { Alert } from '@/infra/alert'
76
import path from 'path'
@@ -48,11 +47,11 @@ export async function postPull(input: InputType, showConfirm = true, mute = fals
4847
let uriPath = PostFileMapManager.getFilePath(post.id)
4948
let fileUri: Uri
5049
if (uriPath == null) {
51-
fileUri = buildLocalPostFileUri(post)
50+
fileUri = PostFileMapManager.buildLocalPostFileUri(post)
5251
} else {
5352
// replace fsPath with uriPath
5453
if (!uriPath.startsWith('/')) uriPath = Uri.file(uriPath).path
55-
if (!PostFileMapManager.isInWorkspace(uriPath)) fileUri = buildLocalPostFileUri(post)
54+
if (!PostFileMapManager.isInWorkspace(uriPath)) fileUri = PostFileMapManager.buildLocalPostFileUri(post)
5655
else fileUri = Uri.parse(uriPath)
5756
}
5857

src/service/post/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export async function createPost() {
2020
return
2121
},
2222
})
23-
if (title === undefined) return
23+
24+
if (title == null) return
2425

2526
const filePath = path.join(workspacePath, `${title}.md`)
2627

src/service/post/post-file-map.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { LocalState } from '@/ctx/local-state'
44
import { Uri } from 'vscode'
55
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
66
import { r } from '@/infra/convert/string-literal'
7+
import { Post } from '@/model/post'
8+
import sanitizeFileName from 'sanitize-filename'
79

810
const validatePostFileMap = (map: PostFileMap) => map[0] >= 0 && map[1] !== ''
911
export type PostFileMap = [postId: number, filePath: string]
@@ -18,6 +20,20 @@ function isUriPath(path: string) {
1820
}
1921

2022
export namespace PostFileMapManager {
23+
export function ensurePostFileUri(post: Post) {
24+
let fileUri = PostFileMapManager.getFileUri(post.id)
25+
if (fileUri == null || !isInWorkspace(fileUri.path)) fileUri = buildLocalPostFileUri(post)
26+
return fileUri
27+
}
28+
29+
export function buildLocalPostFileUri(post: Post, appendToFileName = ''): Uri {
30+
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
31+
const ext = `${post.isMarkdown ? 'md' : 'html'}`
32+
let postTitle = sanitizeFileName(post.title)
33+
if (/\.\d+$/.test(postTitle)) postTitle += '_'
34+
return Uri.joinPath(workspaceUri, `${postTitle}${appendToFileName}.${post.id}.${ext}`)
35+
}
36+
2137
export async function updateOrCreateMany(
2238
arg:
2339
| {
@@ -68,12 +84,16 @@ export namespace PostFileMapManager {
6884
return map
6985
}
7086

71-
export function getFilePath(postId: number) {
87+
export function getFilePath(postId: number): string | undefined {
88+
return getFileUri(postId)?.fsPath
89+
}
90+
91+
export function getFileUri(postId: number): Uri | undefined {
7292
const map = findByPostId(postId)
73-
if (map === undefined) return
93+
if (map == null) return
7494
const path = map[1]
7595
if (path === '') return
76-
return isUriPath(path) ? Uri.parse(path).fsPath : path
96+
return isUriPath(path) ? Uri.parse(path) : Uri.file(path)
7797
}
7898

7999
export function getPostId(filePath: string): number | undefined {

0 commit comments

Comments
 (0)