@@ -4,26 +4,39 @@ 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
+ console . log ( customizationPagesDir )
16
+ const filePath = path . join ( customizationPagesDir , file )
17
+ const destinationPath = path . join ( corePagesDir , file )
14
18
15
19
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 )
18
24
}
19
25
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
+ }
24
32
} )
25
33
}
26
34
35
+ function isAllowedPrefixPage ( file : string ) {
36
+ console . log ( { file } )
37
+ return ALLOWED_PREFIX_PAGES . some ( ( prefix ) => file . startsWith ( prefix ) )
38
+ }
39
+
27
40
export function createNextJsPages ( basePath : string ) {
28
41
const { tmpDir } = withBasePath ( basePath )
29
42
@@ -33,7 +46,21 @@ export function createNextJsPages(basePath: string) {
33
46
'src/customizations/src/pages'
34
47
)
35
48
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
+
36
59
console . log ( { corePagesDir, customizationPagesDir } )
37
60
38
- processExternalPages ( customizationPagesDir , corePagesDir )
61
+ createExternalPages (
62
+ customizationPagesDir ,
63
+ corePagesDir ,
64
+ myAccountPageTemplate
65
+ )
39
66
}
0 commit comments