@cnbcool/cnb-cli 1.3.0 → 1.4.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.
Files changed (265) hide show
  1. package/bin/cnb.js +1 -1
  2. package/dist/index.js +66 -0
  3. package/package.json +10 -5
  4. package/cli/core/config/cag.config.js +0 -58
  5. package/cli/core/core.js +0 -124
  6. package/cli/core/fetch-response-handler.js +0 -34
  7. package/cli/core/flat-options.json +0 -8909
  8. package/cli/core/help.json +0 -38458
  9. package/cli/core/index.js +0 -40
  10. package/cli/core/key-mapping.json +0 -538
  11. package/cli/core/lib/execute-action.js +0 -117
  12. package/cli/core/lib/extra-help.js +0 -20
  13. package/cli/core/lib/flat-options-data.js +0 -16
  14. package/cli/core/lib/format-output.js +0 -85
  15. package/cli/core/lib/format-params.js +0 -217
  16. package/cli/core/lib/help-data.js +0 -15
  17. package/cli/core/lib/key-mapping-data.js +0 -16
  18. package/cli/core/lib/parsers.js +0 -126
  19. package/cli/core/lib/print-json.js +0 -24
  20. package/cli/core/lib/register-fallback.js +0 -15
  21. package/cli/core/lib/register-modules.js +0 -121
  22. package/cli/core/lib/summary-extractors.js +0 -181
  23. package/cli/core/lib/trim-summary.js +0 -17
  24. package/cli/core/shortcuts.js +0 -276
  25. package/cli/core/utils/build-nested-field-map.js +0 -35
  26. package/cli/core/utils/convert-link.js +0 -144
  27. package/cli/core/utils/flat-key-from-segments.js +0 -14
  28. package/cli/core/utils/generate-unique-id.js +0 -10
  29. package/cli/core/utils/match-array-object-field.js +0 -25
  30. package/cli/core/utils/restore-original-keys.js +0 -24
  31. package/cli/core/utils/upload.js +0 -237
  32. package/cli/modules/activities/get-user-activities-by-date.js +0 -75
  33. package/cli/modules/activities/get-user-repo-activity-details.js +0 -77
  34. package/cli/modules/activities/top-contributors.js +0 -75
  35. package/cli/modules/ai/ai-chat-completions.js +0 -69
  36. package/cli/modules/assets/delete-asset.js +0 -75
  37. package/cli/modules/assets/get-files.js +0 -75
  38. package/cli/modules/assets/get-imgs.js +0 -75
  39. package/cli/modules/assets/list-assets.js +0 -75
  40. package/cli/modules/badge/get-badge.js +0 -79
  41. package/cli/modules/badge/list-badge.js +0 -69
  42. package/cli/modules/badge/upload-badge.js +0 -69
  43. package/cli/modules/build/build-crontab-sync.js +0 -74
  44. package/cli/modules/build/build-logs-delete.js +0 -74
  45. package/cli/modules/build/build-runner-download-log.js +0 -74
  46. package/cli/modules/build/get-build-logs.js +0 -75
  47. package/cli/modules/build/get-build-stage.js +0 -78
  48. package/cli/modules/build/get-build-status.js +0 -74
  49. package/cli/modules/build/start-build.js +0 -69
  50. package/cli/modules/build/stop-build.js +0 -74
  51. package/cli/modules/charge/get-special-amount.js +0 -67
  52. package/cli/modules/event/get-events.js +0 -73
  53. package/cli/modules/followers/get-followers-by-user-id.js +0 -75
  54. package/cli/modules/followers/get-following-by-user-id.js +0 -75
  55. package/cli/modules/git/create-blob.js +0 -69
  56. package/cli/modules/git/create-branch-lock.js +0 -74
  57. package/cli/modules/git/create-branch.js +0 -69
  58. package/cli/modules/git/create-tag.js +0 -69
  59. package/cli/modules/git/delete-branch-lock.js +0 -74
  60. package/cli/modules/git/delete-branch.js +0 -74
  61. package/cli/modules/git/delete-commit-annotation.js +0 -76
  62. package/cli/modules/git/delete-commit-asset.js +0 -76
  63. package/cli/modules/git/delete-tag-annotation.js +0 -74
  64. package/cli/modules/git/delete-tag.js +0 -74
  65. package/cli/modules/git/get-archive-commit-changed-files.js +0 -74
  66. package/cli/modules/git/get-archive-compare-changed-files.js +0 -74
  67. package/cli/modules/git/get-archive.js +0 -74
  68. package/cli/modules/git/get-branch.js +0 -74
  69. package/cli/modules/git/get-commit-annotations-in-batch.js +0 -69
  70. package/cli/modules/git/get-commit-annotations.js +0 -74
  71. package/cli/modules/git/get-commit-assets-by-sha.js +0 -74
  72. package/cli/modules/git/get-commit-assets.js +0 -79
  73. package/cli/modules/git/get-commit-statuses.js +0 -74
  74. package/cli/modules/git/get-commit.js +0 -74
  75. package/cli/modules/git/get-compare-commits.js +0 -74
  76. package/cli/modules/git/get-content-without-path.js +0 -75
  77. package/cli/modules/git/get-content.js +0 -77
  78. package/cli/modules/git/get-head.js +0 -66
  79. package/cli/modules/git/get-presigned-lfs-download-link.js +0 -77
  80. package/cli/modules/git/get-raw.js +0 -77
  81. package/cli/modules/git/get-tag-annotations.js +0 -74
  82. package/cli/modules/git/get-tag.js +0 -74
  83. package/cli/modules/git/list-branches.js +0 -75
  84. package/cli/modules/git/list-commits.js +0 -75
  85. package/cli/modules/git/list-tags.js +0 -75
  86. package/cli/modules/git/post-commit-asset-upload-confirmation.js +0 -81
  87. package/cli/modules/git/post-commit-asset-upload-url.js +0 -77
  88. package/cli/modules/git/put-commit-annotations.js +0 -77
  89. package/cli/modules/git/put-tag-annotations.js +0 -77
  90. package/cli/modules/git-settings/delete-branch-protection.js +0 -74
  91. package/cli/modules/git-settings/get-branch-protection.js +0 -74
  92. package/cli/modules/git-settings/get-pipeline-settings.js +0 -66
  93. package/cli/modules/git-settings/get-pull-request-settings.js +0 -66
  94. package/cli/modules/git-settings/get-push-limit-settings.js +0 -66
  95. package/cli/modules/git-settings/list-branch-protections.js +0 -66
  96. package/cli/modules/git-settings/patch-branch-protection.js +0 -77
  97. package/cli/modules/git-settings/post-branch-protection.js +0 -69
  98. package/cli/modules/git-settings/put-pipeline-settings.js +0 -69
  99. package/cli/modules/git-settings/put-pull-request-settings.js +0 -69
  100. package/cli/modules/git-settings/put-push-limit-settings.js +0 -69
  101. package/cli/modules/issues/can-user-be-assigned-to-issue.js +0 -76
  102. package/cli/modules/issues/create-issue.js +0 -69
  103. package/cli/modules/issues/delete-issue-assignees.js +0 -77
  104. package/cli/modules/issues/delete-issue-label.js +0 -76
  105. package/cli/modules/issues/delete-issue-labels.js +0 -74
  106. package/cli/modules/issues/get-issue-comment.js +0 -76
  107. package/cli/modules/issues/get-issue.js +0 -74
  108. package/cli/modules/issues/list-issue-activities-latest.js +0 -76
  109. package/cli/modules/issues/list-issue-activities.js +0 -77
  110. package/cli/modules/issues/list-issue-assignees.js +0 -74
  111. package/cli/modules/issues/list-issue-comments.js +0 -77
  112. package/cli/modules/issues/list-issue-labels.js +0 -77
  113. package/cli/modules/issues/list-issues.js +0 -75
  114. package/cli/modules/issues/list-user-issues.js +0 -71
  115. package/cli/modules/issues/patch-issue-assignees.js +0 -77
  116. package/cli/modules/issues/patch-issue-comment.js +0 -79
  117. package/cli/modules/issues/post-issue-assignees.js +0 -77
  118. package/cli/modules/issues/post-issue-comment.js +0 -77
  119. package/cli/modules/issues/post-issue-file-asset-upload-url.js +0 -77
  120. package/cli/modules/issues/post-issue-image-asset-upload-url.js +0 -77
  121. package/cli/modules/issues/post-issue-labels.js +0 -77
  122. package/cli/modules/issues/put-issue-labels.js +0 -77
  123. package/cli/modules/issues/update-issue-properties.js +0 -78
  124. package/cli/modules/issues/update-issue.js +0 -77
  125. package/cli/modules/knowledge-base/delete-knowledge-base.js +0 -66
  126. package/cli/modules/knowledge-base/get-knowledge-base-info.js +0 -66
  127. package/cli/modules/knowledge-base/get-models.js +0 -66
  128. package/cli/modules/knowledge-base/query-knowledge-base.js +0 -69
  129. package/cli/modules/knowledge-base/search-npc.js +0 -71
  130. package/cli/modules/members/add-members-of-group.js +0 -77
  131. package/cli/modules/members/add-members-of-mission.js +0 -77
  132. package/cli/modules/members/add-members-of-registry.js +0 -77
  133. package/cli/modules/members/add-members-of-repo.js +0 -77
  134. package/cli/modules/members/delete-members-of-group.js +0 -74
  135. package/cli/modules/members/delete-members-of-repo.js +0 -74
  136. package/cli/modules/members/delete-outside-collaborators.js +0 -74
  137. package/cli/modules/members/get-member-access-level-of-group.js +0 -75
  138. package/cli/modules/members/get-member-access-level-of-repo.js +0 -75
  139. package/cli/modules/members/list-all-members.js +0 -75
  140. package/cli/modules/members/list-inherit-members-of-group.js +0 -75
  141. package/cli/modules/members/list-inherit-members-of-repo.js +0 -75
  142. package/cli/modules/members/list-member-access-level-of-group.js +0 -74
  143. package/cli/modules/members/list-member-access-level-of-repo.js +0 -74
  144. package/cli/modules/members/list-members-of-group.js +0 -75
  145. package/cli/modules/members/list-members-of-repo.js +0 -75
  146. package/cli/modules/members/list-outside-collaborators.js +0 -75
  147. package/cli/modules/members/update-members-of-group.js +0 -77
  148. package/cli/modules/members/update-members-of-repo.js +0 -77
  149. package/cli/modules/members/update-outside-collaborators.js +0 -77
  150. package/cli/modules/missions/create-mission.js +0 -69
  151. package/cli/modules/missions/delete-mission.js +0 -66
  152. package/cli/modules/missions/get-group-sub-missions.js +0 -75
  153. package/cli/modules/missions/get-mission-view-config.js +0 -75
  154. package/cli/modules/missions/get-mission-view-list.js +0 -66
  155. package/cli/modules/missions/post-mission-view-config.js +0 -69
  156. package/cli/modules/missions/post-mission-view-list.js +0 -69
  157. package/cli/modules/missions/put-mission-view-list.js +0 -69
  158. package/cli/modules/missions/set-mission-visibility.js +0 -75
  159. package/cli/modules/organizations/create-organization.js +0 -65
  160. package/cli/modules/organizations/delete-organization.js +0 -66
  161. package/cli/modules/organizations/get-group-setting.js +0 -66
  162. package/cli/modules/organizations/get-group.js +0 -66
  163. package/cli/modules/organizations/get-groups-by-user-id.js +0 -75
  164. package/cli/modules/organizations/list-groups.js +0 -75
  165. package/cli/modules/organizations/list-subgroups.js +0 -75
  166. package/cli/modules/organizations/list-top-groups.js +0 -71
  167. package/cli/modules/organizations/transfer-group.js +0 -69
  168. package/cli/modules/organizations/update-group-setting.js +0 -69
  169. package/cli/modules/organizations/update-organization.js +0 -69
  170. package/cli/modules/organizations/upload-logos.js +0 -69
  171. package/cli/modules/pulls/can-user-be-assigned-to-pull.js +0 -76
  172. package/cli/modules/pulls/delete-pull-assignees.js +0 -77
  173. package/cli/modules/pulls/delete-pull-label.js +0 -76
  174. package/cli/modules/pulls/delete-pull-labels.js +0 -74
  175. package/cli/modules/pulls/delete-repo-files.js +0 -75
  176. package/cli/modules/pulls/delete-repo-imgs.js +0 -75
  177. package/cli/modules/pulls/get-pull-comment.js +0 -76
  178. package/cli/modules/pulls/get-pull.js +0 -74
  179. package/cli/modules/pulls/list-pull-assignees.js +0 -74
  180. package/cli/modules/pulls/list-pull-comments.js +0 -77
  181. package/cli/modules/pulls/list-pull-commit-statuses.js +0 -74
  182. package/cli/modules/pulls/list-pull-commits.js +0 -77
  183. package/cli/modules/pulls/list-pull-files.js +0 -74
  184. package/cli/modules/pulls/list-pull-labels.js +0 -77
  185. package/cli/modules/pulls/list-pull-review-comments.js +0 -79
  186. package/cli/modules/pulls/list-pull-reviews.js +0 -77
  187. package/cli/modules/pulls/list-pulls-by-numbers.js +0 -75
  188. package/cli/modules/pulls/list-pulls.js +0 -75
  189. package/cli/modules/pulls/merge-pull.js +0 -77
  190. package/cli/modules/pulls/patch-pull-comment.js +0 -79
  191. package/cli/modules/pulls/patch-pull.js +0 -77
  192. package/cli/modules/pulls/post-pull-assignees.js +0 -77
  193. package/cli/modules/pulls/post-pull-comment.js +0 -77
  194. package/cli/modules/pulls/post-pull-labels.js +0 -77
  195. package/cli/modules/pulls/post-pull-request-review-reply.js +0 -79
  196. package/cli/modules/pulls/post-pull-review.js +0 -77
  197. package/cli/modules/pulls/post-pull.js +0 -69
  198. package/cli/modules/pulls/put-pull-labels.js +0 -77
  199. package/cli/modules/pulls/upload-files.js +0 -69
  200. package/cli/modules/pulls/upload-imgs.js +0 -69
  201. package/cli/modules/rank/get-language-list.js +0 -71
  202. package/cli/modules/rank/get-repo-annual-rank.js +0 -71
  203. package/cli/modules/rank/get-repo-daily-rank.js +0 -71
  204. package/cli/modules/rank/get-repo-monthly-rank.js +0 -71
  205. package/cli/modules/rank/get-repo-weekly-rank.js +0 -71
  206. package/cli/modules/registries/delete-package-tag.js +0 -79
  207. package/cli/modules/registries/delete-package.js +0 -77
  208. package/cli/modules/registries/delete-registry.js +0 -66
  209. package/cli/modules/registries/get-group-sub-registries.js +0 -75
  210. package/cli/modules/registries/get-package-tag-detail.js +0 -82
  211. package/cli/modules/registries/get-package-tag-provenance.js +0 -79
  212. package/cli/modules/registries/get-package.js +0 -77
  213. package/cli/modules/registries/list-package-tags.js +0 -80
  214. package/cli/modules/registries/list-packages.js +0 -76
  215. package/cli/modules/registries/set-registry-visibility.js +0 -75
  216. package/cli/modules/releases/delete-release-asset.js +0 -76
  217. package/cli/modules/releases/delete-release.js +0 -74
  218. package/cli/modules/releases/get-latest-release.js +0 -66
  219. package/cli/modules/releases/get-release-asset.js +0 -76
  220. package/cli/modules/releases/get-release-by-id.js +0 -74
  221. package/cli/modules/releases/get-release-by-tag.js +0 -74
  222. package/cli/modules/releases/get-releases-asset.js +0 -79
  223. package/cli/modules/releases/list-releases.js +0 -75
  224. package/cli/modules/releases/patch-release.js +0 -77
  225. package/cli/modules/releases/post-release-asset-upload-confirmation.js +0 -81
  226. package/cli/modules/releases/post-release-asset-upload-url.js +0 -77
  227. package/cli/modules/releases/post-release.js +0 -69
  228. package/cli/modules/repo-code-issue/get-code-issue-detail-open-api.js +0 -75
  229. package/cli/modules/repo-code-issue/list-code-issue-open-api.js +0 -76
  230. package/cli/modules/repo-contributor/get-repo-contributor-trend.js +0 -75
  231. package/cli/modules/repo-labels/delete-label.js +0 -74
  232. package/cli/modules/repo-labels/list-labels.js +0 -75
  233. package/cli/modules/repo-labels/patch-label.js +0 -77
  234. package/cli/modules/repo-labels/post-label.js +0 -69
  235. package/cli/modules/repositories/archive-repo.js +0 -66
  236. package/cli/modules/repositories/create-repo.js +0 -69
  237. package/cli/modules/repositories/delete-repo.js +0 -66
  238. package/cli/modules/repositories/get-by-id.js +0 -66
  239. package/cli/modules/repositories/get-group-sub-repos.js +0 -75
  240. package/cli/modules/repositories/get-pinned-repo-by-group.js +0 -66
  241. package/cli/modules/repositories/get-pinned-repo-by-id.js +0 -66
  242. package/cli/modules/repositories/get-repos-by-user-name.js +0 -75
  243. package/cli/modules/repositories/get-repos.js +0 -71
  244. package/cli/modules/repositories/list-forks-repos.js +0 -75
  245. package/cli/modules/repositories/set-pinned-repo-by-group.js +0 -69
  246. package/cli/modules/repositories/set-repo-visibility.js +0 -75
  247. package/cli/modules/repositories/transfer-repo.js +0 -69
  248. package/cli/modules/repositories/un-archive-repo.js +0 -66
  249. package/cli/modules/repositories/update-repo.js +0 -69
  250. package/cli/modules/search/list-public-repos.js +0 -71
  251. package/cli/modules/security/get-repo-security-overview.js +0 -75
  252. package/cli/modules/starring/get-user-all-stared-repos.js +0 -71
  253. package/cli/modules/starring/get-user-stared-repos.js +0 -75
  254. package/cli/modules/starring/list-star-users.js +0 -75
  255. package/cli/modules/users/auto-complete-source.js +0 -71
  256. package/cli/modules/users/get-user-info-by-name.js +0 -66
  257. package/cli/modules/users/get-user-info.js +0 -62
  258. package/cli/modules/users/list-email.js +0 -62
  259. package/cli/modules/users/list-gpg-keys.js +0 -71
  260. package/cli/modules/users/update-user-info.js +0 -65
  261. package/cli/modules/workspace/delete-workspace.js +0 -65
  262. package/cli/modules/workspace/get-workspace-detail.js +0 -74
  263. package/cli/modules/workspace/list-workspaces.js +0 -71
  264. package/cli/modules/workspace/start-workspace.js +0 -69
  265. package/cli/modules/workspace/workspace-stop.js +0 -65
