@gogenger/go-gen 1.0.0
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/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.en.md +151 -0
- package/README.md +151 -0
- package/bin/index.js +86 -0
- package/core/config.js +176 -0
- package/core/fetch-mode.js +400 -0
- package/core/openapi-mode.js +328 -0
- package/core/quicktype.js +31 -0
- package/core/writer.js +413 -0
- package/docs/BEST_PRACTICES.md +327 -0
- package/docs/CONFIGURATION.md +161 -0
- package/docs/FEATURES.md +136 -0
- package/docs/TROUBLESHOOTING.md +386 -0
- package/docs/USE_CASES.md +247 -0
- package/package.json +68 -0
- package/utils/load-openapi.js +23 -0
- package/utils/name.js +17 -0
- package/utils/sampler.js +28 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 更新日志 / Changelog
|
|
2
|
+
|
|
3
|
+
本文档记录项目的所有重要变更。
|
|
4
|
+
|
|
5
|
+
All notable changes to this project will be documented in this file.
|
|
6
|
+
|
|
7
|
+
格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.0.0/),
|
|
8
|
+
项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
9
|
+
|
|
10
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
11
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
12
|
+
|
|
13
|
+
## [未发布 / Unreleased]
|
|
14
|
+
|
|
15
|
+
### 新增 / Added
|
|
16
|
+
|
|
17
|
+
- 初始文档结构 / Initial documentation structure
|
|
18
|
+
- 贡献指南 / Contributing guidelines
|
|
19
|
+
- 最佳实践指南 / Best practices guide
|
|
20
|
+
|
|
21
|
+
## [1.0.0] - 2025-12-30
|
|
22
|
+
|
|
23
|
+
### 新增 / Added
|
|
24
|
+
|
|
25
|
+
- 首次发布 / Initial release
|
|
26
|
+
- 🎯 Fetch 模式,支持直接请求 API / Fetch mode for direct API requests
|
|
27
|
+
- 📄 OpenAPI 模式,支持批量生成 / OpenAPI mode for batch generation
|
|
28
|
+
- 🔄 增量写入,智能合并文件 / Incremental write with smart merge
|
|
29
|
+
- 🔀 自动冲突处理 / Automatic conflict resolution
|
|
30
|
+
- 🌐 支持多种 HTTP 方法(GET、POST、PUT、DELETE、PATCH)/ Support for multiple HTTP methods
|
|
31
|
+
- 🔐 认证支持(Bearer Token、Cookie)/ Authentication support
|
|
32
|
+
- 🔁 自动重试机制 / Automatic retry mechanism
|
|
33
|
+
- ⚙️ 双层配置系统(全局 + 项目)/ Dual-layer configuration system
|
|
34
|
+
- ⚡ 批量生成模式 / Batch generation mode
|
|
35
|
+
- 🎨 类型安全的代码生成 / Type-safe code generation
|
|
36
|
+
|
|
37
|
+
[未发布 / Unreleased]: https://github.com/goGenger/go-gen/compare/v1.0.0...HEAD
|
|
38
|
+
[1.0.0]: https://github.com/goGenger/go-gen/compare/v0.9.0...v1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 goGenger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# 🚀 go-gen
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/go-gen)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
|
|
7
|
+
A TypeScript API code generator that supports generating TypeScript interface code and type definitions from API responses or OpenAPI documents with one click.
|
|
8
|
+
|
|
9
|
+
English | [简体中文](./README.md)
|
|
10
|
+
|
|
11
|
+
## ✨ Features
|
|
12
|
+
|
|
13
|
+
- 🎯 **Fetch Mode** - Request API directly and auto-generate type definitions
|
|
14
|
+
- 📄 **OpenAPI Mode** - Batch generate from Swagger/OpenAPI documents
|
|
15
|
+
- 🔄 **Incremental Write** - Smart merge with existing files, no overwrite
|
|
16
|
+
- 🔀 **Conflict Resolution** - Auto-detect and rename duplicate types
|
|
17
|
+
- 🌐 **Multiple HTTP Methods** - Support GET, POST, PUT, DELETE, PATCH
|
|
18
|
+
- 🔐 **Authentication Support** - Bearer Token, Cookie, and more
|
|
19
|
+
- ⚙️ **Dual Configuration** - Global config + Project config for flexibility
|
|
20
|
+
|
|
21
|
+
## 📦 Installation
|
|
22
|
+
|
|
23
|
+
### Global Installation (Recommended)
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install -g @gogenger/go-gen
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Project Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install --save-dev @gogenger/go-gen
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## 🎯 Quick Start
|
|
36
|
+
|
|
37
|
+
### Fetch Mode - Request API Directly
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
go-gen fetch
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Follow the interactive prompts to input API information and generate code:
|
|
44
|
+
|
|
45
|
+
**Generated types.ts**
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
export interface UserResponse {
|
|
49
|
+
id: number;
|
|
50
|
+
name: string;
|
|
51
|
+
email: string;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Generated api.ts**
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import request from '@/utils/request';
|
|
59
|
+
import type { UserResponse } from './types';
|
|
60
|
+
|
|
61
|
+
export function getUsers() {
|
|
62
|
+
return request.get<UserResponse>('https://api.example.com/users');
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### OpenAPI Mode - Generate from Documents
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# Local file
|
|
70
|
+
go-gen openapi ./swagger.json
|
|
71
|
+
|
|
72
|
+
# Remote URL
|
|
73
|
+
go-gen openapi https://api.example.com/swagger.json
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Supports both batch generation and individual generation modes.
|
|
77
|
+
|
|
78
|
+
## 📚 Documentation
|
|
79
|
+
|
|
80
|
+
- [Features Documentation](./docs/FEATURES.md) - Detailed feature descriptions
|
|
81
|
+
- [Configuration Guide](./docs/CONFIGURATION.md) - Configuration system explained
|
|
82
|
+
- [Use Cases](./docs/USE_CASES.md) - Real-world application examples
|
|
83
|
+
- [Best Practices](./docs/BEST_PRACTICES.md) - Team collaboration recommendations
|
|
84
|
+
- [Troubleshooting](./docs/TROUBLESHOOTING.md) - Common issues and solutions
|
|
85
|
+
|
|
86
|
+
## 📝 Main Commands
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# Fetch mode
|
|
90
|
+
go-gen fetch
|
|
91
|
+
|
|
92
|
+
# OpenAPI mode
|
|
93
|
+
go-gen openapi <source>
|
|
94
|
+
|
|
95
|
+
# Initialize project config
|
|
96
|
+
go-gen init
|
|
97
|
+
|
|
98
|
+
# Configuration management
|
|
99
|
+
go-gen config --show # View config
|
|
100
|
+
go-gen config --global # Set global config
|
|
101
|
+
|
|
102
|
+
# View help
|
|
103
|
+
go-gen --help
|
|
104
|
+
go-gen --version
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## ⚙️ Quick Configuration
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# Initialize project configuration
|
|
111
|
+
go-gen init
|
|
112
|
+
|
|
113
|
+
# Set global configuration
|
|
114
|
+
go-gen config --global
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Configuration priority: Project config > Global config > Default config
|
|
118
|
+
|
|
119
|
+
See [Configuration Guide](./docs/CONFIGURATION.md) for details.
|
|
120
|
+
|
|
121
|
+
## 🤝 Contributing
|
|
122
|
+
|
|
123
|
+
Issues and Pull Requests are welcome!
|
|
124
|
+
|
|
125
|
+
Check out [Contributing Guide](./CONTRIBUTING.md) to learn how to participate in development.
|
|
126
|
+
|
|
127
|
+
## 🧪 Testing
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
# Run all tests
|
|
131
|
+
npm test
|
|
132
|
+
|
|
133
|
+
# View coverage
|
|
134
|
+
npm test -- --coverage
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 📄 License
|
|
138
|
+
|
|
139
|
+
[MIT](./LICENSE)
|
|
140
|
+
|
|
141
|
+
## 📮 Contact
|
|
142
|
+
|
|
143
|
+
- GitHub: [@goGenger](https://github.com/goGenger)
|
|
144
|
+
- Email: bg2582266166@gmail.com
|
|
145
|
+
- Issues: [GitHub Issues](https://github.com/goGenger/go-gen/issues)
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
**Made with ❤️ by goGenger**
|
|
150
|
+
|
|
151
|
+
If this project helps you, please give it a ⭐️ Star!
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# 🚀 go-gen
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/go-gen)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
|
|
7
|
+
一款 TypeScript API 代码生成器,支持从 API 响应或 OpenAPI 文档一键生成 TypeScript 接口代码和类型定义。
|
|
8
|
+
|
|
9
|
+
[English](./README.en.md) | 简体中文
|
|
10
|
+
|
|
11
|
+
## ✨ 特性
|
|
12
|
+
|
|
13
|
+
- 🎯 **Fetch 模式** - 直接请求 API,自动生成类型定义
|
|
14
|
+
- 📄 **OpenAPI 模式** - 从 Swagger/OpenAPI 文档批量生成
|
|
15
|
+
- 🔄 **增量写入** - 智能合并已存在的文件,避免覆盖
|
|
16
|
+
- 🔀 **冲突处理** - 自动检测并重命名重复的类型
|
|
17
|
+
- 🌐 **多种 HTTP 方法** - 支持 GET、POST、PUT、DELETE、PATCH
|
|
18
|
+
- 🔐 **认证支持** - Bearer Token、Cookie 等多种认证方式
|
|
19
|
+
- ⚙️ **双层配置** - 全局配置 + 项目配置,灵活适配多项目
|
|
20
|
+
|
|
21
|
+
## 📦 安装
|
|
22
|
+
|
|
23
|
+
### 全局安装(推荐)
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install -g @gogenger/go-gen
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 项目内安装
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install --save-dev @gogenger/go-gen
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## 🎯 快速开始
|
|
36
|
+
|
|
37
|
+
### Fetch 模式 - 直接请求 API
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
go-gen fetch
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
跟随交互式提示输入 API 信息,即可生成代码:
|
|
44
|
+
|
|
45
|
+
**生成的 types.ts**
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
export interface UserResponse {
|
|
49
|
+
id: number;
|
|
50
|
+
name: string;
|
|
51
|
+
email: string;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**生成的 api.ts**
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import request from '@/utils/request';
|
|
59
|
+
import type { UserResponse } from './types';
|
|
60
|
+
|
|
61
|
+
export function getUsers() {
|
|
62
|
+
return request.get<UserResponse>('https://api.example.com/users');
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### OpenAPI 模式 - 从文档生成
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# 本地文件
|
|
70
|
+
go-gen openapi ./swagger.json
|
|
71
|
+
|
|
72
|
+
# 远程 URL
|
|
73
|
+
go-gen openapi https://api.example.com/swagger.json
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
支持批量生成和逐个生成两种模式。
|
|
77
|
+
|
|
78
|
+
## 📚 文档
|
|
79
|
+
|
|
80
|
+
- [完整特性文档](./docs/FEATURES.md) - 详细功能说明
|
|
81
|
+
- [配置指南](./docs/CONFIGURATION.md) - 配置系统详解
|
|
82
|
+
- [使用场景](./docs/USE_CASES.md) - 实际应用案例
|
|
83
|
+
- [最佳实践](./docs/BEST_PRACTICES.md) - 团队协作建议
|
|
84
|
+
- [故障排查](./docs/TROUBLESHOOTING.md) - 常见问题解决
|
|
85
|
+
|
|
86
|
+
## 📝 主要命令
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# Fetch 模式
|
|
90
|
+
go-gen fetch
|
|
91
|
+
|
|
92
|
+
# OpenAPI 模式
|
|
93
|
+
go-gen openapi <source>
|
|
94
|
+
|
|
95
|
+
# 初始化项目配置
|
|
96
|
+
go-gen init
|
|
97
|
+
|
|
98
|
+
# 配置管理
|
|
99
|
+
go-gen config --show # 查看配置
|
|
100
|
+
go-gen config --global # 设置全局配置
|
|
101
|
+
|
|
102
|
+
# 查看帮助
|
|
103
|
+
go-gen --help
|
|
104
|
+
go-gen --version
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## ⚙️ 快速配置
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# 初始化项目配置
|
|
111
|
+
go-gen init
|
|
112
|
+
|
|
113
|
+
# 设置全局配置
|
|
114
|
+
go-gen config --global
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
配置优先级:项目配置 > 全局配置 > 默认配置
|
|
118
|
+
|
|
119
|
+
详见 [配置指南](./docs/CONFIGURATION.md)
|
|
120
|
+
|
|
121
|
+
## 🤝 贡献
|
|
122
|
+
|
|
123
|
+
欢迎提交 Issue 和 Pull Request!
|
|
124
|
+
|
|
125
|
+
查看 [贡献指南](./CONTRIBUTING.md) 了解如何参与项目开发。
|
|
126
|
+
|
|
127
|
+
## 🧪 测试
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
# 运行所有测试
|
|
131
|
+
npm test
|
|
132
|
+
|
|
133
|
+
# 查看覆盖率
|
|
134
|
+
npm test -- --coverage
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 📄 License
|
|
138
|
+
|
|
139
|
+
[MIT](./LICENSE)
|
|
140
|
+
|
|
141
|
+
## 📮 联系方式
|
|
142
|
+
|
|
143
|
+
- GitHub: [@goGenger](https://github.com/goGenger)
|
|
144
|
+
- Email: bg2582266166@gmail.com
|
|
145
|
+
- Issues: [GitHub Issues](https://github.com/goGenger/go-gen/issues)
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
**Made with ❤️ by goGenger**
|
|
150
|
+
|
|
151
|
+
如果这个项目对你有帮助,请给一个 ⭐️ Star!
|
package/bin/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { program } = require('commander');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
const packageJson = require('../package.json');
|
|
6
|
+
|
|
7
|
+
// 导入命令
|
|
8
|
+
const fetchMode = require('../core/fetch-mode');
|
|
9
|
+
const openapiMode = require('../core/openapi-mode');
|
|
10
|
+
const { initLocalConfig, showConfig, configGlobal } = require('../core/config');
|
|
11
|
+
|
|
12
|
+
// 设置程序信息
|
|
13
|
+
program
|
|
14
|
+
.name('go-gen')
|
|
15
|
+
.version(packageJson.version)
|
|
16
|
+
.description('🚀 API 代码生成器');
|
|
17
|
+
|
|
18
|
+
// fetch 命令
|
|
19
|
+
program
|
|
20
|
+
.command('fetch')
|
|
21
|
+
.description('📡 Fetch 模式')
|
|
22
|
+
.action(async () => {
|
|
23
|
+
try {
|
|
24
|
+
console.log(chalk.cyan('🚀 Fetch 模式启动\n'));
|
|
25
|
+
await fetchMode();
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(chalk.red('错误:', error.message));
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// openapi 命令
|
|
33
|
+
program
|
|
34
|
+
.command('openapi <source>')
|
|
35
|
+
.description('📄 OpenAPI 模式')
|
|
36
|
+
.action(async source => {
|
|
37
|
+
try {
|
|
38
|
+
console.log(chalk.cyan('🚀 OpenAPI 模式启动\n'));
|
|
39
|
+
await openapiMode(source);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error(chalk.red('错误:', error.message));
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// init 命令
|
|
47
|
+
program
|
|
48
|
+
.command('init')
|
|
49
|
+
.description('⚙️ 初始化项目配置')
|
|
50
|
+
.action(async () => {
|
|
51
|
+
try {
|
|
52
|
+
await initLocalConfig();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error(chalk.red('错误:', error.message));
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// config 命令
|
|
60
|
+
program
|
|
61
|
+
.command('config')
|
|
62
|
+
.description('🔧 配置管理')
|
|
63
|
+
.option('-s, --show', '显示配置')
|
|
64
|
+
.option('-g, --global', '全局配置')
|
|
65
|
+
.action(async options => {
|
|
66
|
+
try {
|
|
67
|
+
if (options.show) {
|
|
68
|
+
showConfig();
|
|
69
|
+
} else if (options.global) {
|
|
70
|
+
await configGlobal();
|
|
71
|
+
} else {
|
|
72
|
+
showConfig();
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error(chalk.red('错误:', error.message));
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// 解析命令
|
|
81
|
+
program.parse(process.argv);
|
|
82
|
+
|
|
83
|
+
// 没有参数时显示帮助
|
|
84
|
+
if (process.argv.length === 2) {
|
|
85
|
+
program.outputHelp();
|
|
86
|
+
}
|
package/core/config.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const os = require('os');
|
|
4
|
+
|
|
5
|
+
const LOCAL_CONFIG_FILE = '.apirc.json';
|
|
6
|
+
const GLOBAL_CONFIG_FILE = path.join(os.homedir(), '.apirc.json');
|
|
7
|
+
|
|
8
|
+
const defaultConfig = {
|
|
9
|
+
defaultOutputPath: 'current',
|
|
10
|
+
timeout: 10000,
|
|
11
|
+
autoRetry: true,
|
|
12
|
+
maxRetries: 3,
|
|
13
|
+
requestModule: '@/utils/request',
|
|
14
|
+
typePrefix: '',
|
|
15
|
+
apiPrefix: '',
|
|
16
|
+
defaultMethod: 'GET',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function loadConfig() {
|
|
20
|
+
let config = { ...defaultConfig };
|
|
21
|
+
|
|
22
|
+
if (fs.existsSync(GLOBAL_CONFIG_FILE)) {
|
|
23
|
+
try {
|
|
24
|
+
const globalConfig = JSON.parse(
|
|
25
|
+
fs.readFileSync(GLOBAL_CONFIG_FILE, 'utf-8'),
|
|
26
|
+
);
|
|
27
|
+
config = { ...config, ...globalConfig };
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.warn('⚠️ 全局配置文件解析失败');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const localConfigPath = path.join(process.cwd(), LOCAL_CONFIG_FILE);
|
|
34
|
+
if (fs.existsSync(localConfigPath)) {
|
|
35
|
+
try {
|
|
36
|
+
const localConfig = JSON.parse(fs.readFileSync(localConfigPath, 'utf-8'));
|
|
37
|
+
config = { ...config, ...localConfig };
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.warn('⚠️ 项目配置文件解析失败');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return config;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function saveGlobalConfig(config) {
|
|
47
|
+
fs.writeFileSync(GLOBAL_CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
48
|
+
console.log(`✅ 全局配置已保存: ${GLOBAL_CONFIG_FILE}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function saveLocalConfig(config) {
|
|
52
|
+
const localConfigPath = path.join(process.cwd(), LOCAL_CONFIG_FILE);
|
|
53
|
+
fs.writeFileSync(localConfigPath, JSON.stringify(config, null, 2));
|
|
54
|
+
console.log(`✅ 项目配置已保存: ${localConfigPath}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function initLocalConfig() {
|
|
58
|
+
const prompts = require('prompts');
|
|
59
|
+
const chalk = require('chalk');
|
|
60
|
+
|
|
61
|
+
const localConfigPath = path.join(process.cwd(), LOCAL_CONFIG_FILE);
|
|
62
|
+
|
|
63
|
+
if (fs.existsSync(localConfigPath)) {
|
|
64
|
+
console.log(chalk.yellow('⚠️ 项目配置文件已存在: ' + LOCAL_CONFIG_FILE));
|
|
65
|
+
|
|
66
|
+
const response = await prompts({
|
|
67
|
+
type: 'confirm',
|
|
68
|
+
name: 'overwrite',
|
|
69
|
+
message: '是否覆盖现有配置?',
|
|
70
|
+
initial: false,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (!response.overwrite) {
|
|
74
|
+
console.log(chalk.yellow('操作已取消'));
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const projectConfig = {
|
|
80
|
+
requestModule: '@/utils/request',
|
|
81
|
+
typePrefix: '',
|
|
82
|
+
apiPrefix: '',
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
saveLocalConfig(projectConfig);
|
|
86
|
+
console.log(chalk.green('✅ 项目配置创建成功!'));
|
|
87
|
+
console.log(
|
|
88
|
+
chalk.gray('💡 提示: 可以使用 go-gen config --global 设置全局偏好'),
|
|
89
|
+
);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function showConfig() {
|
|
94
|
+
const config = loadConfig();
|
|
95
|
+
|
|
96
|
+
console.log('\n📋 当前生效的配置:\n');
|
|
97
|
+
|
|
98
|
+
const hasLocal = fs.existsSync(path.join(process.cwd(), LOCAL_CONFIG_FILE));
|
|
99
|
+
const hasGlobal = fs.existsSync(GLOBAL_CONFIG_FILE);
|
|
100
|
+
|
|
101
|
+
console.log('配置来源:');
|
|
102
|
+
console.log(` ${hasGlobal ? '✅' : '❌'} 全局配置: ${GLOBAL_CONFIG_FILE}`);
|
|
103
|
+
console.log(
|
|
104
|
+
` ${hasLocal ? '✅' : '❌'} 项目配置: ${path.join(process.cwd(), LOCAL_CONFIG_FILE)}`,
|
|
105
|
+
);
|
|
106
|
+
console.log('');
|
|
107
|
+
|
|
108
|
+
console.log('最终配置:');
|
|
109
|
+
Object.entries(config).forEach(([key, value]) => {
|
|
110
|
+
console.log(` ${key}: ${JSON.stringify(value)}`);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
console.log('\n💡 提示:');
|
|
114
|
+
console.log(' • 项目配置优先级高于全局配置');
|
|
115
|
+
console.log(' • 使用 go-gen init 创建项目配置');
|
|
116
|
+
console.log(' • 使用 go-gen config --global 设置全局偏好\n');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function configGlobal() {
|
|
120
|
+
const prompts = require('prompts');
|
|
121
|
+
const chalk = require('chalk');
|
|
122
|
+
|
|
123
|
+
console.log(chalk.cyan('⚙️ 配置全局设置\n'));
|
|
124
|
+
|
|
125
|
+
const currentConfig = loadConfig();
|
|
126
|
+
|
|
127
|
+
const response = await prompts([
|
|
128
|
+
{
|
|
129
|
+
type: 'select',
|
|
130
|
+
name: 'defaultOutputPath',
|
|
131
|
+
message: '📂 默认输出路径:',
|
|
132
|
+
choices: [
|
|
133
|
+
{ title: '📁 当前目录', value: 'current' },
|
|
134
|
+
{ title: '💻 桌面', value: 'desktop' },
|
|
135
|
+
{ title: '🔍 每次询问', value: 'ask' },
|
|
136
|
+
],
|
|
137
|
+
initial: currentConfig.defaultOutputPath === 'desktop' ? 1 : 0,
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
type: 'number',
|
|
141
|
+
name: 'timeout',
|
|
142
|
+
message: '⏱️ 请求超时时间(毫秒):',
|
|
143
|
+
initial: currentConfig.timeout,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
type: 'confirm',
|
|
147
|
+
name: 'autoRetry',
|
|
148
|
+
message: '🔄 失败时自动重试:',
|
|
149
|
+
initial: currentConfig.autoRetry,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
type: prev => (prev ? 'number' : null),
|
|
153
|
+
name: 'maxRetries',
|
|
154
|
+
message: '🔁 最大重试次数:',
|
|
155
|
+
initial: currentConfig.maxRetries,
|
|
156
|
+
},
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
if (!response.defaultOutputPath) {
|
|
160
|
+
console.log(chalk.yellow('\n✋ 操作已取消'));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
saveGlobalConfig(response);
|
|
165
|
+
console.log(chalk.green('\n✅ 全局配置已更新!\n'));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
loadConfig,
|
|
170
|
+
saveGlobalConfig,
|
|
171
|
+
saveLocalConfig,
|
|
172
|
+
initLocalConfig,
|
|
173
|
+
showConfig,
|
|
174
|
+
configGlobal,
|
|
175
|
+
defaultConfig,
|
|
176
|
+
};
|