@cnbcool/cnb-api-generate 2.2.0 → 2.2.2

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.
@@ -1,11 +1,49 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
  import { Command } from 'commander';
4
- import { showShort, resolveShortcut } from '../shortcuts';
4
+ import { showShort, resolveShortcut, type ResolvedShortcut } from '../shortcuts';
5
5
  import { handleUpload } from '../utils/upload';
6
6
  import { formatParams } from './format-params';
7
7
  import { formatOutput } from './format-output';
8
8
 
9
+ /**
10
+ * 加载 tool 模块并返回其 default 导出函数
11
+ */
12
+ function loadToolFunction(moduleName: string, toolName: string): any {
13
+ const toolPath = path.join(
14
+ __dirname,
15
+ '../../modules',
16
+ `${moduleName}/${toolName}.js`,
17
+ );
18
+
19
+ if (!fs.existsSync(toolPath)) {
20
+ console.error(`工具文件不存在: ${toolPath}`);
21
+ process.exit(1);
22
+ }
23
+
24
+ const toolModule = require(toolPath);
25
+ const toolFunction = toolModule.default;
26
+
27
+ if (!toolFunction) {
28
+ console.error(`工具函数不存在`);
29
+ process.exit(1);
30
+ }
31
+
32
+ return toolFunction;
33
+ }
34
+
35
+ /**
36
+ * 处理上传命令的独立分支
37
+ * 上传命令只需要 --file 和自动注入的 path 参数,不经过 formatParams
38
+ */
39
+ async function executeUpload(
40
+ shortcut: ResolvedShortcut,
41
+ opts: Record<string, any>,
42
+ ): Promise<any> {
43
+ const toolFunction = loadToolFunction(shortcut.module, shortcut.tool);
44
+ return handleUpload(shortcut, opts.file, toolFunction, shortcut.autoPath as any);
45
+ }
46
+
9
47
  export async function executeAction(
10
48
  moduleArg: string | undefined,
11
49
  toolArg: string | undefined,
@@ -59,26 +97,18 @@ export async function executeAction(
59
97
  parentCmd.help();
60
98
  }
61
99
 
62
- const formattedParams = formatParams(params);
63
-
64
- const toolPath = path.join(
65
- __dirname,
66
- '../../modules',
67
- `${formattedParams.module}/${formattedParams.tool}.js`,
68
- );
69
-
70
- if (!fs.existsSync(toolPath)) {
71
- console.error(`工具文件不存在: ${toolPath}`);
72
- process.exit(1);
100
+ // 上传命令走独立分支:只需 --file + autoPath,跳过 formatParams 常规流程
101
+ if (shortcut?.upload) {
102
+ const data = await executeUpload(shortcut, opts);
103
+ const toolKey = `${shortcut.module}/${shortcut.tool}`;
104
+ const isVerbose = !!opts.verbose;
105
+ console.log(formatOutput(data, isVerbose, !isVerbose, toolKey));
106
+ return;
73
107
  }
74
108
 
75
- const toolModule = require(toolPath);
76
- const toolFunction = toolModule.default;
109
+ const formattedParams = formatParams(params);
77
110
 
78
- if (!toolFunction) {
79
- console.error(`工具函数不存在`);
80
- process.exit(1);
81
- }
111
+ const toolFunction = loadToolFunction(formattedParams.module, formattedParams.tool);
82
112
 
83
113
  const toolsParam: any[] = [];
84
114
  let pathAndQueryParams: Record<string, any> | string | null = null;
@@ -109,15 +139,10 @@ export async function executeAction(
109
139
  toolsParam.push(pathAndQueryParams);
110
140
  }
111
141
 
112
- let data: any;
113
- if (shortcut?.upload) {
114
- data = await handleUpload(shortcut, formattedParams.data?.file, toolFunction, pathAndQueryParams as { repo: string; number?: string });
115
- } else {
116
- if (formattedParams.data) {
117
- toolsParam.push(formattedParams.data);
118
- }
119
- data = await toolFunction(...toolsParam);
142
+ if (formattedParams.data) {
143
+ toolsParam.push(formattedParams.data);
120
144
  }
145
+ const data = await toolFunction(...toolsParam);
121
146
 
122
147
  const toolKey = `${formattedParams.module}/${formattedParams.tool}`;
123
148
  const isVerbose = !!formattedParams.verbose;
@@ -131,8 +131,13 @@ export function registerModuleCommands(program: Command): void {
131
131
  await executeAction(moduleName, shortcut.shortName, opts, sub);
132
132
  });
133
133
 
134
- // 复用真实 tool 的选项,跳过 path 参数
135
- registerToolOptions(shortcutCmd, moduleName, shortcut.realTool, true);
134
+ // 上传命令只需要 --file,不复用真实 tool body 选项(name/size/content_type 由上传流程自动获取)
135
+ if (!shortcut.upload) {
136
+ registerToolOptions(shortcutCmd, moduleName, shortcut.realTool, true);
137
+ } else {
138
+ shortcutCmd.requiredOption('--file <string>', '本地文件路径。必填。');
139
+ }
140
+
136
141
  registeredNames.add(shortcut.shortName);
137
142
  }
138
143
 
@@ -181,7 +181,10 @@ export async function handleUpload(
181
181
  ? { name: fileName, size: fileSize, content_type: contentType }
182
182
  : { name: fileName, size: fileSize };
183
183
 
184
- const uploadResponse = await toolFunction(pathParams, requestBody);
184
+ // Issue API 签名: fn({ repo, number }, body)
185
+ // PR API 签名: fn(repo, body)
186
+ const apiFirstArg: any = isIssueUpload ? pathParams : pathParams.repo;
187
+ const uploadResponse = await toolFunction(apiFirstArg, requestBody);
185
188
 
186
189
  // 提取 upload_url
187
190
  const responseData = uploadResponse?.data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnbcool/cnb-api-generate",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -7,6 +7,20 @@ description: CNB OpenAPI 交互能力,支持仓库、Issue、PR、流水线、
7
7
 
8
8
  操作 CNB 平台资源的 CLI 工具。
9
9
 
10
+ ## 安装CLI
11
+
12
+ 检查是否已安装
13
+
14
+ ```bash
15
+ cnb
16
+ ```
17
+
18
+ 如果没有安装,则安装
19
+
20
+ ```bash
21
+ npm install @cnbcool/cnb-cli -g
22
+ ```
23
+
10
24
  ## 快捷命令(优先使用)
11
25
 
12
26
  issues 快捷命令: