@@ -3,8 +3,14 @@ import axios from "axios";
3
3
import chalk from "chalk" ;
4
4
import inquirer from "inquirer" ;
5
5
6
+ import { type Project , getProjects } from "../../utils/shared.js" ;
7
+ import { slugify } from "../../utils/slug.js" ;
6
8
import { readAuthConfig } from "../../utils/utils.js" ;
7
9
10
+ export interface Answers {
11
+ project : Project ;
12
+ }
13
+
8
14
export default class AppCreate extends Command {
9
15
static description = "Create a new application within a project." ;
10
16
@@ -26,110 +32,72 @@ export default class AppCreate extends Command {
26
32
let { projectId } = flags ;
27
33
28
34
if ( ! projectId ) {
29
- // Obtener la lista de proyectos y permitir la selección
30
35
console . log ( chalk . blue . bold ( "\n Listing all Projects \n" ) ) ;
31
36
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
+ {
34
87
headers : {
35
88
Authorization : `Bearer ${ auth . token } ` ,
36
89
"Content-Type" : "application/json" ,
37
90
} ,
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
+ ) ;
63
93
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" ) ) ;
68
96
}
69
- }
70
97
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
+ ) ;
133
101
}
134
102
}
135
103
}
0 commit comments