Merge pull request #15791 from pitwegner/patch-1

add template replace option
This commit is contained in:
Ben Lambert
2023-01-18 11:16:31 +01:00
committed by GitHub
4 changed files with 91 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': minor
---
Added the option to overwrite files in the `targetPath` of the `template:fetch` action
+1
View File
@@ -127,6 +127,7 @@ export function createFetchTemplateAction(options: {
copyWithoutRender?: string[] | undefined;
copyWithoutTemplating?: string[] | undefined;
cookiecutterCompat?: boolean | undefined;
replace?: boolean | undefined;
}>;
// @public
@@ -669,4 +669,81 @@ describe('fetch:template', () => {
).resolves.toEqual('test-project: 1234');
});
});
describe('with replacement of existing files', () => {
let context: ActionContext<FetchTemplateInput>;
beforeEach(async () => {
context = mockContext({
values: {
name: 'test-project',
count: 1234,
},
replace: true,
});
mockFetchContents.mockImplementation(({ outputPath }) => {
mockFs({
...realFiles,
[joinPath(workspacePath, 'target')]: {
'static-content.txt': 'static-content',
},
[outputPath]: {
'static-content.txt': '${{ values.name }}: ${{ values.count }}',
},
});
return Promise.resolve();
});
await action.handler(context);
});
afterEach(() => {
mockFs.restore();
});
it('overwrites existing file', async () => {
await expect(
fs.readFile(`${workspacePath}/target/static-content.txt`, 'utf-8'),
).resolves.toEqual('test-project: 1234');
});
});
describe('without replacement of existing files', () => {
let context: ActionContext<FetchTemplateInput>;
beforeEach(async () => {
context = mockContext({
values: {
name: 'test-project',
count: 1234,
},
targetPath: './target',
replace: false,
});
mockFetchContents.mockImplementation(({ outputPath }) => {
mockFs({
...realFiles,
[joinPath(workspacePath, 'target')]: {
'static-content.txt': 'static-content',
},
[outputPath]: {
'static-content.txt': '${{ values.name }}: ${{ values.count }}',
},
});
return Promise.resolve();
});
await action.handler(context);
});
it('keeps existing file', async () => {
await expect(
fs.readFile(`${workspacePath}/target/static-content.txt`, 'utf-8'),
).resolves.toEqual('static-content');
});
});
});
@@ -62,6 +62,7 @@ export function createFetchTemplateAction(options: {
copyWithoutRender?: string[];
copyWithoutTemplating?: string[];
cookiecutterCompat?: boolean;
replace?: boolean;
}>({
id: 'fetch:template',
description:
@@ -118,6 +119,12 @@ export function createFetchTemplateAction(options: {
'If set, only files with the given extension will be templated. If set to `true`, the default extension `.njk` is used.',
type: ['string', 'boolean'],
},
replace: {
title: 'Replace files',
description:
'If set, replace files in targetPath instead of skipping existing ones.',
type: 'boolean',
},
},
},
},
@@ -261,7 +268,7 @@ export function createFetchTemplateAction(options: {
}
const outputPath = resolveSafeChildPath(outputDir, localOutputPath);
if (fs.existsSync(outputPath)) {
if (fs.existsSync(outputPath) && !ctx.input.replace) {
continue;
}