From b284705dc01625f690af016ce5dbe69cf57af1a4 Mon Sep 17 00:00:00 2001 From: pitwegner Date: Tue, 17 Jan 2023 11:08:32 +0100 Subject: [PATCH 1/7] add template replace option Signed-off-by: pitwegner Signed-off-by: pitwegner --- .../src/scaffolder/actions/builtin/fetch/template.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 10a9d0c055..dc37b04f80 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -118,6 +118,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 +267,7 @@ export function createFetchTemplateAction(options: { } const outputPath = resolveSafeChildPath(outputDir, localOutputPath); - if (fs.existsSync(outputPath)) { + if (fs.existsSync(outputPath) && !ctx.input.replace) { continue; } From ac44b2676321826ee76f34d217ce050b7a1d51d8 Mon Sep 17 00:00:00 2001 From: pitwegner Date: Tue, 17 Jan 2023 13:26:42 +0100 Subject: [PATCH 2/7] add replace to signature Signed-off-by: pitwegner Signed-off-by: pitwegner --- .../src/scaffolder/actions/builtin/fetch/template.ts | 1 + 1 file changed, 1 insertion(+) 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 dc37b04f80..2ebcfd14c7 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -62,6 +62,7 @@ export function createFetchTemplateAction(options: { copyWithoutRender?: string[]; copyWithoutTemplating?: string[]; cookiecutterCompat?: boolean; + replace: boolean; }>({ id: 'fetch:template', description: From 0b2952ee4b3a05c2f7e9cebf9cd052ed1a629645 Mon Sep 17 00:00:00 2001 From: pitwegner Date: Tue, 17 Jan 2023 13:30:21 +0100 Subject: [PATCH 3/7] add changeset Signed-off-by: pitwegner Signed-off-by: pitwegner --- .changeset/perfect-cheetahs-serve.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/perfect-cheetahs-serve.md diff --git a/.changeset/perfect-cheetahs-serve.md b/.changeset/perfect-cheetahs-serve.md new file mode 100644 index 0000000000..2a031a65f7 --- /dev/null +++ b/.changeset/perfect-cheetahs-serve.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Added the option to overwrite files in the targetPath of the template:fetch action From 987d1f3f43cf0d356fda3dc134b77b73bf643f6e Mon Sep 17 00:00:00 2001 From: pitwegner Date: Tue, 17 Jan 2023 13:34:24 +0100 Subject: [PATCH 4/7] format keywords as code Signed-off-by: pitwegner Signed-off-by: pitwegner --- .changeset/perfect-cheetahs-serve.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/perfect-cheetahs-serve.md b/.changeset/perfect-cheetahs-serve.md index 2a031a65f7..bc1835e472 100644 --- a/.changeset/perfect-cheetahs-serve.md +++ b/.changeset/perfect-cheetahs-serve.md @@ -2,4 +2,4 @@ '@backstage/plugin-scaffolder-backend': patch --- -Added the option to overwrite files in the targetPath of the template:fetch action +Added the option to overwrite files in the `targetPath` of the `template:fetch` action From 6d435e5199de37838351406d19ce8f8e9d5a638f Mon Sep 17 00:00:00 2001 From: pitwegner Date: Tue, 17 Jan 2023 13:50:30 +0100 Subject: [PATCH 5/7] make arg optional Signed-off-by: pitwegner --- .../src/scaffolder/actions/builtin/fetch/template.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2ebcfd14c7..81b3f4577c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -62,7 +62,7 @@ export function createFetchTemplateAction(options: { copyWithoutRender?: string[]; copyWithoutTemplating?: string[]; cookiecutterCompat?: boolean; - replace: boolean; + replace?: boolean; }>({ id: 'fetch:template', description: From c5b529ed164b3d387e420db997120e55a048c76a Mon Sep 17 00:00:00 2001 From: pitwegner Date: Tue, 17 Jan 2023 15:55:47 +0100 Subject: [PATCH 6/7] add tests and apireport Signed-off-by: pitwegner --- plugins/scaffolder-backend/api-report.md | 1 + .../actions/builtin/fetch/template.test.ts | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 1a3c1db03f..7f8ab0ad79 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -127,6 +127,7 @@ export function createFetchTemplateAction(options: { copyWithoutRender?: string[] | undefined; copyWithoutTemplating?: string[] | undefined; cookiecutterCompat?: boolean | undefined; + replace?: boolean | undefined; }>; // @public 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 6b03290e1e..6658e1915d 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 @@ -669,4 +669,81 @@ describe('fetch:template', () => { ).resolves.toEqual('test-project: 1234'); }); }); + + describe('with replacement of existing files', () => { + let context: ActionContext; + + 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; + + 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'); + }); + }); }); From f666a8764a757cc5cf69422b579a5cf829397eb2 Mon Sep 17 00:00:00 2001 From: pitwegner Date: Wed, 18 Jan 2023 10:43:05 +0100 Subject: [PATCH 7/7] change version bump to minor Signed-off-by: pitwegner --- .changeset/perfect-cheetahs-serve.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/perfect-cheetahs-serve.md b/.changeset/perfect-cheetahs-serve.md index bc1835e472..08f7c1da21 100644 --- a/.changeset/perfect-cheetahs-serve.md +++ b/.changeset/perfect-cheetahs-serve.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-scaffolder-backend': patch +'@backstage/plugin-scaffolder-backend': minor --- Added the option to overwrite files in the `targetPath` of the `template:fetch` action