Skip to content

Commit 73e8f26

Browse files
developerkunaldependabot[bot]stevenwong-okta
authored
Add Support for Custom Prompts (#930)
* Add Support for Custom Prompts * Fixed Linting * Fixed context.test.js * Add E2e Test * Fix E2e Test * Fix E2e Test * Fix E2E Tests * Update structure * Updated Testcase for checking the file. * Update - Used _getRestClient for making requests. * Added _getRestClient Mock * Fixed Put Request. * Bump chai from 4.4.1 to 4.5.0 (#932) Bumps [chai](https://github.com/chaijs/chai) from 4.4.1 to 4.5.0. - [Release notes](https://github.com/chaijs/chai/releases) - [Changelog](https://github.com/chaijs/chai/blob/main/History.md) - [Commits](chaijs/chai@v4.4.1...v4.5.0) --- updated-dependencies: - dependency-name: chai dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * FIx Formatting * reverted prompt types * Fixed Linting and schema * Make changes in Schema for partial prompts * Update codeowner file with new GitHub team name (#931) Co-authored-by: KunalOfficial <[email protected]> * Removed consolelogs * Fix back codeowners --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: stevenwong-okta <[email protected]>
1 parent 9ab33c3 commit 73e8f26

18 files changed

+1297
-97
lines changed

src/context/directory/handlers/prompts.ts

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import path from 'path';
2-
import { ensureDirSync } from 'fs-extra';
2+
import { ensureDirSync, readFileSync, writeFileSync } from 'fs-extra';
33
import { constants } from '../../../tools';
4-
import { existsMustBeDir, dumpJSON, loadJSON, isFile } from '../../../utils';
4+
import { dumpJSON, existsMustBeDir, isFile, loadJSON } from '../../../utils';
55
import { DirectoryHandler } from '.';
66
import DirectoryContext from '..';
77
import { ParsedAsset } from '../../../types';
88
import {
9+
AllPromptsByLanguage,
10+
CustomPartialsConfig,
11+
CustomPartialsInsertionPoints,
12+
CustomPartialsPromptTypes,
13+
CustomPartialsScreenTypes,
14+
CustomPromptPartialsScreens,
915
Prompts,
1016
PromptSettings,
11-
AllPromptsByLanguage,
17+
ScreenConfig,
1218
} from '../../../tools/auth0/handlers/prompts';
1319

1420
type ParsedPrompts = ParsedAsset<'prompts', Prompts>;
1521

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);
1923

20-
const getPromptsSettingsFile = (promptsDirectory: string) => {
21-
return path.join(promptsDirectory, 'prompts.json');
22-
};
24+
const getPromptsSettingsFile = (promptsDirectory: string) =>
25+
path.join(promptsDirectory, 'prompts.json');
2326

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');
2731

2832
function parse(context: DirectoryContext): ParsedPrompts {
2933
const promptsDirectory = getPromptsDirectory(context.filePath);
@@ -47,10 +51,41 @@ function parse(context: DirectoryContext): ParsedPrompts {
4751
}) as AllPromptsByLanguage;
4852
})();
4953

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+
5084
return {
5185
prompts: {
5286
...promptsSettings,
5387
customText,
88+
partials,
5489
},
5590
};
5691
}
@@ -60,7 +95,7 @@ async function dump(context: DirectoryContext): Promise<void> {
6095

6196
if (!prompts) return;
6297

63-
const { customText, ...promptsSettings } = prompts;
98+
const { customText, partials, ...promptsSettings } = prompts;
6499

65100
const promptsDirectory = getPromptsDirectory(context.filePath);
66101
ensureDirSync(promptsDirectory);
@@ -72,6 +107,40 @@ async function dump(context: DirectoryContext): Promise<void> {
72107
if (!customText) return;
73108
const customTextFile = getCustomTextFile(promptsDirectory);
74109
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);
75144
}
76145

77146
const promptsHandler: DirectoryHandler<ParsedPrompts> = {

0 commit comments

Comments
 (0)