1
1
import type { SFCDescriptor , SFCParseOptions } from '@vue/compiler-sfc'
2
+ import type { TabBarItem } from './config'
2
3
import type { PageContext } from './context'
3
4
import type { PageMetaDatum , PagePath , RouteBlockLang , UserPageMeta } from './types'
4
5
import { readFileSync } from 'node:fs'
@@ -15,60 +16,76 @@ export class Page {
15
16
ctx : PageContext
16
17
17
18
path : PagePath
19
+ uri : string
18
20
19
- private rawOptions : string = ''
20
- private options : PageMetaDatum | undefined
21
+ changed : boolean = true
22
+
23
+ private raw : string = ''
24
+ private meta : UserPageMeta | undefined
21
25
22
26
constructor ( ctx : PageContext , path : PagePath ) {
23
27
this . ctx = ctx
24
28
this . path = path
29
+ this . uri = normalizePath ( path . relativePath . replace ( extname ( path . relativePath ) , '' ) )
25
30
}
26
31
27
- public async getPageMeta ( forceUpdate = false ) {
28
- if ( forceUpdate || ! this . options ) {
29
- await this . readPageMeta ( )
32
+ public async getPageMeta ( forceUpdate = false ) : Promise < PageMetaDatum > {
33
+ if ( forceUpdate || ! this . meta ) {
34
+ await this . read ( )
30
35
}
31
- return this . options !
32
- }
33
36
34
- public async hasChanged ( ) {
35
- const { hasChanged } = await this . readPageMeta ( )
36
- return hasChanged
37
+ const { path, tabBar : _ , ...others } = this . meta !
38
+
39
+ return {
40
+ path : path ?? this . uri ,
41
+ ...others ,
42
+ }
37
43
}
38
44
39
- public async readPageMeta ( ) {
40
- try {
41
- const { relativePath } = this . path
45
+ public async getTabBar ( forceUpdate = false ) : Promise < TabBarItem & { index : number } | undefined > {
46
+ if ( forceUpdate || ! this . meta ) {
47
+ await this . read ( )
48
+ }
42
49
43
- const { path, ...others } = await this . readPageMetaFromFile ( )
44
- this . options = {
45
- path : path ?? normalizePath ( relativePath . replace ( extname ( relativePath ) , '' ) ) ,
46
- ...others ,
47
- }
50
+ const { tabBar } = this . meta !
48
51
49
- const raw = ( this . options ? JSON . stringify ( this . options ) : '' )
50
- const hasChanged = this . rawOptions !== raw
51
- this . rawOptions = raw
52
- return {
53
- options : this . options ,
54
- hasChanged,
55
- }
52
+ if ( tabBar === undefined ) {
53
+ return undefined
56
54
}
57
- catch ( err : any ) {
58
- throw new Error ( `Read page options fail in ${ this . path . relativePath } \n${ err . message } ` )
55
+
56
+ return {
57
+ ...tabBar ,
58
+ pagePath : tabBar . pagePath || this . uri ,
59
+ index : tabBar . index || 0 ,
59
60
}
60
61
}
61
62
63
+ public hasChanged ( ) {
64
+ return this . changed
65
+ }
66
+
67
+ public async read ( ) {
68
+ this . meta = await this . readPageMetaFromFile ( )
69
+ const raw = ( this . meta ? JSON . stringify ( this . meta ) : '' )
70
+ this . changed = this . raw !== raw
71
+ this . raw = raw
72
+ }
73
+
62
74
private async readPageMetaFromFile ( ) : Promise < UserPageMeta > {
63
- const content = readFileSync ( this . path . absolutePath , 'utf-8' )
64
- const sfc = parseSFC ( content , { filename : this . path . absolutePath } )
75
+ try {
76
+ const content = readFileSync ( this . path . absolutePath , 'utf-8' )
77
+ const sfc = parseSFC ( content , { filename : this . path . absolutePath } )
65
78
66
- const meta = await tryPageMetaFromMacro ( sfc )
67
- if ( meta ) {
68
- return meta
69
- }
79
+ const meta = await tryPageMetaFromMacro ( sfc )
80
+ if ( meta ) {
81
+ return meta
82
+ }
70
83
71
- return tryPageMetaFromCustomBlock ( sfc , this . ctx . options . routeBlockLang )
84
+ return tryPageMetaFromCustomBlock ( sfc , this . ctx . options . routeBlockLang )
85
+ }
86
+ catch ( err : any ) {
87
+ throw new Error ( `Read page meta fail in ${ this . path . relativePath } \n${ err . message } ` )
88
+ }
72
89
}
73
90
}
74
91
0 commit comments