@@ -1,20 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getExtraHelpText = getExtraHelpText;
7
- /**
8
- * 生成额外的帮助提示字符串
9
- * 包含模块列表和用法示例,供 commander .addHelpText() 使用
10
- */
11
- function getExtraHelpText() {
12
- const cliCmd = "cnb" || 'cnb';
13
- return `
14
-
15
- 用法: ${cliCmd} issues list-issues --repo my-org/my-repo --page 1 --pageSize 10
16
- ${cliCmd} issues create-issue --repo my-org/my-repo --title Bug
17
- 帮助: ${cliCmd} <module> --help
18
- ${cliCmd} <module> <tool> --help
19
- 快捷: ${cliCmd} --short`;
20
- }
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.flatOptionsData = void 0;
7
- var _fs = _interopRequireDefault(require("fs"));
8
- var _path = _interopRequireDefault(require("path"));
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- const flatOptionsFilePath = _path.default.join(__dirname, '../flat-options.json');
11
- let data = {};
12
- if (_fs.default.existsSync(flatOptionsFilePath)) {
13
- const content = _fs.default.readFileSync(flatOptionsFilePath, 'utf8');
14
- data = JSON.parse(content);
15
- }
16
- const flatOptionsData = exports.flatOptionsData = data;
@@ -1,85 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.compactResponse = compactResponse;
7
- exports.formatOutput = formatOutput;
8
- exports.isStandardResponse = isStandardResponse;
9
- var _printJson = require("./print-json");
10
- var _summaryExtractors = require("./summary-extractors");
11
- /**
12
- * 精简响应对象(非 verbose 模式使用)
13
- * - 去掉 trace(仅错误时保留)
14
- * - 去掉 header(原始 x-cnb-* 头)
15
- * - 仅列表 API 时保留分页信息
16
- */
17
- function compactResponse(response) {
18
- if (!response || typeof response !== 'object') return response;
19
- const compact = {
20
- status: response.status
21
- };
22
-
23
- // 仅错误时保留 trace
24
- if (response.status >= 300 && response.trace) {
25
- compact.trace = response.trace;
26
- }
27
-
28
- // 仅列表 API 时保留分页信息
29
- if (response.total != null) {
30
- compact.page = response.page;
31
- compact.pageSize = response.pageSize;
32
- compact.total = response.total;
33
- compact.totalPages = response.totalPages;
34
- }
35
- compact.data = response.data;
36
- return compact;
37
- }
38
-
39
- /**
40
- * 判断是否是标准响应结构(有 status + data 字段)
41
- * cag.config.js 中的 responseConverter 可能返回裸 data(不含 status),需要区分处理
42
- */
43
- function isStandardResponse(response) {
44
- return response && typeof response === 'object' && typeof response.status === 'number' && 'data' in response;
45
- }
46
-
47
- /**
48
- * 格式化 CLI 输出
49
- * - verbose 模式:完整 JSON(含 trace、header 等全部字段)
50
- * - 快捷命令默认:只输出核心摘要字段
51
- * - 非快捷命令默认:精简 JSON(去掉 trace、header,保留完整 data)
52
- * @param response 原始响应
53
- * @param verbose 是否 verbose 模式
54
- * @param summary 是否 summary 模式
55
- * @param toolKey 当前 tool 标识,格式为 "module/tool",用于匹配摘要配置
56
- */
57
- function formatOutput(response, verbose, summary, toolKey) {
58
- // --verbose 模式:输出完整原始信息
59
- if (verbose) {
60
- return (0, _printJson.printJSON)(response, verbose);
61
- }
62
-
63
- // converter 可能返回裸 data(没有标准 {status, data} 结构),直接输出
64
- if (!isStandardResponse(response)) {
65
- if (summary) {
66
- const summarized = (0, _summaryExtractors.summarizeResponse)(response, toolKey);
67
- if (summarized !== null) {
68
- return (0, _printJson.printJSON)(summarized);
69
- }
70
- }
71
- return (0, _printJson.printJSON)(response);
72
- }
73
-
74
- // 精简响应
75
- const compact = compactResponse(response);
76
-
77
- // summary 模式:对特定 tool 应用摘要提取(仅成功响应)
78
- if (summary && compact.status >= 200 && compact.status < 300) {
79
- const summarized = (0, _summaryExtractors.summarizeResponse)(compact.data, toolKey);
80
- if (summarized !== null) {
81
- compact.data = summarized;
82
- }
83
- }
84
- return (0, _printJson.printJSON)(compact);
85
- }
@@ -1,217 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.formatParams = formatParams;
7
- exports.getToolParamDefs = getToolParamDefs;
8
- var _helpData = require("./help-data");
9
- var _parsers = require("./parsers");
10
- var _buildNestedFieldMap = require("../utils/build-nested-field-map");
11
- var _restoreOriginalKeys = require("../utils/restore-original-keys");
12
- var _matchArrayObjectField = require("../utils/match-array-object-field");
13
- /**
14
- * 获取 tool 的参数定义(用于自动分发 --key value 到 path/query)
15
- */
16
- function getToolParamDefs(moduleName, toolName) {
17
- const toolHelp = _helpData.helpData.modulesHelp?.[moduleName]?.[toolName];
18
- if (!toolHelp) return null;
19
- return toolHelp.help?.parameter || {};
20
- }
21
-
22
- /**
23
- * 格式化参数
24
- * 支持新的 --key value 扁平格式,同时向后兼容旧的 --path/--query JSON 格式。
25
- * 根据 help.json 中的参数定义,自动将扁平参数分发到 path 或 query。
26
- * @param params 原始参数对象
27
- * @returns 格式化后的参数对象,包含 module, tool, path, query, data 等
28
- */
29
- function formatParams(params) {
30
- // 先将 CLI 带 '-' 的参数名还原为原始 '_'/'@' 参数名
31
- params = (0, _restoreOriginalKeys.restoreOriginalKeys)(params);
32
- const formatted = {
33
- module: params.module,
34
- tool: params.tool
35
- };
36
-
37
- // 保留控制标志
38
- if (params.help) formatted.help = true;
39
- if (params.short) formatted.short = true;
40
- if (params.verbose) formatted.verbose = true;
41
-
42
- // 新格式:将其他 --key value 根据 help.json 自动分发到 path/query
43
- const paramDefs = getToolParamDefs(params.module, params.tool);
44
- if (paramDefs) {
45
- const pathDef = paramDefs.path || {};
46
- const queryDef = paramDefs.query || {};
47
- const bodyProps = paramDefs.body?.schema?.properties || {};
48
- const reservedKeys = new Set(['module', 'tool', 'help', 'short', 'verbose', 'path', 'query', 'data', 'h', 'v']);
49
-
50
- // 收集 array<object> 展开字段的临时存储:{ arrayName: { propName: string[] } }
51
- const arrayObjectCollector = {};
52
-
53
- // 收集嵌套对象展开字段的临时存储(深层嵌套结构):{ rootObjectKey: { nested: { path: value } } }
54
- const nestedObjectCollector = {};
55
-
56
- // 预建所有嵌套对象字段的 camelCased flat key → pathKeys 映射表
57
- const nestedFieldMap = new Map();
58
- for (const [rootKey, rootProp] of Object.entries(bodyProps)) {
59
- if (rootProp.type === 'object' && rootProp.properties && Object.keys(rootProp.properties).length > 0) {
60
- const subMap = (0, _buildNestedFieldMap.buildNestedFieldMap)(rootProp.properties, rootKey, [rootKey]);
61
- for (const [k, v] of subMap) {
62
- nestedFieldMap.set(k, v);
63
- }
64
- }
65
- }
66
- for (const [key, value] of Object.entries(params)) {
67
- if (reservedKeys.has(key)) continue;
68
- // boolean 值需要特殊处理:嵌套对象的 boolean 子字段不能跳过
69
- if (typeof value === 'boolean') {
70
- const nestedMapping = nestedFieldMap.get(key);
71
- if (nestedMapping && nestedMapping.leafSchema?.type === 'boolean') {
72
- const {
73
- pathKeys
74
- } = nestedMapping;
75
- const rootKey = pathKeys[0];
76
- if (!nestedObjectCollector[rootKey]) {
77
- nestedObjectCollector[rootKey] = {};
78
- }
79
- const subPathKeys = pathKeys.slice(1);
80
- let target = nestedObjectCollector[rootKey];
81
- for (let pi = 0; pi < subPathKeys.length - 1; pi++) {
82
- if (!target[subPathKeys[pi]] || typeof target[subPathKeys[pi]] !== 'object') {
83
- target[subPathKeys[pi]] = {};
84
- }
85
- target = target[subPathKeys[pi]];
86
- }
87
- target[subPathKeys[subPathKeys.length - 1]] = value;
88
- }
89
- continue;
90
- }
91
- if (pathDef[key]) {
92
- // 归入 path
93
- if (!formatted.path) formatted.path = {};
94
- formatted.path[key] = value;
95
- } else if (queryDef[key]) {
96
- // 归入 query
97
- if (!formatted.query) formatted.query = {};
98
- const paramType = queryDef[key].type;
99
- if (paramType === 'number' && typeof value === 'string' && !isNaN(Number(value))) {
100
- formatted.query[key] = Number(value);
101
- } else {
102
- formatted.query[key] = value;
103
- }
104
- } else if (bodyProps[key]) {
105
- // body 字段(无冲突,直接用原 key)
106
- if (!formatted.data) formatted.data = {};
107
- if (Array.isArray(value)) {
108
- formatted.data[key] = value;
109
- } else if (bodyProps[key].type === 'string') {
110
- // schema 定义为 string 类型时,保持原始字符串,不做 JSON 解析
111
- // 避免纯数字字符串(如 "123")被转为 number
112
- formatted.data[key] = value;
113
- } else {
114
- formatted.data[key] = (0, _parsers.tryParseJSON)(value);
115
- }
116
- } else {
117
- // 处理 d- 前缀(冲突时 CLI 以 d- 前缀传入)
118
- const stripped = key.startsWith('d-') || key.startsWith('d_') ? key.replace(/^d[-_]/, '') : key;
119
-
120
- // 尝试匹配嵌套对象展开字段(通过预建映射表,兼容 Commander camelCase)
121
- const nestedMapping = nestedFieldMap.get(stripped);
122
- // 尝试匹配 array<object> 展开字段
123
- const arrayMatch = (0, _matchArrayObjectField.matchArrayObjectField)(stripped, bodyProps);
124
- if (nestedMapping) {
125
- // 匹配到嵌套对象叶子字段
126
- const {
127
- pathKeys,
128
- leafSchema
129
- } = nestedMapping;
130
- const rootKey = pathKeys[0];
131
- if (!nestedObjectCollector[rootKey]) {
132
- nestedObjectCollector[rootKey] = {};
133
- }
134
- // 类型转换
135
- let convertedValue = value;
136
- if (leafSchema?.type === 'number' && typeof value === 'string' && !isNaN(Number(value))) {
137
- convertedValue = Number(value);
138
- } else if (leafSchema?.type === 'boolean') {
139
- convertedValue = value === 'true' || value === true;
140
- } else if (typeof value === 'string') {
141
- convertedValue = (0, _parsers.tryParseJSON)(value);
142
- }
143
- // 用 pathKeys(去掉根 key)深度设置到 collector 中
144
- const subPathKeys = pathKeys.slice(1);
145
- let target = nestedObjectCollector[rootKey];
146
- for (let pi = 0; pi < subPathKeys.length - 1; pi++) {
147
- if (!target[subPathKeys[pi]] || typeof target[subPathKeys[pi]] !== 'object') {
148
- target[subPathKeys[pi]] = {};
149
- }
150
- target = target[subPathKeys[pi]];
151
- }
152
- target[subPathKeys[subPathKeys.length - 1]] = convertedValue;
153
- } else if (arrayMatch) {
154
- // 收集到 collector 中,后续统一 zip
155
- if (!arrayObjectCollector[arrayMatch.arrayKey]) {
156
- arrayObjectCollector[arrayMatch.arrayKey] = {};
157
- }
158
- const vals = Array.isArray(value) ? value : [value];
159
- arrayObjectCollector[arrayMatch.arrayKey][arrayMatch.subKey] = vals;
160
- } else if (key.startsWith('d-') || key.startsWith('d_')) {
161
- // body 字段(有冲突,CLI 中以 d- 或 d_ 前缀传入)
162
- const originalKey = key.replace(/^d[-_]/, '');
163
- if (bodyProps[originalKey]) {
164
- if (!formatted.data) formatted.data = {};
165
- formatted.data[originalKey] = Array.isArray(value) ? value : (0, _parsers.tryParseJSON)(value);
166
- } else {
167
- if (!formatted.query) formatted.query = {};
168
- formatted.query[key] = typeof value === 'string' && !isNaN(Number(value)) ? Number(value) : value;
169
- }
170
- } else {
171
- // 未知参数,尝试放入 query(兼容)
172
- if (!formatted.query) formatted.query = {};
173
- formatted.query[key] = typeof value === 'string' && !isNaN(Number(value)) ? Number(value) : value;
174
- }
175
- }
176
- }
177
-
178
- // 将 array<object> 展开字段按索引 zip 合并为对象数组
179
- for (const [arrayKey, subProps] of Object.entries(arrayObjectCollector)) {
180
- if (!formatted.data) formatted.data = {};
181
- const subKeys = Object.keys(subProps);
182
- const maxLen = Math.max(...subKeys.map(k => subProps[k].length));
183
- const result = [];
184
- for (let i = 0; i < maxLen; i++) {
185
- const obj = {};
186
- for (const subKey of subKeys) {
187
- const val = subProps[subKey][i];
188
- if (val !== undefined) {
189
- const itemProp = bodyProps[arrayKey]?.items?.properties?.[subKey];
190
- if (itemProp?.type === 'number' && !isNaN(Number(val))) {
191
- obj[subKey] = Number(val);
192
- } else {
193
- obj[subKey] = val;
194
- }
195
- }
196
- }
197
- result.push(obj);
198
- }
199
- formatted.data[arrayKey] = result;
200
- }
201
-
202
- // 将嵌套对象展开字段组装回嵌套结构
203
- for (const [objectKey, subProps] of Object.entries(nestedObjectCollector)) {
204
- if (!formatted.data) formatted.data = {};
205
- formatted.data[objectKey] = subProps;
206
- }
207
- }
208
-
209
- // 当没有传递 query 时,要判断当前 tool 是否支持 query
210
- if (formatted.query === undefined) {
211
- const paramDefs2 = getToolParamDefs(formatted.module, formatted.tool);
212
- if (paramDefs2?.query) {
213
- formatted.query = {};
214
- }
215
- }
216
- return formatted;
217
- }
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.helpData = void 0;
7
- var _fs = _interopRequireDefault(require("fs"));
8
- var _path = _interopRequireDefault(require("path"));
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- const helpFileContent = _fs.default.readFileSync(_path.default.join(__dirname, '../help.json'), 'utf8');
11
- if (!helpFileContent) {
12
- console.error('help.json not found');
13
- process.exit(1);
14
- }
15
- const helpData = exports.helpData = JSON.parse(helpFileContent);
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.keyMappingData = void 0;
7
- var _fs = _interopRequireDefault(require("fs"));
8
- var _path = _interopRequireDefault(require("path"));
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- const keyMappingFilePath = _path.default.join(__dirname, '../key-mapping.json');
11
- let data = {};
12
- if (_fs.default.existsSync(keyMappingFilePath)) {
13
- const content = _fs.default.readFileSync(keyMappingFilePath, 'utf8');
14
- data = JSON.parse(content);
15
- }
16
- const keyMappingData = exports.keyMappingData = data;
@@ -1,126 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.parseUnknownOptions = parseUnknownOptions;
7
- exports.tryParseJSON = tryParseJSON;
8
- exports.tryReadFileRef = tryReadFileRef;
9
- var _fs = _interopRequireDefault(require("fs"));
10
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
- /**
12
- * 尝试解析JSON字符串
13
- * 先将真实控制字符转义为 JSON 合法形式,处理 shell/AI 传入的原始换行等情况
14
- * @param str 要解析的字符串
15
- * @returns 解析后的对象或原始字符串
16
- */
17
- function tryParseJSON(str) {
18
- if (typeof str !== 'string') return str;
19
- const escaped = str.replace(/[\x00-\x1F\x7F]/g, ch => {
20
- const map = {
21
- '\n': '\\n',
22
- '\r': '\\r',
23
- '\t': '\\t',
24
- '\b': '\\b',
25
- '\f': '\\f'
26
- };
27
- return map[ch] || '\\u' + ch.charCodeAt(0).toString(16).padStart(4, '0');
28
- });
29
- try {
30
- return JSON.parse(escaped);
31
- } catch (error) {
32
- return str;
33
- }
34
- }
35
-
36
- /**
37
- * 尝试从文件引用或 stdin 读取内容(类似 curl 的 @file / @- 语法)
38
- */
39
- function tryReadFileRef(str) {
40
- if (typeof str !== 'string' || !str.startsWith('@')) return str;
41
- const ref = str.slice(1);
42
-
43
- // @- 表示从 stdin 读取
44
- if (ref === '-') {
45
- try {
46
- return _fs.default.readFileSync(0, 'utf8').trim();
47
- } catch (e) {
48
- console.error('从 stdin 读取失败:', e.message);
49
- return str;
50
- }
51
- }
52
-
53
- // @/path/to/file 表示从文件读取
54
- if (_fs.default.existsSync(ref)) {
55
- return _fs.default.readFileSync(ref, 'utf8').trim();
56
- }
57
- return str;
58
- }
59
-
60
- /**
61
- * 解析 commander 未识别的未知选项
62
- * commander 开启 allowUnknownOption 后,未知选项会残留在 process.argv 中,
63
- * 这里手动解析它们,行为与原 parseArguments 对未知 --key value 的处理一致。
64
- */
65
- function parseUnknownOptions(params) {
66
- const knownLongOptions = new Set(['help', 'short', 'verbose', 'path', 'query', 'data']);
67
-
68
- // 获取 commander 解析后的剩余参数(未被识别的选项)
69
- const rawArgs = process.argv.slice(2);
70
- // 跳过已被 commander 消费的位置参数和已知选项,手动扫描未知选项
71
- let i = 0;
72
- while (i < rawArgs.length) {
73
- const arg = rawArgs[i];
74
- if (arg.startsWith('--')) {
75
- const fullKey = arg.slice(2);
76
-
77
- // 处理 --key=value 格式
78
- if (fullKey.includes('=')) {
79
- const [key, ...valueParts] = fullKey.split('=');
80
- if (!knownLongOptions.has(key) && key !== 'module' && key !== 'tool') {
81
- params[key] = valueParts.join('=');
82
- }
83
- i++;
84
- continue;
85
- }
86
- const key = fullKey;
87
-
88
- // 已知选项跳过(commander 已处理)
89
- if (knownLongOptions.has(key)) {
90
- // 如果是带值的已知选项(path/query/data),跳过值
91
- if (['path', 'query', 'data'].includes(key) && i + 1 < rawArgs.length && !rawArgs[i + 1].startsWith('-')) {
92
- i += 2;
93
- } else {
94
- i++;
95
- }
96
- continue;
97
- }
98
-
99
- // 未知选项:检查下一个参数是否是值
100
- const nextArg = rawArgs[i + 1];
101
- const isNextArgValue = i + 1 < rawArgs.length && !nextArg.startsWith('--') && !nextArg.startsWith('-');
102
- if (isNextArgValue) {
103
- params[key] = nextArg;
104
- i += 2;
105
- } else {
106
- params[key] = true;
107
- i++;
108
- }
109
- } else if (arg.startsWith('-') && arg.length > 1 && !/^-?\d+$/.test(arg)) {
110
- // 短参数
111
- const key = arg.slice(1);
112
- const nextArg = rawArgs[i + 1];
113
- const isNextArgValue = i + 1 < rawArgs.length && !nextArg.startsWith('--') && !nextArg.startsWith('-');
114
- if (isNextArgValue && key.length === 1) {
115
- params[key] = nextArg;
116
- i += 2;
117
- } else {
118
- params[key] = true;
119
- i++;
120
- }
121
- } else {
122
- // 位置参数,跳过(已被 commander 消费)
123
- i++;
124
- }
125
- }
126
- }
@@ -1,24 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.printJSON = printJSON;
7
- var _toon = require("@toon-format/toon");
8
- /**
9
- * 将数据转换为格式化的 JSON 字符串
10
- * @param data 要序列化的数据(可以是任意类型)缩进)
11
- * @param verbose 是否 verbose 模式
12
- * @returns 格式化的 JSON 字符串,如果序列化失败则返回错误信息的 JSON
13
- */
14
- function printJSON(data, verbose = false) {
15
- if (verbose) {
16
- return JSON.stringify(data, null, 2);
17
- } else {
18
- try {
19
- return (0, _toon.encode)(data);
20
- } catch (e) {
21
- return JSON.stringify(data);
22
- }
23
- }
24
- }
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.registerFallbackAction = registerFallbackAction;
7
- var _executeAction = require("./execute-action");
8
- /**
9
- * 顶层 fallback action:处理快捷命令等未被子命令匹配的情况
10
- */
11
- function registerFallbackAction(program) {
12
- program.argument('[module]', '模块名称').argument('[tool]', '工具名称').action(async (moduleArg, toolArg, opts) => {
13
- await (0, _executeAction.executeAction)(moduleArg, toolArg, opts, program);
14
- });
15
- }
@@ -1,121 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.registerModuleCommands = registerModuleCommands;
7
- var _commander = require("commander");
8
- var _trimSummary = require("./trim-summary");
9
- var _helpData = require("./help-data");
10
- var _flatOptionsData = require("./flat-options-data");
11
- var _executeAction = require("./execute-action");
12
- var _shortcuts = require("../shortcuts");
13
- /**
14
- * 为单个 tool 子命令注册 Commander options
15
- * 直接读取预生成的 flat-options.json 中已扁平化的参数定义,无需运行时计算
16
- */
17
- function registerToolOptions(toolCmd, moduleName, toolName, skipPathMandatory) {
18
- const moduleOptions = _flatOptionsData.flatOptionsData[moduleName];
19
- if (!moduleOptions) return;
20
- const toolOptions = moduleOptions[toolName];
21
- if (!toolOptions) return;
22
- for (const [, opt] of Object.entries(toolOptions)) {
23
- // 快捷命令跳过 path 参数(由环境变量自动注入)
24
- if (skipPathMandatory && opt.source === 'path') continue;
25
- const option = new _commander.Option(`--${opt.optKey} ${opt.valuePlaceholder}`.trim(), opt.description);
26
- if (opt.required && opt.source === 'path' && !skipPathMandatory) {
27
- option.makeOptionMandatory();
28
- }
29
- if (opt.choices) {
30
- option.choices(opt.choices);
31
- }
32
- if (opt.defaultValue !== undefined) {
33
- option.default(opt.defaultValue);
34
- }
35
- if (opt.required && opt.source === 'body' && !opt.isArrayObject && !opt.isNestedField) {
36
- option.makeOptionMandatory();
37
- }
38
- if (opt.isArray || opt.isArrayObject) {
39
- option.argParser((val, prev) => {
40
- return (prev || []).concat(val);
41
- });
42
- option.default(undefined);
43
- }
44
- toolCmd.addOption(option);
45
- }
46
- }
47
-
48
- /**
49
- * 获取指定模块的快捷命令列表
50
- */
51
- function getShortcutsForModule(moduleName) {
52
- if (moduleName === 'issues') return _shortcuts.ISSUE_SHORTCUTS;
53
- if (moduleName === 'pulls') return _shortcuts.PR_SHORTCUTS;
54
- return [];
55
- }
56
-
57
- /**
58
- * 为每个模块注册子命令,使 cnb <module> -h 显示模块帮助
59
- * 每个 tool 的参数通过 Commander 的 option 系统注册,--help 自动展示参数说明
60
- */
61
- function registerModuleCommands(program) {
62
- const modulesHelp = _helpData.helpData.modulesHelp || {};
63
- for (const [moduleName, tools] of Object.entries(modulesHelp)) {
64
- const toolEntries = Object.values(tools);
65
- const sub = program.command(moduleName).description(`${moduleName} 模块 (${toolEntries.length} tools)`).helpOption('-h, --help', '显示帮助文档');
66
-
67
- // 收集已注册的子命令名,避免快捷命令与真实 tool 名冲突
68
- const registeredNames = new Set();
69
-
70
- // 为每个 tool 注册子命令
71
- for (const [toolName, toolInfo] of Object.entries(tools)) {
72
- const summary = (0, _trimSummary.trimSummary)(toolInfo.summary || '');
73
- const toolCmd = sub.command(toolName).description(summary).option('-v, --verbose', '输出完整原始响应').helpOption('-h, --help', '显示帮助文档').showHelpAfterError(true).action(async opts => {
74
- await (0, _executeAction.executeAction)(moduleName, toolName, opts, sub);
75
- });
76
-
77
- // 添加权限信息到帮助文本
78
- if (toolInfo.permission) {
79
- toolCmd.addHelpText('before', `权限: ${toolInfo.permission}\n`);
80
- }
81
-
82
- // 根据 flat-options.json 注册参数选项
83
- registerToolOptions(toolCmd, moduleName, toolName);
84
- registeredNames.add(toolName);
85
- }
86
-
87
- // 为快捷命令注册 Commander 子命令(别名)
88
- // 复用对应真实 tool 的选项定义,但跳过 path 参数(由环境变量自动注入)
89
- const shortcuts = getShortcutsForModule(moduleName);
90
- for (const shortcut of shortcuts) {
91
- // 如果快捷命令名与真实 tool 名重复,跳过(真实 tool 优先)
92
- if (registeredNames.has(shortcut.shortName)) continue;
93
- const shortcutCmd = sub.command(shortcut.shortName).description(`[快捷] ${shortcut.description}`).option('-v, --verbose', '输出完整原始响应').helpOption('-h, --help', '显示帮助文档').showHelpAfterError(true).action(async opts => {
94
- // 传入快捷命令名,executeAction 内部会通过 resolveShortcut 转换
95
- await (0, _executeAction.executeAction)(moduleName, shortcut.shortName, opts, sub);
96
- });
97
-
98
- // 上传命令只需要 --file,不复用真实 tool 的 body 选项(name/size/content_type 由上传流程自动获取)
99
- if (!shortcut.upload) {
100
- registerToolOptions(shortcutCmd, moduleName, shortcut.realTool, true);
101
- } else {
102
- shortcutCmd.requiredOption('--file <string>', '本地文件路径。必填。');
103
- }
104
- registeredNames.add(shortcut.shortName);
105
- }
106
-
107
- // 允许未匹配到子命令时也能正常执行(不报错)
108
- sub.allowUnknownOption();
109
- sub.allowExcessArguments();
110
-
111
- // 模块级 action:处理未匹配到子命令的情况
112
- sub.argument('[tool]', '工具名称(支持快捷命令)').action(async (toolArg, _opts) => {
113
- if (toolArg) {
114
- // 此处已不会再命中快捷命令(因为已注册为子命令),仅作兜底
115
- sub.help();
116
- return;
117
- }
118
- sub.help();
119
- });
120
- }
121
- }