Skip to content

Commit

Permalink
add some feture
Browse files Browse the repository at this point in the history
add generate controller and generate middleware feture
  • Loading branch information
mohamedhoss123 committed Dec 22, 2022
1 parent 05ddaa3 commit 1890fa2
Show file tree
Hide file tree
Showing 24 changed files with 196 additions and 57 deletions.
6 changes: 0 additions & 6 deletions commands/gen.js

This file was deleted.

19 changes: 19 additions & 0 deletions commands/gen/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");
const path = require("path");
const { writeContent } = require("../../util/gen/mdine");
async function generateController(name) {
let FileName = name.split("/").pop();
let controllerContent = fs
.readFileSync(
path.join(__dirname, "../../templates/controllers/controller.js")
)
.toString();
controllerContent = controllerContent.replaceAll(
"thisisplaceholder",
FileName + "Controller"
);
writeContent(name, controllerContent, "controllers");
}
module.exports = {
generateController,
};
19 changes: 19 additions & 0 deletions commands/gen/gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { generateController } = require("./controller");
const { generatemiddleware } = require("./middleware");

let ActionsToTake = {
"c":generateController,
"mi":generatemiddleware
}
async function genInit(type,fileName){
if(ActionsToTake[type]!= undefined){
ActionsToTake[type](fileName)
}else{
return console.log(`type "${type}" is not valide`);
}
}


module.exports = {
genInit
};
19 changes: 19 additions & 0 deletions commands/gen/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");
const path = require("path");
const { writeContent } = require("../../util/gen/mdine");
async function generatemiddleware(name) {
let FileName = name.split("/").pop();
let middlewareContent = fs
.readFileSync(
path.join(__dirname, "../../templates/middlewares/middleware.js")
)
.toString();
middlewareContent = middlewareContent.replaceAll(
"thisisplaceholder",
FileName + "Middleware"
);
writeContent(name, middlewareContent, "middlewares");
}
module.exports = {
generatemiddleware,
};
Empty file added commands/gen/model.js
Empty file.
Empty file added commands/gen/router.js
Empty file.
31 changes: 22 additions & 9 deletions commands/new/mvc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
async function MVCInit(prompt){
const questions = {
type:"list",
name:"viewChose",
message:"chose view engine :",
choices:["ejs","pug","hbs"]
}
let viewEngine = await prompt(questions).viewChose
const { changeDependency } = require("../../util/changeDependency");
const { unzip } = require("../../util/decompress");
const {templateEngines} = require("./packges.json");
async function MVCInit(prompt) {
console.log("mvc");
await unzip("MVC");
const questions = {
type: "list",
name: "viewChose",
message: "chose view engine :",
choices: ["ejs", "pug"],
};
let viewEngine = await prompt(questions);
viewEngine = viewEngine.viewChose;
console.log();
let tm = {}
tm[`${viewEngine}`] = templateEngines[`${viewEngine}`]
console.log(tm);
changeDependency(tm);
// console.clear();
console.log("test");
}
module.exports = {
MVCInit
MVCInit,
};
22 changes: 12 additions & 10 deletions commands/new/new.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
const { unzip } = require("../../test");
const { changePjName } = require("../../util/changePJname");
const inquirer = require('inquirer');
const { MVCInit } = require("./mvc");
const { optinalPackges,optinalDevPackges } = require("./packgesToAdd");
const prompt = inquirer.createPromptModule();


let newTemplate = {
"MVC":MVCInit,
"Rest":() => {console.log("comming soon");process.exit(0)}
}
async function newGen(str){
console.clear();
const questions = {
type:"list",
name:"choseProject",
name:"choseTemplate",
message:"chose new application type :",
choices:["MVC","Rest"]
}
const data = await prompt(questions)
await unzip()
await newTemplate[data.choseTemplate](prompt)
// console.clear();
changePjName(str)
await optinalPackges(prompt)
await optinalDevPackges(prompt)
// if(data.choseProject == "MVC"){
// MVCInit(prompt)
// }

// changePjName(str);

console.log("run the following commands :\n");
console.log("npm install");
console.log("npm start");
}


Expand Down
16 changes: 16 additions & 0 deletions commands/new/packges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"pakgesDetails": {
"express-session": "^1.17.3",
"body-parser": "^1.20.1",
"cookie-parser": "^1.4.6",
"multer": "^1.4.5-lts.1",
"morgan": "^1.10.0"
},
"devPakgesDetails":{
"nodemon:": "^2.0.20"
},
"templateEngines":{
"ejs":"^3.1.8",
"pug":"^3.0.2"
}
}
14 changes: 5 additions & 9 deletions commands/new/packgesToAdd.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
const { changeDependency } = require("../../util/changeDependency");
const { changeDevDependency } = require("../../util/changeDevDep");
const {devPakgesDetails,pakgesDetails} = require("./packges.json")

