@bmstravel/nvp-cli
Version:
🏆 Command line tool
456 lines (454 loc) • 17.1 kB
JavaScript
import _ from "lodash";
import handlebarHelpers from "handlebars-helpers";
import path from "path";
var _dirname = process.cwd();
function likesFood(aFood) {
return function (answers) {
return answers.serviceType == aFood;
};
}
function hasId(val) {
return function (answers) {
return answers.hasId == val;
};
}
export default function (plop) {
// const helpers = require("handlebars-helpers")()
var helpers = handlebarHelpers();
// @ts-ignore
plop.setHelper(helpers);
// -------------------------- Model Generator --------------------------
plop.setGenerator("model", {
description: "Create a model",
prompts: [{
type: "list",
name: "kindModel",
message: "What a kind of model?",
choices: ["sequelize", "mongose"]
}, {
type: "input",
name: "name",
message: "What is your model name?"
}, {
type: "input",
name: "tableName",
message: "What is your table name?"
}, {
type: "confirm",
name: "hasId",
message: "Do you want create model with id field?"
}, {
type: "list",
name: "dataTypeId",
message: "What a type of id?",
choices: ["integer", "uuid"],
filter: function filter(val) {
return val.toLowerCase();
},
when: hasId(true)
}, {
type: "confirm",
name: "hasCrudDate",
message: "Do you want create model with created,updated,deleted field?"
}],
actions: function actions(data) {
var actions = [];
if (data.kindModel == "sequelize") {
actions = actions.concat([{
type: "add",
path: path.join(_dirname, "src/models/sequelize/{{camelCase name}}.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/schemas/model.hbs"),
skipIfExists: true,
force: true
}, {
type: "add",
path: path.join(_dirname, "src/types/define/{{camelCase name}}.d.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/type.d.ts.hbs"),
skipIfExists: true,
force: true
}, {
type: "append",
path: path.join(_dirname, "src/types/define/{{camelCase name}}.d.ts"),
pattern: "/* PLOP_INJECT_IMPORT */",
template: 'import { STATUS } from "@src/common"'
}, {
type: "append",
path: path.join(_dirname, "src/types/define/{{camelCase name}}.d.ts"),
pattern: "/* MOL_MODEL_ZONE */",
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/schemas/types/model.type.hbs")
}, {
type: "append",
path: path.join(_dirname, "src/types/index.ts"),
pattern: "/* PLOP_INJECT_IMPORT */",
template: 'export { I{{pascalCase name}} } from "{{pascalCase name}}Service.I{{pascalCase name}}";'
}]);
}
return actions;
}
});
// -------------------------- Service Generator --------------------------
plop.setGenerator("service", {
description: "Create a service",
prompts: [{
type: "input",
name: "serviceName",
message: "What is your service name?"
}, {
type: "list",
name: "serviceVersion",
message: "What a service version?",
choices: ["none", "v1", "v2", "v3", "v4", "v5"],
filter: function filter(val) {
return val.toLowerCase();
}
}, {
type: "list",
name: "serviceType",
message: "What a service do you need?",
choices: ["with_model", "no_model"],
filter: function filter(val) {
return val.toLowerCase();
}
}, {
type: "input",
name: "model",
message: "What is your model table name?",
when: likesFood("with_model")
}, {
type: "confirm",
name: "hasHelloAction",
message: "Do you want create service which has example hello action?"
}, {
type: "confirm",
name: "hasSeedDb",
message: "Do you want create service with seedDb when first initialize table?",
when: likesFood("with_model")
}],
actions: function actions(data) {
var actions = [];
if (data.serviceType === "with_model") {
actions = actions.concat([{
type: "add",
path: path.join(_dirname, "src/services/{{camelCase serviceName}}.service.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/services/serviceWithModel.hbs"),
skipIfExists: false,
force: true
}, {
type: "add",
path: path.join(_dirname, "src/types/define/{{camelCase serviceName}}.d.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/type.d.ts.hbs"),
skipIfExists: true,
force: true
}, {
type: "append",
path: path.join(_dirname, "src/types/define/{{camelCase serviceName}}.d.ts"),
pattern: "/* PLOP_INJECT_IMPORT */",
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/services/types/modelWithModel.type.import.hbs")
}, {
type: "append",
path: path.join(_dirname, "src/types/define/{{camelCase serviceName}}.d.ts"),
pattern: "/* MOL_SERVICE_ZONE */",
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/services/types/modelWithModel.hbs")
}]);
} else {
actions = actions.concat([{
type: "add",
path: path.join(_dirname, "src/services/{{camelCase serviceName}}.service.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/services/service.hbs"),
skipIfExists: false,
force: true
}, {
type: "add",
path: path.join(_dirname, "src/types/define/{{camelCase serviceName}}.d.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/type.d.ts.hbs"),
skipIfExists: true,
force: true
}, {
type: "append",
path: path.join(_dirname, "src/types/define/{{camelCase serviceName}}.d.ts"),
pattern: "/* PLOP_INJECT_IMPORT */",
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/services/types/model.type.import.hbs")
}, {
type: "append",
path: path.join(_dirname, "src/types/define/{{camelCase serviceName}}.d.ts"),
pattern: "/* MOL_SERVICE_ZONE */",
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/services/types/model.hbs")
}]);
}
return actions;
}
});
// -------------------------- Action Generator --------------------------
plop.setGenerator("action", {
description: "Create a action of service",
prompts: [{
type: "input",
name: "actionName",
message: "What is your action name?"
}, {
type: "input",
name: "serviceName",
message: "What is your service name?"
}, {
type: "list",
name: "serviceVersion",
message: "What a service version?",
choices: ["none", "v1", "v2", "v3", "v4", "v5"],
filter: function filter(val) {
return val.toLowerCase();
}
}, {
type: "input",
name: "actionPath",
message: "What is path of action?"
}, {
type: "list",
name: "method",
message: "Choose a method",
choices: ["GET", "POST", "PUT", "PATCH", "DELETE"],
when: function when(answers) {
return answers.actionPath;
}
}, {
type: "list",
name: "visibility",
message: "Choose a visibility",
choices: ["published", "public", "protected", "private"]
}],
actions: function actions(data) {
var serviceVersion = data.serviceVersion;
if (serviceVersion === "none" || serviceVersion === "v1") {
serviceVersion = "";
} else {
serviceVersion = ".".concat(serviceVersion);
}
var pathAction = data.actionPath;
var missingPath = "";
if (pathAction && pathAction != "") {
var arrFolderAction = pathAction.split("/").filter(function (p) {
return p != "";
});
arrFolderAction = arrFolderAction.slice(0, arrFolderAction.length - 1).map(function (p) {
return _.camelCase(p);
});
missingPath = arrFolderAction.join("/");
}
var acName = "".concat(missingPath.split(",").map(function (i) {
return _.camelCase(i);
}).join(".").toString(), ".").concat(_.camelCase(data.actionName));
var actions = [];
actions = actions.concat([{
type: "add",
path: path.join(_dirname, missingPath != "" ? "src/helpers/serviceActions/{{camelCase serviceName}}".concat(serviceVersion, "/").concat(missingPath, "/{{camelCase actionName}}.action.ts") : "src/helpers/serviceActions/{{camelCase serviceName}}".concat(serviceVersion, "/{{camelCase actionName}}.action.ts")),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/actions/action.hbs"),
skipIfExists: false,
force: true
}
// {
// type: "append",
// path: path.join(_dirname, "src/types/{{camelCase serviceName}}.ts"),
// pattern: `/* MOL_ACTION */`,
// template: `{{ ${acName} }}: ServiceActionsSchema`,
// },
]);
return actions;
}
});
// -------------------------- Method Generator --------------------------
plop.setGenerator("method", {
description: "Create a method of service",
prompts: [{
type: "input",
name: "methodName",
message: "What is your method name?"
}, {
type: "input",
name: "serviceName",
message: "What is your service name?"
}, {
type: "list",
name: "serviceVersion",
message: "What a service version?",
choices: ["none", "v1", "v2", "v3", "v4", "v5"],
filter: function filter(val) {
return val.toLowerCase();
}
}],
actions: function actions(data) {
var serviceVersion = data.serviceVersion;
if (serviceVersion === "none" || serviceVersion === "v1") {
serviceVersion = "";
} else {
serviceVersion = ".".concat(serviceVersion);
}
var actions = [];
actions = actions.concat([{
type: "add",
path: path.join(_dirname, "src/helpers/serviceMethods/{{camelCase serviceName}}".concat(serviceVersion, "/{{camelCase methodName}}.method.ts")),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/methods/method.hbs"),
skipIfExists: false,
force: true
}, {
type: "append",
path: path.join(_dirname, "src/types/{{camelCase serviceName}}.ts"),
pattern: "/* MOL_METHOD_INJECT */",
template: 'import {{methodName}} from "@src/helpers/serviceMethods/{{camelCase serviceName}}/{{camelCase methodName}}.method""'
}, {
type: "append",
path: path.join(_dirname, "src/types/{{camelCase serviceName}}.ts"),
pattern: "/* MOL_METHOD_TYPE */",
template: "type {{pascalCase methodName}}Method = typeof {{methodName}}"
}, {
type: "append",
path: path.join(_dirname, "src/types/{{camelCase serviceName}}.ts"),
pattern: "/* MOL_METHOD */",
template: "{{methodName}}: {{pascalCase methodName}}Method"
}]);
return actions;
}
});
// -------------------------- Mixin Generator --------------------------
plop.setGenerator("mixin", {
description: "Create a mixin using every where",
prompts: [{
type: "input",
name: "mixinType",
message: "What is your type of mixin?"
}, {
type: "input",
name: "mixinName",
message: "What is your mixin name?"
}, {
type: "checkbox",
name: "mixinIncludes",
message: "Choose mixin includes",
choices: [{
name: "methods",
value: "methods"
}, {
name: "events",
value: "events"
}, {
name: "actions",
value: "actions"
}, {
name: "mixins",
value: "mixins"
}],
default: ["methods"]
}, {
type: "input",
name: "mixinMethod",
message: "What is your method name?",
when: function when(answers) {
return answers.mixinIncludes.find(function (n) {
return n === "methods";
});
}
}, {
type: "input",
name: "mixinAction",
message: "What is your action name?",
when: function when(answers) {
return answers.mixinIncludes.find(function (n) {
return n === "actions";
});
}
}, {
type: "input",
name: "mixinEvent",
message: "What is your event name?",
when: function when(answers) {
return answers.mixinIncludes.find(function (n) {
return n === "events";
});
}
}],
actions: function actions(data) {
var actions = [];
actions = actions.concat([{
type: "add",
path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/{{camelCase mixinName}}.mixin.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/mixins/mixin.hbs"),
skipIfExists: true,
force: false
}, {
type: "add",
path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/interfaces/{{camelCase mixinName}}.type.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/mixins/type.hbs"),
skipIfExists: true,
force: false
}, {
type: "add",
path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/index.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/mixins/index.hbs"),
skipIfExists: true
}, {
type: "add",
path: path.join(_dirname, "src/mixins/index.ts"),
templateFile: path.join(_dirname, "node_modules/@bmstravel/nvp-plop-templates/lib/base-api/mixins/root.hbs"),
skipIfExists: true,
force: false
}, {
type: "append",
path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/index.ts"),
pattern: "/* MOL_IMPORT_MIXIN */",
template: 'export * from "./{{camelCase mixinName}}.mixin"'
}, {
type: "append",
path: path.join(_dirname, "src/mixins/index.ts"),
pattern: "/* MOL_INDEX_MIXIN_EXPORT_INJECT */",
template: 'export * from "./{{camelCase mixinType}}"'
// template: "{{pascalCase mixinType}}Mixin,",
}
// {
// type: "append",
// path: path.join(_dirname, "src/mixins/index.ts"),
// pattern: `/* MOL_INDEX_MIXIN_IMPORT_INJECT */`,
// template: 'import { {{pascalCase mixinType}}Mixin } from "./{{camelCase mixinType}}"',
// },
]);
if (data.mixinMethod) {
// const tempMeActions = [
// {
// type: "append",
// path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/{{camelCase mixinName}}.mixin.ts"),
// pattern: `/* MOL_METHOD_MIXIN */`,
// template:
// "\t\tasync {{camelCase mixinMethod}}({ }: {{pascalCase mixinMethod}}Params) {\n\t\t\tconst $this: Service = this\n\t\t\tconst $cacher = $this.broker.cacher as RedisCacher\n\t\t\treturn {}\n\t\t},",
// },
// {
// type: "append",
// path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/interfaces/{{camelCase mixinName}}.type.ts"),
// pattern: `/* MOL_METHOD_MIXIN */`,
// template: "export type {{pascalCase mixinMethod}}Params = {}",
// },
// {
// type: "append",
// path: path.join(_dirname, "src/mixins/{{camelCase mixinType}}/{{camelCase mixinName}}.mixin.ts"),
// pattern: `/* MOL_METHODTYPE_MIXIN */`,
// template: "{{pascalCase mixinMethod}}Params,",
// },
// ]
var tempMeActions = [{
type: "append",
path: path.join(_dirname, "src/types/mixins.ts"),
pattern: "/* MOL_TYPES_MIXIN_IMPORT_INJECT */",
template: 'import { {{pascalCase mixinName}}MethodMixin } from "@src/mixins/{{camelCase mixinType}}"'
}, {
type: "append",
path: path.join(_dirname, "src/types/mixins.ts"),
pattern: "/* MOL_TYPES_MIXIN_EXTEND_INJECT */",
template: " {{pascalCase mixinName}}MethodMixin,"
}];
actions = actions.concat(tempMeActions);
}
if (data.mixinAction) {}
if (data.mixinEvent) {}
if (data.mixinIncludes.find(function (n) {
return n.name === "mixins";
})) {}
return actions;
}
});
}