express-module-cli 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env node
2
+ import fs from "fs-extra";
3
+ import path from "path";
4
+ import chalk from "chalk";
5
+ import { hideBin } from "yargs/helpers";
6
+ import yargs from "yargs";
7
+ // Utility function to capitalize the first letter of a string
8
+ const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);
9
+ // Main CLI logic
10
+ yargs(hideBin(process.argv))
11
+ .scriptName("create") // Allows using "npx create" or "create" if globally installed
12
+ .command("module <module>", "Create a new module", (yargs) => {
13
+ return yargs.positional("module", {
14
+ describe: "The name of the module",
15
+ type: "string",
16
+ });
17
+ }, async (args) => {
18
+ const moduleName = args.module.toLowerCase();
19
+ const modulePath = path.join(process.cwd(), "src", "modules", moduleName);
20
+ // Define templates for files
21
+ const files = [
22
+ {
23
+ name: `${moduleName}.controller.ts`,
24
+ content: `
25
+ import { Request, Response } from "express";
26
+ import { ${capitalize(moduleName)}Service } from "./${moduleName}.service";
27
+
28
+ const findAll = async (req: Request, res: Response) => {
29
+ const data = await ${capitalize(moduleName)}Service.findAll();
30
+ res.json(data);
31
+ };
32
+
33
+ export const ${capitalize(moduleName)}Controller = {
34
+ findAll,
35
+ };
36
+ `,
37
+ },
38
+ {
39
+ name: `${moduleName}.service.ts`,
40
+ content: `
41
+ const findAll = async (): Promise<string> => {
42
+ return "hello world";
43
+ };
44
+
45
+ export const ${capitalize(moduleName)}Service = {
46
+ findAll,
47
+ };
48
+ `,
49
+ },
50
+ {
51
+ name: `${moduleName}.route.ts`,
52
+ content: `
53
+ import { Router } from "express";
54
+ import { ${capitalize(moduleName)}Controller } from "./${moduleName}.controller";
55
+
56
+ const router = Router();
57
+
58
+ router.get("/", ${capitalize(moduleName)}Controller.findAll);
59
+
60
+ export default router;
61
+ `,
62
+ },
63
+ {
64
+ name: `${moduleName}.model.ts`,
65
+ content: `// ${moduleName} model`,
66
+ },
67
+ {
68
+ name: `${moduleName}.interface.ts`,
69
+ content: `// ${moduleName} interface`,
70
+ },
71
+ ];
72
+ // Check if the module folder already exists
73
+ if (fs.existsSync(modulePath)) {
74
+ console.log(chalk.red(`The module "${moduleName}" already exists.`));
75
+ process.exit(1);
76
+ }
77
+ // Create the module folder
78
+ fs.mkdirSync(modulePath, { recursive: true });
79
+ console.log(chalk.green(`Module folder created at: ${modulePath}`));
80
+ // Create the files in the module folder
81
+ files.forEach((file) => {
82
+ const filePath = path.join(modulePath, file.name);
83
+ fs.writeFileSync(filePath, file.content.trimStart());
84
+ console.log(chalk.green(`Created file: ${filePath}`));
85
+ });
86
+ console.log(chalk.blue(`Module "${moduleName}" created successfully.`));
87
+ })
88
+ .demandCommand(1, "You need to specify a valid command")
89
+ .strict()
90
+ .help().argv;
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "express-module-cli",
3
+ "version": "1.0.0",
4
+ "description": "A CLI tool to create modules for TypeScript-based Express apps",
5
+ "bin": {
6
+ "create": "./dist/index.js"
7
+ },
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "start": "node dist/index.js"
11
+ },
12
+ "dependencies": {
13
+ "chalk": "^5.0.0",
14
+ "fs-extra": "^10.1.0",
15
+ "yargs": "^17.6.2"
16
+ },
17
+ "devDependencies": {
18
+ "@types/fs-extra": "^9.0.13",
19
+ "@types/node": "^20.4.2",
20
+ "@types/yargs": "^17.0.33",
21
+ "typescript": "^5.2.0"
22
+ },
23
+ "type": "module",
24
+ "main": "./dist/index.js"
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "fs-extra";
4
+ import path from "path";
5
+ import chalk from "chalk";
6
+ import { hideBin } from "yargs/helpers";
7
+ import yargs from "yargs";
8
+
9
+ // Utility function to capitalize the first letter of a string
10
+ const capitalize = (name: string): string =>
11
+ name.charAt(0).toUpperCase() + name.slice(1);
12
+
13
+ // Main CLI logic
14
+ yargs(hideBin(process.argv))
15
+ .scriptName("create") // Allows using "npx create" or "create" if globally installed
16
+ .command(
17
+ "module <module>",
18
+ "Create a new module",
19
+ (yargs) => {
20
+ return yargs.positional("module", {
21
+ describe: "The name of the module",
22
+ type: "string",
23
+ });
24
+ },
25
+ async (args) => {
26
+ const moduleName = (args.module as string).toLowerCase();
27
+ const modulePath = path.join(process.cwd(), "src", "modules", moduleName);
28
+
29
+ // Define templates for files
30
+ const files = [
31
+ {
32
+ name: `${moduleName}.controller.ts`,
33
+ content: `
34
+ import { Request, Response } from "express";
35
+ import { ${capitalize(moduleName)}Service } from "./${moduleName}.service";
36
+
37
+ const findAll = async (req: Request, res: Response) => {
38
+ const data = await ${capitalize(moduleName)}Service.findAll();
39
+ res.json(data);
40
+ };
41
+
42
+ export const ${capitalize(moduleName)}Controller = {
43
+ findAll,
44
+ };
45
+ `,
46
+ },
47
+ {
48
+ name: `${moduleName}.service.ts`,
49
+ content: `
50
+ const findAll = async (): Promise<string> => {
51
+ return "hello world";
52
+ };
53
+
54
+ export const ${capitalize(moduleName)}Service = {
55
+ findAll,
56
+ };
57
+ `,
58
+ },
59
+ {
60
+ name: `${moduleName}.route.ts`,
61
+ content: `
62
+ import { Router } from "express";
63
+ import { ${capitalize(
64
+ moduleName
65
+ )}Controller } from "./${moduleName}.controller";
66
+
67
+ const router = Router();
68
+
69
+ router.get("/", ${capitalize(moduleName)}Controller.findAll);
70
+
71
+ export default router;
72
+ `,
73
+ },
74
+ {
75
+ name: `${moduleName}.model.ts`,
76
+ content: `// ${moduleName} model`,
77
+ },
78
+ {
79
+ name: `${moduleName}.interface.ts`,
80
+ content: `// ${moduleName} interface`,
81
+ },
82
+ ];
83
+
84
+ // Check if the module folder already exists
85
+ if (fs.existsSync(modulePath)) {
86
+ console.log(chalk.red(`The module "${moduleName}" already exists.`));
87
+ process.exit(1);
88
+ }
89
+
90
+ // Create the module folder
91
+ fs.mkdirSync(modulePath, { recursive: true });
92
+ console.log(chalk.green(`Module folder created at: ${modulePath}`));
93
+
94
+ // Create the files in the module folder
95
+ files.forEach((file) => {
96
+ const filePath = path.join(modulePath, file.name);
97
+ fs.writeFileSync(filePath, file.content.trimStart());
98
+ console.log(chalk.green(`Created file: ${filePath}`));
99
+ });
100
+
101
+ console.log(chalk.blue(`Module "${moduleName}" created successfully.`));
102
+ }
103
+ )
104
+ .demandCommand(1, "You need to specify a valid command")
105
+ .strict()
106
+ .help().argv;
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2020",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "outDir": "./dist",
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "forceConsistentCasingInFileNames": true
11
+ },
12
+ "include": ["src/**/*", "test.js"],
13
+ "exclude": ["node_modules", "dist"]
14
+ }