ai-yuca 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.
Files changed (62) hide show
  1. package/.eslintrc.js +25 -0
  2. package/CONFIG_UPLOAD.md +154 -0
  3. package/CONTRIBUTING.md +58 -0
  4. package/INSTALLATION.md +192 -0
  5. package/README.md +80 -0
  6. package/bin/cli.js +85 -0
  7. package/bin/cli.ts +302 -0
  8. package/dist/bin/cli.d.ts +2 -0
  9. package/dist/bin/cli.js +297 -0
  10. package/dist/package.json +51 -0
  11. package/dist/src/config.d.ts +56 -0
  12. package/dist/src/config.js +101 -0
  13. package/dist/src/download.d.ts +30 -0
  14. package/dist/src/download.js +214 -0
  15. package/dist/src/index.d.ts +18 -0
  16. package/dist/src/index.js +126 -0
  17. package/dist/src/types/analyze.d.ts +33 -0
  18. package/dist/src/types/analyze.js +5 -0
  19. package/dist/src/types/download.d.ts +60 -0
  20. package/dist/src/types/download.js +2 -0
  21. package/dist/src/types/index.d.ts +8 -0
  22. package/dist/src/types/index.js +28 -0
  23. package/dist/src/types/upload.d.ts +89 -0
  24. package/dist/src/types/upload.js +2 -0
  25. package/dist/src/upload.d.ts +24 -0
  26. package/dist/src/upload.js +252 -0
  27. package/dist/src/uploadWithConfig.d.ts +34 -0
  28. package/dist/src/uploadWithConfig.js +82 -0
  29. package/dist/src/utils/compression.d.ts +16 -0
  30. package/dist/src/utils/compression.js +85 -0
  31. package/dist/test/compression.test.d.ts +1 -0
  32. package/dist/test/compression.test.js +109 -0
  33. package/dist/test/download.test.d.ts +1 -0
  34. package/dist/test/download.test.js +168 -0
  35. package/dist/test/index.test.d.ts +1 -0
  36. package/dist/test/index.test.js +33 -0
  37. package/dist/test/upload.test.d.ts +1 -0
  38. package/dist/test/upload.test.js +140 -0
  39. package/docs/usage.md +223 -0
  40. package/examples/sample.txt +7 -0
  41. package/examples/upload-example.js +53 -0
  42. package/out/test.txt +1 -0
  43. package/package.json +51 -0
  44. package/src/config.ts +104 -0
  45. package/src/download.ts +216 -0
  46. package/src/index.js +88 -0
  47. package/src/index.ts +98 -0
  48. package/src/types/analyze.ts +37 -0
  49. package/src/types/download.ts +67 -0
  50. package/src/types/index.ts +16 -0
  51. package/src/types/upload.ts +97 -0
  52. package/src/upload.js +197 -0
  53. package/src/upload.ts +254 -0
  54. package/src/uploadWithConfig.ts +122 -0
  55. package/src/utils/compression.ts +61 -0
  56. package/test/compression.test.ts +88 -0
  57. package/test/download.test.ts +162 -0
  58. package/test/index.test.js +38 -0
  59. package/test/index.test.ts +39 -0
  60. package/test/upload.test.ts +131 -0
  61. package/tsconfig.json +17 -0
  62. package/vs.config.json +42 -0
