diff --git a/.changeset/heavy-monkeys-drum.md b/.changeset/heavy-monkeys-drum.md index 24124ad273..a58c3f291d 100644 --- a/.changeset/heavy-monkeys-drum.md +++ b/.changeset/heavy-monkeys-drum.md @@ -1,10 +1,10 @@ --- -'@backstage/plugin-scaffolder-backend': minor +'@backstage/plugin-scaffolder-backend': patch --- Add partial templating to `fetch:template` action. -If an `extension` input is given, only files with that extension get their content processed. If `extension` is `true`, the `.njk` extension is used. The `extension` input is incompatible with both `cookiecutterCompat` and `copyWithoutRender`. +If an `templateFileExtension` input is given, only files with that extension get their content processed. If `templateFileExtension` is `true`, the `.njk` extension is used. The `templateFileExtension` input is incompatible with both `cookiecutterCompat` and `copyWithoutRender`. All other files get copied. diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts index 7a094831ea..752212606f 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts @@ -110,7 +110,7 @@ describe('fetch:template', () => { action.handler( mockContext({ copyWithoutRender: ['abc'], - extension: true, + templateFileExtension: true, }), ), ).rejects.toThrowError( @@ -123,7 +123,7 @@ describe('fetch:template', () => { action.handler( mockContext({ cookiecutterCompat: true, - extension: true, + templateFileExtension: true, }), ), ).rejects.toThrowError( @@ -329,7 +329,7 @@ describe('fetch:template', () => { count: 1234, itemList: ['first', 'second', 'third'], }, - extension: true, + templateFileExtension: true, }); mockFetchContents.mockImplementation(({ outputPath }) => { @@ -406,7 +406,7 @@ describe('fetch:template', () => { beforeEach(async () => { context = mockContext({ - extension: '.jinja2', + templateFileExtension: '.jinja2', values: { name: 'test-project', count: 1234, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts index 5c55f02f13..316e032c00 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -44,7 +44,7 @@ type CookieCompatInput = { }; type ExtensionInput = { - extension?: string | boolean; + templateFileExtension?: string | boolean; }; export type FetchTemplateInput = { @@ -101,9 +101,10 @@ export function createFetchTemplateAction(options: { 'Enable features to maximise compatibility with templates built for fetch:cookiecutter', type: 'boolean', }, - extension: { - title: 'Extension to Process (.njk)', - description: 'Extension to use for templated files.', + templateFileExtension: { + title: 'Template File Extension', + description: + 'If set, only files with the given extension will be templated. If set to `true`, the default extension `.njk` is used.', type: ['string', 'boolean'], }, }, @@ -128,7 +129,7 @@ export function createFetchTemplateAction(options: { } if ( - ctx.input.extension && + ctx.input.templateFileExtension && (ctx.input.copyWithoutRender || ctx.input.cookiecutterCompat) ) { throw new InputError( @@ -137,8 +138,11 @@ export function createFetchTemplateAction(options: { } let extension: string | false = false; - if (ctx.input.extension) { - extension = ctx.input.extension === true ? '.njk' : ctx.input.extension; + if (ctx.input.templateFileExtension) { + extension = + ctx.input.templateFileExtension === true + ? '.njk' + : ctx.input.templateFileExtension; if (!extension.startsWith('.')) { extension = `.${extension}`; } @@ -220,8 +224,8 @@ export function createFetchTemplateAction(options: { ); for (const location of allEntriesInTemplate) { - let renderFilename = !nonTemplatedEntries.has(location); - let renderContents = renderFilename; + let renderFilename: boolean; + let renderContents: boolean; let localOutputPath = location; if (extension) { @@ -230,6 +234,8 @@ export function createFetchTemplateAction(options: { if (renderContents) { localOutputPath = localOutputPath.slice(0, -extension.length); } + } else { + renderFilename = renderContents = !nonTemplatedEntries.has(location); } if (renderFilename) { localOutputPath = templater.renderString(localOutputPath, context);