Skip to content

Commit ce51e91

Browse files
fix: update pages discovery and test cases (#2614)
1 parent 584fc84 commit ce51e91

File tree

2 files changed

+834
-42
lines changed

2 files changed

+834
-42
lines changed

apps/web/client/src/components/store/editor/pages/helper.ts

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ const extractMetadata = async (content: string): Promise<PageMetadata | undefine
195195
}
196196
};
197197

198-
const scanAppDirectory = async (
198+
export const scanAppDirectory = async (
199199
sandboxManager: SandboxManager,
200200
dir: string,
201201
parentPath = '',
@@ -210,19 +210,7 @@ const scanAppDirectory = async (
210210
return nodes;
211211
}
212212

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);
226214

227215
const childDirectories = entries.filter(
228216
(entry: ReaddirEntry) =>
@@ -251,27 +239,14 @@ const scanAppDirectory = async (
251239
Promise.all(childPromises),
252240
]);
253241

254-
const [pageFileResult, layoutFileResult] = fileResults;
255242
const children = childResults.flat();
256243

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);
267245

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+
};
275250

276251
// Create page node
277252
const currentDir = getBaseName(dir);
@@ -288,10 +263,6 @@ const scanAppDirectory = async (
288263
cleanPath = '/' + cleanPath.replace(/^\/|\/$/g, '');
289264
const isRoot = ROOT_PATH_IDENTIFIERS.includes(cleanPath);
290265

291-
const metadata = {
292-
...layoutMetadata,
293-
...pageMetadata,
294-
};
295266

296267
nodes.push({
297268
id: nanoid(),
@@ -307,18 +278,18 @@ const scanAppDirectory = async (
307278
metadata: metadata ?? {},
308279
});
309280
} else {
310-
311281
const childPromises = childDirectories.map(async (entry) => {
312282
const fullPath = `${dir}/${entry.name}`;
313283
const relativePath = joinPath(parentPath, entry.name);
314284
const children = await scanAppDirectory(sandboxManager, fullPath, relativePath);
315285

316286
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, '/');
319290
return {
320291
id: nanoid(),
321-
name: entry.name,
292+
name: currentDirName,
322293
path: cleanPath,
323294
children,
324295
isActive: false,
@@ -330,7 +301,8 @@ const scanAppDirectory = async (
330301
});
331302

332303
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);
334306
}
335307

336308
return nodes;
@@ -1058,4 +1030,52 @@ export const parseRepoUrl = (repoUrl: string): { owner: string; repo: string } =
10581030
owner: match[1],
10591031
repo: match[2],
10601032
};
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

Comments
 (0)