Skip to content

Commit b8959ba

Browse files
committed
feat: wip
1 parent c0bf765 commit b8959ba

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

packages/cli/src/utils/createNextjsPage.ts

+39-13
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@ 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+
const filePath = path.join(customizationPagesDir, file)
16+
const destinationPath = path.join(corePagesDir, file)
1417

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

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

34+
function isAllowedPrefixPage(file: string) {
35+
console.log({ file })
36+
return ALLOWED_PREFIX_PAGES.some((prefix) => file.startsWith(prefix))
37+
}
38+
2739
export function createNextJsPages(basePath: string) {
2840
const { tmpDir } = withBasePath(basePath)
2941

@@ -33,7 +45,21 @@ export function createNextJsPages(basePath: string) {
3345
'src/customizations/src/pages'
3446
)
3547

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

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

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)