create-mern-omar 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # create-mern-omar
2
+
3
+ > A simple CLI generator to quickly scaffold a MERN stack project with Express and Mongoose.
4
+
5
+ This generator is an opinionated generator that is using express as a framework and mongoose as the ODM.
6
+
7
+ This generator creates a **ready-to-use backend structure** with the following:
8
+
9
+ - Express server (`server.js`)
10
+ - MongoDB connection setup (`db/index.js`)
11
+ - Models folder for Mongoose schemas (`models/`)
12
+ - Controllers folder for route logic (`controllers/`)
13
+ - `.env` and `.gitignore` setup
14
+
15
+ This generator is implemented using **ES Modules** and works as a CLI tool. However the project is using CommonJS
16
+
17
+ ---
18
+
19
+ ## Installation
20
+
21
+ 1. Start your application with the following commands:
22
+
23
+ ```bash
24
+ mkdir project-name
25
+ cd project-name
26
+ npm create mern-omar
package/index.js CHANGED
@@ -1,37 +1,32 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import fs from "fs";
4
4
  import path from "path";
5
- import url from "url";
5
+ import { fileURLToPath } from "url";
6
+ import { execSync } from "child_process";
6
7
 
7
- /**
8
- * Absolute path to:
9
- * - where the user ran the command
10
- * - NOT where the package lives
11
- */
12
- const targetDir = process.cwd();
13
-
14
- /**
15
- * Absolute path to the templates folder
16
- * (inside your generator package)
17
- */
18
- const __filename = url.fileURLToPath(import.meta.url);
8
+ // Resolve __dirname in ES Modules
9
+ const __filename = fileURLToPath(import.meta.url);
19
10
  const __dirname = path.dirname(__filename);
20
- const templateDir = path.join(__dirname, "templates");
21
11
 
22
- console.log(`Generating project in: ${targetDir}`);
12
+ const templatesDir = path.join(__dirname, "templates");
13
+ const targetDir = process.cwd();
14
+
15
+ const renamedFiles = {
16
+ ".env.example": ".env",
17
+ ".gitignore.template": ".gitignore"
18
+ };
23
19
 
24
- /**
25
- * Copy files recursively
26
- */
20
+ // -------------------------
21
+ // Recursive copy function
22
+ // -------------------------
27
23
  function copyRecursive(src, dest) {
28
- if (!fs.existsSync(dest)) {
29
- fs.mkdirSync(dest, { recursive: true });
30
- }
24
+ if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
31
25
 
32
26
  for (const item of fs.readdirSync(src)) {
33
27
  const srcPath = path.join(src, item);
34
- const destPath = path.join(dest, item);
28
+ const finalName = renamedFiles[item] || item;
29
+ const destPath = path.join(dest, finalName);
35
30
 
36
31
  const stat = fs.statSync(srcPath);
37
32
 
@@ -39,10 +34,46 @@ function copyRecursive(src, dest) {
39
34
  copyRecursive(srcPath, destPath);
40
35
  } else {
41
36
  fs.copyFileSync(srcPath, destPath);
37
+ console.log(`Created: ${destPath}`);
42
38
  }
43
39
  }
44
40
  }
45
41
 
46
- copyRecursive(templateDir, targetDir);
42
+ // -------------------------
43
+ // Main generator function
44
+ // -------------------------
45
+ try {
46
+ console.log("Generating project structure...");
47
+ copyRecursive(templatesDir, targetDir);
48
+ console.log("✅ Project files created successfully");
49
+
50
+ // Initialize Node.js project
51
+ console.log("Initializing Node.js project...");
52
+ execSync("npm init -y", { stdio: "inherit", cwd: targetDir });
47
53
 
48
- console.log("✅ Project structure created!");
54
+ // Read package.json
55
+ const pkgPath = path.join(targetDir, "package.json");
56
+ const pkgJSON = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
57
+
58
+ // Modify package.json
59
+ pkgJSON.scripts = {
60
+ start: "node server.js",
61
+ dev: "nodemon server.js"
62
+ };
63
+ pkgJSON.dependencies = pkgJSON.dependencies || {};
64
+ pkgJSON.devDependencies = pkgJSON.devDependencies || {};
65
+ pkgJSON.keywords = ["mern", "express", "mongoose", "generator"];
66
+
67
+ // Write back
68
+ fs.writeFileSync(pkgPath, JSON.stringify(pkgJSON, null, 2));
69
+ console.log("✅ package.json configured successfully");
70
+
71
+ // Install dependencies
72
+ console.log("Installing dependencies...");
73
+ execSync("npm install express mongoose dotenv morgan cors", { stdio: "inherit", cwd: targetDir });
74
+ execSync("npm install --save-dev nodemon", { stdio: "inherit", cwd: targetDir });
75
+
76
+ console.log("🚀 Project setup complete! Your Node.js app is ready.");
77
+ } catch (err) {
78
+ console.error("❌ Error generating project:", err);
79
+ }
package/package.json CHANGED
@@ -1,16 +1,15 @@
1
1
  {
2
2
  "name": "create-mern-omar",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "bin": {
6
- "create-express-mongoose": "./index.js"
6
+ "create-mern-omar": "./index.js"
7
7
  },
8
8
  "keywords": [
9
- "express",
10
- "mongoose",
11
- "mern",
12
- "generator",
13
- "scaffold",
14
- "cli"
15
- ]
9
+ "express",
10
+ "mongoose",
11
+ "generator",
12
+ "scaffold",
13
+ "cli"
14
+ ]
16
15
  }
@@ -0,0 +1,2 @@
1
+ MONGODB_URI=mongodb://localhost:27017/
2
+ PORT=3000
@@ -0,0 +1,2 @@
1
+ node_modules
2
+ .env
@@ -0,0 +1,10 @@
1
+ const router = require('express').Router()
2
+
3
+
4
+
5
+ router.get('/',(req,res)=>{
6
+ res.json({messgae:'Success, Your app is up and running'})
7
+ })
8
+
9
+
10
+ module.exports = router
@@ -1,6 +1,7 @@
1
1
  const mongoose = require("mongoose") // importing mongoose
2
2
 
3
3
 
4
+ const DB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/'
4
5
  async function conntectToDB(){ //connection to the database
5
6
  try{
6
7
  await mongoose.connect(process.env.MONGODB_URI)
@@ -0,0 +1,26 @@
1
+ const mongoose = require("mongoose");
2
+
3
+ const userSchema = new mongoose.Schema(
4
+ {
5
+ name: {
6
+ type: String,
7
+ required: true,
8
+ trim: true
9
+ },
10
+ email: {
11
+ type: String,
12
+ required: true,
13
+ unique: true,
14
+ lowercase: true
15
+ },
16
+ password: {
17
+ type: String,
18
+ required: true
19
+ }
20
+ },
21
+ {
22
+ timestamps: true
23
+ }
24
+ );
25
+
26
+ module.exports = mongoose.model("User", userSchema);
@@ -6,8 +6,10 @@ const app = express() // creates a express application
6
6
  require("dotenv").config() // allows us to use the .env variables
7
7
  const morgan = require("morgan")
8
8
  const conntectToDB = require('./db/index')
9
+ const indexRoutes = require("./controllers/index.routes.js");
10
+ const cors = require('cors')
9
11
 
10
-
12
+ const PORT = process.env.PORT || 3000; // Set port from .env
11
13
 
12
14
 
13
15
 
@@ -33,6 +35,7 @@ conntectToDB() // connect to database
33
35
 
34
36
 
35
37
  // Routes go here
38
+ app.use('/api',indexRoutes)
36
39
 
37
40
 
38
41
 
@@ -45,11 +48,7 @@ conntectToDB() // connect to database
45
48
 
46
49
 
47
50
 
48
-
49
-
50
-
51
- app.listen(3000,()=>{
52
- console.log('App is running on port 3000')
53
- }) // app will be waiting for requests on port 3000
54
-
51
+ app.listen(PORT, () => {
52
+ console.log(`Server is running on port ${PORT}`);
53
+ }); // app will be waiting for requests on port from .env or 3000
55
54