@expressots/cli
Version:
Expressots CLI - modern, fast, lightweight nodejs web framework (@cli)
251 lines (250 loc) • 9.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nonOpinionatedProcess = void 0;
const string_utils_1 = require("./string-utils");
const cli_ui_1 = require("../../utils/cli-ui");
const command_utils_1 = require("./command-utils");
/**
* Process the non-opinionated command
* @param schematic - The schematic
* @param target - The target
* @param method - The method
* @param expressoConfig - The expresso config
*/
async function nonOpinionatedProcess(schematic, target, method, expressoConfig) {
let f = await (0, command_utils_1.validateAndPrepareFile)({
schematic,
target,
method,
expressoConfig,
});
switch (schematic) {
case "service":
f = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "controller",
target,
method,
expressoConfig,
});
await generateController(f.outputPath, f.className, f.path, method, f.file, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
f = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "usecase",
target,
method,
expressoConfig,
});
await generateUseCase(f.outputPath, f.className, f.moduleName, f.path, f.fileName, f.schematic, "../templates/nonopinionated/usecase.tpl");
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
f = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "dto",
target,
method,
expressoConfig,
});
await generateDTO(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
f = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "module",
target,
method,
expressoConfig,
});
await generateModule(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "usecase":
await generateUseCase(f.outputPath, f.className, f.moduleName, f.path, f.fileName, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "controller":
await generateController(f.outputPath, f.className, f.path, method, f.file, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "dto":
await generateDTO(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "provider":
await generateProvider(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "entity":
await generateEntity(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "middleware":
await generateMiddleware(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
case "module":
await generateModule(f.outputPath, f.className, f.moduleName, f.path, f.schematic);
await (0, cli_ui_1.printGenerateSuccess)(f.schematic, f.file);
break;
}
return f.file;
}
exports.nonOpinionatedProcess = nonOpinionatedProcess;
/* Generate Resource */
/**
* Generate a use case
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
* @param template - The template
*/
async function generateUseCase(outputPath, className, moduleName, path, fileName, schematic, template) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: template
? template
: "../templates/nonopinionated/usecase.tpl",
data: {
className,
moduleName,
path,
fileName,
schematic: schematic === "usecase"
? "UseCase"
: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}
/**
* Generate a controller
* @param outputPath - The output path
* @param className - The class name
* @param path - The path
* @param method - The method
* @param file - The file
*/
async function generateController(outputPath, className, path, method, file, schematic) {
const templateBasedMethod = "../templates/nonopinionated/controller.tpl";
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: templateBasedMethod,
data: {
className,
fileName: (0, command_utils_1.getFileNameWithoutExtension)(file),
useCase: (0, string_utils_1.anyCaseToCamelCase)(className),
route: className
? className.toLowerCase()
: path.replace(/\/$/, ""),
construct: (0, string_utils_1.anyCaseToKebabCase)(className),
method: (0, command_utils_1.getHttpMethod)(method),
schematic: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}
/**
* Generate a DTO
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateDTO(outputPath, className, moduleName, path, schematic) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/nonopinionated/dto.tpl",
data: {
className,
moduleName,
path,
schematic: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}
/**
* Generate a provider
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateProvider(outputPath, className, moduleName, path, schematic) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/nonopinionated/provider.tpl",
data: {
className,
moduleName,
path,
schematic: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}
/**
* Generate an entity
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateEntity(outputPath, className, moduleName, path, schematic) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/nonopinionated/entity.tpl",
data: {
className,
moduleName,
path,
schematic: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}
/**
* Generate a middleware
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateMiddleware(outputPath, className, moduleName, path, schematic) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/nonopinionated/middleware.tpl",
data: {
className,
moduleName,
path,
schematic: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}
/**
* Generate a module
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateModule(outputPath, className, moduleName, path, schematic) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/nonopinionated/module.tpl",
data: {
className,
moduleName: className
? (0, string_utils_1.anyCaseToPascalCase)(className)
: (0, string_utils_1.anyCaseToPascalCase)(moduleName),
path,
schematic: (0, string_utils_1.anyCaseToPascalCase)(schematic),
},
},
});
}