refactor: add commands

This commit is contained in:
Mauricio Siu
2024-06-22 00:52:17 -06:00
parent fbf6b7f5cc
commit d610c967d9
19 changed files with 779 additions and 1154 deletions

View File

@@ -4,9 +4,10 @@ import chalk from "chalk";
import inquirer from "inquirer";
import { readAuthConfig } from "../../../utils/utils.js";
import { getProject, getProjects } from "../../../utils/shared.js";
export default class DatabaseMongoDelete extends Command {
static description = "Delete an application from a project.";
static description = "Delete a MongoDB database from a project.";
static examples = [
"$ <%= config.bin %> mongo delete",
@@ -28,80 +29,45 @@ export default class DatabaseMongoDelete extends Command {
let { projectId } = flags;
if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
const projects = await getProjects(auth, this);
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
// Permitir al usuario seleccionar un proyecto
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the mongo database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
try {
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
const apps = response.data.result.data.json;
if (apps.mongo.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the MongoDB database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
}
try {
const project = await getProject(projectId, auth, this);
if (!project.mongo || project.mongo.length === 0) {
this.log(chalk.yellow("No MongoDB databases found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.mongo.map((app: any) => ({
name: app.name,
value: app.mongoId,
choices: project.mongo.map((db: any) => ({
name: db.name,
value: db.mongoId,
})),
message: "Select the mongo database to delete:",
message: "Select the MongoDB database to delete:",
name: "selectedApp",
type: "list",
},
@@ -109,22 +75,20 @@ export default class DatabaseMongoDelete extends Command {
const mongoId = appAnswers.selectedApp;
// Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this mongo database?",
message: "Are you sure you want to delete this MongoDB database?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
this.log(chalk.yellow("Database deletion cancelled."));
return;
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/mongo.remove`,
{
@@ -141,13 +105,15 @@ export default class DatabaseMongoDelete extends Command {
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
this.error(chalk.red("Error deleting MongoDB database"));
}
this.log(chalk.green("Application deleted successfully."));
this.log(chalk.green("MongoDB database deleted successfully."));
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to delete application: ${error.message}`));
this.error(
// @ts-ignore
chalk.red(`Failed to delete MongoDB database: ${error.message}`),
);
}
}
}