Skip to content

Commit c0bf765

Browse files
committed
feat: test customization
1 parent 8f562eb commit c0bf765

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
4+
import { withBasePath } from './directory'
5+
import { myAccountPageTemplate } from './templates/myAccountPage'
6+
7+
const processExternalPages = (
8+
externalPagesPath: string,
9+
corePagesPath: string
10+
) => {
11+
fs.readdirSync(externalPagesPath).forEach((file) => {
12+
const filePath = path.join(externalPagesPath, file)
13+
const destinationPath = path.join(corePagesPath, file)
14+
15+
if (fs.statSync(filePath).isDirectory()) {
16+
// nested pages are not allowed
17+
throw new Error('Nested pages are not allowed')
18+
}
19+
20+
const externalPagePath = `customizations/src/${filePath.replace(externalPagesPath, '')}`
21+
console.log({ externalPagePath })
22+
const content = myAccountPageTemplate(externalPagePath)
23+
fs.writeFileSync(destinationPath, content)
24+
})
25+
}
26+
27+
export function createNextJsPages(basePath: string) {
28+
const { tmpDir } = withBasePath(basePath)
29+
30+
const corePagesDir = path.join(tmpDir, 'src/pages')
31+
const customizationPagesDir = path.join(
32+
tmpDir,
33+
'src/customizations/src/pages'
34+
)
35+
36+
console.log({ corePagesDir, customizationPagesDir })
37+
38+
processExternalPages(customizationPagesDir, corePagesDir)
39+
}

packages/cli/src/utils/generate.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { withBasePath } from './directory'
1818
import { installDependencies } from './dependencies'
1919
import { logger } from './logger'
2020
import { installPlugins } from './plugins'
21+
import { createNextJsPages } from './createNextjsPage'
2122

2223
interface GenerateOptions {
2324
setup?: boolean
@@ -373,7 +374,7 @@ function checkDependencies(basePath: string, packagesToCheck: string[]) {
373374
logger.warn(
374375
`${chalk.yellow(
375376
'warning'
376-
)} - Version mismatch detected for ${packageName}.
377+
)} - Version mismatch detected for ${packageName}.
377378
Core: ${coreVersion}, Customization: ${rootVersion}. Please align both versions to prevent issues`
378379
)
379380
}
@@ -531,6 +532,7 @@ export async function generate(options: GenerateOptions) {
531532

532533
await Promise.all([
533534
setupPromise,
535+
createNextJsPages(basePath),
534536
checkDependencies(basePath, ['typescript']),
535537
enableSearchSSR(basePath),
536538
updateBuildTime(basePath),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export const myAccountPageTemplate = (pagePath: string) => `
2+
import type { Locator } from '@vtex/client-cms'
3+
import type { GetServerSideProps } from 'next'
4+
import type { ComponentType } from 'react'
5+
import {
6+
type GlobalSectionsData,
7+
getGlobalSectionsData,
8+
} from 'src/components/cms/GlobalSections'
9+
import RenderSections from 'src/components/cms/RenderSections'
10+
import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components'
11+
import CUSTOM_COMPONENTS from 'src/customizations/src/components'
12+
import dynamicPage from '${pagePath}';
13+
type Props = {
14+
globalSections: GlobalSectionsData
15+
}
16+
/* A list of components that can be used in the CMS. */
17+
const COMPONENTS: Record<string, ComponentType<any>> = {
18+
...GLOBAL_COMPONENTS,
19+
...CUSTOM_COMPONENTS,
20+
}
21+
function Page({ globalSections }: Props) {
22+
return (
23+
<RenderSections
24+
globalSections={globalSections.sections}
25+
components={COMPONENTS}
26+
>
27+
{dynamicPage()}
28+
</RenderSections>
29+
)
30+
}
31+
export const getServerSideProps: GetServerSideProps<
32+
Props,
33+
Record<string, string>,
34+
Locator
35+
> = async ({ previewData }) => {
36+
const globalSections = await getGlobalSectionsData(previewData)
37+
return {
38+
props: { globalSections },
39+
}
40+
}
41+
export default Page
42+
`

0 commit comments

Comments
 (0)