@@ -195,7 +195,7 @@ const extractMetadata = async (content: string): Promise<PageMetadata | undefine
195
195
}
196
196
} ;
197
197
198
- const scanAppDirectory = async (
198
+ export const scanAppDirectory = async (
199
199
sandboxManager : SandboxManager ,
200
200
dir : string ,
201
201
parentPath = '' ,
@@ -210,19 +210,7 @@ const scanAppDirectory = async (
210
210
return nodes ;
211
211
}
212
212
213
- const pageFile = entries . find (
214
- ( entry : ReaddirEntry ) =>
215
- entry . type === 'file' &&
216
- entry . name . startsWith ( 'page.' ) &&
217
- ALLOWED_EXTENSIONS . includes ( getFileExtension ( entry . name ) ) ,
218
- ) ;
219
-
220
- const layoutFile = entries . find (
221
- ( entry : ReaddirEntry ) =>
222
- entry . type === 'file' &&
223
- entry . name . startsWith ( 'layout.' ) &&
224
- ALLOWED_EXTENSIONS . includes ( getFileExtension ( entry . name ) ) ,
225
- ) ;
213
+ const { pageFile, layoutFile } = getPageAndLayoutFiles ( entries ) ;
226
214
227
215
const childDirectories = entries . filter (
228
216
( entry : ReaddirEntry ) =>
@@ -251,27 +239,14 @@ const scanAppDirectory = async (
251
239
Promise . all ( childPromises ) ,
252
240
] ) ;
253
241
254
- const [ pageFileResult , layoutFileResult ] = fileResults ;
255
242
const children = childResults . flat ( ) ;
256
243
257
- let pageMetadata : PageMetadata | undefined ;
258
- let layoutMetadata : PageMetadata | undefined ;
259
-
260
- if ( pageFileResult && pageFileResult . type === 'text' ) {
261
- try {
262
- pageMetadata = await extractMetadata ( pageFileResult . content ) ;
263
- } catch ( error ) {
264
- console . error ( `Error reading page file ${ dir } /${ pageFile . name } :` , error ) ;
265
- }
266
- }
244
+ const { pageMetadata, layoutMetadata } = await getPageAndLayoutMetadata ( fileResults ) ;
267
245
268
- if ( layoutFileResult && layoutFileResult . type === 'text' ) {
269
- try {
270
- layoutMetadata = await extractMetadata ( layoutFileResult . content ) ;
271
- } catch ( error ) {
272
- console . error ( `Error reading layout file ${ dir } /${ layoutFile ?. name } :` , error ) ;
273
- }
274
- }
246
+ const metadata = {
247
+ ...layoutMetadata ,
248
+ ...pageMetadata ,
249
+ } ;
275
250
276
251
// Create page node
277
252
const currentDir = getBaseName ( dir ) ;
@@ -288,10 +263,6 @@ const scanAppDirectory = async (
288
263
cleanPath = '/' + cleanPath . replace ( / ^ \/ | \/ $ / g, '' ) ;
289
264
const isRoot = ROOT_PATH_IDENTIFIERS . includes ( cleanPath ) ;
290
265
291
- const metadata = {
292
- ...layoutMetadata ,
293
- ...pageMetadata ,
294
- } ;
295
266
296
267
nodes . push ( {
297
268
id : nanoid ( ) ,
@@ -307,18 +278,18 @@ const scanAppDirectory = async (
307
278
metadata : metadata ?? { } ,
308
279
} ) ;
309
280
} else {
310
-
311
281
const childPromises = childDirectories . map ( async ( entry ) => {
312
282
const fullPath = `${ dir } /${ entry . name } ` ;
313
283
const relativePath = joinPath ( parentPath , entry . name ) ;
314
284
const children = await scanAppDirectory ( sandboxManager , fullPath , relativePath ) ;
315
285
316
286
if ( children . length > 0 ) {
317
- const dirPath = relativePath . replace ( / \\ / g, '/' ) ;
318
- const cleanPath = '/' + dirPath . replace ( / ^ \/ | \/ $ / g, '' ) ;
287
+ const currentDirName = getBaseName ( dir ) ;
288
+ const containerPath = parentPath ? `/${ parentPath } ` : `/${ currentDirName } ` ;
289
+ const cleanPath = containerPath . replace ( / \/ + / g, '/' ) ;
319
290
return {
320
291
id : nanoid ( ) ,
321
- name : entry . name ,
292
+ name : currentDirName ,
322
293
path : cleanPath ,
323
294
children,
324
295
isActive : false ,
@@ -330,7 +301,8 @@ const scanAppDirectory = async (
330
301
} ) ;
331
302
332
303
const childResults = await Promise . all ( childPromises ) ;
333
- nodes . push ( ...childResults . filter ( ( node ) => node !== null ) ) ;
304
+ const validNodes = childResults . filter ( ( node ) => node !== null ) ;
305
+ nodes . push ( ...validNodes ) ;
334
306
}
335
307
336
308
return nodes ;
@@ -1058,4 +1030,52 @@ export const parseRepoUrl = (repoUrl: string): { owner: string; repo: string } =
1058
1030
owner : match [ 1 ] ,
1059
1031
repo : match [ 2 ] ,
1060
1032
} ;
1061
- } ;
1033
+ } ;
1034
+
1035
+ const getPageAndLayoutFiles = ( entries : ReaddirEntry [ ] ) => {
1036
+ const pageFile = entries . find (
1037
+ ( entry : ReaddirEntry ) =>
1038
+ entry . type === 'file' &&
1039
+ entry . name . startsWith ( 'page.' ) &&
1040
+ ALLOWED_EXTENSIONS . includes ( getFileExtension ( entry . name ) ) ,
1041
+ ) ;
1042
+
1043
+ const layoutFile = entries . find (
1044
+ ( entry : ReaddirEntry ) =>
1045
+ entry . type === 'file' &&
1046
+ entry . name . startsWith ( 'layout.' ) &&
1047
+ ALLOWED_EXTENSIONS . includes ( getFileExtension ( entry . name ) ) ,
1048
+ ) ;
1049
+
1050
+ return { pageFile, layoutFile } ;
1051
+ }
1052
+
1053
+ const getPageAndLayoutMetadata = async ( fileResults : ( SandboxFile | null ) [ ] ) : Promise < { pageMetadata : PageMetadata | undefined ; layoutMetadata : PageMetadata | undefined } > => {
1054
+
1055
+ if ( ! fileResults || fileResults . length === 0 ) {
1056
+ return { pageMetadata : undefined , layoutMetadata : undefined } ;
1057
+ }
1058
+
1059
+ const [ pageFileResult , layoutFileResult ] = fileResults ;
1060
+
1061
+ let pageMetadata : PageMetadata | undefined ;
1062
+ let layoutMetadata : PageMetadata | undefined ;
1063
+
1064
+ if ( pageFileResult && pageFileResult . type === 'text' ) {
1065
+ try {
1066
+ pageMetadata = await extractMetadata ( pageFileResult . content ) ;
1067
+ } catch ( error ) {
1068
+ console . error ( `Error reading page file ${ pageFileResult . path } :` , error ) ;
1069
+ }
1070
+ }
1071
+
1072
+ if ( layoutFileResult && layoutFileResult . type === 'text' ) {
1073
+ try {
1074
+ layoutMetadata = await extractMetadata ( layoutFileResult . content ) ;
1075
+ } catch ( error ) {
1076
+ console . error ( `Error reading layout file ${ layoutFileResult . path } :` , error ) ;
1077
+ }
1078
+ }
1079
+
1080
+ return { pageMetadata, layoutMetadata } ;
1081
+ }
0 commit comments