generator-chek-ms
Version:
boilerplate de creación de microservicios en Chek
253 lines (231 loc) • 8.14 kB
JavaScript
"use strict";
const Generator = require("yeoman-generator");
const chalk = require("chalk");
const yosay = require("yosay");
const fs = require("fs");
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function bienvenidos(log){
log(
yosay(
`Bienvenido a la generación de ${chalk.red(
"Microservicios CHEK"
)} juegueeee!!`
)
);
}
function replaceAppModule(props, origin, destination) {
let archivo = fs.readFileSync(origin,"utf-8");
let contenido = archivo.toString();
let nuevoContenido = contenido.replace(
/Hello/g,
capitalizeFirstLetter(`${props.microservice}`)
);
nuevoContenido = nuevoContenido.replace(/hello/g,`${props.microservice}`);
fs.writeFile(destination, nuevoContenido, function(err) {
if (err) throw err;
});
}
function replaceJenkinsParams(props, origin, destination) {
let archivo = fs.readFileSync(origin,"utf-8");
let contenido = archivo.toString();
let nuevoContenido = contenido.replace(/<CLOUDRUNNAME>/g, `${props.name}`);
fs.writeFile(destination, nuevoContenido, function(err) {
if (err) throw err;
});
}
module.exports = class extends Generator {
prompting() {
// Have Yeoman greet the user.
bienvenidos(this.log);
const prompts = [
{
type: "input",
name: "name",
message: "Cuál será el nombre de tu proyecto?",
default: "my-microservice-chek"
},
{
type: "input",
name: "microservice",
message: "Cuál será el nombre de tu microservicio? (sólo letras)",
default: "myMicroservice"
},
{
type: "list",
name: "tipoms",
message: "De qué tipo es tu servicio?",
choices: ["SAGA", "AMBASSADOR", "MICROSERVICE"]
},
{
type: "confirm",
name: "confirmacion",
message: "Estás seguro que quieres continuar?",
default: true
}
];
return this.prompt(prompts).then(props => {
this.props = props;
});
}
writing() {
if (!this.props.confirmacion) process.exit();
this.fs.copyTpl(
this.templatePath("src/main.ts"),
this.destinationPath(`${this.props.name}/src/main.ts`)
);
this.fs.copyTpl(
this.templatePath("src/app.module.ts"),
this.destinationPath(`${this.props.name}/src/app.module.ts`)
);
this.fs.copy(
this.templatePath("src/core/**/*"),
this.destinationPath(`${this.props.name}/src/core`)
);
this.fs.copy(
this.templatePath("src/entities/**/*"),
this.destinationPath(`${this.props.name}/src/entities`)
);
this.fs.copy(
this.templatePath("src/hello/services/**/*"),
this.destinationPath(`${this.props.name}/src/${this.props.microservice}/services`)
);
this.fs.copy(
this.templatePath("src/hello/hello.controller.spec.ts"),
this.destinationPath(`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.controller.spec.ts`)
);
this.fs.copy(
this.templatePath("src/hello/hello.controller.ts"),
this.destinationPath(`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.controller.ts`)
);
this.fs.copy(
this.templatePath("src/hello/hello.module.ts"),
this.destinationPath(`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.module.ts`)
);
// Carpetas iniciales
this.fs.copy(
this.templatePath(".husky/**/*"),
this.destinationPath(`${this.props.name}/.husky`)
);
this.fs.copy(
this.templatePath(".devcontainer/**/*"),
this.destinationPath(`${this.props.name}/.devcontainer`)
);
this.fs.copy(
this.templatePath(".vscode/**/*"),
this.destinationPath(`${this.props.name}/.vscode`)
);
this.fs.copy(
this.templatePath("test/**/*"),
this.destinationPath(`${this.props.name}/test`)
);
// Copy config files, root folder
this.fs.copyTpl(
this.templatePath(".dockerignore"),
this.destinationPath(`${this.props.name}/.dockerignore`)
);
this.fs.copyTpl(
this.templatePath(".eslintignore"),
this.destinationPath(`${this.props.name}/.eslintignore`)
);
this.fs.copyTpl(
this.templatePath(".eslintrc.js"),
this.destinationPath(`${this.props.name}/.eslintrc.js`)
);
/*
this.fs.copyTpl(
this.templatePath("_gitignore"),
this.destinationPath(`${this.props.name}/.gitignore`)
);
*/
this.fs.copyTpl(
this.templatePath(".lintstagedrc.json"),
this.destinationPath(`${this.props.name}/.lintstagedrc.json`)
);
this.fs.copyTpl(
this.templatePath(".prettierignore"),
this.destinationPath(`${this.props.name}/.prettierignore`)
);
this.fs.copyTpl(
this.templatePath(".prettierrc"),
this.destinationPath(`${this.props.name}/.prettierrc`)
);
this.fs.copyTpl(
this.templatePath("Dockerfile"),
this.destinationPath(`${this.props.name}/Dockerfile`)
);
this.fs.copyTpl(
this.templatePath("jest.config.js"),
this.destinationPath(`${this.props.name}/jest.config.js`)
);
this.fs.copyTpl(
this.templatePath("nest-cli.json"),
this.destinationPath(`${this.props.name}/nest-cli.json`)
);
this.fs.copyTpl(
this.templatePath("tsconfig.json"),
this.destinationPath(`${this.props.name}/tsconfig.json`)
);
this.fs.copyTpl(
this.templatePath("README.md"),
this.destinationPath(`${this.props.name}/README.md`)
);
this.fs.copyTpl(
this.templatePath("package.json"),
this.destinationPath(`${this.props.name}/package.json`)
);
this.fs.copyTpl(
this.templatePath("package-lock.json"),
this.destinationPath(`${this.props.name}/package-lock.json`)
);
this.fs.copyTpl(
this.templatePath("tsconfig.build.json"),
this.destinationPath(`${this.props.name}/tsconfig.build.json`)
);
this.fs.copyTpl(
this.templatePath("yarn.lock"),
this.destinationPath(`${this.props.name}/yarn.lock`)
);
switch (this.props.tipoms) {
case "SAGA":
this.fs.copyTpl(
this.templatePath("JenkinsParams.saga"),
this.destinationPath(`${this.props.name}/JenkinsParams`)
);
break;
case "AMBASSADOR":
this.fs.copyTpl(
this.templatePath("JenkinsParams.ambassador"),
this.destinationPath(`${this.props.name}/JenkinsParams`)
);
break;
case "MICROSERVICE":
this.fs.copyTpl(
this.templatePath("JenkinsParams.microservice"),
this.destinationPath(`${this.props.name}/JenkinsParams`)
);
break;
default:
break;
}
}
install() {
/*
Var npmdir = `${process.cwd()}/${this.props.name}`;
this.yarnInstall([], {}, {cwd: npmdir});
*/
replaceAppModule(this.props,`${this.props.name}/src/app.module.ts`,`${this.props.name}/src/app.module.ts`);
replaceAppModule(this.props,`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.module.ts`,`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.module.ts`);
replaceAppModule(this.props,`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.controller.ts`,`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.controller.ts`);
replaceAppModule(this.props,`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.controller.spec.ts`,`${this.props.name}/src/${this.props.microservice}/${this.props.microservice}.controller.spec.ts`);
replaceJenkinsParams(this.props,`${this.props.name}/JenkinsParams`,`${this.props.name}/JenkinsParams`);
const nuevoContenido = "node_modules\ncoverage\n";
fs.writeFile(`${this.props.name}/.gitignore`, nuevoContenido, function(err) {
if (err) throw err;
});
var npmdir = `${process.cwd()}/${this.props.name}`;
process.chdir(npmdir);
this.npmInstall(["vue"], { loglevel: "error" });
}
};