From d11c5754968b8ee7eb0331937c7d4af5beba54b7 Mon Sep 17 00:00:00 2001 From: Tom Meyer Date: Wed, 25 Oct 2023 09:59:23 -0400 Subject: [PATCH] Draft. --- .../src/orchestrators/ParsingOrchestrator.ts | 9 ++++++++- .../orchestrators/createFilenameMapping.ts | 7 +++++-- .../ParsingOrchestrator/src/pages/_client.tsx | 12 ++++++++++++ .../ParsingOrchestrator/src/pages/_server.tsx | 19 +++++++++++++++++++ .../orchestrators/ParsingOrchestrator.test.ts | 4 +++- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_client.tsx create mode 100644 packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_server.tsx diff --git a/packages/studio-plugin/src/orchestrators/ParsingOrchestrator.ts b/packages/studio-plugin/src/orchestrators/ParsingOrchestrator.ts index 91d44e6fc..5a4baf0a3 100644 --- a/packages/studio-plugin/src/orchestrators/ParsingOrchestrator.ts +++ b/packages/studio-plugin/src/orchestrators/ParsingOrchestrator.ts @@ -214,7 +214,14 @@ export default class ParsingOrchestrator { `The pages directory does not exist, expected directory to be at "${this.paths.pages}".` ); } - return createFilenameMapping(this.paths.pages, this.getOrCreatePageFile); + + const excludeReservedPagesJSFiles = (filename: string) => + !(filename.includes("_server") || filename.includes("_client")); + return createFilenameMapping( + this.paths.pages, + this.getOrCreatePageFile, + excludeReservedPagesJSFiles + ); } private getSiteSettings(): SiteSettings | undefined { diff --git a/packages/studio-plugin/src/orchestrators/createFilenameMapping.ts b/packages/studio-plugin/src/orchestrators/createFilenameMapping.ts index 8eea2eb00..704565117 100644 --- a/packages/studio-plugin/src/orchestrators/createFilenameMapping.ts +++ b/packages/studio-plugin/src/orchestrators/createFilenameMapping.ts @@ -3,11 +3,14 @@ import upath from "upath"; export default function createFilenameMapping( dirPath: string, - getMappedValue: (name: string) => T + getMappedValue: (name: string) => T, + fileFilter?: (filename: string) => boolean ): Record { const files = fs.readdirSync(dirPath, "utf-8").filter((filename) => { const absPath = upath.join(dirPath, filename); - return fs.lstatSync(absPath).isFile(); + + const isFile = fs.lstatSync(absPath).isFile(); + return fileFilter ? isFile && fileFilter(filename) : isFile; }); return files.reduce((filepathMapping, filename) => { const name = upath.basename(filename, ".tsx"); diff --git a/packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_client.tsx b/packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_client.tsx new file mode 100644 index 000000000..4ed088a76 --- /dev/null +++ b/packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_client.tsx @@ -0,0 +1,12 @@ +import { PageContext } from "@yext/pages"; +import { hydrateRoot } from "react-dom/client"; + +export { render }; + +const render = async (pageContext: PageContext) => { + const { Page, pageProps } = pageContext; + const rootElement = document.getElementById("reactele"); + if (rootElement) { + hydrateRoot(rootElement, ); + } +}; diff --git a/packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_server.tsx b/packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_server.tsx new file mode 100644 index 000000000..799f01df0 --- /dev/null +++ b/packages/studio-plugin/tests/__fixtures__/ParsingOrchestrator/src/pages/_server.tsx @@ -0,0 +1,19 @@ +import * as ReactDOMServer from "react-dom/server"; +import { PageContext } from "@yext/pages"; + +export { render }; + +const render = async (pageContext: PageContext) => { + const { Page, pageProps } = pageContext; + const viewHtml = ReactDOMServer.renderToString(); + return ` + + + + + + +
${viewHtml}
+ + `; +}; diff --git a/packages/studio-plugin/tests/orchestrators/ParsingOrchestrator.test.ts b/packages/studio-plugin/tests/orchestrators/ParsingOrchestrator.test.ts index 9f596f408..f7cfa0abe 100644 --- a/packages/studio-plugin/tests/orchestrators/ParsingOrchestrator.test.ts +++ b/packages/studio-plugin/tests/orchestrators/ParsingOrchestrator.test.ts @@ -68,10 +68,12 @@ describe("aggregates data as expected", () => { ); }); - it("properly populates pageNameToPageState, ignoring sub-directories", () => { + it("properly populates PageState Records, ignoring sub-directories and reserved PagesJS Files", () => { expect(studioData.pageNameToPageState).toEqual({ basicPage: basicPageState, }); + + expect(studioData.pageNameToErrorPageState).toEqual({}); }); it("properly populates siteSettings", () => {