const pakgesDetails = {
"express-session": "^1.17.3",
"body-parser": "^1.20.1",
};
const devPakgesDetails = {
nodemon: "^2.0.20",
};
async function optinalPackges(prompt) {
const questions = {
type: "checkbox",
name: "Chose",
message: "want to add some pakages ? :",
choices: ["express-session", "body-parser"],
choices: Object.keys(pakgesDetails),
};
let packges = await prompt(questions);
console.clear();
let newPkg = Object.fromEntries(
Object.entries(pakgesDetails).filter(([key]) => packges.Chose.includes(key))
);
console.log(newPkg);
changeDependency(newPkg);
}
async function optinalDevPackges(prompt) {
const questions = {
type: "checkbox",
name: "Chose",
message: "want to add some devPakages ? :",
choices: ["nodemon"],
choices: Object.keys(devPakgesDetails),
};
let packges = await prompt(questions);
if (packges.Chose.length != 0) {
Expand Down
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#!/usr/bin/env node
const { Command } = require("commander");
const { genInit } = require("./commands/gen/gen");
const { newGen } = require("./commands/new/new");

const program = new Command();

program
.name("string-util")
.description("CLI to some JavaScript string utilities")
.version("0.0.1");
.name("exp-cli")
.description("CLI tool to make your express project more fun")
.version("0.0.7");

program
.command("new")
.description("generate new express application")
.argument("<string>", "project name")
.action(async (str) => {
newGen(str)
newGen(str);
});

program
.command("gen")
.description("generate new express application")
.argument("<string>", "project name")
.action(async (str) => {
await unzip();
changePjName(str);
.argument("<type>", "project name")
.argument("<filename>", "name of generated file")
.action(async (type, filename) => {
await genInit(type, filename);
});

program.parse();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exp-cli",
"version": "0.0.1",
"name": "excli",
"version": "0.0.7",
"description": "this is a tool to make your express development easer",
"bin": "./index.js",
"main": "index.js",
Expand Down
4 changes: 4 additions & 0 deletions templates/controllers/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function thisisplaceholder(req, res) {
res.send("this is thisisplaceholder");
}
export default thisisplaceholder;
Binary file removed templates/expressMVC.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions templates/middlewares/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function thisisplaceholder(req, res, next) {
res.send("this is thisisplaceholder");
next();
}
export default thisisplaceholder;
Empty file added templates/models/mongoose.js
Empty file.
30 changes: 30 additions & 0 deletions templates/models/typeorm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let jsTypeorm = `
var EntitySchema = require("typeorm").EntitySchema
module.exports = new EntitySchema({
name: "thisiplaceHolder",
tableName: "thisiplaceHolder",
columns: {
id: {
primary: true,
type: "int",
generated: true,
},
},
})
`

let tsTypeorm = `
import { Entity, PrimaryGeneratedColumn } from "typeorm"
@Entity()
export class thisiplaceHolder {
@PrimaryGeneratedColumn()
id: number
}
`
Binary file renamed templates/test.zip → templates/zip/MVC.zip
Binary file not shown.
4 changes: 1 addition & 3 deletions util/changeDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ const fs = require("fs");
const path = require("path");

function changeDependency(packges) {
console.log("here manga");
let fileContent = fs.readFileSync("package.json");
let jsonFile = JSON.parse(fileContent);
jsonFile.dependencies = Object.assign(packges,jsonFile.dependencies);
console.log(jsonFile);
let data = JSON.stringify(jsonFile,null," ")
console.log(data);
fs.writeFileSync(
"package.json",
data,
"utf8",
function (err) {
if (err) throw err;
// if no error
console.log("Data is appended to file successfully.");
}
);
}
Expand Down
6 changes: 1 addition & 5 deletions util/changeDevDep.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ function changeDevDependency(packges) {
let fileContent = fs.readFileSync("package.json");
let jsonFile = JSON.parse(fileContent);
jsonFile.devDependencies = Object.assign(packges,jsonFile.devDependencies);
console.log(jsonFile);
let data = JSON.stringify(jsonFile,null," ")
console.log(data);
fs.writeFileSync(
"package.json",
data,
"utf8",
function (err) {
if (err) throw err;
// if no error
console.log("Data is appended to file successfully.");
}
}
);
}
module.exports = {
Expand Down
2 changes: 0 additions & 2 deletions util/changePJname.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ function changePjName(name) {
let fileContent = fs.readFileSync("package.json");
let jsonFile = JSON.parse(fileContent);
jsonFile.name = name;
console.log(jsonFile);
let data = JSON.stringify(jsonFile,null," ")
console.log(data);
fs.writeFileSync(
"package.json",
data,
Expand Down
6 changes: 3 additions & 3 deletions util/decompress.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var fs = require("fs");
var decompress = require("decompress");
const path = require("path")
async function unzip(templateName = "test") {
async function unzip(name="test") {

try {
const files = await decompress(
path.join(__dirname,`templates/${templateName}.zip`),
path.join(__dirname,`../templates/zip/${name}.zip`),
process.cwd()
);
console.log("done");
} catch (e) {
console.log(e);
}
Expand Down
19 changes: 19 additions & 0 deletions util/gen/mdine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");
const path = require("path");
function writeContent (name,content,place) {
let theArray = name.split("/")
let FileName = theArray.pop()
let DirectoryName = theArray.join("/")
let FilePth = path.join(process.cwd(),place,DirectoryName,FileName+".controller.js")
if (!fs.existsSync(path.join(process.cwd(),place,DirectoryName))){
fs.mkdirSync(path.join(process.cwd(),place,DirectoryName), { recursive: true });
}
fs.writeFileSync(FilePth,content,{
flag:"a"
})
console.log("create !");
}

module.exports = {
writeContent
};
9 changes: 9 additions & 0 deletions util/mvc/putViewEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require('fs');
const path = require('path');
function setViewEngine(name){
let data = fs.readFileSync(path.join(process.cwd(),"main.js"))
console.log(data);
}
module.exports = {
setViewEngine
};

0 comments on commit 1890fa2

Please sign in to comment.