feat: refactor file operations and add utility functions for file management

This commit is contained in:
Space-Banane
2026-04-05 17:46:38 +02:00
parent 5c18138dd2
commit e6e2203751
8 changed files with 143 additions and 82 deletions
+6 -14
View File
@@ -1,5 +1,6 @@
import chalk from "chalk";
import { getApiClient } from "../../api.js";
import { listFiles, handleAxiosError } from "../../utils/fileops.js";
export const fileListDefinition = {
name: "list",
@@ -11,10 +12,9 @@ export const fileListDefinition = {
const client = await getApiClient();
try {
const response = await client.get(`/api/function/${options.functionId}/files`);
const files = await listFiles(client, options.functionId);
if (response.status === 200 && response.data.data) {
const files = response.data.data;
if (files) {
if (!Array.isArray(files) || files.length === 0) {
console.log(`${chalk.yellow("!")} No files found.`);
@@ -24,23 +24,15 @@ export const fileListDefinition = {
console.log(chalk.blue("Files:"));
console.log(chalk.gray(`${"ID".padEnd(25)} ${"Filename"}`));
files.forEach((file: any) => {
console.log(`${chalk.cyan(file.id.padEnd(25))} ${chalk.white(file.filename)}`);
const id = String(file.id ?? file.name ?? "");
console.log(`${chalk.cyan(id.padEnd(25))} ${chalk.white(file.filename || file.name)}`);
});
console.log(`\n${chalk.green("✓")} Found ${chalk.bgGreen.black(` ${files.length} `)} files.`);
} else {
console.log(`${chalk.yellow("!")} Unexpected response from server.`);
console.log(`Status Code: ${chalk.blue(response.status)}`);
}
} catch (error: any) {
if (error.response) {
console.error(
`${chalk.red("✗")} Failed to list files: ${chalk.yellow(error.response.data?.message || "Unknown error")}`,
);
} else if (error.request) {
console.error(`${chalk.red("✗")} No response received from server.`);
} else {
console.error(`${chalk.red("✗")} Error: ${error.message}`);
}
handleAxiosError(error);
}
},
};