|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const shelljs = require('shelljs'); |
| 4 | +const chalk = require('chalk'); |
| 5 | +const figlet = require('figlet'); |
| 6 | +const inquirer = require('inquirer'); |
| 7 | + |
| 8 | +ficheroOK = filepath => { |
| 9 | + console.log( |
| 10 | + chalk.white.bgGreen.bold( |
| 11 | + `¡Muy bien! Fichero correctamente creado en el directorio ${filepath}` |
| 12 | + ) |
| 13 | + ); |
| 14 | +} |
| 15 | +const crearFichero = (nombreFichero, extension) => { |
| 16 | + const pathFichero = `${process.cwd()}/${nombreFichero}.${extension}`; |
| 17 | + shelljs.touch(pathFichero); |
| 18 | + return pathFichero; |
| 19 | +} |
| 20 | +const hacerPreguntas = () => { |
| 21 | + const preguntas = [ |
| 22 | + { |
| 23 | + name: "FICHERO", |
| 24 | + type: "input", |
| 25 | + message: "¿Cómo se va a llamar tu fichero? (sin la extensión)" |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "EXTENSION", |
| 29 | + type: "list", |
| 30 | + message: "¿Qué extensión tiene tu fichero", |
| 31 | + choices: [".rb", ".js", ".kt", ".java", ".ts", ".php"], |
| 32 | + filter: function(val) { |
| 33 | + return val.split(".")[1]; |
| 34 | + } |
| 35 | + } |
| 36 | + ]; |
| 37 | + return inquirer.prompt(preguntas); |
| 38 | +} |
| 39 | +const iniciar = () => { |
| 40 | + console.log( |
| 41 | + chalk.green( |
| 42 | + figlet.textSync( "Creador Ficheros", { |
| 43 | + font: "Bubble", |
| 44 | + horizontalLayout: "default", |
| 45 | + verticalLayout: "default" |
| 46 | + }) |
| 47 | + ) |
| 48 | + ); |
| 49 | +}; |
| 50 | +const ejecutar = async () => { |
| 51 | + // Mostrar la información de la librería en la cabecera, el título con figlet |
| 52 | + iniciar(); |
| 53 | + // Preguntas necesarias para crear el fichero, es decir, el nombre y la extensión |
| 54 | + const respuestas = await hacerPreguntas(); |
| 55 | + const { FICHERO, EXTENSION } = respuestas; |
| 56 | + console.log(respuestas); |
| 57 | + // Creamos el fichero |
| 58 | + const pathFichero = crearFichero(FICHERO, EXTENSION); |
| 59 | + // Añadimos mensaje que fichero se ha creado correctamente |
| 60 | + ficheroOK(pathFichero); |
| 61 | +}; |
| 62 | + |
| 63 | + |
| 64 | +ejecutar(); |
0 commit comments