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 DatabasePostgresDelete extends Command {
static description = "Delete an application from a project.";
static description = "Delete a PostgreSQL database from a project.";
static examples = [
"$ <%= config.bin %> postgres delete",
@@ -28,80 +29,47 @@ export default class DatabasePostgresDelete 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 postgres 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.postgres.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 PostgreSQL database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
}
try {
const project = await getProject(projectId, auth, this);
if (!project.postgres || project.postgres.length === 0) {
this.log(
chalk.yellow("No PostgreSQL databases found in this project."),
);
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.postgres.map((app: any) => ({
name: app.name,
value: app.postgresId,
choices: project.postgres.map((db: any) => ({
name: db.name,
value: db.postgresId,
})),
message: "Select the postgres database to delete:",
message: "Select the PostgreSQL database to delete:",
name: "selectedApp",
type: "list",
},
@@ -109,7 +77,6 @@ export default class DatabasePostgresDelete extends Command {
const postgresId = appAnswers.selectedApp;
// Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
@@ -120,11 +87,10 @@ export default class DatabasePostgresDelete extends Command {
]);
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/postgres.remove`,
{
@@ -141,10 +107,10 @@ export default class DatabasePostgresDelete extends Command {
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
this.error(chalk.red("Error deleting PostgreSQL database"));
}
this.log(chalk.green("Application deleted successfully."));
this.log(chalk.green("PostgreSQL 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}`));