Skip to content

Commit a9db9c1

Browse files
committed
feat: wip
1 parent c0bf765 commit a9db9c1

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

packages/cli/src/utils/createNextjsPage.ts

+40-13
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,39 @@ 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
7+
const ALLOWED_PREFIX_PAGES = ['/account']
8+
9+
const createExternalPages = (
10+
customizationPagesDir: string,
11+
corePagesDir: string,
12+
template: (pagePath: string) => string
1013
) => {
11-
fs.readdirSync(externalPagesPath).forEach((file) => {
12-
const filePath = path.join(externalPagesPath, file)
13-
const destinationPath = path.join(corePagesPath, file)
14+
fs.readdirSync(customizationPagesDir).forEach((file) => {
15+
console.log(customizationPagesDir)
16+
const filePath = path.join(customizationPagesDir, file)
17+
const destinationPath = path.join(corePagesDir, file)
1418

1519
if (fs.statSync(filePath).isDirectory()) {
16-
// nested pages are not allowed
17-
throw new Error('Nested pages are not allowed')
20+
if (!fs.existsSync(destinationPath)) {
21+
fs.mkdirSync(destinationPath, { recursive: true })
22+
}
23+
return createExternalPages(filePath, destinationPath, template)
1824
}
1925

20-
const externalPagePath = `customizations/src/${filePath.replace(externalPagesPath, '')}`
21-
console.log({ externalPagePath })
22-
const content = myAccountPageTemplate(externalPagePath)
23-
fs.writeFileSync(destinationPath, content)
26+
if (file.endsWith('.tsx')) {
27+
const externalPagePath = `src/customizations/src/pages${filePath.replace(customizationPagesDir, '')}`
28+
console.log({ externalPagePath })
29+
const content = template(externalPagePath)
30+
fs.writeFileSync(destinationPath, content)
31+
}
2432
})
2533
}
2634

35+
function isAllowedPrefixPage(file: string) {
36+
console.log({ file })
37+
return ALLOWED_PREFIX_PAGES.some((prefix) => file.startsWith(prefix))
38+
}
39+
2740
export function createNextJsPages(basePath: string) {
2841
const { tmpDir } = withBasePath(basePath)
2942

@@ -33,7 +46,21 @@ export function createNextJsPages(basePath: string) {
3346
'src/customizations/src/pages'
3447
)
3548

49+
const allPagesAreAllowed = fs
50+
.readdirSync(customizationPagesDir)
51+
.every((filePath) => isAllowedPrefixPage(path.join('/', filePath)))
52+
53+
if (!allPagesAreAllowed) {
54+
throw new Error(
55+
`Only these prefix pages: (${ALLOWED_PREFIX_PAGES.join(', ')}) are allowed`
56+
)
57+
}
58+
3659
console.log({ corePagesDir, customizationPagesDir })
3760

38-
processExternalPages(customizationPagesDir, corePagesDir)
61+
createExternalPages(
62+
customizationPagesDir,
63+
corePagesDir,
64+
myAccountPageTemplate
65+
)
3966
}

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),

packages/cli/src/utils/templates/myAccountPage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const myAccountPageTemplate = (pagePath: string) => `
99
import RenderSections from 'src/components/cms/RenderSections'
1010
import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components'
1111
import CUSTOM_COMPONENTS from 'src/customizations/src/components'
12-
import dynamicPage from '${pagePath}';
12+
import DynamicPage from '${pagePath}';
1313
type Props = {
1414
globalSections: GlobalSectionsData
1515
}
@@ -24,7 +24,7 @@ export const myAccountPageTemplate = (pagePath: string) => `
2424
globalSections={globalSections.sections}
2525
components={COMPONENTS}
2626
>
27-
{dynamicPage()}
27+
<DynamicPage />
2828
</RenderSections>
2929
)
3030
}

packages/core/src/pages/account.tsx packages/core/src/pages/account/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import RenderSections from 'src/components/cms/RenderSections'
1111
import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components'
1212
import CUSTOM_COMPONENTS from 'src/customizations/src/components'
1313
import { injectGlobalSections } from 'src/server/cms/global'
14-
import storeConfig from '../../discovery.config'
14+
import storeConfig from 'discovery.config'
1515

1616
type Props = {
1717
globalSections: GlobalSectionsData

0 commit comments

Comments
 (0)