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 CHANGED
@@ -1,65 +1,87 @@
1
1
  # Gorig CLI
2
2
 
3
- Gorig CLI 是一个基于 Node.js 的脚手架工具,用于快速创建基于 Gorig 框架的 Go 项目结构和模块。
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
- 使用 npm 全局安装:
7
+ Install globally using npm:
8
8
 
9
9
  ```sh
10
10
  npm install -g gorig-cli
11
11
  ```
12
12
 
13
- 或者使用 npx 直接运行:
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
- 使用 `init` 命令创建一个新项目:
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
- 或者使用 npx
29
+ Or use npx:
30
30
 
31
31
  ```sh
32
32
  npx gorig-cli@latest init my-new-project
33
33
  ```
34
34
 
35
- 这将在当前目录下创建一个新项目,包含 `_cmd/main.go`、`domain/init.go`、`cron/cron.go` 等基本文件和目录。
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
- 在项目根目录下使用 `create` 命令创建一个新模块:
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
- 或者使用 npx
45
+ Or use npx:
46
46
 
47
47
  ```sh
48
48
  npx gorig-cli@latest create user
49
49
  ```
50
50
 
51
- 这将在项目中创建一个名为 `user` 的模块,包含 `api/`、`internal/`、`model/` 等文件夹和必要的代码。
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}`));
@@ -4,11 +4,11 @@ import ejs from 'ejs';
4
4
  import chalk from 'chalk';
5
5
  import { fileURLToPath } from 'url';
6
6
 
7
- // 定义 __dirname 变量
7
+ // Define __dirname variable
8
8
  const __filename = fileURLToPath(import.meta.url);
9
9
  const __dirname = path.dirname(__filename);
10
10
 
11
- // 获取 go.mod 文件中的模块名称
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('无法读取 go.mod 文件。请确认您是否在 Go 项目的根目录下运行该命令。'));
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('请使用正确的命令格式: npx <你的包名> create <模块名>'));
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
- // 构建 domain 目录和模块目录的路径
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(`\n开始创建模块: ${chalk.bold(moduleName)}`));
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
- // 创建并写入 model.go 文件
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(`成功创建 ${chalk.bold('model.go')} 文件`));
69
+ console.log(chalk.green(`Successfully created ${chalk.bold('model.go')} file`));
70
70
 
71
- // 创建并写入 req.go 文件
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(`成功创建 ${chalk.bold('req.go')} 文件`));
78
+ console.log(chalk.green(`Successfully created ${chalk.bold('req.go')} file`));
79
79
 
80
- // 创建并写入 resp.go 文件
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(`成功创建 ${chalk.bold('resp.go')} 文件`));
85
+ console.log(chalk.green(`Successfully created ${chalk.bold('resp.go')} file`));
86
86
 
87
- // 创建并写入 internal.go 文件
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(`成功创建 ${chalk.bold('internal.go')} 文件`));
93
+ console.log(chalk.green(`Successfully created ${chalk.bold('internal.go')} file`));
94
94
 
95
- // 创建并写入 service.pub.go 文件
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(`成功创建 ${chalk.bold('service.pub.go')} 文件`));
100
+ console.log(chalk.green(`Successfully created ${chalk.bold('service.pub.go')} file`));
101
101
 
102
- // 创建并写入 service.go 文件
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(`成功创建 ${chalk.bold('service.go')} 文件`));
107
+ console.log(chalk.green(`Successfully created ${chalk.bold('service.go')} file`));
108
108
 
109
- // 创建并写入 controller.go 文件
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(`成功创建 ${chalk.bold('controller.go')} 文件`));
114
+ console.log(chalk.green(`Successfully created ${chalk.bold('controller.go')} file`));
115
115
 
116
- // 创建并写入 router.go 文件
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(`成功创建 ${chalk.bold('router.go')} 文件`));
121
+ console.log(chalk.green(`Successfully created ${chalk.bold('router.go')} file`));
122
122
 
123
- // 检查并更新 domain/init.go 文件
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
- // 找到 init 函数,确保在 init 函数上方添加 import 语句
134
+ // Find init function, ensure import statement is added above init function
135
135
  const insertPosition = initFuncIndex;
136
- initFileContent =
137
- initFileContent.slice(0, insertPosition) +
138
- `${importStatement}\n\n` +
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(`成功更新 ${chalk.bold('init.go')} 文件,添加 import 语句`));
143
+ console.log(chalk.green(`Successfully updated ${chalk.bold('init.go')} file, added import statement`));
143
144
  } else {
144
- console.log(chalk.yellow(`未找到 init() 函数,跳过 import 语句添加`));
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 文件中已存在相应的 import 语句,无需重复添加`));
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(`未找到 ${chalk.bold('init.go')} 文件,跳过 import 语句添加`));
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(`\n模块 ${chalk.bold(moduleName)} 已成功创建在 ${chalk.bold(`domain/${moduleName}`)} 目录下`));
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('创建模块时出错:'), chalk.redBright(error.message));
156
+ console.error(chalk.red('Error creating module:'), chalk.redBright(error.message));
156
157
  }
157
158
  };
158
159