create-uk-node-server 1.0.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 +57 -0
- package/bin/cli.js +4 -0
- package/index.js +39 -0
- package/package.json +30 -0
- package/templates/javascript/index.js +23 -0
- package/templates/javascript/package.json +22 -0
- package/templates/javascript/src/controllers/test.controller.js +0 -0
- package/templates/javascript/src/middlewares/errorHandler.js +7 -0
- package/templates/javascript/src/models/user.model.js +33 -0
- package/templates/javascript/src/routes/index.route.js +8 -0
- package/templates/javascript/src/utils/db.config.js +13 -0
- package/templates/javascript/src/utils/general.js +15 -0
- package/templates/typescript/package.json +28 -0
- package/templates/typescript/src/controllers/test.controller.ts +0 -0
- package/templates/typescript/src/index.ts +38 -0
- package/templates/typescript/src/middlewares/errorHandler.ts +0 -0
- package/templates/typescript/src/models/user.model.ts +33 -0
- package/templates/typescript/src/routes/index.route.ts +9 -0
- package/templates/typescript/src/utils/db.config.ts +12 -0
- package/templates/typescript/src/utils/general.ts +19 -0
- package/templates/typescript/tsconfig.json +20 -0
package/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Server Package CLI
|
2
|
+
|
3
|
+
A command-line tool to quickly scaffold server projects with JavaScript or TypeScript templates.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
```bash
|
7
|
+
npx create-node-server
|
8
|
+
```
|
9
|
+
|
10
|
+
The tool will prompt you to:
|
11
|
+
1. Choose your preferred language (JavaScript or TypeScript)
|
12
|
+
2. Enter your project name
|
13
|
+
|
14
|
+
## What it does
|
15
|
+
|
16
|
+
This tool creates a new server project with:
|
17
|
+
- Express.js setup
|
18
|
+
- Basic folder structure (controllers, middlewares, models, routes, utils)
|
19
|
+
- Database configuration
|
20
|
+
- Environment variables setup
|
21
|
+
- Basic routing structure
|
22
|
+
|
23
|
+
## Project Structure
|
24
|
+
|
25
|
+
After running the tool, you'll get a project with this structure:
|
26
|
+
|
27
|
+
```
|
28
|
+
your-project/
|
29
|
+
├── src/
|
30
|
+
│ ├── controllers/
|
31
|
+
│ ├── middlewares/
|
32
|
+
│ ├── models/
|
33
|
+
│ ├── routes/
|
34
|
+
│ │ └── index.route.js
|
35
|
+
│ └── utils/
|
36
|
+
│ ├── db.config.js
|
37
|
+
│ └── general.js
|
38
|
+
├── package.json
|
39
|
+
└── .env
|
40
|
+
```
|
41
|
+
|
42
|
+
## Getting Started
|
43
|
+
|
44
|
+
1. Run the CLI tool
|
45
|
+
2. Choose your language preference
|
46
|
+
3. Enter your project name
|
47
|
+
4. Navigate to your project directory
|
48
|
+
5. Update the database connection in the `.env` file
|
49
|
+
6. Run `npm start` to start your server
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
ISC
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Feel free to submit issues and enhancement requests!
|
package/bin/cli.js
ADDED
package/index.js
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
import inquirer from "inquirer";
|
2
|
+
import path from "path";
|
3
|
+
import fs from "fs-extra";
|
4
|
+
import { fileURLToPath } from "url";
|
5
|
+
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
7
|
+
|
8
|
+
async function main() {
|
9
|
+
const answers = await inquirer.prompt([
|
10
|
+
{
|
11
|
+
type: "list",
|
12
|
+
name: "language",
|
13
|
+
message: "Choose language",
|
14
|
+
choices: ["Javascript", "Typescript"]
|
15
|
+
},
|
16
|
+
{
|
17
|
+
type: "input",
|
18
|
+
name: "projectName",
|
19
|
+
message: "Enter the name of your project",
|
20
|
+
default: "my-project"
|
21
|
+
}
|
22
|
+
]);
|
23
|
+
|
24
|
+
const templatePath = path.join(__dirname, "templates", answers.language.toLowerCase());
|
25
|
+
// const projectPath = path.join(__dirname, answers.projectName);
|
26
|
+
const projectPath = path.join(process.cwd(), answers.projectName);
|
27
|
+
|
28
|
+
console.log("templatePath--------->", templatePath)
|
29
|
+
console.log("projectPath---------->", projectPath)
|
30
|
+
|
31
|
+
fs.copy(templatePath, projectPath, err => {
|
32
|
+
if (err) return console.error(err)
|
33
|
+
console.log('success!')
|
34
|
+
})
|
35
|
+
|
36
|
+
console.log(`cd ${answers.projectName} and change DB connection in .env file and run npm start`)
|
37
|
+
}
|
38
|
+
|
39
|
+
export default main;
|
package/package.json
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
{
|
2
|
+
"name": "create-uk-node-server",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A CLI tool to quickly scaffold server projects with JavaScript or TypeScript templates",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
8
|
+
"start": "node bin/cli.js"
|
9
|
+
},
|
10
|
+
"bin": {
|
11
|
+
"uk-node-server": "bin/cli.js"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"cli",
|
15
|
+
"scaffold",
|
16
|
+
"server",
|
17
|
+
"javascript",
|
18
|
+
"typescript",
|
19
|
+
"express",
|
20
|
+
"boilerplate",
|
21
|
+
"generator"
|
22
|
+
],
|
23
|
+
"author": "utsav.Karkar",
|
24
|
+
"license": "ISC",
|
25
|
+
"type": "module",
|
26
|
+
"dependencies": {
|
27
|
+
"fs-extra": "^11.3.0",
|
28
|
+
"inquirer": "^12.6.3"
|
29
|
+
}
|
30
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import express from "express"
|
2
|
+
import { connectDB } from "./src/utils/db.config.js"
|
3
|
+
import indexRoutes from "./src/routes/index.route.js"
|
4
|
+
import { errorHandler } from "./src/middlewares/errorHandler.js"
|
5
|
+
|
6
|
+
const app = express()
|
7
|
+
const PORT = 3000
|
8
|
+
|
9
|
+
app.use(express.json());
|
10
|
+
app.use(express.urlencoded({ extended: true }));
|
11
|
+
app.use(errorHandler);
|
12
|
+
|
13
|
+
|
14
|
+
app.use("/", indexRoutes)
|
15
|
+
|
16
|
+
connectDB().then(() => {
|
17
|
+
app.listen(PORT, () => {
|
18
|
+
console.log(`Server is running on port ${PORT}`)
|
19
|
+
})
|
20
|
+
}).catch((error) => {
|
21
|
+
console.error("Error connecting to MongoDB:", error)
|
22
|
+
process.exit(1)
|
23
|
+
})
|
@@ -0,0 +1,22 @@
|
|
1
|
+
{
|
2
|
+
"name": "server-setup",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"start": "node index.js",
|
8
|
+
"dev": "nodemon index.js",
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
10
|
+
},
|
11
|
+
"keywords": [],
|
12
|
+
"author": "",
|
13
|
+
"license": "ISC",
|
14
|
+
"type": "module",
|
15
|
+
"dependencies": {
|
16
|
+
"bcrypt": "^6.0.0",
|
17
|
+
"dotenv": "^16.5.0",
|
18
|
+
"express": "^5.1.0",
|
19
|
+
"mongoose": "^8.15.0",
|
20
|
+
"nodemon": "^3.1.10"
|
21
|
+
}
|
22
|
+
}
|
File without changes
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import mongoose from "mongoose";
|
2
|
+
|
3
|
+
const userRole = {
|
4
|
+
ADMIN: 1,
|
5
|
+
USER: 2,
|
6
|
+
}
|
7
|
+
|
8
|
+
const userSchema = new mongoose.Schema({
|
9
|
+
name: {
|
10
|
+
type: String,
|
11
|
+
required: true
|
12
|
+
},
|
13
|
+
email: {
|
14
|
+
type: String,
|
15
|
+
required: true,
|
16
|
+
trim: true,
|
17
|
+
unique: true,
|
18
|
+
lowercase: true,
|
19
|
+
},
|
20
|
+
password: {
|
21
|
+
type: String,
|
22
|
+
required: true,
|
23
|
+
minlength: [8, "Password must be at least 8 characters long"],
|
24
|
+
},
|
25
|
+
role: {
|
26
|
+
type: Number,
|
27
|
+
enum: [userRole.ADMIN, userRole.USER],
|
28
|
+
default: userRole.USER,
|
29
|
+
}
|
30
|
+
}, { timestamps: true });
|
31
|
+
|
32
|
+
const User = mongoose.model("User", userSchema);
|
33
|
+
export default User;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import dotenv from 'dotenv'
|
2
|
+
dotenv.config()
|
3
|
+
import mongoose from 'mongoose'
|
4
|
+
|
5
|
+
export const connectDB = async () => {
|
6
|
+
try {
|
7
|
+
await mongoose.connect(process.env.DB_URL)
|
8
|
+
console.log('MongoDB connected successfully')
|
9
|
+
} catch (error) {
|
10
|
+
console.error('Error connecting to MongoDB:', error)
|
11
|
+
process.exit(1)
|
12
|
+
}
|
13
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export const successResponse = (msg, data = {}) => {
|
2
|
+
return {
|
3
|
+
flag: 1,
|
4
|
+
message: msg ?? "Success",
|
5
|
+
data
|
6
|
+
}
|
7
|
+
}
|
8
|
+
|
9
|
+
export const errorResponse = (msg, data = {}) => {
|
10
|
+
return {
|
11
|
+
flag: 0,
|
12
|
+
message: msg ?? "Something went wrong",
|
13
|
+
data
|
14
|
+
}
|
15
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
{
|
2
|
+
"name": "typescript-server",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "TypeScript server template",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"scripts": {
|
7
|
+
"start": "node dist/index.js",
|
8
|
+
"dev": "ts-node src/index.ts",
|
9
|
+
"build": "tsc",
|
10
|
+
"watch": "tsc --watch"
|
11
|
+
},
|
12
|
+
"dependencies": {
|
13
|
+
"express": "^4.18.2",
|
14
|
+
"dotenv": "^16.3.1",
|
15
|
+
"cors": "^2.8.5",
|
16
|
+
"mongoose": "^8.0.3"
|
17
|
+
},
|
18
|
+
"devDependencies": {
|
19
|
+
"@types/express": "^4.17.21",
|
20
|
+
"@types/node": "^20.10.0",
|
21
|
+
"@types/cors": "^2.8.17",
|
22
|
+
"typescript": "^5.3.2",
|
23
|
+
"ts-node": "^10.9.1"
|
24
|
+
},
|
25
|
+
"keywords": [],
|
26
|
+
"author": "",
|
27
|
+
"license": "ISC"
|
28
|
+
}
|
File without changes
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import express from 'express';
|
2
|
+
import cors from 'cors';
|
3
|
+
import dotenv from 'dotenv';
|
4
|
+
import { connectDB } from './utils/db.config';
|
5
|
+
|
6
|
+
dotenv.config();
|
7
|
+
|
8
|
+
const app = express();
|
9
|
+
const PORT = process.env.APP_PORT || 3000;
|
10
|
+
|
11
|
+
// Middleware
|
12
|
+
app.use(cors());
|
13
|
+
app.use(express.json());
|
14
|
+
app.use(express.urlencoded({ extended: true }));
|
15
|
+
|
16
|
+
// Routes
|
17
|
+
app.get('/', (req, res) => {
|
18
|
+
res.json({ message: 'Welcome to TypeScript Server!' });
|
19
|
+
});
|
20
|
+
|
21
|
+
// Import routes
|
22
|
+
import indexRoutes from './routes/index.route';
|
23
|
+
app.use('/api', indexRoutes);
|
24
|
+
|
25
|
+
// Start server
|
26
|
+
const startServer = async () => {
|
27
|
+
try {
|
28
|
+
await connectDB();
|
29
|
+
app.listen(PORT, () => {
|
30
|
+
console.log(`Server is running on port ${PORT}`);
|
31
|
+
});
|
32
|
+
} catch (error) {
|
33
|
+
console.error('Failed to start server:', error);
|
34
|
+
process.exit(1);
|
35
|
+
}
|
36
|
+
};
|
37
|
+
|
38
|
+
startServer();
|
File without changes
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import mongoose from "mongoose";
|
2
|
+
|
3
|
+
const userRole = {
|
4
|
+
ADMIN: 1,
|
5
|
+
USER: 2,
|
6
|
+
}
|
7
|
+
|
8
|
+
const userSchema = new mongoose.Schema({
|
9
|
+
name: {
|
10
|
+
type: String,
|
11
|
+
required: true
|
12
|
+
},
|
13
|
+
email: {
|
14
|
+
type: String,
|
15
|
+
required: true,
|
16
|
+
trim: true,
|
17
|
+
unique: true,
|
18
|
+
lowercase: true,
|
19
|
+
},
|
20
|
+
password: {
|
21
|
+
type: String,
|
22
|
+
required: true,
|
23
|
+
minlength: [8, "Password must be at least 8 characters long"],
|
24
|
+
},
|
25
|
+
role: {
|
26
|
+
type: Number,
|
27
|
+
enum: [userRole.ADMIN, userRole.USER],
|
28
|
+
default: userRole.USER,
|
29
|
+
}
|
30
|
+
}, { timestamps: true });
|
31
|
+
|
32
|
+
const User = mongoose.model("User", userSchema);
|
33
|
+
export default User;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import mongoose from 'mongoose';
|
2
|
+
|
3
|
+
export const connectDB = async (): Promise<void> => {
|
4
|
+
try {
|
5
|
+
const mongoURI = process.env.DB_URL || 'mongodb://localhost:27017/your-database';
|
6
|
+
await mongoose.connect(mongoURI);
|
7
|
+
console.log('MongoDB connected successfully');
|
8
|
+
} catch (error) {
|
9
|
+
console.error('MongoDB connection error:', error);
|
10
|
+
process.exit(1);
|
11
|
+
}
|
12
|
+
};
|
@@ -0,0 +1,19 @@
|
|
1
|
+
export const formatDate = (date: Date): string => {
|
2
|
+
return date.toISOString();
|
3
|
+
};
|
4
|
+
|
5
|
+
export const successResponse = (msg: string, data: any) => {
|
6
|
+
return {
|
7
|
+
flag: 1,
|
8
|
+
msg: msg ?? "Success",
|
9
|
+
data: data ?? null
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
export const errorResponse = (msg: string, data: any) => {
|
14
|
+
return {
|
15
|
+
flag: 0,
|
16
|
+
msg: msg ?? "Something went wrong",
|
17
|
+
data: data ?? null
|
18
|
+
}
|
19
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ES2020",
|
4
|
+
"module": "ESNext",
|
5
|
+
"lib": ["ES2020"],
|
6
|
+
"outDir": "./dist",
|
7
|
+
"rootDir": "./src",
|
8
|
+
"strict": true,
|
9
|
+
"esModuleInterop": true,
|
10
|
+
"skipLibCheck": true,
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
12
|
+
"moduleResolution": "node",
|
13
|
+
"resolveJsonModule": true,
|
14
|
+
"declaration": true,
|
15
|
+
"declarationMap": true,
|
16
|
+
"sourceMap": true
|
17
|
+
},
|
18
|
+
"include": ["src/**/*"],
|
19
|
+
"exclude": ["node_modules", "dist"]
|
20
|
+
}
|