Skip to content

Commit

Permalink
Add Support for Custom Prompts (#930)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent 9ab33c3 commit 73e8f26
Show file tree
Hide file tree
Showing 18 changed files with 1,297 additions and 97 deletions.
95 changes: 82 additions & 13 deletions src/context/directory/handlers/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import path from 'path';
import { ensureDirSync } from 'fs-extra';
import { ensureDirSync, readFileSync, writeFileSync } from 'fs-extra';
import { constants } from '../../../tools';
import { existsMustBeDir, dumpJSON, loadJSON, isFile } from '../../../utils';
import { dumpJSON, existsMustBeDir, isFile, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { ParsedAsset } from '../../../types';
import {
AllPromptsByLanguage,
CustomPartialsConfig,
CustomPartialsInsertionPoints,
CustomPartialsPromptTypes,
CustomPartialsScreenTypes,
CustomPromptPartialsScreens,
Prompts,
PromptSettings,
AllPromptsByLanguage,
ScreenConfig,
} from '../../../tools/auth0/handlers/prompts';

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

const getPromptsDirectory = (filePath: string) => {
return path.join(filePath, constants.PROMPTS_DIRECTORY);
};
const getPromptsDirectory = (filePath: string) => path.join(filePath, constants.PROMPTS_DIRECTORY);

const getPromptsSettingsFile = (promptsDirectory: string) => {
return path.join(promptsDirectory, 'prompts.json');
};
const getPromptsSettingsFile = (promptsDirectory: string) =>
path.join(promptsDirectory, 'prompts.json');

const getCustomTextFile = (promptsDirectory: string) => {
return path.join(promptsDirectory, 'custom-text.json');
};
const getCustomTextFile = (promptsDirectory: string) =>
path.join(promptsDirectory, 'custom-text.json');

const getPartialsFile = (promptsDirectory: string) => path.join(promptsDirectory, 'partials.json');

function parse(context: DirectoryContext): ParsedPrompts {
const promptsDirectory = getPromptsDirectory(context.filePath);
Expand All @@ -47,10 +51,41 @@ function parse(context: DirectoryContext): ParsedPrompts {
}) as AllPromptsByLanguage;
})();

const partials = (() => {
const partialsFile = getPartialsFile(promptsDirectory);
if (!isFile(partialsFile)) return {};
const partialsFileContent = loadJSON(partialsFile, {
mappings: context.mappings,
disableKeywordReplacement: context.disableKeywordReplacement,
}) as CustomPartialsConfig;

return Object.entries(partialsFileContent).reduce((acc, [promptName, screensArray]) => {
const screensObject = screensArray[0] as Record<CustomPartialsScreenTypes, ScreenConfig[]>;
acc[promptName as CustomPartialsPromptTypes] = Object.entries(screensObject).reduce(
(screenAcc, [screenName, items]) => {
screenAcc[screenName as CustomPartialsScreenTypes] = items.reduce(
(insertionAcc, { name, template }) => {
const templateFilePath = path.join(promptsDirectory, template);
insertionAcc[name] = isFile(templateFilePath)
? readFileSync(templateFilePath, 'utf8').trim()
: '';
return insertionAcc;
},
{} as Record<string, string>
);
return screenAcc;
},
{} as Record<CustomPartialsScreenTypes, Record<string, string>>
);
return acc;
}, {} as Record<CustomPartialsPromptTypes, Record<CustomPartialsScreenTypes, Record<string, string>>>);
})();

return {
prompts: {
...promptsSettings,
customText,
partials,
},
};
}
Expand All @@ -60,7 +95,7 @@ async function dump(context: DirectoryContext): Promise<void> {

if (!prompts) return;

const { customText, ...promptsSettings } = prompts;
const { customText, partials, ...promptsSettings } = prompts;

const promptsDirectory = getPromptsDirectory(context.filePath);
ensureDirSync(promptsDirectory);
Expand All @@ -72,6 +107,40 @@ async function dump(context: DirectoryContext): Promise<void> {
if (!customText) return;
const customTextFile = getCustomTextFile(promptsDirectory);
dumpJSON(customTextFile, customText);

if (!partials) return;
const partialsFile = getPartialsFile(promptsDirectory);

const transformedPartials = Object.entries(partials).reduce((acc, [promptName, screens]) => {
acc[promptName as CustomPartialsPromptTypes] = [
Object.entries(screens as CustomPromptPartialsScreens).reduce(
(screenAcc, [screenName, insertionPoints]) => {
screenAcc[screenName as CustomPartialsScreenTypes] = Object.entries(
insertionPoints as Partial<Record<CustomPartialsInsertionPoints, string>>
).map(([insertionPoint, template]) => {
const templateFilePath = path.join(
promptsDirectory,
'partials',
promptName,
screenName,
`${insertionPoint}.liquid`
);
ensureDirSync(path.dirname(templateFilePath));
writeFileSync(templateFilePath, template, 'utf8');
return {
name: insertionPoint,
template: path.relative(promptsDirectory, templateFilePath), // Path relative to `promptsDirectory`
};
});
return screenAcc;
},
{} as Record<CustomPartialsScreenTypes, ScreenConfig[]>
),
];
return acc;
}, {} as CustomPartialsConfig);

dumpJSON(partialsFile, transformedPartials);
}

const promptsHandler: DirectoryHandler<ParsedPrompts> = {
Expand Down
Loading

0 comments on commit 73e8f26

Please sign in to comment.