1
1
import path from 'path' ;
2
- import { ensureDirSync } from 'fs-extra' ;
2
+ import { ensureDirSync , readFileSync , writeFileSync } from 'fs-extra' ;
3
3
import { constants } from '../../../tools' ;
4
- import { existsMustBeDir , dumpJSON , loadJSON , isFile } from '../../../utils' ;
4
+ import { dumpJSON , existsMustBeDir , isFile , loadJSON } from '../../../utils' ;
5
5
import { DirectoryHandler } from '.' ;
6
6
import DirectoryContext from '..' ;
7
7
import { ParsedAsset } from '../../../types' ;
8
8
import {
9
+ AllPromptsByLanguage ,
10
+ CustomPartialsConfig ,
11
+ CustomPartialsInsertionPoints ,
12
+ CustomPartialsPromptTypes ,
13
+ CustomPartialsScreenTypes ,
14
+ CustomPromptPartialsScreens ,
9
15
Prompts ,
10
16
PromptSettings ,
11
- AllPromptsByLanguage ,
17
+ ScreenConfig ,
12
18
} from '../../../tools/auth0/handlers/prompts' ;
13
19
14
20
type ParsedPrompts = ParsedAsset < 'prompts' , Prompts > ;
15
21
16
- const getPromptsDirectory = ( filePath : string ) => {
17
- return path . join ( filePath , constants . PROMPTS_DIRECTORY ) ;
18
- } ;
22
+ const getPromptsDirectory = ( filePath : string ) => path . join ( filePath , constants . PROMPTS_DIRECTORY ) ;
19
23
20
- const getPromptsSettingsFile = ( promptsDirectory : string ) => {
21
- return path . join ( promptsDirectory , 'prompts.json' ) ;
22
- } ;
24
+ const getPromptsSettingsFile = ( promptsDirectory : string ) =>
25
+ path . join ( promptsDirectory , 'prompts.json' ) ;
23
26
24
- const getCustomTextFile = ( promptsDirectory : string ) => {
25
- return path . join ( promptsDirectory , 'custom-text.json' ) ;
26
- } ;
27
+ const getCustomTextFile = ( promptsDirectory : string ) =>
28
+ path . join ( promptsDirectory , 'custom-text.json' ) ;
29
+
30
+ const getPartialsFile = ( promptsDirectory : string ) => path . join ( promptsDirectory , 'partials.json' ) ;
27
31
28
32
function parse ( context : DirectoryContext ) : ParsedPrompts {
29
33
const promptsDirectory = getPromptsDirectory ( context . filePath ) ;
@@ -47,10 +51,41 @@ function parse(context: DirectoryContext): ParsedPrompts {
47
51
} ) as AllPromptsByLanguage ;
48
52
} ) ( ) ;
49
53
54
+ const partials = ( ( ) => {
55
+ const partialsFile = getPartialsFile ( promptsDirectory ) ;
56
+ if ( ! isFile ( partialsFile ) ) return { } ;
57
+ const partialsFileContent = loadJSON ( partialsFile , {
58
+ mappings : context . mappings ,
59
+ disableKeywordReplacement : context . disableKeywordReplacement ,
60
+ } ) as CustomPartialsConfig ;
61
+
62
+ return Object . entries ( partialsFileContent ) . reduce ( ( acc , [ promptName , screensArray ] ) => {
63
+ const screensObject = screensArray [ 0 ] as Record < CustomPartialsScreenTypes , ScreenConfig [ ] > ;
64
+ acc [ promptName as CustomPartialsPromptTypes ] = Object . entries ( screensObject ) . reduce (
65
+ ( screenAcc , [ screenName , items ] ) => {
66
+ screenAcc [ screenName as CustomPartialsScreenTypes ] = items . reduce (
67
+ ( insertionAcc , { name, template } ) => {
68
+ const templateFilePath = path . join ( promptsDirectory , template ) ;
69
+ insertionAcc [ name ] = isFile ( templateFilePath )
70
+ ? readFileSync ( templateFilePath , 'utf8' ) . trim ( )
71
+ : '' ;
72
+ return insertionAcc ;
73
+ } ,
74
+ { } as Record < string , string >
75
+ ) ;
76
+ return screenAcc ;
77
+ } ,
78
+ { } as Record < CustomPartialsScreenTypes , Record < string , string > >
79
+ ) ;
80
+ return acc ;
81
+ } , { } as Record < CustomPartialsPromptTypes , Record < CustomPartialsScreenTypes , Record < string , string > > > ) ;
82
+ } ) ( ) ;
83
+
50
84
return {
51
85
prompts : {
52
86
...promptsSettings ,
53
87
customText,
88
+ partials,
54
89
} ,
55
90
} ;
56
91
}
@@ -60,7 +95,7 @@ async function dump(context: DirectoryContext): Promise<void> {
60
95
61
96
if ( ! prompts ) return ;
62
97
63
- const { customText, ...promptsSettings } = prompts ;
98
+ const { customText, partials , ...promptsSettings } = prompts ;
64
99
65
100
const promptsDirectory = getPromptsDirectory ( context . filePath ) ;
66
101
ensureDirSync ( promptsDirectory ) ;
@@ -72,6 +107,40 @@ async function dump(context: DirectoryContext): Promise<void> {
72
107
if ( ! customText ) return ;
73
108
const customTextFile = getCustomTextFile ( promptsDirectory ) ;
74
109
dumpJSON ( customTextFile , customText ) ;
110
+
111
+ if ( ! partials ) return ;
112
+ const partialsFile = getPartialsFile ( promptsDirectory ) ;
113
+
114
+ const transformedPartials = Object . entries ( partials ) . reduce ( ( acc , [ promptName , screens ] ) => {
115
+ acc [ promptName as CustomPartialsPromptTypes ] = [
116
+ Object . entries ( screens as CustomPromptPartialsScreens ) . reduce (
117
+ ( screenAcc , [ screenName , insertionPoints ] ) => {
118
+ screenAcc [ screenName as CustomPartialsScreenTypes ] = Object . entries (
119
+ insertionPoints as Partial < Record < CustomPartialsInsertionPoints , string > >
120
+ ) . map ( ( [ insertionPoint , template ] ) => {
121
+ const templateFilePath = path . join (
122
+ promptsDirectory ,
123
+ 'partials' ,
124
+ promptName ,
125
+ screenName ,
126
+ `${ insertionPoint } .liquid`
127
+ ) ;
128
+ ensureDirSync ( path . dirname ( templateFilePath ) ) ;
129
+ writeFileSync ( templateFilePath , template , 'utf8' ) ;
130
+ return {
131
+ name : insertionPoint ,
132
+ template : path . relative ( promptsDirectory , templateFilePath ) , // Path relative to `promptsDirectory`
133
+ } ;
134
+ } ) ;
135
+ return screenAcc ;
136
+ } ,
137
+ { } as Record < CustomPartialsScreenTypes , ScreenConfig [ ] >
138
+ ) ,
139
+ ] ;
140
+ return acc ;
141
+ } , { } as CustomPartialsConfig ) ;
142
+
143
+ dumpJSON ( partialsFile , transformedPartials ) ;
75
144
}
76
145
77
146
const promptsHandler : DirectoryHandler < ParsedPrompts > = {
0 commit comments