@expressots/cli
Version:
Expressots CLI - modern, fast, lightweight nodejs web framework (@cli)
482 lines (481 loc) • 17.9 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.opinionatedProcess = void 0;
const string_utils_1 = require("./string-utils");
const nodePath = __importStar(require("node:path"));
const fs_1 = __importDefault(require("fs"));
const cli_ui_1 = require("../../utils/cli-ui");
const command_utils_1 = require("./command-utils");
const add_controller_to_module_1 = require("../../utils/add-controller-to-module");
const add_module_to_container_1 = require("../../utils/add-module-to-container");
/**
* Process commands for opinionated service scaffolding
* @param schematic - Resource to scaffold
* @param target - Target path
* @param method - HTTP method
* @param expressoConfig - Expresso configuration [expressots.config.ts]
* @param pathStyle - Path command style [sugar, nested, single]
* @returns
*/
async function opinionatedProcess(schematic, target, method, expressoConfig, pathStyle) {
const f = await (0, command_utils_1.validateAndPrepareFile)({
schematic,
target,
method,
expressoConfig,
});
switch (schematic) {
case "service": {
await generateControllerService(f.outputPath, f.className, f.path, method, f.file);
const u = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "usecase",
target,
method,
expressoConfig,
});
await generateUseCaseService(u.outputPath, u.className, method, u.moduleName, u.path, u.fileName);
const d = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "dto",
target,
method,
expressoConfig,
});
await generateDTO(d.outputPath, d.className, d.moduleName, d.path);
const m = await (0, command_utils_1.validateAndPrepareFile)({
schematic: "module",
target,
method,
expressoConfig,
});
if (pathStyle === "sugar" /* PathStyle.Sugar */) {
await generateModuleServiceSugarPath(f.outputPath, m.className, m.moduleName, m.path, m.file, m.folderToScaffold);
}
else if (pathStyle === "nested" /* PathStyle.Nested */) {
await generateModuleServiceNestedPath(f.outputPath, m.className, m.path, m.folderToScaffold);
}
else if (pathStyle === "single" /* PathStyle.Single */) {
await generateModuleServiceSinglePath(f.outputPath, m.className, m.moduleName, m.path, m.file, m.folderToScaffold);
}
await (0, cli_ui_1.printGenerateSuccess)("controller", f.file);
await (0, cli_ui_1.printGenerateSuccess)("usecase", f.file);
await (0, cli_ui_1.printGenerateSuccess)("dto", f.file);
await (0, cli_ui_1.printGenerateSuccess)("module", f.file);
break;
}
case "usecase":
await generateUseCase(f.outputPath, f.className, f.moduleName, f.path, f.fileName);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
case "controller":
await generateController(f.outputPath, f.className, f.path, method, f.file);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
case "dto":
await generateDTO(f.outputPath, f.className, f.moduleName, f.path);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
case "provider":
await generateProvider(f.outputPath, f.className, f.moduleName, f.path);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
case "entity":
await generateEntity(f.outputPath, f.className, f.moduleName, f.path);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
case "middleware":
await generateMiddleware(f.outputPath, f.className, f.moduleName, f.path);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
case "module":
await generateModule(f.outputPath, f.className, f.moduleName, f.path);
await (0, cli_ui_1.printGenerateSuccess)(schematic, f.file);
break;
}
return f.file;
}
exports.opinionatedProcess = opinionatedProcess;
/* Generate Resource */
/**
* Generate a controller service
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
* @param method - The method
* @param file - The file
*/
async function generateControllerService(outputPath, className, path, method, file) {
let templateBasedMethod = "";
switch (method) {
case "put":
templateBasedMethod =
"../templates/opinionated/controller-service-put.tpl";
break;
case "patch":
templateBasedMethod =
"../templates/opinionated/controller-service-patch.tpl";
break;
case "post":
templateBasedMethod =
"../templates/opinionated/controller-service-post.tpl";
break;
case "delete":
templateBasedMethod =
"../templates/opinionated/controller-service-delete.tpl";
break;
default:
templateBasedMethod =
"../templates/opinionated/controller-service-get.tpl";
break;
}
(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: path.replace(/\/$/, ""),
construct: (0, string_utils_1.anyCaseToKebabCase)(className),
method: (0, command_utils_1.getHttpMethod)(method),
},
},
});
}
/**
* 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 generateUseCaseService(outputPath, className, method, moduleName, path, fileName) {
let templateBasedMethod = "";
switch (method) {
case "put":
templateBasedMethod =
"../templates/opinionated/usecase-service.tpl";
break;
case "patch":
templateBasedMethod =
"../templates/opinionated/usecase-service.tpl";
break;
case "post":
templateBasedMethod =
"../templates/opinionated/usecase-service.tpl";
break;
case "delete":
templateBasedMethod =
"../templates/opinionated/usecase-service-delete.tpl";
break;
default:
templateBasedMethod = "../templates/opinionated/usecase.tpl";
break;
}
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: templateBasedMethod,
data: {
className,
moduleName,
path,
fileName,
},
},
});
}
/**
* Generate a use case
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
* @param fileName - The file name
*/
async function generateUseCase(outputPath, className, moduleName, path, fileName) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/opinionated/usecase.tpl",
data: {
className,
moduleName,
path,
fileName,
},
},
});
}
/**
* 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) {
const templateBasedMethod = "../templates/opinionated/controller-service.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: path.replace(/\/$/, ""),
construct: (0, string_utils_1.anyCaseToKebabCase)(className),
method: (0, command_utils_1.getHttpMethod)(method),
},
},
});
}
/**
* 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) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/opinionated/dto.tpl",
data: {
className,
moduleName,
path,
},
},
});
}
/**
* 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) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/opinionated/provider.tpl",
data: {
className,
moduleName,
path,
},
},
});
}
/**
* 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) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/opinionated/entity.tpl",
data: {
className,
moduleName,
path,
},
},
});
}
/**
* 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) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/opinionated/middleware.tpl",
data: {
className,
moduleName,
path,
},
},
});
}
/**
* Generate a module for service scaffolding with sugar path
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateModuleServiceSugarPath(outputPathController, className, moduleName, path, file, folderToScaffold) {
const newModuleFile = await (0, command_utils_1.extractFirstWord)(file);
const newModulePath = nodePath
.join(folderToScaffold, path, "..")
.normalize();
const newModuleName = `${newModuleFile}.module.ts`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace("\\", "/");
const controllerToModule = nodePath
.relative(newModuleOutputPath, outputPathController)
.normalize()
.replace(/\.ts$/, "")
.replace(/\\/g, "/")
.replace(/\.\./g, ".");
const controllerFullPath = nodePath
.join(folderToScaffold, path, "..", newModuleName)
.normalize();
if (fs_1.default.existsSync(newModuleOutputPath)) {
await (0, add_controller_to_module_1.addControllerToModule)(controllerFullPath, `${className}Controller`, controllerToModule);
return;
}
(0, command_utils_1.writeTemplate)({
outputPath: newModuleOutputPath,
template: {
path: "../templates/opinionated/module-service.tpl",
data: {
className,
moduleName: (0, string_utils_1.anyCaseToPascalCase)(moduleName),
path: controllerToModule,
},
},
});
await (0, add_module_to_container_1.addModuleToContainer)((0, string_utils_1.anyCaseToPascalCase)(moduleName), `${moduleName}/${file.replace(".ts", "")}`, path);
}
/**
* Generate a module for service scaffolding with single path
* @param outputPath - The output path
* @param className - The class name
* @param moduleName - The module name
* @param path - The path
*/
async function generateModuleServiceSinglePath(outputPathController, className, moduleName, path, file, folderToScaffold) {
const newModuleFile = await (0, command_utils_1.extractFirstWord)(file);
const newModulePath = nodePath.join(folderToScaffold, path).normalize();
const newModuleName = `${newModuleFile}.module.ts`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace("\\", "/");
const controllerToModule = nodePath
.relative(newModuleOutputPath, outputPathController)
.normalize()
.replace(/\.ts$/, "")
.replace(/\\/g, "/")
.replace(/\.\./g, ".");
const controllerFullPath = nodePath
.join(folderToScaffold, path, "..", newModuleName)
.normalize();
if (fs_1.default.existsSync(newModuleOutputPath)) {
await (0, add_controller_to_module_1.addControllerToModule)(controllerFullPath, `${className}Controller`, controllerToModule);
return;
}
(0, command_utils_1.writeTemplate)({
outputPath: newModuleOutputPath,
template: {
path: "../templates/opinionated/module-service.tpl",
data: {
className,
moduleName: (0, string_utils_1.anyCaseToPascalCase)(moduleName),
path: controllerToModule,
},
},
});
await (0, add_module_to_container_1.addModuleToContainer)((0, string_utils_1.anyCaseToPascalCase)(moduleName), `${moduleName}/${file.replace(".ts", "")}`, path);
}
/**
* Generate a module for service scaffolding with nested path
* @param outputPathController
* @param className
* @param path
* @param folderToScaffold
* @returns
*/
async function generateModuleServiceNestedPath(outputPathController, className, path, folderToScaffold) {
const moduleFileName = nodePath.basename(path, "/");
const newModulePath = nodePath
.join(folderToScaffold, path, "..")
.normalize();
const newModuleName = `${moduleFileName}.module.ts`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace("\\", "/");
const controllerToModule = nodePath
.relative(newModuleOutputPath, outputPathController)
.normalize()
.replace(/\.ts$/, "")
.replace(/\\/g, "/")
.replace(/\.\./g, ".");
const controllerFullPath = nodePath
.join(folderToScaffold, path, "..", newModuleName)
.normalize();
if (fs_1.default.existsSync(newModuleOutputPath)) {
await (0, add_controller_to_module_1.addControllerToModule)(controllerFullPath, `${className}Controller`, controllerToModule);
return;
}
(0, command_utils_1.writeTemplate)({
outputPath: newModuleOutputPath,
template: {
path: "../templates/opinionated/module-service.tpl",
data: {
className,
moduleName: (0, string_utils_1.anyCaseToPascalCase)(moduleFileName),
path: controllerToModule,
},
},
});
await (0, add_module_to_container_1.addModuleToContainerNestedPath)((0, string_utils_1.anyCaseToPascalCase)(moduleFileName), path);
}
/**
* 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) {
(0, command_utils_1.writeTemplate)({
outputPath,
template: {
path: "../templates/opinionated/module.tpl",
data: {
className,
moduleName: (0, string_utils_1.anyCaseToPascalCase)(moduleName),
path,
},
},
});
}