Skip to content

Commit

Permalink
Merge pull request #4 from MusahMusah/dev
Browse files Browse the repository at this point in the history
feat: added cli to automatically generate module files
  • Loading branch information
MusahMusah authored Sep 8, 2024
2 parents e91a8ee + c484bb8 commit 96b3229
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Core/cli/module-generator.js
Original file line number Diff line number Diff line change
@@ -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 <module-name>"
);
}

0 comments on commit 96b3229

Please sign in to comment.