@@ -4,26 +4,38 @@ import fs from 'fs'
4
4
import { withBasePath } from './directory'
5
5
import { myAccountPageTemplate } from './templates/myAccountPage'
6
6
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
10
13
) => {
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 )
14
17
15
18
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 )
18
23
}
19
24
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
+ }
24
31
} )
25
32
}
26
33
34
+ function isAllowedPrefixPage ( file : string ) {
35
+ console . log ( { file } )
36
+ return ALLOWED_PREFIX_PAGES . some ( ( prefix ) => file . startsWith ( prefix ) )
37
+ }
38
+
27
39
export function createNextJsPages ( basePath : string ) {
28
40
const { tmpDir } = withBasePath ( basePath )
29
41
@@ -33,7 +45,21 @@ export function createNextJsPages(basePath: string) {
33
45
'src/customizations/src/pages'
34
46
)
35
47
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
+
36
58
console . log ( { corePagesDir, customizationPagesDir } )
37
59
38
- processExternalPages ( customizationPagesDir , corePagesDir )
60
+ createExternalPages (
61
+ customizationPagesDir ,
62
+ corePagesDir ,
63
+ myAccountPageTemplate
64
+ )
39
65
}
0 commit comments