chore: fixing func
Signed-off-by: benjdlambert <ben@blam.sh> Signed-off-by: benjdlambert <ben@blam.sh>
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user