@@ -7,29 +7,29 @@ import { ensureArray } from '../helpers';
7
7
import type { PluginOptions } from '../plugin' ;
8
8
import { FSTree } from './FSTree' ;
9
9
10
- export type ReactAppChange = ( this : ReactApp ) => unknown ;
11
- export class ReactApp {
10
+ export type ReactPagesChange = ( this : ReactPages ) => unknown ;
11
+ export class ReactPages {
12
12
logger : Logger ;
13
13
fsTree : FSTree ;
14
- absApp : string ;
14
+ absPagesDir : string ;
15
15
16
16
constructor (
17
17
readonly options : PluginOptions ,
18
18
readonly config : ResolvedConfig ,
19
19
) {
20
20
this . logger = createLogger ( options . logLevel , { prefix : `[${ pkgName } ]` } ) ;
21
21
this . logger . info ( `new Options: ${ JSON . stringify ( options ) } ` ) ;
22
- this . absApp = nodePath . join ( this . config . root , options . appDir ) ;
22
+ this . absPagesDir = nodePath . join ( this . config . root , options . pagesDir ) ;
23
23
this . fsTree = new FSTree ( {
24
- cwd : nodePath . join ( '/' , options . appDir ) ,
24
+ cwd : nodePath . join ( '/' , options . pagesDir ) ,
25
25
logger : this . logger ,
26
26
resolveFileName : ( page , fileType ) => {
27
27
const fileNames = ensureArray ( this . options . fileNames [ fileType ] ) ;
28
28
if ( fileNames . length === 0 )
29
29
return ;
30
30
31
31
return fileNames . find ( ( fileName ) => {
32
- const absFile = nodePath . join ( this . absApp , page . dirName , fileName ) ;
32
+ const absFile = nodePath . join ( this . absPagesDir , page . dirName , fileName ) ;
33
33
return nodeFS . existsSync ( absFile ) && nodeFS . statSync ( absFile ) . isFile ( ) ;
34
34
} ) ;
35
35
} ,
@@ -46,39 +46,44 @@ export class ReactApp {
46
46
} ) ;
47
47
}
48
48
49
- get output ( ) {
49
+ async generate ( ) {
50
+ if ( ! this . isSetup ) {
51
+ this . logger . info ( `is not setup, try to setup again` ) ;
52
+ await this . setup ( ) ;
53
+ }
54
+
50
55
const output = this . fsTree . render ( ) ;
51
56
this . logger . info ( `Generated: ${ output } ` ) ;
52
57
return output ;
53
58
}
54
59
55
- private _inApp ( path : string ) {
56
- return path . startsWith ( this . absApp ) ;
60
+ private _inPagesDir ( path : string ) {
61
+ return path . startsWith ( this . absPagesDir ) ;
57
62
}
58
63
59
64
private devServer ?: ViteDevServer ;
60
65
private onChange ?: ( ) => unknown ;
61
- async connect ( devServer : ViteDevServer , onChange : ReactAppChange ) {
66
+ async watch ( devServer : ViteDevServer , onChange : ReactPagesChange ) {
62
67
this . devServer = devServer ;
63
68
this . onChange = onChange ;
64
69
this . logger . info ( `setup` ) ;
65
70
66
71
devServer . watcher . on ( 'unlink' , async ( path ) => {
67
- if ( ! this . _inApp ( path ) )
72
+ if ( ! this . _inPagesDir ( path ) )
68
73
return ;
69
74
70
75
await this . removeFile ( path ) ;
71
76
} ) ;
72
77
73
78
devServer . watcher . on ( 'add' , async ( path ) => {
74
- if ( ! this . _inApp ( path ) )
79
+ if ( ! this . _inPagesDir ( path ) )
75
80
return ;
76
81
77
82
await this . addFile ( path ) ;
78
83
} ) ;
79
84
80
85
devServer . watcher . on ( 'change' , async ( path ) => {
81
- if ( ! this . _inApp ( path ) )
86
+ if ( ! this . _inPagesDir ( path ) )
82
87
return ;
83
88
84
89
await this . updateFile ( path ) ;
@@ -87,16 +92,26 @@ export class ReactApp {
87
92
await this . setup ( ) ;
88
93
}
89
94
95
+ private _isSetup = false ;
96
+ get isSetup ( ) {
97
+ return this . _isSetup ;
98
+ }
99
+
90
100
async setup ( ) {
91
- this . logger . info ( `setup` ) ;
101
+ if ( this . isSetup ) {
102
+ this . logger . error ( `is already setup` ) ;
103
+ return ;
104
+ }
105
+
106
+ this . logger . info ( `setup react pages ${ this . absPagesDir } ` ) ;
92
107
93
108
const pageFileNames = ensureArray ( this . options . fileNames . page ) ;
94
109
95
110
if ( pageFileNames . length > 0 ) {
96
111
const pageFiles = await glob (
97
112
nodePath . join ( '**' , pageFileNames . length > 1 ? `{${ pageFileNames . join ( ',' ) } }` : pageFileNames [ 0 ] ) ,
98
113
{
99
- cwd : this . absApp ,
114
+ cwd : this . absPagesDir ,
100
115
nodir : true ,
101
116
dot : false ,
102
117
ignore : this . options . excludes ,
0 commit comments