create-proyect-cli 2.2.0

CLI para crear proyectos rápidamente (Express, Rust, Python, Angular, Vue, React)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use inquire::{Confirm, Select, Text};

use crate::actions::*;

use crate::generators::router::create_project;

pub const VERSION: &str = "2.2.0";

pub fn print_help() {
    println!("\n=== AYUDA DEL CLI v{} ===\n", VERSION);
    println!("MODO INTERACTIVO:");
    println!("  Ejecutar sin argumentos para iniciar el modo interactivo");
    println!("  o usar: create-proyect-cli interactive\n");
    println!("MODO COMANDOS:");
    println!("  create-proyect-cli create --template=react --name=my-app");
    println!("  create-proyect-cli generate --template=vue --name=my-vue-app");
    println!("  create-proyect-cli new --template=api --name=my-api --orm=drizzle\n");
    println!("OPCIONES DE TEMPLATE:");
    println!("  react        - React con Vite");
    println!("  vue          - Vue con Vite");
    println!("  angular     - Angular");
    println!("  ionic       - Ionic");
    println!("  api         - API Express (TypeScript)");
    println!("  rust        - Rust");
    println!("  python      - Python\n");
    println!("OPCIONES DE API:");
    println!("  --orm=drizzle|prisma|typeorm  - ORM a usar");
    println!("  --database=mysql|postgresql|sqlite - Base de datos");
    println!("  --jwt       - Incluir autenticación JWT");
    println!("  --security  - Incluir seguridad (helmet, rate limit, validation)");
    println!("  --swagger   - Incluir Swagger");
    println!("  --jest     - Incluir Jest para testing");
    println!("  --winston  - Incluir Winston para logs");
    println!("  --docker   - Incluir Docker");
    println!("  --docker-compose - Incluir Docker Compose");
    println!("  --git      - Inicializar repositorio Git");
    println!("  --install  - Instalar dependencias automáticamente\n");
    println!("EJEMPLOS:");
    println!("  # Crear proyecto React");
    println!("  create-proyect-cli create --template=react --name=my-app --install");
    println!();
    println!("  # Crear API con Drizzle y todas las opciones");
    println!("  create-proyect-cli create --template=api --name=my-api \\");
    println!("    --orm=drizzle --database=postgresql --jwt --security --swagger --install\n");
    println!("Presione Enter para continuar...");
    let _ = Text::new("").with_default("").prompt();
}

