@@ -6,7 +6,6 @@ import type { ComponentType } from 'react';
66import { glob } from 'tinyglobby' ;
77import type { PluginDriver } from '../PluginDriver' ;
88import { PUBLIC_DIR } from '../constants' ;
9- import { getPageKey , normalizePath } from '../utils' ;
109import { RoutePage } from './RoutePage' ;
1110import { getRoutePathParts , normalizeRoutePath } from './normalizeRoutePath' ;
1211
@@ -69,13 +68,13 @@ export class RouteService {
6968 runtimeTempDir ,
7069 pluginDriver ,
7170 ) ;
71+ RouteService . __instance__ = routeService ;
7272 await routeService . #init( ) ;
7373 await pluginDriver . routeServiceGenerated ( routeService ) ;
74- RouteService . __instance__ = routeService ;
7574 return routeService ;
7675 }
7776
78- constructor (
77+ private constructor (
7978 scanDir : string ,
8079 userConfig : UserConfig ,
8180 tempDir : string ,
@@ -127,23 +126,10 @@ export class RouteService {
127126 ) . sort ( ) ;
128127
129128 files . forEach ( filePath => {
130- const fileRelativePath = normalizePath (
131- path . relative ( this . #scanDir, filePath ) ,
132- ) ;
133- const { routePath, lang, version } =
134- this . normalizeRoutePath ( fileRelativePath ) ;
135- const absolutePath = path . join ( this . #scanDir, fileRelativePath ) ;
136-
137- const routeMeta = {
138- routePath,
139- absolutePath : normalizePath ( absolutePath ) ,
140- relativePath : fileRelativePath ,
141- pageName : getPageKey ( fileRelativePath ) ,
142- lang,
143- version,
144- } ;
145- this . addRoute ( routeMeta ) ;
129+ const routePage = RoutePage . create ( filePath , this . #scanDir) ;
130+ this . addRoute ( routePage ) ;
146131 } ) ;
132+
147133 // 2. external pages added by plugins
148134 const externalPages = await this . #pluginDriver. addPages ( ) ;
149135
@@ -152,38 +138,40 @@ export class RouteService {
152138 const { routePath, content, filepath } = route ;
153139 // case1: specify the filepath
154140 if ( filepath ) {
155- const routeMeta = this . #generateRouteMeta( routePath , filepath ) ;
156- this . addRoute ( routeMeta ) ;
141+ const routePage = RoutePage . createFromExternal (
142+ routePath ,
143+ filepath ,
144+ this . #scanDir,
145+ ) ;
146+ this . addRoute ( routePage ) ;
157147 return ;
158148 }
159149 // case2: specify the content
160150 if ( content ) {
161151 const filepath = await this . #writeTempFile( index , content ) ;
162- const routeMeta = this . #generateRouteMeta( routePath , filepath ) ;
163- this . addRoute ( routeMeta ) ;
152+ const routePage = RoutePage . createFromExternal (
153+ routePath ,
154+ filepath ,
155+ this . #scanDir,
156+ ) ;
157+ this . addRoute ( routePage ) ;
164158 }
165159 } ) ,
166160 ) ;
167161
168162 await this . #pluginDriver. routeGenerated ( this . getRoutes ( ) ) ;
169163 }
170164
171- async addRoute ( routeMeta : RouteMeta ) {
172- const { routePath } = routeMeta ;
165+ async addRoute ( routePage : RoutePage ) : Promise < void > {
166+ const {
167+ routeMeta : { routePath } ,
168+ } = routePage ;
173169 if ( this . routeData . has ( routePath ) ) {
174170 throw new Error ( `routePath ${ routePath } has already been added` ) ;
175171 }
176-
177- const routePage = RoutePage . create ( routeMeta ) ;
178172 this . routeData . set ( routePath , routePage ) ;
179173 }
180174
181- removeRoute ( filePath : string ) : void {
182- const fileRelativePath = path . relative ( this . #scanDir, filePath ) ;
183- const { routePath } = this . normalizeRoutePath ( fileRelativePath ) ;
184- this . routeData . delete ( routePath ) ;
185- }
186-
187175 getRoutes ( ) : RouteMeta [ ] {
188176 return Array . from ( this . routeData . values ( ) ) . map ( i => i . routeMeta ) ;
189177 }
@@ -260,22 +248,6 @@ ${routeMeta
260248 return tempFilePath ;
261249 }
262250
263- #generateRouteMeta( routePath : string , filepath : string ) : RouteMeta {
264- const {
265- routePath : normalizedPath ,
266- lang,
267- version,
268- } = this . normalizeRoutePath ( routePath ) ;
269- return {
270- routePath : normalizedPath ,
271- absolutePath : normalizePath ( filepath ) ,
272- relativePath : normalizePath ( path . relative ( this . #scanDir, filepath ) ) ,
273- pageName : getPageKey ( routePath ) ,
274- lang,
275- version,
276- } ;
277- }
278-
279251 getRoutePageByRoutePath ( routePath : string ) {
280252 return this . routeData . get ( routePath ) ;
281253 }
0 commit comments