Skip to content

Commit c488bbc

Browse files
committed
feat: wip
1 parent c0bf765 commit c488bbc

File tree

6 files changed

+144
-111
lines changed

6 files changed

+144
-111
lines changed

packages/cli/src/utils/createNextjsPage.ts

+50-16
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,70 @@ import fs from 'fs'
44
import { withBasePath } from './directory'
55
import { myAccountPageTemplate } from './templates/myAccountPage'
66

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)
7+
const ALLOWED_PREFIX_PAGES = ['/account']
148

9+
const createExternalPages = (
10+
customizationPagesDir: string,
11+
corePagesDir: string,
12+
baseCustomizationPagesDir: string,
13+
template: (pagePath: string) => string
14+
) => {
15+
fs.readdirSync(customizationPagesDir).forEach((file) => {
16+
console.log(customizationPagesDir)
17+
const filePath = path.join(customizationPagesDir, file)
18+
const destinationPath = path.join(corePagesDir, file)
19+
console.log({ filePath, destinationPath })
1520
if (fs.statSync(filePath).isDirectory()) {
16-
// nested pages are not allowed
17-
throw new Error('Nested pages are not allowed')
21+
if (!fs.existsSync(destinationPath)) {
22+
fs.mkdirSync(destinationPath, { recursive: true })
23+
}
24+
return createExternalPages(
25+
filePath,
26+
destinationPath,
27+
baseCustomizationPagesDir,
28+
template
29+
)
1830
}
1931

20-
const externalPagePath = `customizations/src/${filePath.replace(externalPagesPath, '')}`
21-
console.log({ externalPagePath })
22-
const content = myAccountPageTemplate(externalPagePath)
23-
fs.writeFileSync(destinationPath, content)
32+
if (file.endsWith('.tsx')) {
33+
const externalPagePath = `src/customizations/src/pages${filePath.replace(baseCustomizationPagesDir, '').replace('.tsx', '')}`
34+
console.log({ externalPagePath })
35+
const content = template(externalPagePath)
36+
fs.writeFileSync(destinationPath, content)
37+
}
2438
})
2539
}
2640

41+
function isAllowedPrefixPage(file: string) {
42+
console.log({ file })
43+
return ALLOWED_PREFIX_PAGES.some((prefix) => file.startsWith(prefix))
44+
}
45+
2746
export function createNextJsPages(basePath: string) {
2847
const { tmpDir } = withBasePath(basePath)
2948

30-
const corePagesDir = path.join(tmpDir, 'src/pages')
49+
const corePagesDir = path.join(tmpDir, 'src/pages') // .faststore/src/pages
3150
const customizationPagesDir = path.join(
3251
tmpDir,
3352
'src/customizations/src/pages'
34-
)
53+
) // .faststore/src/customizations/src/pages
54+
55+
const allPagesAreAllowed = fs
56+
.readdirSync(customizationPagesDir)
57+
.every((filePath) => isAllowedPrefixPage(path.join('/', filePath)))
58+
59+
if (!allPagesAreAllowed) {
60+
throw new Error(
61+
`Only these prefix pages: (${ALLOWED_PREFIX_PAGES.join(', ')}) are allowed`
62+
)
63+
}
3564

3665
console.log({ corePagesDir, customizationPagesDir })
3766

38-
processExternalPages(customizationPagesDir, corePagesDir)
67+
createExternalPages(
68+
customizationPagesDir,
69+
corePagesDir,
70+
customizationPagesDir,
71+
myAccountPageTemplate
72+
)
3973
}

