From 1d17902107520820a5a104e543946b6c3521b2cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Can=CC=83ete?= Date: Mon, 4 Jul 2022 11:07:29 +0200 Subject: [PATCH] Fix issues with empty directories and files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel CaƱete --- .../actions/builtin/fetch/template.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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 a1afabc39e..15e1a6fd75 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -206,16 +206,17 @@ export function createFetchTemplateAction(options: { } else { renderFilename = renderContents = !nonTemplatedEntries.has(location); } + if (renderFilename) { localOutputPath = renderTemplate(localOutputPath, context); } - const outputPath = resolveSafeChildPath(outputDir, localOutputPath); - // variables have been expanded to make an empty file name - // this is due to a conditional like if values.my_condition then file-name.txt else empty string so skip - if (outputDir === outputPath) { + + if (containsSkippedContent(localOutputPath)) { continue; } + const outputPath = resolveSafeChildPath(outputDir, localOutputPath); + if (!renderContents && !extension) { ctx.logger.info( `Copying file/directory ${location} without processing.`, @@ -257,3 +258,12 @@ export function createFetchTemplateAction(options: { }, }); } + +function containsSkippedContent(localOutputPath: string): boolean { + // if the path starts with / means that the root directory has been skipped + // if the path is empty means that there is a file skipped in the root + // if the path includes // means that there is a subdirectory skipped + return localOutputPath.startsWith('/') + || localOutputPath === '' + || localOutputPath.includes('//'); +}