gorig-cli 1.0.7 → 1.0.9
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 +38 -16
- package/bin/cli.js +8 -0
- package/commands/create.js +41 -40
- package/commands/doc.js +682 -0
- package/commands/init.js +110 -28
- package/package.json +6 -3
- package/templates/controller.go.ejs +3 -3
- package/templates/init.go.ejs +6 -0
- package/templates/model.go.ejs +3 -3
- package/templates/redoc_template.html +102 -0
- package/templates/router.go.ejs +7 -7
package/README.md
CHANGED
|
@@ -1,65 +1,87 @@
|
|
|
1
1
|
# Gorig CLI
|
|
2
2
|
|
|
3
|
-
Gorig CLI
|
|
3
|
+
Gorig CLI is a scaffolding tool based on Node.js, used for quickly creating the project structure and modules based on the Gorig framework for Go.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Install globally using npm:
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
10
|
npm install -g gorig-cli
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Or run directly using npx:
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
16
|
npx gorig-cli@latest <command>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
##
|
|
19
|
+
## Quick Start
|
|
20
20
|
|
|
21
|
-
###
|
|
21
|
+
### Initialize a New Project
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
Use the `init` command to create a new project:
|
|
24
24
|
|
|
25
25
|
```sh
|
|
26
26
|
gorig-cli init my-new-project
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
Or use npx:
|
|
30
30
|
|
|
31
31
|
```sh
|
|
32
32
|
npx gorig-cli@latest init my-new-project
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
This will create a new project in the current directory, including basic files and directories like `_cmd/main.go`, `domain/init.go`, `cron/cron.go`, etc.
|
|
36
36
|
|
|
37
|
-
###
|
|
37
|
+
### Create a New Module
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
Use the `create` command in the project root directory to create a new module:
|
|
40
40
|
|
|
41
41
|
```sh
|
|
42
42
|
gorig-cli create user
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
Or use npx:
|
|
46
46
|
|
|
47
47
|
```sh
|
|
48
48
|
npx gorig-cli@latest create user
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
This will create a module named `user` in the project, including folders like `api/`, `internal/`, `model/`, and necessary code.
|
|
52
52
|
|
|
53
|
-
###
|
|
53
|
+
### Generate API Documentation
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
Use the `doc` command to generate OpenAPI documentation:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
# Generate documentation for all modules
|
|
59
|
+
gorig-cli doc
|
|
60
|
+
|
|
61
|
+
# Generate documentation for a specific module
|
|
62
|
+
gorig-cli doc user
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Or use npx:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npx gorig-cli@latest doc
|
|
69
|
+
npx gorig-cli@latest doc user
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
After generating the documentation, you can access it through:
|
|
73
|
+
http://127.0.0.1:8080/redoc.html
|
|
74
|
+
|
|
75
|
+
### Run the Project
|
|
76
|
+
|
|
77
|
+
After entering the project directory, you can run the project using the following commands:
|
|
56
78
|
|
|
57
79
|
```sh
|
|
58
80
|
cd my-new-project
|
|
59
81
|
go run _cmd/main.go
|
|
60
82
|
```
|
|
61
83
|
|
|
62
|
-
|
|
84
|
+
Or run it after building:
|
|
63
85
|
|
|
64
86
|
```sh
|
|
65
87
|
go build -o my-new-project _cmd/main.go && ./my-new-project
|
package/bin/cli.js
CHANGED
|
@@ -39,6 +39,14 @@ switch (command) {
|
|
|
39
39
|
console.error(chalk.red('无法加载 init 命令模块:', error.message));
|
|
40
40
|
});
|
|
41
41
|
break;
|
|
42
|
+
case 'doc':
|
|
43
|
+
import(path.join(__dirname, '../commands/doc.js')).then(module => {
|
|
44
|
+
const docModule = module.default;
|
|
45
|
+
docModule(); // 执行 doc 命令
|
|
46
|
+
}).catch(error => {
|
|
47
|
+
console.error(chalk.red('无法加载 doc 命令模块:', error.message));
|
|
48
|
+
});
|
|
49
|
+
break;
|
|
42
50
|
|
|
43
51
|
default:
|
|
44
52
|
console.error(chalk.red(`未知的命令: ${command}`));
|
package/commands/create.js
CHANGED
|
@@ -4,11 +4,11 @@ import ejs from 'ejs';
|
|
|
4
4
|
import chalk from 'chalk';
|
|
5
5
|
import { fileURLToPath } from 'url';
|
|
6
6
|
|
|
7
|
-
//
|
|
7
|
+
// Define __dirname variable
|
|
8
8
|
const __filename = fileURLToPath(import.meta.url);
|
|
9
9
|
const __dirname = path.dirname(__filename);
|
|
10
10
|
|
|
11
|
-
//
|
|
11
|
+
// Get the module name from the go.mod file
|
|
12
12
|
const getGoModModuleName = async () => {
|
|
13
13
|
try {
|
|
14
14
|
const goModPath = path.join(process.cwd(), 'go.mod');
|
|
@@ -17,35 +17,35 @@ const getGoModModuleName = async () => {
|
|
|
17
17
|
if (moduleLine) {
|
|
18
18
|
return moduleLine.split(' ')[1].trim();
|
|
19
19
|
} else {
|
|
20
|
-
throw new Error('
|
|
20
|
+
throw new Error('Module name not found');
|
|
21
21
|
}
|
|
22
22
|
} catch (error) {
|
|
23
|
-
console.error(chalk.red('
|
|
23
|
+
console.error(chalk.red('Unable to read go.mod file. Please confirm that you are running this command in the root directory of the Go project.'));
|
|
24
24
|
process.exit(1);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
//
|
|
28
|
+
// Create module main function
|
|
29
29
|
const createModule = async (args) => {
|
|
30
30
|
if (args.length < 1) {
|
|
31
|
-
console.error(chalk.yellow('
|
|
31
|
+
console.error(chalk.yellow('Please use the correct command format: npx <your package name> create <module name>'));
|
|
32
32
|
process.exit(1);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const moduleName = args[0];
|
|
36
36
|
const ModuleName = moduleName.charAt(0).toUpperCase() + moduleName.slice(1);
|
|
37
37
|
|
|
38
|
-
//
|
|
38
|
+
// Get the current working directory, which is the directory where the command is executed
|
|
39
39
|
const currentDir = process.cwd();
|
|
40
40
|
|
|
41
|
-
//
|
|
41
|
+
// Build the paths for the domain directory and module directory
|
|
42
42
|
const domainDir = path.join(currentDir, 'domain');
|
|
43
43
|
const moduleDir = path.join(domainDir, moduleName);
|
|
44
44
|
|
|
45
|
-
//
|
|
45
|
+
// Define the subdirectories to be created
|
|
46
46
|
const subDirs = ['api', 'model', 'internal', 'api/req'];
|
|
47
47
|
|
|
48
|
-
console.log(chalk.blue(`\
|
|
48
|
+
console.log(chalk.blue(`\nStarting to create module: ${chalk.bold(moduleName)}`));
|
|
49
49
|
|
|
50
50
|
try {
|
|
51
51
|
const projectName = await getGoModModuleName();
|
|
@@ -53,106 +53,107 @@ const createModule = async (args) => {
|
|
|
53
53
|
await fs.ensureDir(domainDir);
|
|
54
54
|
await fs.ensureDir(moduleDir);
|
|
55
55
|
|
|
56
|
-
//
|
|
56
|
+
// Create subdirectories
|
|
57
57
|
const createSubDirs = subDirs.map((subDir) => {
|
|
58
58
|
const subDirPath = path.join(moduleDir, subDir);
|
|
59
59
|
return fs.ensureDir(subDirPath);
|
|
60
60
|
});
|
|
61
61
|
await Promise.all(createSubDirs);
|
|
62
62
|
|
|
63
|
-
//
|
|
63
|
+
// Create and write model.go file
|
|
64
64
|
const modelDir = path.join(moduleDir, 'model');
|
|
65
65
|
const modelFilePath = path.join(modelDir, `${moduleName}.go`);
|
|
66
66
|
const modelTemplatePath = path.join(__dirname, '../templates/model.go.ejs');
|
|
67
67
|
const modelContent = await ejs.renderFile(modelTemplatePath, { moduleName, ModuleName, projectName });
|
|
68
68
|
await fs.writeFile(modelFilePath, modelContent);
|
|
69
|
-
console.log(chalk.green(
|
|
69
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('model.go')} file`));
|
|
70
70
|
|
|
71
|
-
//
|
|
71
|
+
// Create and write req.go file
|
|
72
72
|
const apiDir = path.join(moduleDir, 'api');
|
|
73
73
|
const reqDir = path.join(apiDir, 'req');
|
|
74
74
|
const reqFilePath = path.join(reqDir, `req.go`);
|
|
75
75
|
const reqTemplatePath = path.join(__dirname, '../templates/req.go.ejs');
|
|
76
76
|
const reqContent = await ejs.renderFile(reqTemplatePath, { moduleName, ModuleName, projectName });
|
|
77
77
|
await fs.writeFile(reqFilePath, reqContent);
|
|
78
|
-
console.log(chalk.green(
|
|
78
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('req.go')} file`));
|
|
79
79
|
|
|
80
|
-
//
|
|
80
|
+
// Create and write resp.go file
|
|
81
81
|
const respFilePath = path.join(reqDir, `resp.go`);
|
|
82
82
|
const respTemplatePath = path.join(__dirname, '../templates/resp.go.ejs');
|
|
83
83
|
const respContent = await ejs.renderFile(respTemplatePath, { moduleName, ModuleName, projectName });
|
|
84
84
|
await fs.writeFile(respFilePath, respContent);
|
|
85
|
-
console.log(chalk.green(
|
|
85
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('resp.go')} file`));
|
|
86
86
|
|
|
87
|
-
//
|
|
87
|
+
// Create and write internal.go file
|
|
88
88
|
const internalDir = path.join(moduleDir, 'internal');
|
|
89
89
|
const internalFilePath = path.join(internalDir, `${moduleName}.go`);
|
|
90
90
|
const internalTemplatePath = path.join(__dirname, '../templates/internal.go.ejs');
|
|
91
91
|
const internalContent = await ejs.renderFile(internalTemplatePath, { moduleName, ModuleName, projectName });
|
|
92
92
|
await fs.writeFile(internalFilePath, internalContent);
|
|
93
|
-
console.log(chalk.green(
|
|
93
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('internal.go')} file`));
|
|
94
94
|
|
|
95
|
-
//
|
|
95
|
+
// Create and write service.pub.go file
|
|
96
96
|
const serviceFilePath = path.join(apiDir, `service.pub.go`);
|
|
97
97
|
const serviceTemplatePath = path.join(__dirname, '../templates/service.pub.go.ejs');
|
|
98
98
|
const serviceContent = await ejs.renderFile(serviceTemplatePath, { moduleName, ModuleName, projectName });
|
|
99
99
|
await fs.writeFile(serviceFilePath, serviceContent);
|
|
100
|
-
console.log(chalk.green(
|
|
100
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('service.pub.go')} file`));
|
|
101
101
|
|
|
102
|
-
//
|
|
102
|
+
// Create and write service.go file
|
|
103
103
|
const serviceGoFilePath = path.join(apiDir, `service.go`);
|
|
104
104
|
const serviceGoTemplatePath = path.join(__dirname, '../templates/service.go.ejs');
|
|
105
105
|
const serviceGoContent = await ejs.renderFile(serviceGoTemplatePath, { moduleName, ModuleName, projectName });
|
|
106
106
|
await fs.writeFile(serviceGoFilePath, serviceGoContent);
|
|
107
|
-
console.log(chalk.green(
|
|
107
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('service.go')} file`));
|
|
108
108
|
|
|
109
|
-
//
|
|
109
|
+
// Create and write controller.go file
|
|
110
110
|
const controllerGoFilePath = path.join(apiDir, `controller.go`);
|
|
111
111
|
const controllerGoTemplatePath = path.join(__dirname, '../templates/controller.go.ejs');
|
|
112
112
|
const controllerGoContent = await ejs.renderFile(controllerGoTemplatePath, { moduleName, ModuleName, projectName });
|
|
113
113
|
await fs.writeFile(controllerGoFilePath, controllerGoContent);
|
|
114
|
-
console.log(chalk.green(
|
|
114
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('controller.go')} file`));
|
|
115
115
|
|
|
116
|
-
//
|
|
116
|
+
// Create and write router.go file
|
|
117
117
|
const routerGoFilePath = path.join(apiDir, `router.go`);
|
|
118
118
|
const routerGoTemplatePath = path.join(__dirname, '../templates/router.go.ejs');
|
|
119
119
|
const routerGoContent = await ejs.renderFile(routerGoTemplatePath, { moduleName, ModuleName, projectName });
|
|
120
120
|
await fs.writeFile(routerGoFilePath, routerGoContent);
|
|
121
|
-
console.log(chalk.green(
|
|
121
|
+
console.log(chalk.green(`Successfully created ${chalk.bold('router.go')} file`));
|
|
122
122
|
|
|
123
|
-
//
|
|
123
|
+
// Check and update domain/init.go file
|
|
124
124
|
const initFilePath = path.join(domainDir, 'init.go');
|
|
125
125
|
if (await fs.pathExists(initFilePath)) {
|
|
126
126
|
let initFileContent = await fs.readFile(initFilePath, 'utf-8');
|
|
127
|
-
|
|
127
|
+
|
|
128
128
|
const importStatement = `import _ "${projectName}/domain/${moduleName}/api"`;
|
|
129
129
|
|
|
130
130
|
if (!initFileContent.includes(importStatement)) {
|
|
131
131
|
const initFuncIndex = initFileContent.indexOf('func init()');
|
|
132
|
-
|
|
132
|
+
|
|
133
133
|
if (initFuncIndex !== -1) {
|
|
134
|
-
//
|
|
134
|
+
// Find init function, ensure import statement is added above init function
|
|
135
135
|
const insertPosition = initFuncIndex;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
`${
|
|
136
|
+
const headCode = initFileContent.slice(0, insertPosition).trimEnd();
|
|
137
|
+
initFileContent =
|
|
138
|
+
`${headCode}\n` +
|
|
139
|
+
`${importStatement}\n\n` +
|
|
139
140
|
initFileContent.slice(insertPosition);
|
|
140
141
|
|
|
141
142
|
await fs.writeFile(initFilePath, initFileContent);
|
|
142
|
-
console.log(chalk.green(
|
|
143
|
+
console.log(chalk.green(`Successfully updated ${chalk.bold('init.go')} file, added import statement`));
|
|
143
144
|
} else {
|
|
144
|
-
console.log(chalk.yellow(
|
|
145
|
+
console.log(chalk.yellow(`init() function not found, skipping import statement addition`));
|
|
145
146
|
}
|
|
146
147
|
} else {
|
|
147
|
-
console.log(chalk.yellow(`init.go
|
|
148
|
+
console.log(chalk.yellow(`The corresponding import statement already exists in init.go, no need to add it again`));
|
|
148
149
|
}
|
|
149
150
|
} else {
|
|
150
|
-
console.log(chalk.yellow(
|
|
151
|
+
console.log(chalk.yellow(`Could not find ${chalk.bold('init.go')} file, skipping import statement addition`));
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
console.log(chalk.blue(`\
|
|
154
|
+
console.log(chalk.blue(`\nModule ${chalk.bold(moduleName)} has been successfully created in ${chalk.bold(`domain/${moduleName}`)} directory`));
|
|
154
155
|
} catch (error) {
|
|
155
|
-
console.error(chalk.red('
|
|
156
|
+
console.error(chalk.red('Error creating module:'), chalk.redBright(error.message));
|
|
156
157
|
}
|
|
157
158
|
};
|
|
158
159
|
|