Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Custom Prompts #930

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading