From c484bb8496d7ab1fffb431234ff3293b17d3a291 Mon Sep 17 00:00:00 2001 From: MusahMusah Date: Sun, 8 Sep 2024 22:55:56 +0100 Subject: [PATCH] feat: added cli to automatically generate module files --- src/Core/cli/module-generator.js | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Core/cli/module-generator.js diff --git a/src/Core/cli/module-generator.js b/src/Core/cli/module-generator.js new file mode 100644 index 0000000..76cab7b --- /dev/null +++ b/src/Core/cli/module-generator.js @@ -0,0 +1,60 @@ +const { existsSync, mkdirSync, writeFileSync } = require("fs"); +const { join } = require("path"); + +const _files = [ + { folder: "entity", fileNameSuffix: "entity" }, + { folder: "service", fileNameSuffix: "service" }, + { folder: "repository", fileNameSuffix: "repository" }, + { folder: "facade", fileNameSuffix: "facade" }, + { folder: "validation", fileNameSuffix: "validation" }, + { folder: "route", fileNameSuffix: "route" }, + { folder: "resource", fileNameSuffix: "resource" }, + { folder: "middlewares", fileNameSuffix: "middlewares" }, +]; + +class ModuleGenerator { + constructor(basePath = "./src/Modules") { + this.basePath = basePath; + } + + generate(moduleName) { + const capitalizedModuleName = this._capitalize(moduleName); + const modulePath = join(this.basePath, capitalizedModuleName); + + _files.forEach(({ folder, fileNameSuffix }) => { + const folderPath = join(modulePath, folder); + this._createDirectory(folderPath); + this._createFile(folderPath, `${moduleName}.${fileNameSuffix}.js`, ""); + }); + + console.log( + `Module '${capitalizedModuleName}' has been created successfully.` + ); + } + + _createDirectory(dirPath) { + if (!existsSync(dirPath)) { + mkdirSync(dirPath, { recursive: true }); + } + } + + _createFile(dirPath, fileName, content) { + const filePath = join(dirPath, fileName); + writeFileSync(filePath, content); + } + + _capitalize(str) { + return str.charAt(0).toUpperCase() + str.slice(1); + } +} + +const generator = new ModuleGenerator(); +const args = process.argv.slice(2); + +if (args.length === 2 && args[0] === "make:module") { + generator.generate(args[1]); +} else { + console.log( + "Usage: node src/Core/cli/module-generator.js make:module " + ); +}