Skip to content

Commit d610c96

Browse files
committed
refactor: add commands
1 parent fbf6b7f commit d610c96

19 files changed

+755
-1130
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": ["oclif", "oclif-typescript", "prettier"]
2+
"extends": [ "prettier"]
33
}

config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"token": "5qdderpq2ejrjlu90hrnbjohvgc8j1u1k7i00um4",
2+
"token": "icsy0appti460sbh5be1sevami702rc8a57l2e8h",
33
"url": "http://localhost:3000"
44
}

src/commands/app/create.ts

+63-95
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import axios from "axios";
33
import chalk from "chalk";
44
import inquirer from "inquirer";
55

6+
import { type Project, getProjects } from "../../utils/shared.js";
7+
import { slugify } from "../../utils/slug.js";
68
import { readAuthConfig } from "../../utils/utils.js";
79

10+
export interface Answers {
11+
project: Project;
12+
}
13+
814
export default class AppCreate extends Command {
915
static description = "Create a new application within a project.";
1016

@@ -26,110 +32,72 @@ export default class AppCreate extends Command {
2632
let { projectId } = flags;
2733

2834
if (!projectId) {
29-
// Obtener la lista de proyectos y permitir la selección
3035
console.log(chalk.blue.bold("\n Listing all Projects \n"));
3136

32-
try {
33-
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
37+
const projects = await getProjects(auth, this);
38+
39+
const { project } = await inquirer.prompt<Answers>([
40+
{
41+
choices: projects.map((project) => ({
42+
name: project.name,
43+
value: project,
44+
})),
45+
message: "Select a project to create the application in:",
46+
name: "project",
47+
type: "list",
48+
},
49+
]);
50+
51+
projectId = project.projectId;
52+
53+
const appDetails = await inquirer.prompt([
54+
{
55+
message: "Enter the application name:",
56+
name: "name",
57+
type: "input",
58+
validate: (input) => (input ? true : "Application name is required"),
59+
},
60+
{
61+
message: "Enter the application description (optional):",
62+
name: "appDescription",
63+
type: "input",
64+
},
65+
]);
66+
67+
const appName = await inquirer.prompt([
68+
{
69+
default: `${slugify(project.name)}-${appDetails.name}`,
70+
message: "Enter the App name: (optional):",
71+
name: "appName",
72+
type: "input",
73+
validate: (input) => (input ? true : "App name is required"),
74+
},
75+
]);
76+
77+
const response = await axios.post(
78+
`${auth.url}/api/trpc/application.create`,
79+
{
80+
json: {
81+
...appDetails,
82+
appName: appName.appName,
83+
projectId: project.projectId,
84+
},
85+
},
86+
{
3487
headers: {
3588
Authorization: `Bearer ${auth.token}`,
3689
"Content-Type": "application/json",
3790
},
38-
});
39-
40-
if (!response.data.result.data.json) {
41-
this.error(chalk.red("Error fetching projects"));
42-
}
43-
44-
const projects = response.data.result.data.json;
45-
46-
if (projects.length === 0) {
47-
this.log(chalk.yellow("No projects found."));
48-
return;
49-
}
50-
51-
// Permitir al usuario seleccionar un proyecto
52-
const answers = await inquirer.prompt([
53-
{
54-
choices: projects.map((project: any) => ({
55-
name: project.name,
56-
value: project.projectId,
57-
})),
58-
message: "Select a project to create the database in:",
59-
name: "selectedProject",
60-
type: "list",
61-
},
62-
]);
91+
},
92+
);
6393

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

71-
const databases = ["postgres", "mysql", "redis", "mariadb", "mongo"];
72-
73-
const databaseSelect = await inquirer.prompt([
74-
{
75-
choices: databases.map((database: any) => ({
76-
name: database,
77-
value: database,
78-
})),
79-
message: "Select a database to create the application in:",
80-
name: "selectedDatabase",
81-
type: "list",
82-
},
83-
]);
84-
85-
const urlSelected = `${auth.url}/api/trpc/${databaseSelect.selectedDatabase}.create`;
86-
87-
// Solicitar detalles de la nueva aplicación
88-
const appDetails = await inquirer.prompt([
89-
{
90-
message: "Enter the database name:",
91-
name: "appName",
92-
type: "input",
93-
validate: (input) => (input ? true : "Database name is required"),
94-
},
95-
{
96-
message: "Enter the database description (optional):",
97-
name: "appDescription",
98-
type: "input",
99-
},
100-
]);
101-
102-
const { appDescription, appName } = appDetails;
103-
104-
// Crear la aplicación en el proyecto seleccionado
105-
try {
106-
// const response = await axios.post(
107-
// `${auth.url}/api/trpc/application.create`,
108-
// {
109-
// json: {
110-
// description: appDescription,
111-
// name: appName,
112-
// projectId,
113-
// },
114-
// },
115-
// {
116-
// headers: {
117-
// Authorization: `Bearer ${auth.token}`,
118-
// "Content-Type": "application/json",
119-
// },
120-
// },
121-
// );
122-
// if (!response.data.result.data.json) {
123-
// this.error(chalk.red("Error creating application"));
124-
// }
125-
// this.log(
126-
// chalk.green(
127-
// `Application '${appName}' created successfully in project ID '${projectId}'.`,
128-
// ),
129-
// );
130-
} catch (error) {
131-
// @ts-expect-error TODO: Fix this
132-
this.error(chalk.red(`Failed to create application: ${error.message}`));
98+
this.log(
99+
chalk.green(`Application '${appDetails.name}' created successfully.`),
100+
);
133101
}
134102
}
135103
}

