create-craftjs 1.0.3 → 1.0.5
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 +137 -137
- package/bin/index.js +158 -158
- package/package.json +24 -24
- package/template/Dockerfile +57 -12
- package/template/babel.config.json +3 -3
- package/template/craft/commands/build.js +16 -15
- package/template/craft/commands/db-fresh.js +22 -22
- package/template/craft/commands/db-generate.js +23 -23
- package/template/craft/commands/db-migrate.js +22 -22
- package/template/craft/commands/dev.js +16 -16
- package/template/craft/commands/key-generate.js +41 -41
- package/template/craft/commands/make-apidocs.js +121 -121
- package/template/craft/commands/make-command.js +38 -38
- package/template/craft/commands/make-controller.js +95 -71
- package/template/craft/commands/make-dto.js +39 -39
- package/template/craft/commands/make-middleware.js +46 -46
- package/template/craft/commands/make-repository.js +36 -36
- package/template/craft/commands/make-route.js +92 -88
- package/template/craft/commands/make-service.js +39 -39
- package/template/craft/commands/make-test.js +48 -48
- package/template/craft/commands/make-utils.js +30 -30
- package/template/craft/commands/make-validation.js +42 -42
- package/template/craft/commands/make-view.js +42 -42
- package/template/craft/commands/start.js +29 -29
- package/template/craft/commands/test.js +20 -20
- package/template/craft.js +256 -256
- package/template/docker-compose.yml +8 -0
- package/template/nodemon.json +6 -6
- package/template/package-lock.json +8877 -8877
- package/template/package.json +84 -84
- package/template/prisma/schema.prisma +22 -22
- package/template/prisma/seed.ts +29 -29
- package/template/src/apidocs/auth-docs.ts +314 -314
- package/template/src/apidocs/users-docs.ts +240 -240
- package/template/src/config/cloudinary.ts +21 -21
- package/template/src/config/database.ts +90 -90
- package/template/src/config/env.ts +67 -67
- package/template/src/config/logger.ts +139 -139
- package/template/src/config/nodemailer.ts +23 -23
- package/template/src/config/web.ts +47 -47
- package/template/src/controllers/auth-controller.ts +88 -88
- package/template/src/controllers/user-controller.ts +79 -79
- package/template/src/dtos/list-dto.ts +12 -12
- package/template/src/dtos/user-dto.ts +57 -57
- package/template/src/main.ts +28 -28
- package/template/src/middleware/auth-middleware.ts +44 -44
- package/template/src/middleware/error-middleware.ts +27 -27
- package/template/src/middleware/http-logger-middleware.ts +31 -31
- package/template/src/repositories/user-repository.ts +75 -75
- package/template/src/routes/auth-route.ts +20 -20
- package/template/src/routes/main-route.ts +25 -25
- package/template/src/routes/user-route.ts +35 -35
- package/template/src/services/auth-service.ts +162 -162
- package/template/src/services/user-service.ts +102 -102
- package/template/src/types/type-request.ts +6 -6
- package/template/src/utils/async-handler.ts +9 -9
- package/template/src/utils/response-error.ts +10 -10
- package/template/src/utils/response.ts +60 -60
- package/template/src/utils/swagger.ts +135 -135
- package/template/src/utils/validation.ts +7 -7
- package/template/src/validations/user-validation.ts +127 -127
- package/template/src/views/index.ejs +6 -6
- package/template/src/views/layouts/main.ejs +14 -14
- package/template/src/views/partials/header.ejs +3 -3
- package/template/test/user.test.ts +16 -16
- package/template/tsconfig.json +13 -13
- package/template/.dockerignore +0 -4
- package/template/src/controllers/sass-controller.ts +0 -24
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const chalk = require("chalk");
|
|
4
|
-
|
|
5
|
-
function makeCommand(name) {
|
|
6
|
-
if (!name) {
|
|
7
|
-
console.log(chalk.red("❌ Please provide a command name."));
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const fileName = `${name.toLowerCase()}.js`;
|
|
12
|
-
const targetDir = path.resolve("craft", "commands");
|
|
13
|
-
|
|
14
|
-
if (!fs.existsSync(targetDir)) {
|
|
15
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const filePath = path.join(targetDir, fileName);
|
|
19
|
-
if (fs.existsSync(filePath)) {
|
|
20
|
-
console.log(chalk.yellow("⚠️ Command already exists."));
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const content = `const fs = require("fs");
|
|
25
|
-
const path = require("path");
|
|
26
|
-
const chalk = require("chalk");
|
|
27
|
-
|
|
28
|
-
function make${name}(name) {
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
module.exports = make${name};
|
|
33
|
-
`;
|
|
34
|
-
|
|
35
|
-
fs.writeFileSync(filePath, content);
|
|
36
|
-
console.log(chalk.green(`✅ Command created at ${filePath}`));
|
|
37
|
-
}
|
|
38
|
-
module.exports = makeCommand;
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
|
|
5
|
+
function makeCommand(name) {
|
|
6
|
+
if (!name) {
|
|
7
|
+
console.log(chalk.red("❌ Please provide a command name."));
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const fileName = `${name.toLowerCase()}.js`;
|
|
12
|
+
const targetDir = path.resolve("craft", "commands");
|
|
13
|
+
|
|
14
|
+
if (!fs.existsSync(targetDir)) {
|
|
15
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const filePath = path.join(targetDir, fileName);
|
|
19
|
+
if (fs.existsSync(filePath)) {
|
|
20
|
+
console.log(chalk.yellow("⚠️ Command already exists."));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const content = `const fs = require("fs");
|
|
25
|
+
const path = require("path");
|
|
26
|
+
const chalk = require("chalk");
|
|
27
|
+
|
|
28
|
+
function make${name}(name) {
|
|
29
|
+
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = make${name};
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
fs.writeFileSync(filePath, content);
|
|
36
|
+
console.log(chalk.green(`✅ Command created at ${filePath}`));
|
|
37
|
+
}
|
|
38
|
+
module.exports = makeCommand;
|
|
@@ -1,71 +1,95 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const chalk = require("chalk");
|
|
4
|
-
|
|
5
|
-
function toPascalCase(str) {
|
|
6
|
-
return str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function makeController(name, options = {}) {
|
|
10
|
-
if (!name) {
|
|
11
|
-
console.log(chalk.red("❌ Please provide a controller name."));
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const parts = name.split("/");
|
|
16
|
-
const rawName = parts.pop();
|
|
17
|
-
const className = `${toPascalCase(rawName)}Controller`;
|
|
18
|
-
const fileName = `${rawName.toLowerCase()}-controller.ts`;
|
|
19
|
-
const targetDir = path.resolve("src", "controllers", ...parts);
|
|
20
|
-
|
|
21
|
-
if (!fs.existsSync(targetDir)) {
|
|
22
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const filePath = path.join(targetDir, fileName);
|
|
26
|
-
if (fs.existsSync(filePath)) {
|
|
27
|
-
console.log(chalk.yellow("⚠️ Controller already exists."));
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const resourceMethods = `
|
|
32
|
-
static async getAll(req: Request, res: Response) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static async
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
static async
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
|
|
5
|
+
function toPascalCase(str) {
|
|
6
|
+
return str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function makeController(name, options = {}) {
|
|
10
|
+
if (!name) {
|
|
11
|
+
console.log(chalk.red("❌ Please provide a controller name."));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const parts = name.split("/");
|
|
16
|
+
const rawName = parts.pop();
|
|
17
|
+
const className = `${toPascalCase(rawName)}Controller`;
|
|
18
|
+
const fileName = `${rawName.toLowerCase()}-controller.ts`;
|
|
19
|
+
const targetDir = path.resolve("src", "controllers", ...parts);
|
|
20
|
+
|
|
21
|
+
if (!fs.existsSync(targetDir)) {
|
|
22
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const filePath = path.join(targetDir, fileName);
|
|
26
|
+
if (fs.existsSync(filePath)) {
|
|
27
|
+
console.log(chalk.yellow("⚠️ Controller already exists."));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const resourceMethods = `
|
|
32
|
+
static async getAll(req: Request, res: Response, next: NextFunction) {
|
|
33
|
+
try {
|
|
34
|
+
res.status(200).json({ message: "Listing all resources" });
|
|
35
|
+
} catch (error) {
|
|
36
|
+
next(error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static async getOne(req: Request, res: Response, next: NextFunction) {
|
|
41
|
+
try {
|
|
42
|
+
res.status(200).json({ message: "Showing single resource" });
|
|
43
|
+
} catch (error) {
|
|
44
|
+
next(error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static async create(req: Request, res: Response, next: NextFunction) {
|
|
49
|
+
try {
|
|
50
|
+
res.status(201).json({ message: "Resource created" });
|
|
51
|
+
} catch (error) {
|
|
52
|
+
next(error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static async update(req: Request, res: Response, next: NextFunction) {
|
|
57
|
+
try {
|
|
58
|
+
res.status(201).json({ message: "Resource updated" });
|
|
59
|
+
} catch (error) {
|
|
60
|
+
next(error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static async delete(req: Request, res: Response, next: NextFunction) {
|
|
65
|
+
try {
|
|
66
|
+
res.status(201).json({ message: "Resource deleted" });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
next(error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
`;
|
|
72
|
+
|
|
73
|
+
const defaultMethod = `
|
|
74
|
+
static async getAll(req: Request, res: Response, next: NextFunction) {
|
|
75
|
+
try {
|
|
76
|
+
res.status(201).json({ message: "ok" });
|
|
77
|
+
} catch (error) {
|
|
78
|
+
next(error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
`;
|
|
82
|
+
|
|
83
|
+
const methods = options.resource ? resourceMethods : defaultMethod;
|
|
84
|
+
|
|
85
|
+
const content = `import { Request, Response,NextFunction } from "express";
|
|
86
|
+
|
|
87
|
+
export class ${className} {${methods}
|
|
88
|
+
}
|
|
89
|
+
`;
|
|
90
|
+
|
|
91
|
+
fs.writeFileSync(filePath, content);
|
|
92
|
+
console.log(chalk.green(`✅ Controller created at ${filePath}`));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = makeController;
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const chalk = require("chalk");
|
|
4
|
-
|
|
5
|
-
const toPascalCase = (str) =>
|
|
6
|
-
str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
7
|
-
|
|
8
|
-
function makeDto(name) {
|
|
9
|
-
if (!name) {
|
|
10
|
-
console.log(chalk.red("❌ Please provide a dto name."));
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const typeName = `${toPascalCase(name)}Request`;
|
|
15
|
-
const fileName = `${name.toLowerCase()}-dto.ts`;
|
|
16
|
-
const targetDir = path.resolve("src", "dtos");
|
|
17
|
-
|
|
18
|
-
if (!fs.existsSync(targetDir)) {
|
|
19
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const filePath = path.join(targetDir, fileName);
|
|
23
|
-
if (fs.existsSync(filePath)) {
|
|
24
|
-
console.log(chalk.yellow("⚠️ Dto already exists."));
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const content = `export type ${typeName} = {
|
|
29
|
-
field1: string;
|
|
30
|
-
field2?: string; // optional
|
|
31
|
-
|
|
32
|
-
};
|
|
33
|
-
`;
|
|
34
|
-
|
|
35
|
-
fs.writeFileSync(filePath, content);
|
|
36
|
-
console.log(chalk.green(`✅ Dto created at ${filePath}`));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = makeDto;
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
|
|
5
|
+
const toPascalCase = (str) =>
|
|
6
|
+
str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
7
|
+
|
|
8
|
+
function makeDto(name) {
|
|
9
|
+
if (!name) {
|
|
10
|
+
console.log(chalk.red("❌ Please provide a dto name."));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const typeName = `${toPascalCase(name)}Request`;
|
|
15
|
+
const fileName = `${name.toLowerCase()}-dto.ts`;
|
|
16
|
+
const targetDir = path.resolve("src", "dtos");
|
|
17
|
+
|
|
18
|
+
if (!fs.existsSync(targetDir)) {
|
|
19
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const filePath = path.join(targetDir, fileName);
|
|
23
|
+
if (fs.existsSync(filePath)) {
|
|
24
|
+
console.log(chalk.yellow("⚠️ Dto already exists."));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const content = `export type ${typeName} = {
|
|
29
|
+
field1: string;
|
|
30
|
+
field2?: string; // optional
|
|
31
|
+
|
|
32
|
+
};
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
fs.writeFileSync(filePath, content);
|
|
36
|
+
console.log(chalk.green(`✅ Dto created at ${filePath}`));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = makeDto;
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const chalk = require("chalk");
|
|
4
|
-
|
|
5
|
-
const toCamelCase = (str) =>
|
|
6
|
-
str
|
|
7
|
-
.toLowerCase()
|
|
8
|
-
.split("-")
|
|
9
|
-
.map((word, i) => (i === 0 ? word : word[0].toUpperCase() + word.slice(1)))
|
|
10
|
-
.join("");
|
|
11
|
-
|
|
12
|
-
function makeMiddleware(name) {
|
|
13
|
-
if (!name) {
|
|
14
|
-
console.log(chalk.red("❌ Please provide a middleware name."));
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const className = `${toCamelCase(name)}Middleware`;
|
|
19
|
-
const fileName = `${name.toLowerCase()}-middleware.ts`;
|
|
20
|
-
const targetDir = path.resolve("src", "middleware");
|
|
21
|
-
|
|
22
|
-
if (!fs.existsSync(targetDir)) {
|
|
23
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const filePath = path.join(targetDir, fileName);
|
|
27
|
-
if (fs.existsSync(filePath)) {
|
|
28
|
-
console.log(chalk.yellow("⚠️ Middleware already exists."));
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const content = `import { NextFunction, Request, Response } from "express";
|
|
33
|
-
|
|
34
|
-
export const ${className} = async (
|
|
35
|
-
req: Request,
|
|
36
|
-
res: Response,
|
|
37
|
-
next: NextFunction
|
|
38
|
-
) => {
|
|
39
|
-
next();
|
|
40
|
-
};
|
|
41
|
-
`;
|
|
42
|
-
|
|
43
|
-
fs.writeFileSync(filePath, content);
|
|
44
|
-
console.log(chalk.green(`✅ Middleware created at ${filePath}`));
|
|
45
|
-
}
|
|
46
|
-
module.exports = makeMiddleware;
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
|
|
5
|
+
const toCamelCase = (str) =>
|
|
6
|
+
str
|
|
7
|
+
.toLowerCase()
|
|
8
|
+
.split("-")
|
|
9
|
+
.map((word, i) => (i === 0 ? word : word[0].toUpperCase() + word.slice(1)))
|
|
10
|
+
.join("");
|
|
11
|
+
|
|
12
|
+
function makeMiddleware(name) {
|
|
13
|
+
if (!name) {
|
|
14
|
+
console.log(chalk.red("❌ Please provide a middleware name."));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const className = `${toCamelCase(name)}Middleware`;
|
|
19
|
+
const fileName = `${name.toLowerCase()}-middleware.ts`;
|
|
20
|
+
const targetDir = path.resolve("src", "middleware");
|
|
21
|
+
|
|
22
|
+
if (!fs.existsSync(targetDir)) {
|
|
23
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const filePath = path.join(targetDir, fileName);
|
|
27
|
+
if (fs.existsSync(filePath)) {
|
|
28
|
+
console.log(chalk.yellow("⚠️ Middleware already exists."));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const content = `import { NextFunction, Request, Response } from "express";
|
|
33
|
+
|
|
34
|
+
export const ${className} = async (
|
|
35
|
+
req: Request,
|
|
36
|
+
res: Response,
|
|
37
|
+
next: NextFunction
|
|
38
|
+
) => {
|
|
39
|
+
next();
|
|
40
|
+
};
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
fs.writeFileSync(filePath, content);
|
|
44
|
+
console.log(chalk.green(`✅ Middleware created at ${filePath}`));
|
|
45
|
+
}
|
|
46
|
+
module.exports = makeMiddleware;
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const chalk = require("chalk");
|
|
4
|
-
const toPascalCase = (str) =>
|
|
5
|
-
str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
6
|
-
|
|
7
|
-
function makeRepository(name) {
|
|
8
|
-
if (!name) {
|
|
9
|
-
console.log(chalk.red("❌ Please provide a repository name."));
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const className = `${toPascalCase(name)}Repository`;
|
|
14
|
-
const fileName = `${name.toLowerCase()}-repository.ts`;
|
|
15
|
-
const targetDir = path.resolve("src", "repositories");
|
|
16
|
-
|
|
17
|
-
if (!fs.existsSync(targetDir)) {
|
|
18
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const filePath = path.join(targetDir, fileName);
|
|
22
|
-
if (fs.existsSync(filePath)) {
|
|
23
|
-
console.log(chalk.yellow("⚠️ Repository already exists."));
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const content = `import { prismaClient } from "../config/database";
|
|
28
|
-
|
|
29
|
-
export class ${className} {
|
|
30
|
-
}
|
|
31
|
-
`;
|
|
32
|
-
|
|
33
|
-
fs.writeFileSync(filePath, content);
|
|
34
|
-
console.log(chalk.green(`✅ Repository created at ${filePath}`));
|
|
35
|
-
}
|
|
36
|
-
module.exports = makeRepository;
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
const toPascalCase = (str) =>
|
|
5
|
+
str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
6
|
+
|
|
7
|
+
function makeRepository(name) {
|
|
8
|
+
if (!name) {
|
|
9
|
+
console.log(chalk.red("❌ Please provide a repository name."));
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const className = `${toPascalCase(name)}Repository`;
|
|
14
|
+
const fileName = `${name.toLowerCase()}-repository.ts`;
|
|
15
|
+
const targetDir = path.resolve("src", "repositories");
|
|
16
|
+
|
|
17
|
+
if (!fs.existsSync(targetDir)) {
|
|
18
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const filePath = path.join(targetDir, fileName);
|
|
22
|
+
if (fs.existsSync(filePath)) {
|
|
23
|
+
console.log(chalk.yellow("⚠️ Repository already exists."));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const content = `import { prismaClient } from "../config/database";
|
|
28
|
+
|
|
29
|
+
export class ${className} {
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
fs.writeFileSync(filePath, content);
|
|
34
|
+
console.log(chalk.green(`✅ Repository created at ${filePath}`));
|
|
35
|
+
}
|
|
36
|
+
module.exports = makeRepository;
|
|
@@ -1,88 +1,92 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const chalk = require("chalk");
|
|
4
|
-
|
|
5
|
-
const toPascalCase = (str) =>
|
|
6
|
-
str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
7
|
-
|
|
8
|
-
function makeRoute(name) {
|
|
9
|
-
if (!name) {
|
|
10
|
-
console.log(chalk.red("❌ Please provide a route name."));
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const routeName = name.toLowerCase();
|
|
15
|
-
const className = `${toPascalCase(name)}Controller`;
|
|
16
|
-
const routeConst = `${routeName}Router`;
|
|
17
|
-
const fileName = `${routeName}-route.ts`;
|
|
18
|
-
const targetDir = path.resolve("src", "routes");
|
|
19
|
-
|
|
20
|
-
if (!fs.existsSync(targetDir)) {
|
|
21
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const filePath = path.join(targetDir, fileName);
|
|
25
|
-
if (fs.existsSync(filePath)) {
|
|
26
|
-
console.log(chalk.yellow("⚠️ Route already exists."));
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const content = `import express from "express";
|
|
31
|
-
import { asyncHandler } from "../utils/async-handler";
|
|
32
|
-
import { authMiddleware } from "../middleware/auth-middleware";
|
|
33
|
-
|
|
34
|
-
import { ${className} } from "../controllers/${routeName}-controller";
|
|
35
|
-
|
|
36
|
-
export const ${routeConst} = express.Router();
|
|
37
|
-
|
|
38
|
-
// Example routes:
|
|
39
|
-
${routeConst}.get("/api/${routeName}s",
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
|
|
5
|
+
const toPascalCase = (str) =>
|
|
6
|
+
str.replace(/(^\w|-\w)/g, (m) => m.replace("-", "").toUpperCase());
|
|
7
|
+
|
|
8
|
+
function makeRoute(name) {
|
|
9
|
+
if (!name) {
|
|
10
|
+
console.log(chalk.red("❌ Please provide a route name."));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const routeName = name.toLowerCase();
|
|
15
|
+
const className = `${toPascalCase(name)}Controller`;
|
|
16
|
+
const routeConst = `${routeName}Router`;
|
|
17
|
+
const fileName = `${routeName}-route.ts`;
|
|
18
|
+
const targetDir = path.resolve("src", "routes");
|
|
19
|
+
|
|
20
|
+
if (!fs.existsSync(targetDir)) {
|
|
21
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const filePath = path.join(targetDir, fileName);
|
|
25
|
+
if (fs.existsSync(filePath)) {
|
|
26
|
+
console.log(chalk.yellow("⚠️ Route already exists."));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const content = `import express from "express";
|
|
31
|
+
import { asyncHandler } from "../utils/async-handler";
|
|
32
|
+
import { authMiddleware } from "../middleware/auth-middleware";
|
|
33
|
+
|
|
34
|
+
import { ${className} } from "../controllers/${routeName}-controller";
|
|
35
|
+
|
|
36
|
+
export const ${routeConst} = express.Router();
|
|
37
|
+
|
|
38
|
+
// Example routes:
|
|
39
|
+
${routeConst}.get("/api/${routeName}s", ${className}.getAll);
|
|
40
|
+
${routeConst}.get("/api/${routeName}s/:id", ${className}.getOne);
|
|
41
|
+
${routeConst}.post("/api/${routeName}s", ${className}.create);
|
|
42
|
+
${routeConst}.get("/api/${routeName}s/:id", ${className}.update);
|
|
43
|
+
${routeConst}.get("/api/${routeName}s/:id", ${className}.delete);
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
fs.writeFileSync(filePath, content);
|
|
47
|
+
console.log(chalk.green(`✅ Route created at ${filePath}`));
|
|
48
|
+
|
|
49
|
+
const mainRouterPath = path.resolve("src", "routes", "main-route.ts");
|
|
50
|
+
|
|
51
|
+
if (fs.existsSync(mainRouterPath)) {
|
|
52
|
+
let mainRouterContent = fs.readFileSync(mainRouterPath, "utf-8");
|
|
53
|
+
|
|
54
|
+
const importStatement = `import { ${routeConst} } from "./${routeName}-route";`;
|
|
55
|
+
const useStatement = `mainRouter.use(${routeConst});`;
|
|
56
|
+
|
|
57
|
+
if (!mainRouterContent.includes(importStatement)) {
|
|
58
|
+
const lines = mainRouterContent.split("\n");
|
|
59
|
+
const importIndex = lines.findIndex((line) => line.startsWith("import"));
|
|
60
|
+
const lastImportIndex = [...lines]
|
|
61
|
+
.reverse()
|
|
62
|
+
.findIndex((line) => line.startsWith("import"));
|
|
63
|
+
const insertIndex =
|
|
64
|
+
lastImportIndex >= 0 ? lines.length - lastImportIndex : 0;
|
|
65
|
+
lines.splice(insertIndex, 0, importStatement);
|
|
66
|
+
mainRouterContent = lines.join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!mainRouterContent.includes(useStatement)) {
|
|
70
|
+
const routerEndIndex = mainRouterContent.lastIndexOf("mainRouter.use(");
|
|
71
|
+
const insertIndex =
|
|
72
|
+
routerEndIndex >= 0
|
|
73
|
+
? mainRouterContent.indexOf("\n", routerEndIndex) + 1
|
|
74
|
+
: mainRouterContent.length;
|
|
75
|
+
|
|
76
|
+
mainRouterContent =
|
|
77
|
+
mainRouterContent.slice(0, insertIndex) +
|
|
78
|
+
useStatement +
|
|
79
|
+
"\n" +
|
|
80
|
+
mainRouterContent.slice(insertIndex);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fs.writeFileSync(mainRouterPath, mainRouterContent);
|
|
84
|
+
console.log(chalk.green("✅ Route registered in main-router."));
|
|
85
|
+
} else {
|
|
86
|
+
console.log(
|
|
87
|
+
chalk.yellow("⚠️ main-route.ts not found. Please register manually.")
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = makeRoute;
|