v-express-bulid 1.0.1

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.
@@ -0,0 +1,133 @@
1
+ import fse from 'fs/promises';
2
+ import { fileURLToPath } from 'url'
3
+ import path from 'path'
4
+ import chalk, { Chalk } from 'chalk';
5
+
6
+ export const addFolder = async () => {
7
+ console.log('adding the folder')
8
+ // creating a express folder
9
+
10
+ const config = [
11
+ {
12
+ name: "controller",
13
+ fileName: "controller.js",
14
+ content: `export const getData = (req, res) => {
15
+ res.send("Hello from controller");
16
+ };`
17
+ },
18
+
19
+ {
20
+ name: "src",
21
+ fileName: "app.js",
22
+ content: `import express from 'express';
23
+ import router from '../router/router.js';
24
+ import { connectDB } from '../db/dbConnect.js';
25
+ import { config } from 'dotenv';
26
+
27
+ config();
28
+
29
+ // creating an express app
30
+ const port = process.env.PORT || 8000;
31
+ const app = express();
32
+
33
+ // middlewares
34
+ app.use(router);
35
+
36
+ // connecting to database
37
+ connectDB();
38
+
39
+ app.listen(port, (error) => {
40
+ if (error) throw error;
41
+ console.log(\`server is listening on port \${port}\`);
42
+ });`
43
+ },
44
+
45
+ {
46
+ name: "router",
47
+ fileName: "router.js",
48
+ content: `import express from "express";
49
+ import { getData } from "../controller/controller.js";
50
+
51
+ const router = express.Router();
52
+
53
+ router.get("/", getData);
54
+
55
+ export default router;`
56
+ },
57
+
58
+ {
59
+ name: "db",
60
+ fileName: "dbConnect.js",
61
+ content: `import mongoose from "mongoose";
62
+
63
+ export const connectDB = async () => {
64
+ try {
65
+ await mongoose.connect(process.env.MONGO_URI);
66
+ console.log("MongoDB connected");
67
+ } catch (err) {
68
+ console.log("DB Error:", err.message);
69
+ process.exit(1);
70
+ }
71
+ };`
72
+ }
73
+ ];
74
+
75
+ // now reading the file from folder
76
+
77
+ // here we have change dir path
78
+
79
+ // const __filename = fileURLToPath(import.meta.url);
80
+ // const __dirname = path.dirname(__filename);
81
+
82
+
83
+ const repoPath = path.resolve(process.cwd(), 'express-App')
84
+
85
+ await fse.mkdir(repoPath, { recursive: true })
86
+
87
+
88
+ console.log(chalk.yellow(`express folder created successfully ✅ ✅ ✅ 🧑‍🚀`))
89
+
90
+
91
+ // creating package.json file here
92
+
93
+
94
+ await fse.writeFile(path.join(repoPath, 'package.json'), JSON.stringify({
95
+ "type": "module",
96
+ "scripts": {
97
+ "start": "nodemon src/app.js"
98
+ },
99
+ "dependencies": {
100
+ "express": "^4.19.0",
101
+ "mongoose": "^8.0.0",
102
+ "dotenv": "^16.0.0",
103
+ "chalk": "^5.0.0",
104
+ "inquirer": "^9.0.0"
105
+ }
106
+ }), 'utf-8')
107
+
108
+ console.log(chalk.green('package.json file is created ✅ ✅ ✅ ✅'))
109
+
110
+ // creating dotenv file here
111
+
112
+ await fse.writeFile(path.join(repoPath, '.env'), 'PORT=4000\nMONGO_URI=mongodb://127.0.0.1:27017/mydb', 'utf-8')
113
+ console.log(chalk.green('.env file is created now ✅ ✅ ✅'))
114
+
115
+ // const template = path.join(__dirname, "template");
116
+
117
+ // const templatePath = path.resolve(process.cwd()) // 'bin'
118
+
119
+ // const template = path.join(templatePath, 'template')
120
+ // const src = path.join(template, 'src')
121
+
122
+ for (const items of config) {
123
+ // const data = await fse.readFile(path.join(template, items.name, items.fileName.replace('.js', '.txt')), 'utf-8')
124
+ console.log(chalk.bgBlue(`folder created ${items.name} ⭐ ⭐ ⭐`))
125
+ await fse.mkdir(path.join(repoPath, items["name"]), { recursive: true })
126
+ await fse.writeFile(`${path.join(repoPath, items['name'], items.fileName)}`, `${items.content}`, 'utf-8')
127
+ console.log(chalk.bgCyan(`file created ${items.fileName} ✔️✔️✔️`))
128
+
129
+
130
+ }
131
+
132
+ console.log(chalk.green('folder created : enjoy now : - Author (vikash sharma 🧑‍🚀🧑‍🚀🚀)'))
133
+ }
package/index.js ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import yargs from 'yargs'
3
+ import { hideBin } from 'yargs/helpers'
4
+ import { addFolder } from './controller/addFolder.js'
5
+ yargs(hideBin(process.argv)).command(
6
+ 'express', 'initiating the express folder', {},
7
+ addFolder
8
+ ).demandCommand(1, 'at least pass one command').help().parse()
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "type": "module",
3
+ "name": "v-express-bulid",
4
+ "version": "1.0.1",
5
+ "description": "CLI to create Express app",
6
+ "main": "index.js",
7
+ "bin": {
8
+ "v-express": "./bin/index.js"
9
+ },
10
+ "scripts": {
11
+ "start": "node bin/index.js"
12
+ },
13
+ "author": "Vikash",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "chalk": "^5.6.2",
17
+ "yargs": "^18.0.0"
18
+ }
19
+ }