packages/cli/src/utils/generate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function copyUserStarterToCustomizations(basePath: string) {
210210
try {
211211
if (existsSync(userSrcDir) && readdirSync(userSrcDir).length > 0) {
212212
copySync(userSrcDir, tmpCustomizationsSrcDir, { dereference: true })
213+
createNextJsPages(basePath)
213214
}
214215

215216
if (existsSync(userStoreConfigFile)) {
@@ -532,7 +533,6 @@ export async function generate(options: GenerateOptions) {
532533

533534
await Promise.all([
534535
setupPromise,
535-
createNextJsPages(basePath),
536536
checkDependencies(basePath, ['typescript']),
537537
enableSearchSSR(basePath),
538538
updateBuildTime(basePath),
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
11
export const myAccountPageTemplate = (pagePath: string) => `
2-
import type { Locator } from '@vtex/client-cms'
3-
import type { GetServerSideProps } from 'next'
42
import type { ComponentType } from 'react'
5-
import {
6-
type GlobalSectionsData,
7-
getGlobalSectionsData,
8-
} from 'src/components/cms/GlobalSections'
93
import RenderSections from 'src/components/cms/RenderSections'
104
import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components'
115
import CUSTOM_COMPONENTS from 'src/customizations/src/components'
12-
import dynamicPage from '${pagePath}';
13-
type Props = {
14-
globalSections: GlobalSectionsData
15-
}
6+
import {
7+
getServerSideProps,
8+
type MyAccountProps,
9+
} from 'src/experimental/myAccountSeverSideProps'
10+
import type DynamicPage from '${pagePath}';
1611
/* A list of components that can be used in the CMS. */
1712
const COMPONENTS: Record<string, ComponentType<any>> = {
1813
...GLOBAL_COMPONENTS,
1914
...CUSTOM_COMPONENTS,
2015
}
21-
function Page({ globalSections }: Props) {
16+
17+
function Page({ globalSections }: MyAccountProps) {
18+
2219
return (
2320
<RenderSections
2421
globalSections={globalSections.sections}
2522
components={COMPONENTS}
2623
>
27-
{dynamicPage()}
24+
<DynamicPage />
2825
</RenderSections>
2926
)
3027
}
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-
}
28+
29+
export { getServerSideProps }
30+
4131
export default Page
32+
4233
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Locator } from '@vtex/client-cms'
2+
import type { GetServerSideProps } from 'next'
3+
4+
import {
5+
type GlobalSectionsData,
6+
getGlobalSectionsData,
7+
} from 'src/components/cms/GlobalSections'
8+
9+
import { injectGlobalSections } from 'src/server/cms/global'
10+
11+
export type MyAccountProps = {
12+
globalSections: GlobalSectionsData
13+
}
14+
15+
export const getServerSideProps: GetServerSideProps<
16+
MyAccountProps,
17+
Record<string, string>,
18+
Locator
19+
> = async ({ previewData }) => {
20+
const [
21+
globalSectionsPromise,
22+
globalSectionsHeaderPromise,
23+
globalSectionsFooterPromise,
24+
] = getGlobalSectionsData(previewData)
25+
26+
const [globalSections, globalSectionsHeader, globalSectionsFooter] =
27+
await Promise.all([
28+
globalSectionsPromise,
29+
globalSectionsHeaderPromise,
30+
globalSectionsFooterPromise,
31+
])
32+
33+
const globalSectionsResult = injectGlobalSections({
34+
globalSections,
35+
globalSectionsHeader,
36+
globalSectionsFooter,
37+
})
38+
39+
return {
40+
props: { globalSections: globalSectionsResult },
41+
}
42+
}

packages/core/src/pages/account.tsx

-72
This file was deleted.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NextSeo } from 'next-seo'
2+
import type { ComponentType } from 'react'
3+
import { useEffect } from 'react'
4+
import RenderSections from 'src/components/cms/RenderSections'
5+
import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components'
6+
import CUSTOM_COMPONENTS from 'src/customizations/src/components'
7+
import storeConfig from 'discovery.config'
8+
import {
9+
getServerSideProps,
10+
type MyAccountProps,
11+
} from 'src/experimental/myAccountSeverSideProps'
12+
13+
/* A list of components that can be used in the CMS. */
14+
const COMPONENTS: Record<string, ComponentType<any>> = {
15+
...GLOBAL_COMPONENTS,
16+
...CUSTOM_COMPONENTS,
17+
}
18+
19+
function Page({ globalSections }: MyAccountProps) {
20+
useEffect(() => {
21+
window.location.href = `${storeConfig.accountUrl}${window.location.search}`
22+
}, [])
23+
24+
return (
25+
<RenderSections
26+
globalSections={globalSections.sections}
27+
components={COMPONENTS}
28+
>
29+
<NextSeo noindex nofollow />
30+
31+
<div>loading...</div>
32+
</RenderSections>
33+
)
34+
}
35+
36+
export { getServerSideProps }
37+
38+
export default Page

0 commit comments

Comments
 (0)