@@ -3,16 +3,43 @@ import { execCmd } from '@/infra/cmd'
3
3
import { Post } from '@/model/post'
4
4
import { PostFileMapManager } from '@/service/post/post-file-map'
5
5
import { LocalPost } from '@/service/local-post'
6
+ import { fsUtil } from '@/infra/fs/fsUtil'
7
+ import { postPull } from './post-pull'
8
+ import { Alert } from '@/infra/alert'
6
9
7
- export async function openPostFile ( post : LocalPost | Post | string , options ?: TextDocumentShowOptions ) {
10
+ export async function openPostFile (
11
+ post : LocalPost | Post | string ,
12
+ options ?: TextDocumentShowOptions ,
13
+ autoPull = false
14
+ ) {
8
15
let filePath = ''
9
- if ( post instanceof LocalPost ) filePath = post . filePath
10
- else if ( post instanceof Post ) filePath = PostFileMapManager . getFilePath ( post . id ) ?? ''
11
- else filePath = post
16
+ if ( post instanceof LocalPost ) {
17
+ filePath = post . filePath
18
+ } else if ( post instanceof Post ) {
19
+ filePath = PostFileMapManager . getFilePath ( post . id ) ?? ''
20
+ if ( autoPull ) if ( ! ( await fsUtil . exists ( filePath ) ) ) await postPull ( post , false , true )
21
+ } else {
22
+ filePath = post
23
+ }
12
24
13
25
if ( filePath === '' ) return
14
26
15
- await execCmd (
27
+ if ( ! ( await fsUtil . exists ( filePath ) ) ) await new Promise ( f => setTimeout ( f , 200 ) )
28
+
29
+ try {
30
+ await openFile ( filePath , options )
31
+ } catch ( e ) {
32
+ await new Promise ( f => setTimeout ( f , 500 ) )
33
+ try {
34
+ await openFile ( filePath , options )
35
+ } catch ( e2 ) {
36
+ await Alert . err ( `打开本地博文文件失败,重新操作通常可恢复,错误信息: ${ < string > e2 } ` )
37
+ }
38
+ }
39
+ }
40
+
41
+ function openFile ( filePath : string , options ?: TextDocumentShowOptions ) {
42
+ return execCmd (
16
43
'vscode.open' ,
17
44
Uri . file ( filePath ) ,
18
45
Object . assign ( { preview : false } as TextDocumentShowOptions , options ?? { } )
0 commit comments