scaffolder: prefix input values in fetch:template templates

Signed-off-by: Mike Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
Mike Lewis
2021-07-07 11:26:00 +01:00
parent 51513c6420
commit 56b61c2bc0
3 changed files with 20 additions and 16 deletions
@@ -41,10 +41,10 @@ verbose template variables expressions.
`fetch:cookiecutter` to `fetch:template`.
2. Update variable syntax in file names and content. `fetch:cookiecutter`
expects variables to be enclosed in `{{` `}}` and prefixed with
`cookiecutter.`, while `fetch:template` doesn't require prefixing and expects
variables to be enclosed in `${{` `}}`. For example, a reference to variable
`cookiecutter.`, while `fetch:template` expects variables to be enclosed in
`${{` `}}` and prefixed with `values.`. For example, a reference to variable
`myInputVariable` would need to be migrated from
`{{ cookiecutter.myInputVariable }}` to `${{ myInputVariable }}`.
`{{ cookiecutter.myInputVariable }}` to `${{ values.myInputVariable }}`.
3. Replace uses of `jsonify` with `dump`. The `jsonify` filter is built in to
`cookiecutter`, and is not available by default when using `fetch:template`.
The `dump` filter is equivalent, so an expression like
@@ -120,13 +120,14 @@ describe('fetch:template', () => {
mockFetchContents.mockImplementation(({ outputPath }) => {
mockFs({
[outputPath]: {
'empty-dir-${{ count }}': {},
'empty-dir-${{ values.count }}': {},
'static.txt': 'static content',
'${{ name }}.txt': 'static content',
'${{ values.name }}.txt': 'static content',
subdir: {
'templated-content.txt': '${{ name }}: ${{ count }}',
'templated-content.txt':
'${{ values.name }}: ${{ values.count }}',
},
'.${{ name }}': '${{ itemList | dump }}',
'.${{ values.name }}': '${{ values.itemList | dump }}',
'a-binary-file.png': aBinaryFile,
},
});
@@ -202,10 +203,12 @@ describe('fetch:template', () => {
mockFs({
[outputPath]: {
processed: {
'templated-content-${{ name }}.txt': '${{ count }}',
'templated-content-${{ values.name }}.txt':
'${{ values.count }}',
},
'.unprocessed': {
'templated-content-${{ name }}.txt': '${{ count }}',
'templated-content-${{ values.name }}.txt':
'${{ values.count }}',
},
},
});
@@ -219,10 +222,10 @@ describe('fetch:template', () => {
it('ignores template syntax in files matched in copyWithoutRender', async () => {
await expect(
fs.readFile(
`${workspacePath}/target/.unprocessed/templated-content-\${{ name }}.txt`,
`${workspacePath}/target/.unprocessed/templated-content-\${{ values.name }}.txt`,
'utf-8',
),
).resolves.toEqual('${{ count }}');
).resolves.toEqual('${{ values.count }}');
});
it('processes files not matched in copyWithoutRender', async () => {
@@ -180,9 +180,10 @@ export function createFetchTemplateAction(options: {
// `cookiecutter.`. To replicate this, we wrap our parameters
// in an object with a `cookiecutter` property when compat
// mode is enabled.
const parameters = ctx.input.cookiecutterCompat
? { cookiecutter: ctx.input.values }
: ctx.input.values;
const { cookiecutterCompat, values } = ctx.input;
const context = {
[cookiecutterCompat ? 'cookiecutter' : 'values']: values,
};
ctx.logger.info(
`Processing ${allEntriesInTemplate.length} template files/directories with input values`,
@@ -196,7 +197,7 @@ export function createFetchTemplateAction(options: {
outputDir,
shouldCopyWithoutRender
? location
: templater.renderString(location, parameters),
: templater.renderString(location, context),
);
if (shouldCopyWithoutRender) {
@@ -227,7 +228,7 @@ export function createFetchTemplateAction(options: {
outputPath,
shouldCopyWithoutRender
? inputFileContents
: templater.renderString(inputFileContents, parameters),
: templater.renderString(inputFileContents, context),
);
}
}