From 5d3a395f75a4728bd44351ed128c0aed2edd6801 Mon Sep 17 00:00:00 2001 From: benjdlambert Date: Wed, 4 Jun 2025 11:57:58 +0200 Subject: [PATCH] chore: fixing func Signed-off-by: benjdlambert Signed-off-by: benjdlambert --- .../actions/builtin/fetch/plainFile.ts | 3 +- .../actions/builtin/filesystem/rename.ts | 32 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/plainFile.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/plainFile.ts index 4eaa10d29a..c132214c10 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/plainFile.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/plainFile.ts @@ -63,8 +63,9 @@ export function createFetchPlainFileAction(options: { }, supportsDryRun: true, async handler(ctx) { - ctx.logger.info('Fetching plain file content from remote URL'); + ctx.logger.info('Fetching plain content from remote URL'); + // Finally move the template result into the task workspace const outputPath = resolveSafeChildPath( ctx.workspacePath, ctx.input.targetPath, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.ts index 213ac8bf39..7ced3ca6b0 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.ts @@ -18,6 +18,7 @@ import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import { resolveSafeChildPath } from '@backstage/backend-plugin-api'; import fs from 'fs-extra'; import { examples } from './rename.examples'; +import { InputError } from '@backstage/errors'; /** * Creates a new action that allows renames of files and directories in the workspace. @@ -55,29 +56,34 @@ export const createFilesystemRenameAction = () => { }, supportsDryRun: true, async handler(ctx) { + if (!Array.isArray(ctx.input?.files)) { + throw new InputError('files must be an Array'); + } + for (const file of ctx.input.files) { + if (!file.from || !file.to) { + throw new InputError('each file must have a from and to property'); + } const sourceFilepath = resolveSafeChildPath( ctx.workspacePath, file.from, ); const destFilepath = resolveSafeChildPath(ctx.workspacePath, file.to); - if (!fs.existsSync(sourceFilepath)) { - throw new Error( - `File ${sourceFilepath} does not exist, so rename cannot succeed.`, + try { + await fs.move(sourceFilepath, destFilepath, { + overwrite: file.overwrite ?? false, + }); + ctx.logger.info( + `File ${sourceFilepath} renamed to ${destFilepath} successfully`, ); - } - - const destExists = fs.existsSync(destFilepath); - - if (destExists && !file.overwrite) { - throw new Error( - `File ${destFilepath} already exists, refusing to overwrite.`, + } catch (err) { + ctx.logger.error( + `Failed to rename file ${sourceFilepath} to ${destFilepath}:`, + err, ); + throw err; } - - await fs.move(sourceFilepath, destFilepath, { overwrite: true }); - ctx.logger.info(`Moved file from ${sourceFilepath} to ${destFilepath}`); } }, });