Skip to content

Commit

Permalink
refactor: add commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jun 22, 2024
1 parent fbf6b7f commit d610c96
Show file tree
Hide file tree
Showing 19 changed files with 755 additions and 1,130 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": ["oclif", "oclif-typescript", "prettier"]
"extends": [ "prettier"]
}
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"token": "5qdderpq2ejrjlu90hrnbjohvgc8j1u1k7i00um4",
"token": "icsy0appti460sbh5be1sevami702rc8a57l2e8h",
"url": "http://localhost:3000"
}
158 changes: 63 additions & 95 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";

import { type Project, getProjects } from "../../utils/shared.js";
import { slugify } from "../../utils/slug.js";
import { readAuthConfig } from "../../utils/utils.js";

export interface Answers {
project: Project;
}

export default class AppCreate extends Command {
static description = "Create a new application within a project.";

Expand All @@ -26,110 +32,72 @@ export default class AppCreate 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`, {
const projects = await getProjects(auth, this);

const { project } = await inquirer.prompt<Answers>([
{
choices: projects.map((project) => ({
name: project.name,
value: project,
})),
message: "Select a project to create the application in:",
name: "project",
type: "list",
},
]);

projectId = project.projectId;

const appDetails = await inquirer.prompt([
{
message: "Enter the application name:",
name: "name",
type: "input",
validate: (input) => (input ? true : "Application name is required"),
},
{
message: "Enter the application description (optional):",
name: "appDescription",
type: "input",
},
]);

const appName = await inquirer.prompt([
{
default: `${slugify(project.name)}-${appDetails.name}`,
message: "Enter the App name: (optional):",
name: "appName",
type: "input",
validate: (input) => (input ? true : "App name is required"),
},
]);

const response = await axios.post(
`${auth.url}/api/trpc/application.create`,
{
json: {
...appDetails,
appName: appName.appName,
projectId: project.projectId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});

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 create the database in:",
name: "selectedProject",
type: "list",
},
]);
},
);

projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error TODO: Fix this
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
if (response.status !== 200) {
this.error(chalk.red("Error creating application"));
}
}

const databases = ["postgres", "mysql", "redis", "mariadb", "mongo"];

const databaseSelect = await inquirer.prompt([
{
choices: databases.map((database: any) => ({
name: database,
value: database,
})),
message: "Select a database to create the application in:",
name: "selectedDatabase",
type: "list",
},
]);

const urlSelected = `${auth.url}/api/trpc/${databaseSelect.selectedDatabase}.create`;

// Solicitar detalles de la nueva aplicación
const appDetails = await inquirer.prompt([
{
message: "Enter the database name:",
name: "appName",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
},
{
message: "Enter the database description (optional):",
name: "appDescription",
type: "input",
},
]);

const { appDescription, appName } = appDetails;

// Crear la aplicación en el proyecto seleccionado
try {
// const response = await axios.post(
// `${auth.url}/api/trpc/application.create`,
// {
// json: {
// description: appDescription,
// name: appName,
// projectId,
// },
// },
// {
// headers: {
// Authorization: `Bearer ${auth.token}`,
// "Content-Type": "application/json",
// },
// },
// );
// if (!response.data.result.data.json) {
// this.error(chalk.red("Error creating application"));
// }
// this.log(
// chalk.green(
// `Application '${appName}' created successfully in project ID '${projectId}'.`,
// ),
// );
} catch (error) {
// @ts-expect-error TODO: Fix this
this.error(chalk.red(`Failed to create application: ${error.message}`));
this.log(
chalk.green(`Application '${appDetails.name}' created successfully.`),
);
}
}
}
89 changes: 21 additions & 68 deletions src/commands/app/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";

import { getProject, getProjects } from "../../utils/shared.js";
import { readAuthConfig } from "../../utils/utils.js";
import type { Answers } from "./create.js";

export default class AppDelete extends Command {
static description = "Delete an application from a project.";
Expand All @@ -29,77 +31,33 @@ export default class AppDelete 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",
},
});

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 application 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}`));
}
}
const projects = await getProjects(auth, this);

// Obtener la lista de aplicaciones del proyecto seleccionado
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 },
}),
const { project } = await inquirer.prompt<Answers>([
{
choices: projects.map((project) => ({
name: project.name,
value: project,
})),
message: "Select a project to create the application in:",
name: "project",
type: "list",
},
});

if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
]);

const apps = response.data.result.data.json;
projectId = project.projectId;
const projectSelected = await getProject(projectId, auth, this);

if (apps.applications.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
return;
if (projectSelected.applications.length === 0) {
this.error(chalk.yellow("No applications found in this project."));
}

// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.applications.map((app: any) => ({
// @ts-ignore
choices: projectSelected.applications.map((app) => ({
name: app.name,
value: app.applicationId,
})),
Expand All @@ -111,7 +69,7 @@ export default class AppDelete extends Command {

const applicationId = appAnswers.selectedApp;

// Confirmar eliminación
// // Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
Expand All @@ -122,11 +80,9 @@ export default class AppDelete extends Command {
]);

if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
this.error(chalk.yellow("Application deletion cancelled."));
}

// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/application.delete`,
{
Expand All @@ -147,9 +103,6 @@ export default class AppDelete extends Command {
}

this.log(chalk.green("Application 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}`));
}
}
}
Loading

0 comments on commit d610c96

Please sign in to comment.