pub fn handle_create_command(
    template: Option<String>,
    name: Option<String>,
    orm: Option<String>,
    database: Option<String>,
    jwt: bool,
    security: bool,
    swagger: bool,
    jest: bool,
    winston: bool,
    docker: bool,
    docker_compose: bool,
    git: bool,
    install: bool,
) {
    let template = template.unwrap_or_else(|| {
        let templates = vec!["react", "vue", "angular", "ionic", "api", "rust", "python"];
        let selected = Select::new("Seleccione el tipo de proyecto", templates)
            .prompt()
            .unwrap_or("react")
            .to_string();
        selected
    });

    let project_name = name.unwrap_or_else(|| {
        Text::new("Nombre del proyecto")
            .prompt()
            .unwrap_or_else(|_| "my-project".to_string())
    });

    match template.as_str() {
        "react" => {
            generate_react_with_vite(&project_name, install);
        }
        "vue" => {
            generate_vue_with_vite(&project_name, install);
        }
        "angular" => {
            create_project("Angular", &project_name);
        }
        "ionic" => {
            create_project("Ionic", &project_name);
        }
        "api" => {
            let orm_value = orm.unwrap_or_else(|| {
                let orm_options = vec![
                    "Ninguno".to_string(),
                    "Drizzle".to_string(),
                    "Prisma".to_string(),
                    "TypeORM".to_string(),
                ];
                Select::new("Seleccione el ORM", orm_options)
                    .prompt()
                    .unwrap_or_else(|_| "Ninguno".to_string())
            });

            let db = database.unwrap_or_else(|| {
                let db_options = vec![
                    "MySQL".to_string(),
                    "PostgreSQL".to_string(),
                    "SQLite".to_string(),
                ];
                Select::new("Seleccione la base de datos", db_options)
                    .prompt()
                    .unwrap_or_else(|_| "PostgreSQL".to_string())
            });

            let use_jwt = if !jwt {
                Confirm::new("¿Desea incluir autenticación JWT?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                jwt
            };

            let use_security = if !security {
                Confirm::new("¿Desea incluir seguridad (helmet, rate limit, validation)?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                security
            };

            let use_swagger = if !swagger {
                Confirm::new("¿Desea incluir Swagger?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                swagger
            };

            let use_jest = if !jest {
                Confirm::new("¿Desea incluir Jest para testing?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                jest
            };

            let use_winston = if !winston {
                Confirm::new("¿Desea incluir Winston para logs?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                winston
            };

            let use_docker = if !docker {
                Confirm::new("¿Desea incluir Docker?")
                    .with_default(false)
                    .prompt()
                    .unwrap_or(false)
            } else {
                docker
            };

            let use_docker_compose = if docker_compose {
                true
            } else if use_docker {
                Confirm::new("¿Desea incluir Docker Compose con base de datos?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                false
            };

            generate_express_api(
                &project_name,
                &db,
                &orm_value,
                use_jwt,
                use_security,
                use_swagger,
                use_jest,
                use_winston,
                use_docker,
                use_docker_compose,
            );

            if git {
                init_git_repo(&project_name);
            }

            if install {
                install_dependencies(&project_name);
            }
        }
        "rust" => {
            create_project("Rust", &project_name);
        }
        "python" => {
            create_project("Python", &project_name);
        }
        _ => {
            println!("Template '{}' no soportado", template);
        }
    }
}

fn generate_react_with_vite(name: &str, install: bool) {
    println!("\n🚀  Creando proyecto React con Vite: {}\n", name);

    let output = std::process::Command::new("cmd")
        .args([
            "/C",
            &format!("npx create-vite@latest {} --template react-ts", name),
        ])
        .output();

    match output {
        Ok(result) => {
            if result.status.success() {
                println!("✅  Proyecto React creado con Vite");
                if install {
                    install_dependencies(name);
                }
            } else {
                let error = String::from_utf8_lossy(&result.stderr);
                println!("❌  Error al crear proyecto: {}", error);
            }
        }
        Err(e) => {
            println!("❌  Error ejecutando npx: {}", e);
        }
    }
}

fn generate_vue_with_vite(name: &str, install: bool) {
    println!("\n🚀  Creando proyecto Vue con Vite: {}\n", name);

    let output = std::process::Command::new("cmd")
        .args([
            "/C",
            &format!("npx create-vite@latest {} --template vue-ts", name),
        ])
        .output();

    match output {
        Ok(result) => {
            if result.status.success() {
                println!("✅  Proyecto Vue creado con Vite");
                if install {
                    install_dependencies(name);
                }
            } else {
                let error = String::from_utf8_lossy(&result.stderr);
                println!("❌  Error al crear proyecto: {}", error);
            }
        }
        Err(e) => {
            println!("❌  Error ejecutando npx: {}", e);
        }
    }
}

fn generate_express_api(
    name: &str,
    db: &str,
    orm: &str,
    jwt: bool,
    security: bool,
    swagger: bool,
    jest: bool,
    winston: bool,
    docker: bool,
    docker_compose: bool,
) {
    use crate::generators::express_api::generate_express_api as gen_api;
    gen_api(
        name,
        db,
        orm,
        jwt,
        security,
        swagger,
        jest,
        winston,
        docker,
        docker_compose,
    );
}

fn init_git_repo(name: &str) {
    println!("\n🔧  Initializing git repository...");
    let output = std::process::Command::new("git")
        .args(["init"])
        .current_dir(name)
        .output();

    match output {
        Ok(result) => {
            if result.status.success() {
                println!("✅  Git repository initialized successfully!\n");
            } else {
                println!("⚠️  Failed to initialize git repository");
            }
        }
        Err(e) => {
            println!("⚠️  Error running git: {}", e);
        }
    }
}

pub fn start_cli() {
    loop {
        let actions = vec![
            "Clonar repositorio",
            "Crear proyecto",
            "Instalar dependencias",
            "Eliminar repositorio local",
            "Ayuda",
            "Salir",
        ];

        let action = Select::new(
            &format!("CLI v{} - ¿Qué acción desea realizar?", VERSION),
            actions,
        )
        .prompt()
        .unwrap();

        match action {
            "Clonar repositorio" => {
                let repo = Text::new("URL del repositorio").prompt().unwrap();

                let location = Text::new("Carpeta destino")
                    .with_default("./")
                    .prompt()
                    .unwrap();

                clone_repository(&repo, &location);
            }

            "Crear proyecto" => {
                let project_types = vec![
                    "React",
                    "Angular",
                    "Vue",
                    "Ionic",
                    "Rust",
                    "Python",
                    "API Express (TypeScript)",
                ];

                let project = Select::new("Seleccione el tipo de proyecto", project_types)
                    .prompt()
                    .unwrap();

                let name = Text::new("Nombre del proyecto").prompt().unwrap();

                create_project(project, &name);
            }

            "Instalar dependencias" => {
                let location = Text::new("Ruta del proyecto")
                    .with_default("./")
                    .prompt()
                    .unwrap();

                install_dependencies(&location);
            }

            "Eliminar repositorio local" => {
                let location = Text::new("Ruta del repositorio").prompt().unwrap();

                delete_repo(&location);
            }

            "Ayuda" => {
                println!("\n=== AYUDA DEL CLI v{} ===\n", VERSION);
                println!("MODO INTERACTIVO:");
                println!("  1. Clonar repositorio: Clona un repositorio git desde una URL a una carpeta local");
                println!("  2. Crear proyecto: Crea un nuevo proyecto según el tipo seleccionado");
                println!("     - React: Crea una aplicación React (usa Vite)");
                println!("     - Angular: Crea un proyecto Angular");
                println!("     - Vue: Crea un proyecto Vue (usa Vite)");
                println!("     - Ionic: Crea un proyecto Ionic");
                println!("     - Rust: Crea un proyecto Rust");
                println!("     - Python: Crea un proyecto Python");
                println!("     - API Express (TypeScript): Crea una API con Express y TypeScript");
                println!("       * Opciones: ORM (Drizzle/Prisma/TypeORM), JWT, Seguridad, Swagger, Jest, Winston, Docker, Docker Compose");
                println!("       * Seguridad: helmet, express-rate-limit, zod validation, CORS");
                println!("       * Arquitectura: SOLID (Domain/Application/Infrastructure layers)");
                println!("  3. Instalación de dependencias: Instala las dependencias del proyecto");
                println!("  4. Eliminar repositorio local: Elimina un repositorio local");
                println!("  5. Salir: Termina la ejecución del CLI\n");
                println!("MODO COMANDOS:");
                println!("  create-proyect-cli create --template=react --name=my-app");
                println!("  create-proyect-cli generate --template=vue --name=my-vue-app");
                println!("  create-proyect-cli new --template=api --name=my-api --orm=drizzle\n");
                println!("  Presione Enter para continuar...");
                let _ = Text::new("").with_default("").prompt();
            }

            "Salir" => {
                println!("¡Hasta luego!");
                break;
            }

            _ => {}
        }
    }
}