gorig-cli 1.0.8 → 1.0.10
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 +22 -0
- package/commands/init.js +85 -3
- package/package.json +1 -1
- package/templates/init.go.ejs +6 -0
package/README.md
CHANGED
|
@@ -50,6 +50,28 @@ npx gorig-cli@latest create user
|
|
|
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
|
+
### Generate API Documentation
|
|
54
|
+
|
|
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
|
+
|
|
53
75
|
### Run the Project
|
|
54
76
|
|
|
55
77
|
After entering the project directory, you can run the project using the following commands:
|
package/commands/init.js
CHANGED
|
@@ -2,7 +2,7 @@ import fs from 'fs-extra';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import ejs from 'ejs';
|
|
5
|
-
import { exec } from 'child_process';
|
|
5
|
+
import { exec, spawn } from 'child_process';
|
|
6
6
|
import { fileURLToPath } from 'url';
|
|
7
7
|
import readline from 'readline';
|
|
8
8
|
|
|
@@ -70,6 +70,32 @@ const askOverwriteConfirmation = (projectDir) => {
|
|
|
70
70
|
});
|
|
71
71
|
};
|
|
72
72
|
|
|
73
|
+
// 询问用户是否启动项目,超时5秒
|
|
74
|
+
const askStartConfirmation = () => {
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
const rl = readline.createInterface({
|
|
77
|
+
input: process.stdin,
|
|
78
|
+
output: process.stdout
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 设置5秒超时
|
|
82
|
+
const timeout = setTimeout(() => {
|
|
83
|
+
rl.close();
|
|
84
|
+
resolve(true);
|
|
85
|
+
}, 6000);
|
|
86
|
+
|
|
87
|
+
rl.question('\nDo you want to start the project now? (y/N): ', (answer) => {
|
|
88
|
+
clearTimeout(timeout);
|
|
89
|
+
rl.close();
|
|
90
|
+
if (answer.toLowerCase() === 'y') {
|
|
91
|
+
resolve(true);
|
|
92
|
+
} else {
|
|
93
|
+
resolve(false);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
73
99
|
// 初始化项目的主函数
|
|
74
100
|
const initModule = async (args) => {
|
|
75
101
|
if (args.length < 1) {
|
|
@@ -128,7 +154,7 @@ const initModule = async (args) => {
|
|
|
128
154
|
// 创建 _bin 目录下的配置文件
|
|
129
155
|
const binDir = path.join(projectDir, '_bin');
|
|
130
156
|
const configTemplatePath = path.join(__dirname, '../templates/config.yaml.ejs');
|
|
131
|
-
|
|
157
|
+
|
|
132
158
|
const environments = ['dev', 'local', 'prod'];
|
|
133
159
|
for (const env of environments) {
|
|
134
160
|
const configFilePath = path.join(binDir, `${env}.yaml`);
|
|
@@ -178,7 +204,7 @@ const initModule = async (args) => {
|
|
|
178
204
|
await runGoModTidy(projectDir);
|
|
179
205
|
console.log(chalk.green('Successfully organized Go module dependencies'));
|
|
180
206
|
|
|
181
|
-
//
|
|
207
|
+
// 提示用户项目创建成功
|
|
182
208
|
console.log(chalk.blue(`\nProject ${chalk.bold(projectName)} has been successfully created in ${chalk.bold(projectDir)} directory`));
|
|
183
209
|
console.log(chalk.yellow('\nHow to run the project:'));
|
|
184
210
|
console.log(chalk.green(`1. Enter the project directory: cd ${projectName}`));
|
|
@@ -186,6 +212,62 @@ const initModule = async (args) => {
|
|
|
186
212
|
console.log(chalk.cyan(` go run _cmd/main.go`));
|
|
187
213
|
console.log(chalk.green(`\nOr compile and run directly:`));
|
|
188
214
|
console.log(chalk.cyan(` go build -o ${projectName} _cmd/main.go && ./${projectName}`));
|
|
215
|
+
|
|
216
|
+
// 询问用户是否启动项目
|
|
217
|
+
const shouldStart = await askStartConfirmation();
|
|
218
|
+
if (shouldStart) {
|
|
219
|
+
// 启动项目
|
|
220
|
+
console.log(chalk.blue('\nStarting the project...'));
|
|
221
|
+
try {
|
|
222
|
+
const goProcess = spawn('go', ['run', '_cmd/main.go'], {
|
|
223
|
+
cwd: projectDir,
|
|
224
|
+
stdio: ['inherit', 'pipe', 'pipe'] // 继承 stdin 和 stdout,pipe stderr
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
goProcess.on('error', (err) => {
|
|
228
|
+
console.error(chalk.red('Error starting project:'), chalk.redBright(err.message));
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
goProcess.on('close', (code) => {
|
|
232
|
+
if (code !== 0) {
|
|
233
|
+
console.error(chalk.red(`Project process exited with code ${code}`));
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// 检查 goProcess.stdout 是否存在
|
|
238
|
+
if (goProcess.stdout) {
|
|
239
|
+
goProcess.stdout.on('data', (data) => { // 监听 stdout
|
|
240
|
+
const output = data.toString();
|
|
241
|
+
process.stdout.write(output); // 将输出写入控制台
|
|
242
|
+
if (output.includes('System startup successful')) {
|
|
243
|
+
console.log(chalk.green(`\nVisit ${chalk.bold('http://localhost:9527')} to view the project`));
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
} else {
|
|
247
|
+
console.error(chalk.red('stdout is not available.'));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// 检查 goProcess.stderr 是否存在
|
|
251
|
+
// if (goProcess.stderr) {
|
|
252
|
+
// goProcess.stderr.on('data', (data) => { // 监听 stderr
|
|
253
|
+
// const errorOutput = data.toString();
|
|
254
|
+
// if (errorOutput.includes('System startup successful')) {
|
|
255
|
+
// console.log(chalk.green(`\nVisit ${chalk.bold('http://localhost:9527')} to view the project`));
|
|
256
|
+
// } else {
|
|
257
|
+
// console.error(chalk.red(`Error: ${errorOutput}`));
|
|
258
|
+
// }
|
|
259
|
+
// });
|
|
260
|
+
// } else {
|
|
261
|
+
// console.error(chalk.red('stderr is not available.'));
|
|
262
|
+
// }
|
|
263
|
+
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.error(chalk.red('Error starting project:'), chalk.redBright(error.message));
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
console.log(chalk.blue('\nProject initialization completed without starting the project.'));
|
|
269
|
+
}
|
|
270
|
+
|
|
189
271
|
} catch (error) {
|
|
190
272
|
console.error(chalk.red('Error during project initialization:'), chalk.redBright(error.message));
|
|
191
273
|
}
|
package/package.json
CHANGED
package/templates/init.go.ejs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package domain
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"github.com/gin-gonic/gin"
|
|
4
5
|
"github.com/jom-io/gorig/httpx"
|
|
5
6
|
"github.com/jom-io/gorig/serv"
|
|
6
7
|
configure "github.com/jom-io/gorig/utils/cofigure"
|
|
@@ -8,6 +9,11 @@ import (
|
|
|
8
9
|
)
|
|
9
10
|
|
|
10
11
|
func init() {
|
|
12
|
+
httpx.RegisterRouter(func(groupRouter *gin.RouterGroup) {
|
|
13
|
+
groupRouter.GET("/", func(ctx *gin.Context) {
|
|
14
|
+
httpx.Success(ctx, "hello world, this is <%= projectNameUpper %> project generated by gorig-cli")
|
|
15
|
+
})
|
|
16
|
+
})
|
|
11
17
|
err := serv.RegisterService(
|
|
12
18
|
serv.Service{
|
|
13
19
|
Code: "<%= projectNameUpper %>_HTTP",
|