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

Support keyword replacement for custom prompts when using the directory format #963

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
9 changes: 6 additions & 3 deletions src/context/directory/handlers/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { ensureDirSync, readFileSync, writeFileSync } from 'fs-extra';
import { constants } from '../../../tools';
import { ensureDirSync, writeFileSync } from 'fs-extra';
import { constants, loadFileAndReplaceKeywords } from '../../../tools';
import { dumpJSON, existsMustBeDir, isFile, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
Expand Down Expand Up @@ -67,7 +67,10 @@ function parse(context: DirectoryContext): ParsedPrompts {
(insertionAcc, { name, template }) => {
const templateFilePath = path.join(promptsDirectory, template);
insertionAcc[name] = isFile(templateFilePath)
? readFileSync(templateFilePath, 'utf8').trim()
? loadFileAndReplaceKeywords(templateFilePath, {
mappings: context.mappings,
disableKeywordReplacement: context.disableKeywordReplacement,
}).trim()
: '';
return insertionAcc;
},
Expand Down
103 changes: 103 additions & 0 deletions test/context/directory/prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,109 @@ describe('#directory context prompts', () => {
});
});

it('should replace keywords', async () => {
const files = {
[constants.PROMPTS_DIRECTORY]: {
[promptsSettingsFile]: JSON.stringify({
universal_login_experience: 'classic',
identifier_first: true,
}),
[customTextFile]: JSON.stringify({
en: {
login: {
login: {
buttonText: 'Hello, ##SOME_REPLACED_LITERAL##!',
description: 'english login description text',
},
},
},
}),
[partialsFile]: JSON.stringify({
login: [
{
login: [
{
name: 'form-content-start',
template: 'partials/login/login/form-content-start.liquid',
},
],
},
],
signup: [
{
signup: [
{
name: 'form-content-end',
template: 'partials/signup/signup/form-content-end.liquid',
},
],
},
],
}),
},
};

const repoDir = path.join(testDataDir, 'directory', 'prompts');
createDir(repoDir, files);

const partialsDir = path.join(
repoDir,
constants.PROMPTS_DIRECTORY,
constants.PARTIALS_DIRECTORY
);

const partialsFiles = {
login: {
login: {
'form-content-start.liquid': '<p>Hello, ##SOME_REPLACED_LITERAL##!</p>',
},
},
signup: {
signup: {
'form-content-end.liquid': '<script>const someArray = @@SOME_REPLACED_ARRAY@@;</script>',
},
},
};

createDirWithNestedDir(partialsDir, partialsFiles);

const config = {
AUTH0_INPUT_FILE: repoDir,
AUTH0_KEYWORD_REPLACE_MAPPINGS: {
SOME_REPLACED_LITERAL: 'world',
SOME_REPLACED_ARRAY: ['foo', 'bar', 'baz'],
},
};
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();
expect(context.assets.prompts).to.deep.equal({
universal_login_experience: 'classic',
identifier_first: true,
partials: {
login: {
login: {
'form-content-start': '<p>Hello, world!</p>',
},
},
signup: {
signup: {
'form-content-end': '<script>const someArray = ["foo","bar","baz"];</script>',
},
},
},
customText: {
en: {
login: {
login: {
buttonText: 'Hello, world!',
description: 'english login description text',
},
},
},
},
});
});

describe('should parse prompts even if one or both files are absent', async () => {
it('should parse prompts even if one or more files are absent', async () => {
cleanThenMkdir(promptsDirectory);
Expand Down
68 changes: 68 additions & 0 deletions test/context/yaml/prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,74 @@ describe('#YAML context prompts', () => {
});
});

it('should replace keywords', async () => {
const dir = path.join(testDataDir, 'yaml', 'prompts');
cleanThenMkdir(dir);

const yaml = `
prompts:
identifier_first: true
universal_login_experience: classic
customText:
en:
login:
login:
description: text in english
title: Hello, ##SOME_REPLACED_LITERAL##!
buttonText: Button text
partials:
login:
login:
form-content-end: >-
<p>Hello, ##SOME_REPLACED_LITERAL##!</p>
login-id:
login-id:
form-content-end: >-
<script>const someArray = @@SOME_REPLACED_ARRAY@@;</script>
`;

const yamlFile = path.join(dir, 'config.yaml');
fs.writeFileSync(yamlFile, yaml);

const config = {
AUTH0_INPUT_FILE: yamlFile,
AUTH0_KEYWORD_REPLACE_MAPPINGS: {
SOME_REPLACED_LITERAL: 'world',
SOME_REPLACED_ARRAY: ['foo', 'bar', 'baz'],
},
};
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();

expect(context.assets.prompts).to.deep.equal({
customText: {
en: {
login: {
login: {
buttonText: 'Button text',
description: 'text in english',
title: 'Hello, world!',
},
},
},
},
identifier_first: true,
partials: {
login: {
login: {
'form-content-end': '<p>Hello, world!</p>',
},
},
'login-id': {
'login-id': {
'form-content-end': '<script>const someArray = ["foo","bar","baz"];</script>',
},
},
},
universal_login_experience: 'classic',
});
});

it('should dump prompts settings and prompts custom text', async () => {
const context = new Context({ AUTH0_INPUT_FILE: './test.yml' }, mockMgmtClient());
context.assets.prompts = {
Expand Down
Loading