@@ -12,22 +12,34 @@ import { fsUtil } from '@/infra/fs/fsUtil'
12
12
13
13
export async function postPull ( input : Post | PostTreeItem | Uri | undefined | null , showConfirm = true , mute = false ) {
14
14
const ctxList : CmdCtx [ ] = [ ]
15
+ let isFreshPull = false
15
16
input = input instanceof PostTreeItem ? input . post : input
16
17
if ( parsePostInput ( input ) && input . id > 0 ) {
17
- await handlePostInput ( input , ctxList )
18
+ const post = input
19
+ const path = PostFileMapManager . getFilePath ( post . id )
20
+ if ( path === undefined || ! ( await fsUtil . exists ( path ) ) ) {
21
+ isFreshPull = true
22
+ const uri = await buildLocalPostFileUri ( post , false )
23
+ await workspace . fs . writeFile ( uri , Buffer . from ( post . postBody ) )
24
+ await PostFileMapManager . updateOrCreate ( post . id , uri . path )
25
+ await handlePostInput ( input , ctxList , uri . path )
26
+ } else {
27
+ isFreshPull = ! ( await fsUtil . exists ( path ) )
28
+ await handlePostInput ( input , ctxList , path )
29
+ }
18
30
} else {
19
31
const uri = parseUriInput ( input )
20
32
if ( uri !== undefined ) handleUriInput ( uri , ctxList )
21
33
}
22
34
23
35
const fileName = resolveFileNames ( ctxList )
24
36
25
- if ( showConfirm && MarkdownCfg . isShowConfirmMsgWhenPullPost ( ) ) {
37
+ if ( showConfirm && ! isFreshPull && MarkdownCfg . isShowConfirmMsgWhenPullPost ( ) ) {
26
38
const answer = await Alert . warn (
27
39
'确认要拉取远程博文吗?' ,
28
40
{
29
41
modal : true ,
30
- detail : `本地文件 ${ fileName } 将被覆盖(可通过设置关闭对话框)` ,
42
+ detail : `本地文件「 ${ fileName } 」 将被覆盖(可通过设置关闭对话框)` ,
31
43
} ,
32
44
'确认'
33
45
)
@@ -38,7 +50,10 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
38
50
39
51
await update ( ctxList )
40
52
41
- if ( ! mute ) void Alert . info ( `本地文件 ${ resolveFileNames ( ctxList ) } 已更新` )
53
+ if ( ! mute ) {
54
+ if ( isFreshPull ) await Alert . info ( `博文已下载至本地:${ resolveFileNames ( ctxList ) } ` )
55
+ else await Alert . info ( `本地文件已更新: ${ resolveFileNames ( ctxList ) } ` )
56
+ }
42
57
}
43
58
44
59
type InputType = Post | Uri | undefined | null
@@ -49,16 +64,7 @@ type CmdCtx = {
49
64
50
65
const parsePostInput = ( input : InputType ) : input is Post => input instanceof Post
51
66
52
- async function handlePostInput ( post : Post , contexts : CmdCtx [ ] ) {
53
- const path = PostFileMapManager . getFilePath ( post . id )
54
- // 本地没有博文或关联到的文件不存在
55
- if ( path === undefined || ! ( await fsUtil . exists ( path ) ) ) {
56
- const uri = await buildLocalPostFileUri ( post , false )
57
- await workspace . fs . writeFile ( uri , Buffer . from ( post . postBody ) )
58
- await PostFileMapManager . updateOrCreate ( post . id , uri . path )
59
- return
60
- }
61
-
67
+ async function handlePostInput ( post : Post , contexts : CmdCtx [ ] , path : string ) {
62
68
await revealPostListItem ( post )
63
69
contexts . push ( { postId : post . id , fileUri : Uri . file ( path ) } )
64
70
}
@@ -91,5 +97,5 @@ async function update(contexts: CmdCtx[]) {
91
97
92
98
function resolveFileNames ( ctxList : CmdCtx [ ] ) {
93
99
const arr = ctxList . map ( x => path . basename ( x . fileUri . fsPath ) )
94
- return `" ${ arr . join ( '" , ' ) } `
100
+ return `${ arr . join ( ', ' ) } `
95
101
}
0 commit comments