@@ -4,36 +4,70 @@ 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
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' ]
14
8
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 } )
15
20
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
+ )
18
30
}
19
31
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 , '' ) } `
34
+ console . log ( { externalPagePath } )
35
+ const content = template ( externalPagePath )
36
+ fs . writeFileSync ( destinationPath , content )
37
+ }
24
38
} )
25
39
}
26
40
41
+ function isAllowedPrefixPage ( file : string ) {
42
+ console . log ( { file } )
43
+ return ALLOWED_PREFIX_PAGES . some ( ( prefix ) => file . startsWith ( prefix ) )
44
+ }
45
+
27
46
export function createNextJsPages ( basePath : string ) {
28
47
const { tmpDir } = withBasePath ( basePath )
29
48
30
- const corePagesDir = path . join ( tmpDir , 'src/pages' )
49
+ const corePagesDir = path . join ( tmpDir , 'src/pages' ) // .faststore/src/pages
31
50
const customizationPagesDir = path . join (
32
51
tmpDir ,
33
52
'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
+ }
35
64
36
65
console . log ( { corePagesDir, customizationPagesDir } )
37
66
38
- processExternalPages ( customizationPagesDir , corePagesDir )
67
+ createExternalPages (
68
+ customizationPagesDir ,
69
+ corePagesDir ,
70
+ customizationPagesDir ,
71
+ myAccountPageTemplate
72
+ )
39
73
}
0 commit comments