@cnbcool/cnb-api-generate 2.1.1 → 2.2.1

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;
@@ -24,10 +24,8 @@ export function getToolParamDefs(moduleName: string, toolName: string) {
24
24
  export function formatParams(
25
25
  params: Record<string, string | string[] | boolean | undefined>,
26
26
  ): Record<string, any> {
27
- console.log(JSON.stringify(params))
28
27
  // 先将 CLI 带 '-' 的参数名还原为原始 '_'/'@' 参数名
29
28
  params = restoreOriginalKeys(params);
30
- console.log(params)
31
29
  const formatted: Record<string, any> = {
32
30
  module: params.module,
33
31
  tool: params.tool,
@@ -1,3 +1,4 @@
1
+ import { encode } from '@toon-format/toon'
1
2
  /**
2
3
  * 将数据转换为格式化的 JSON 字符串
3
4
  * @param data 要序列化的数据(可以是任意类型)缩进)
@@ -8,5 +9,13 @@ export function printJSON(
8
9
  data: Record<string, any> | Array<any>,
9
10
  verbose: boolean = false
10
11
  ): string {
11
- return verbose ? JSON.stringify(data, null, 2) : JSON.stringify(data);
12
+ if (verbose) {
13
+ return JSON.stringify(data, null, 2);
14
+ } else {
15
+ try {
16
+ return encode(data);
17
+ } catch (e) {
18
+ return JSON.stringify(data);
19
+ }
20
+ }
12
21
  }
@@ -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.1.1",
3
+ "version": "2.2.1",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -33,6 +33,7 @@
33
33
  "@babel/preset-env": "^7.29.0",
34
34
  "@babel/preset-typescript": "^7.28.5",
35
35
  "@babel/traverse": "^7.29.0",
36
+ "@toon-format/toon": "^2.1.0",
36
37
  "@types/cli-progress": "^3.11.6",
37
38
  "@types/lodash": "4.17.20",
38
39
  "babel-plugin-transform-define": "^2.1.4",