src/commands/app/delete.ts

+21-68
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import axios from "axios";
33
import chalk from "chalk";
44
import inquirer from "inquirer";
55

6+
import { getProject, getProjects } from "../../utils/shared.js";
67
import { readAuthConfig } from "../../utils/utils.js";
8+
import type { Answers } from "./create.js";
79

810
export default class AppDelete extends Command {
911
static description = "Delete an application from a project.";
@@ -29,77 +31,33 @@ export default class AppDelete extends Command {
2931
let { projectId } = flags;
3032

3133
if (!projectId) {
32-
// Obtener la lista de proyectos y permitir la selección
3334
console.log(chalk.blue.bold("\n Listing all Projects \n"));
3435

35-
try {
36-
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
37-
headers: {
38-
Authorization: `Bearer ${auth.token}`,
39-
"Content-Type": "application/json",
40-
},
41-
});
42-
43-
if (!response.data.result.data.json) {
44-
this.error(chalk.red("Error fetching projects"));
45-
}
46-
47-
const projects = response.data.result.data.json;
48-
49-
if (projects.length === 0) {
50-
this.log(chalk.yellow("No projects found."));
51-
return;
52-
}
53-
54-
// Permitir al usuario seleccionar un proyecto
55-
const answers = await inquirer.prompt([
56-
{
57-
choices: projects.map((project: any) => ({
58-
name: project.name,
59-
value: project.projectId,
60-
})),
61-
message: "Select a project to delete the application from:",
62-
name: "selectedProject",
63-
type: "list",
64-
},
65-
]);
66-
67-
projectId = answers.selectedProject;
68-
} catch (error) {
69-
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
70-
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
71-
}
72-
}
36+
const projects = await getProjects(auth, this);
7337

74-
// Obtener la lista de aplicaciones del proyecto seleccionado
75-
try {
76-
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
77-
headers: {
78-
Authorization: `Bearer ${auth.token}`,
79-
"Content-Type": "application/json",
80-
},
81-
params: {
82-
input: JSON.stringify({
83-
json: { projectId },
84-
}),
38+
const { project } = await inquirer.prompt<Answers>([
39+
{
40+
choices: projects.map((project) => ({
41+
name: project.name,
42+
value: project,
43+
})),
44+
message: "Select a project to create the application in:",
45+
name: "project",
46+
type: "list",
8547
},
86-
});
87-
88-
if (!response.data.result.data.json) {
89-
this.error(chalk.red("Error fetching applications"));
90-
}
48+
]);
9149

92-
const apps = response.data.result.data.json;
50+
projectId = project.projectId;
51+
const projectSelected = await getProject(projectId, auth, this);
9352

94-
if (apps.applications.length === 0) {
95-
this.log(chalk.yellow("No applications found in this project."));
96-
return;
53+
if (projectSelected.applications.length === 0) {
54+
this.error(chalk.yellow("No applications found in this project."));
9755
}
9856

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

11270
const applicationId = appAnswers.selectedApp;
11371

114-
// Confirmar eliminación
72+
// // Confirmar eliminación
11573
const confirmAnswers = await inquirer.prompt([
11674
{
11775
default: false,
@@ -122,11 +80,9 @@ export default class AppDelete extends Command {
12280
]);
12381

12482
if (!confirmAnswers.confirmDelete) {
125-
this.log(chalk.yellow("Application deletion cancelled."));
126-
return;
83+
this.error(chalk.yellow("Application deletion cancelled."));
12784
}
12885

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

149105
this.log(chalk.green("Application deleted successfully."));
150-
} catch (error) {
151-
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
152-
this.error(chalk.red(`Failed to delete application: ${error.message}`));
153106
}
154107
}
155108
}

0 commit comments

Comments
 (0)