package/.eslintrc.js ADDED
@@ -0,0 +1,25 @@
1
+ module.exports = {
2
+ env: {
3
+ node: true,
4
+ commonjs: true,
5
+ es2021: true,
6
+ mocha: true
7
+ },
8
+ extends: [
9
+ 'eslint:recommended',
10
+ 'plugin:@typescript-eslint/recommended'
11
+ ],
12
+ parser: '@typescript-eslint/parser',
13
+ parserOptions: {
14
+ ecmaVersion: 12,
15
+ sourceType: 'module'
16
+ },
17
+ plugins: ['@typescript-eslint'],
18
+ rules: {
19
+ 'no-unused-vars': 'off',
20
+ '@typescript-eslint/no-unused-vars': 'warn',
21
+ 'no-console': 'off',
22
+ 'indent': ['error', 2],
23
+ 'semi': ['error', 'always']
24
+ }
25
+ };
@@ -0,0 +1,154 @@
1
+ # 基于配置文件的上传功能
2
+
3
+ ## 概述
4
+
5
+ 本项目新增了基于 `vs.config.json` 配置文件的批量上传功能,可以自动读取配置信息并执行上传操作,无需手动指定桶名称、源路径和目标路径。
6
+
7
+ ## 配置文件格式
8
+
9
+ 在项目根目录创建 `vs.config.json` 文件,格式如下:
10
+
11
+ ```json
12
+ {
13
+ "upload": {
14
+ "uploadPath": "out",
15
+ "s3Static": "static/aiAgent"
16
+ },
17
+ "aws": {
18
+ "Bucket": "your-bucket-name",
19
+ "prefix": "your-prefix",
20
+ "Region": "us-east-1",
21
+ "HostName": "https://your-cdn-domain.com"
22
+ }
23
+ }
24
+ ```
25
+
26
+ ### 配置项说明
27
+
28
+ - `upload.uploadPath`: 本地上传源文件夹路径(相对于项目根目录)
29
+ - `upload.s3Static`: 存储桶中的子路径
30
+ - `aws.Bucket`: GCP 存储桶名称
31
+ - `aws.prefix`: 存储桶中的前缀路径
32
+ - `aws.Region`: 存储区域
33
+ - `aws.HostName`: CDN 域名
34
+
35
+ **最终上传路径**: `{aws.prefix}/{upload.s3Static}`
36
+
37
+ ## 使用方法
38
+
39
+ ### 1. 命令行使用
40
+
41
+ #### 基本上传
42
+ ```bash
43
+ # 使用默认配置文件上传
44
+ node dist/bin/cli.js upload-config
45
+
46
+ # 指定自定义配置文件
47
+ node dist/bin/cli.js upload-config -c /path/to/custom-config.json
48
+
49
+ # 使用服务账号密钥文件
50
+ node dist/bin/cli.js upload-config -k /path/to/service-account.json
51
+ ```
52
+
53
+ #### 覆盖配置选项
54
+ ```bash
55
+ # 自定义源路径(覆盖配置文件中的 uploadPath)
56
+ node dist/bin/cli.js upload-config -s /custom/source/path
57
+
58
+ # 自定义目标路径(覆盖配置文件中的目标路径)
59
+ node dist/bin/cli.js upload-config -d custom/destination/path
60
+
61
+ # 禁用递归上传
62
+ node dist/bin/cli.js upload-config --no-recursive
63
+
64
+ # 禁用 GZIP 压缩
65
+ node dist/bin/cli.js upload-config --no-compression
66
+ ```
67
+
68
+ #### 查看配置信息
69
+ ```bash
70
+ # 显示当前配置信息(不执行上传)
71
+ node dist/bin/cli.js upload-config --show-config
72
+ ```
73
+
74
+ #### 查看帮助信息
75
+ ```bash
76
+ # 查看 upload-config 命令的详细帮助
77
+ node dist/bin/cli.js upload-config --help
78
+ ```
79
+
80
+ ### 2. 代码中使用
81
+
82
+ ```typescript
83
+ import { uploadFilesWithConfig, getConfigSummary } from 'ai-yuca';
84
+
85
+ // 基本使用
86
+ async function uploadWithConfig() {
87
+ try {
88
+ const result = await uploadFilesWithConfig();
89
+ console.log(`上传完成: 成功 ${result.success.length}, 失败 ${result.failed.length}`);
90
+ } catch (error) {
91
+ console.error('上传失败:', error.message);
92
+ }
93
+ }
94
+
95
+ // 自定义选项
96
+ async function uploadWithCustomOptions() {
97
+ const result = await uploadFilesWithConfig({
98
+ configPath: '/path/to/custom-config.json',
99
+ storageClientOptions: {
100
+ keyFilename: '/path/to/service-account.json'
101
+ },
102
+ customSourcePath: '/custom/source',
103
+ customDestination: 'custom/destination',
104
+ recursive: true,
105
+ enableCompression: true
106
+ });
107
+ }
108
+
109
+ // 获取配置信息
110
+ function showConfig() {
111
+ const config = getConfigSummary();
112
+ console.log('桶名称:', config.bucketName);
113
+ console.log('源路径:', config.sourcePath);
114
+ console.log('目标路径:', config.destination);
115
+ }
116
+ ```
117
+
118
+ ## 功能特性
119
+
120
+ 1. **自动配置读取**: 自动读取 `vs.config.json` 配置文件
121
+ 2. **路径自动组合**: 自动组合 `aws.prefix` 和 `upload.s3Static` 作为目标路径
122
+ 3. **灵活覆盖**: 支持通过命令行参数覆盖配置文件中的设置
123
+ 4. **配置验证**: 自动验证配置文件的必要字段
124
+ 5. **错误处理**: 详细的错误信息和异常处理
125
+ 6. **压缩支持**: 自动对 js、css、json、html、woff 文件启用 GZIP 压缩
126
+ 7. **递归上传**: 支持递归上传目录中的所有文件
127
+
128
+ ## 示例配置
129
+
130
+ 根据提供的配置文件,实际的上传行为如下:
131
+
132
+ - **源路径**: `{项目根目录}/out`
133
+ - **目标桶**: `decom-cdn`
134
+ - **目标路径**: `fed/static/aiAgent`
135
+ - **完整 URL**: `https://cdn.cn2u.xyz/fed/static/aiAgent/{文件名}`
136
+
137
+ ## 错误处理
138
+
139
+ 常见错误及解决方法:
140
+
141
+ 1. **配置文件不存在**: 确保项目根目录存在 `vs.config.json` 文件
142
+ 2. **配置格式错误**: 检查 JSON 格式是否正确
143
+ 3. **缺少必要配置**: 确保包含 `upload` 和 `aws` 配置项
144
+ 4. **源路径不存在**: 确保 `uploadPath` 指定的目录存在
145
+ 5. **认证失败**: 确保 GCP 认证配置正确
146
+
147
+ ## 与原有功能的关系
148
+
149
+ 新的 `upload-config` 命令是对原有 `upload` 命令的补充:
150
+
151
+ - `upload`: 需要手动指定所有参数,适合临时上传
152
+ - `upload-config`: 基于配置文件,适合项目中的常规上传操作
153
+
154
+ 两个命令可以并存使用,根据不同场景选择合适的方式。
@@ -0,0 +1,58 @@
1
+ # 贡献指南
2
+
3
+ 感谢您考虑为AI-Yuca项目做出贡献!以下是一些指导方针,帮助您参与项目开发。
4
+
5
+ ## 开发环境设置
6
+
7
+ 1. 克隆仓库
8
+
9
+ ```bash
10
+ git clone https://github.com/yourusername/ai-yuca.git
11
+ cd ai-yuca
12
+ ```
13
+
14
+ 2. 安装依赖
15
+
16
+ ```bash
17
+ npm install
18
+ ```
19
+
20
+ 3. 运行测试
21
+
22
+ ```bash
23
+ npm test
24
+ ```
25
+
26
+ ## 代码风格
27
+
28
+ 本项目使用ESLint进行代码风格检查。在提交代码前,请确保您的代码通过了lint检查:
29
+
30
+ ```bash
31
+ npm run lint
32
+ ```
33
+
34
+ ## 提交Pull Request
35
+
36
+ 1. Fork本仓库
37
+ 2. 创建您的特性分支 (`git checkout -b feature/amazing-feature`)
38
+ 3. 提交您的更改 (`git commit -m 'Add some amazing feature'`)
39
+ 4. 推送到分支 (`git push origin feature/amazing-feature`)
40
+ 5. 打开一个Pull Request
41
+
42
+ ## 功能建议
43
+
44
+ 如果您有新功能的想法,请先开一个issue讨论您想要添加的内容,以确保它符合项目的方向。
45
+
46
+ ## 报告Bug
47
+
48
+ 如果您发现了bug,请创建一个issue,并尽可能详细地描述问题,包括:
49
+
50
+ - 问题的简要描述
51
+ - 复现步骤
52
+ - 预期行为
53
+ - 实际行为
54
+ - 环境信息(操作系统、Node.js版本等)
55
+
56
+ ## 许可证
57
+
58
+ 通过贡献代码,您同意您的贡献将在MIT许可证下发布。
@@ -0,0 +1,192 @@
1
+ # AI-Yuca CLI 安装指南
2
+
3
+ 本文档介绍如何将 AI-Yuca CLI 工具注册到系统中,使其可以在任何地方使用。
4
+
5
+ ## 方法一:本地开发链接(推荐用于开发)
6
+
7
+ 如果你正在开发或测试这个项目,可以使用 `npm link` 将项目链接到全局:
8
+
9
+ ```bash
10
+ # 在项目根目录下执行
11
+ npm run build # 确保项目已编译
12
+ npm link # 创建全局链接
13
+ ```
14
+
15
+ 执行后,你就可以在任何地方使用 `ai-yuca` 命令了:
16
+
17
+ ```bash
18
+ # 在任何目录下都可以使用
19
+ ai-yuca --help
20
+ ai-yuca init
21
+ ai-yuca upload-config --show-config
22
+ ```
23
+
24
+ ### 取消链接
25
+
26
+ 如果不再需要全局链接,可以取消:
27
+
28
+ ```bash
29
+ npm unlink -g ai-yuca
30
+ ```
31
+
32
+ ## 方法二:发布到 NPM Registry
33
+
34
+ ### 准备发布
35
+
36
+ 1. **确保项目信息完整**:
37
+ ```json
38
+ {
39
+ "name": "ai-yuca",
40
+ "version": "1.0.0",
41
+ "description": "一个实用的AI辅助工具",
42
+ "author": "你的名字",
43
+ "license": "MIT",
44
+ "repository": {
45
+ "type": "git",
46
+ "url": "https://github.com/你的用户名/ai-yuca.git"
47
+ }
48
+ }
49
+ ```
50
+
51
+ 2. **登录 NPM**:
52
+ ```bash
53
+ npm login
54
+ ```
55
+
56
+ 3. **发布包**:
57
+ ```bash
58
+ npm publish
59
+ ```
60
+
61
+ ### 用户安装
62
+
63
+ 发布后,其他用户可以通过以下方式安装:
64
+
65
+ ```bash
66
+ # 全局安装
67
+ npm install -g ai-yuca
68
+
69
+ # 使用 yarn
70
+ yarn global add ai-yuca
71
+
72
+ # 使用 pnpm
73
+ pnpm add -g ai-yuca
74
+ ```
75
+
76
+ ## 方法三:直接从 Git 安装
77
+
78
+ 用户也可以直接从 Git 仓库安装:
79
+
80
+ ```bash
81
+ # 从 GitHub 安装
82
+ npm install -g git+https://github.com/你的用户名/ai-yuca.git
83
+
84
+ # 从特定分支安装
85
+ npm install -g git+https://github.com/你的用户名/ai-yuca.git#分支名
86
+ ```
87
+
88
+ ## 方法四:本地安装包
89
+
90
+ 如果你有打包好的 `.tgz` 文件:
91
+
92
+ ```bash
93
+ # 创建包文件
94
+ npm pack
95
+
96
+ # 安装本地包
97
+ npm install -g ./ai-yuca-1.0.0.tgz
98
+ ```
99
+
100
+ ## 验证安装
101
+
102
+ 无论使用哪种方法,安装完成后都可以验证:
103
+
104
+ ```bash
105
+ # 检查命令是否可用
106
+ ai-yuca --version
107
+ ai-yuca --help
108
+
109
+ # 检查所有子命令
110
+ ai-yuca analyze --help
111
+ ai-yuca upload --help
112
+ ai-yuca download --help
113
+ ai-yuca upload-config --help
114
+ ai-yuca init --help
115
+ ```
116
+
117
+ ## 卸载
118
+
119
+ ```bash
120
+ # 卸载全局安装的包
121
+ npm uninstall -g ai-yuca
122
+
123
+ # 使用 yarn
124
+ yarn global remove ai-yuca
125
+
126
+ # 使用 pnpm
127
+ pnpm remove -g ai-yuca
128
+ ```
129
+
130
+ ## 故障排除
131
+
132
+ ### 权限问题
133
+
134
+ 如果在 macOS 或 Linux 上遇到权限问题:
135
+
136
+ ```bash
137
+ # 使用 sudo(不推荐)
138
+ sudo npm install -g ai-yuca
139
+
140
+ # 或者配置 npm 使用不同的目录(推荐)
141
+ npm config set prefix ~/.npm-global
142
+ echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
143
+ source ~/.bashrc
144
+ ```
145
+
146
+ ### 命令找不到
147
+
148
+ 如果安装后命令找不到,检查:
149
+
150
+ 1. **PATH 环境变量**:
151
+ ```bash
152
+ echo $PATH
153
+ npm config get prefix
154
+ ```
155
+
156
+ 2. **npm 全局目录**:
157
+ ```bash
158
+ npm list -g --depth=0
159
+ ```
160
+
161
+ 3. **重新加载 shell**:
162
+ ```bash
163
+ source ~/.bashrc # 或 ~/.zshrc
164
+ ```
165
+
166
+ ## 开发者注意事项
167
+
168
+ ### package.json 配置
169
+
170
+ 确保 `package.json` 中的 `bin` 字段正确配置:
171
+
172
+ ```json
173
+ {
174
+ "bin": {
175
+ "ai-yuca": "./dist/bin/cli.js"
176
+ }
177
+ }
178
+ ```
179
+
180
+ ### 构建脚本
181
+
182
+ 确保在发布前运行构建:
183
+
184
+ ```json
185
+ {
186
+ "scripts": {
187
+ "prepublishOnly": "npm run build"
188
+ }
189
+ }
190
+ ```
191
+
192
+ 这样可以确保每次发布前都会自动构建最新版本。
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # AI-Yuca
2
+
3
+ 一个实用的AI辅助命令行工具
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install -g ai-yuca
9
+ ```
10
+
11
+ ## 使用方法
12
+
13
+ ```bash
14
+ ai-yuca <命令> [选项]
15
+ ```
16
+
17
+ ### 可用命令
18
+
19
+ - `help`: 显示帮助信息
20
+ - `version`: 显示版本信息
21
+ - `analyze`: 分析文本内容
22
+ - `upload`: 上传文件到GCP存储桶
23
+
24
+ ### 示例
25
+
26
+ ```bash
27
+ # 分析文本文件
28
+ ai-yuca analyze --file example.txt
29
+
30
+ # 上传单个文件到GCP存储桶(使用密钥文件)
31
+ ai-yuca upload --source path/to/file.txt --bucket your-bucket-name --key-file path/to/keyfile.json
32
+
33
+ # 使用应用默认凭证免密上传(需先设置GOOGLE_APPLICATION_CREDENTIALS环境变量)
34
+ ai-yuca upload --source path/to/file.txt --bucket your-bucket-name
35
+
36
+ # 批量上传多个文件
37
+ ai-yuca upload --source file1.txt file2.txt file3.txt --bucket your-bucket-name --key-file path/to/keyfile.json
38
+
39
+ # 递归上传目录中的所有文件
40
+ ai-yuca upload --source path/to/directory --bucket your-bucket-name --key-file path/to/keyfile.json --recursive
41
+ ```
42
+
43
+ ## 特性
44
+
45
+ - 简单易用的命令行界面
46
+ - 强大的文本分析功能
47
+ - GCP文件上传功能,支持批量和递归上传
48
+ - 支持多种GCP认证方式,包括密钥文件和应用默认凭证(免密上传)
49
+ - 完整的TypeScript类型支持
50
+ - 可扩展的插件系统
51
+
52
+ ## 开发
53
+
54
+ ### 安装依赖
55
+
56
+ ```bash
57
+ npm install
58
+ ```
59
+
60
+ ### 构建
61
+
62
+ ```bash
63
+ npm run build
64
+ ```
65
+
66
+ ### 开发模式运行
67
+
68
+ ```bash
69
+ npm run dev
70
+ ```
71
+
72
+ ### 运行测试
73
+
74
+ ```bash
75
+ npm test
76
+ ```
77
+
78
+ ## 许可证
79
+
80
+ MIT
package/bin/cli.js ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { program } = require('commander');
4
+ const pkg = require('../package.json');
5
+ const { analyze } = require('../src/index');
6
+ const { createStorageClient, uploadFiles } = require('../src/upload');
7
+
8
+ // 设置版本和描述
9
+ program
10
+ .version(pkg.version, '-v, --version')
11
+ .description(pkg.description);
12
+
13
+ // 添加analyze命令
14
+ program
15
+ .command('analyze')
16
+ .description('分析文本内容')
17
+ .option('-f, --file <path>', '指定要分析的文件路径')
18
+ .option('-t, --text <text>', '直接分析提供的文本')
19
+ .action((options) => {
20
+ analyze(options);
21
+ });
22
+
23
+ // 添加upload命令
24
+ program
25
+ .command('upload')
26
+ .description('上传文件到GCP存储桶')
27
+ .option('-s, --source <paths...>', '指定要上传的文件或目录路径(支持多个)')
28
+ .option('-b, --bucket <name>', 'GCP存储桶名称')
29
+ .option('-d, --destination <path>', '目标路径(存储桶中的路径)')
30
+ .option('-k, --key-file <path>', 'GCP服务账号密钥文件路径')
31
+ .option('-r, --recursive', '递归上传目录中的文件', false)
32
+ .action(async (options) => {
33
+ try {
34
+ const { source, bucket, destination, keyFile, recursive } = options;
35
+
36
+ if (!source || !bucket || !keyFile) {
37
+ console.error('错误: 必须提供源文件路径、存储桶名称和密钥文件路径');
38
+ process.exit(1);
39
+ }
40
+
41
+ // 创建存储客户端
42
+ const storageClient = createStorageClient({ keyFilename: keyFile });
43
+
44
+ console.log('开始上传文件...');
45
+
46
+ // 执行上传
47
+ const results = await uploadFiles({
48
+ bucketName: bucket,
49
+ sourcePath: source,
50
+ destination,
51
+ storageClient,
52
+ recursive
53
+ });
54
+
55
+ // 输出结果
56
+ console.log('\n上传完成!');
57
+ console.log(`成功: ${results.success.length} 个文件`);
58
+ console.log(`失败: ${results.failed.length} 个文件`);
59
+
60
+ if (results.success.length > 0) {
61
+ console.log('\n成功上传的文件:');
62
+ results.success.forEach(file => {
63
+ console.log(`- ${file.file} (${file.url})`);
64
+ });
65
+ }
66
+
67
+ if (results.failed.length > 0) {
68
+ console.log('\n上传失败的文件:');
69
+ results.failed.forEach(file => {
70
+ console.log(`- ${file.file}: ${file.error}`);
71
+ });
72
+ }
73
+ } catch (err) {
74
+ console.error(`上传错误: ${err.message}`);
75
+ process.exit(1);
76
+ }
77
+ });
78
+
79
+ // 解析命令行参数
80
+ program.parse(process.argv);
81
+
82
+ // 如果没有提供任何命令,显示帮助信息
83
+ if (!process.argv.slice(2).length) {
84
+ program.outputHelp();
85
+ }