@cnbcool/cnb-api-generate 2.17.2 → 2.17.4

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.
@@ -42,7 +42,6 @@ const babel = __importStar(require("@babel/core"));
42
42
  const fs = __importStar(require("fs"));
43
43
  const path = __importStar(require("path"));
44
44
  const debug_1 = __importDefault(require("debug"));
45
- const skills_1 = require("../../constants/skills");
46
45
  const logger = (0, debug_1.default)('csg:babel');
47
46
  function transformTsToJs(tsCode, filename) {
48
47
  return babel.transformSync(tsCode, {
@@ -55,7 +54,7 @@ function transformTsToJs(tsCode, filename) {
55
54
  [
56
55
  'transform-define',
57
56
  {
58
- 'process.env.CNB_CLI_CMD': process.env.CNB_CLI_CMD || skills_1.CNB_CLI_PROD_CMD,
57
+ 'process.env.CNB_CLI_CMD': process.env.CNB_CLI_CMD || 'cnb',
59
58
  },
60
59
  ],
61
60
  ],
@@ -8,8 +8,6 @@ const debug_1 = __importDefault(require("debug"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const get_skills_codegen_target_1 = require("../utils/get-skills-codegen-target");
11
- const get_config_1 = require("../utils/get-config");
12
- const skills_1 = require("../constants/skills");
13
11
  const render_quick_commands_md_1 = require("./render-quick-commands-md");
14
12
  const logger = (0, debug_1.default)('csg:skills');
15
13
  async function generateSkillMd(
@@ -24,17 +22,13 @@ _requestMap, quickCommandsByModule) {
24
22
  const SKILL_MD = path_1.default.join(skillMDTempateDir, 'SKILL.md');
25
23
  const targetSkillMD = path_1.default.join(skillMDDir, 'SKILL.md');
26
24
  const skillMDContent = fs_1.default.readFileSync(SKILL_MD, 'utf8');
27
- const config = (0, get_config_1.getConfig)();
28
- const cliCmd = config.skillsDev ? skills_1.CNB_CLI_DEV_CMD : skills_1.CNB_CLI_PROD_CMD;
29
- // 仅替换两类占位符:
30
- // - <$QUICK_COMMANDS$>:配置驱动的快捷命令清单(标题/使用约定写在模板里即可)
31
- // - <$CNB_CLI_CMD$> :CLI 命令前缀
32
- const quickCommandsMd = (0, render_quick_commands_md_1.renderQuickCommandsMarkdown)(quickCommandsByModule, cliCmd);
25
+ // 仅替换 <$QUICK_COMMANDS$>:配置驱动的快捷命令清单。
26
+ // 模板正文、渲染出的快捷命令前缀都写死 `cnb`(skillsDev 只影响产物运行时的 CNB_CLI_CMD),
27
+ // 不在这里传 CLI 前缀,避免同一份 SKILL.md 出现两种命令写法。
28
+ const quickCommandsMd = (0, render_quick_commands_md_1.renderQuickCommandsMarkdown)(quickCommandsByModule);
33
29
  const total = Object.values(quickCommandsByModule).reduce((n, arr) => n + arr.length, 0);
34
30
  logger(`quick commands rendered: ${Object.keys(quickCommandsByModule).length} modules, ${total} commands`);
35
- const newSkillMDContent = skillMDContent
36
- .replaceAll('<$QUICK_COMMANDS$>', quickCommandsMd)
37
- .replaceAll('<$CNB_CLI_CMD$>', cliCmd);
31
+ const newSkillMDContent = skillMDContent.replaceAll('<$QUICK_COMMANDS$>', quickCommandsMd);
38
32
  fs_1.default.writeFileSync(targetSkillMD, newSkillMDContent);
39
33
  logger(`generate ${SKILL_MD} to ${targetSkillMD}`);
40
34
  }
@@ -9,10 +9,11 @@ exports.renderQuickCommandsMarkdown = renderQuickCommandsMarkdown;
9
9
  *
10
10
  * 渲染规则:
11
11
  * - 每个模块先输出 `<moduleName>:` 标题行
12
- * - 每条命令渲染为 ``- `<cliCmd> <module> <shortName>[ <dataTip>]` — <description>`` 的列表项
12
+ * - 每条命令渲染为 ``- `cnb <module> <shortName>[ <dataTip>]` — <description>`` 的列表项。
13
+ * 前缀固定为 `cnb`,与模板正文写法一致,不跟随 skillsDev 切换
13
14
  * - 没有任何模块/命令时返回空字符串(让模板里的占位符消失)
14
15
  */
15
- function renderQuickCommandsMarkdown(byModule, cliCmd) {
16
+ function renderQuickCommandsMarkdown(byModule) {
16
17
  const moduleNames = Object.keys(byModule);
17
18
  if (!moduleNames.length)
18
19
  return '';
@@ -25,7 +26,7 @@ function renderQuickCommandsMarkdown(byModule, cliCmd) {
25
26
  lines.push(`${moduleName}:`);
26
27
  for (const cmd of items) {
27
28
  const dataPart = cmd.dataTip ? ` ${cmd.dataTip}` : '';
28
- const cmdLine = `${cliCmd} ${moduleName} ${cmd.shortName}${dataPart}`;
29
+ const cmdLine = `cnb ${moduleName} ${cmd.shortName}${dataPart}`;
29
30
  lines.push(`- \`${cmdLine}\` — ${cmd.description}`);
30
31
  }
31
32
  lines.push('');
@@ -9,7 +9,6 @@ const fs_1 = __importDefault(require("fs"));
9
9
  const lodash_1 = __importDefault(require("lodash"));
10
10
  const constants_1 = require("../constants");
11
11
  const is_skills_1 = require("./is-skills");
12
- const skills_1 = require("../constants/skills");
13
12
  const defaultConfig = {
14
13
  printeReduxAction: true,
15
14
  requestDependencyMap: {
@@ -61,8 +60,8 @@ function getConfig() {
61
60
  : {});
62
61
  if (globalConfig && !process.env.CNB_CLI_CMD) {
63
62
  process.env.CNB_CLI_CMD = globalConfig.skillsDev
64
- ? skills_1.CNB_CLI_DEV_CMD
65
- : skills_1.CNB_CLI_PROD_CMD;
63
+ ? 'node scripts/core/index.js'
64
+ : 'cnb';
66
65
  }
67
66
  return globalConfig;
68
67
  }
@@ -1,3 +1,5 @@
1
+ import { resolveApiDomain } from '../utils/resolve-api-domain';
2
+
1
3
  /**
2
4
  * 把文本里的相对路径转换为绝对路径
3
5
  * 与 src/helpers/convertLink.ts 逻辑一致
@@ -110,8 +112,7 @@ export default function convertLink(text: string, repoSlug: string | undefined,
110
112
  * @returns {String}
111
113
  */
112
114
  function normaliseLink(link: string, repoSlug: string | undefined, branchOrSha = 'HEAD') {
113
- const { CNB_WEB_ENDPOINT } = process.env;
114
- const baseURL = `${CNB_WEB_ENDPOINT}/${repoSlug}`;
115
+ const baseURL = `${resolveWebEndpoint()}/${repoSlug}`;
115
116
 
116
117
  if (link.startsWith('/-/')) {
117
118
  return `${baseURL}${link}`;
@@ -137,3 +138,31 @@ function normaliseLink(link: string, repoSlug: string | undefined, branchOrSha =
137
138
 
138
139
  return link;
139
140
  }
141
+
142
+ /**
143
+ * 解析 Web 平台地址(形如 https://cnb.cool)。
144
+ *
145
+ * 直接以 CLI 实际调用的 OpenAPI 地址(resolveApiDomain)为准,
146
+ * 去掉主机名的 `api.` 前缀即得到 Web 地址;保留协议与自定义端口。
147
+ * 不再依赖 CNB_WEB_ENDPOINT 环境变量。
148
+ *
149
+ * @returns {String} Web 平台地址,解析失败返回空字符串
150
+ */
151
+ function resolveWebEndpoint(): string {
152
+ let apiDomain = '';
153
+ try {
154
+ apiDomain = resolveApiDomain() || '';
155
+ } catch {
156
+ return '';
157
+ }
158
+ const trimmed = String(apiDomain).trim();
159
+ if (!trimmed) return '';
160
+ try {
161
+ const u = new URL(trimmed);
162
+ // 只去掉主机名的 api. 前缀,保留协议与自定义端口
163
+ return `${u.protocol}//${u.host.replace(/^api\./, '')}`;
164
+ } catch {
165
+ // 非法地址:按纯主机名处理,仍尝试去 api. 前缀
166
+ return trimmed.replace(/^api\./, '');
167
+ }
168
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnbcool/cnb-api-generate",
3
- "version": "2.17.2",
3
+ "version": "2.17.4",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -11,26 +11,37 @@ description: CNB 平台交互命令,支持代码仓库、Issue、PR、CI、制
11
11
 
12
12
  ## 使用约定
13
13
 
14
- - **编号自识别**:Issue/PR 编号自动从环境变量识别,无需额外传递。
15
- - **可显式覆盖**:`--repo <组织/仓库>` 与 `--number <编号>` 均为可选参数。
16
- 不传时沿用环境变量(即当前仓库的当前 Issue/PR);传入时覆盖环境变量,
17
- 可用于跨仓库或跨编号场景,例如 `cnb pulls get --repo x/y --number 97`、
18
- `cnb issues get --number 68`。两个参数相互独立,可只传其中一个。
19
14
  - **默认摘要省流**:默认精简输出,加 `--verbose` 输出完整数据。
20
15
  - **多行用单引号**:bash 参数为多行文本时用单引号,降低命令注入风险。
21
- - **提及不召唤**:评论中直接 @npc 会召唤 npc;仅提及不召唤时,用反引号包裹 `@npc`。
22
- NPC 的触发条件见 `cnb-npc-search`,单独 `@仓库名` 不会触发。
23
16
 
24
- ## 取正文里的图片与附件
17
+ ## issue 访问
25
18
 
26
- 评论正文里的图片、附件 URL,一律用 `get-imgs` / `get-files` 取回,URL 从 `list-comments` 原文取:
19
+ 编号同 `--repo` 一样默认从环境变量识别,无需传递;用 `--repo <组织/仓库>`、`--number <编号>`
20
+ 可覆盖,例:`cnb issues get --number 68`。
27
21
 
28
- - **参数取 URL 的尾段,且必须含数字 ID 段**:图片取 `/imgs/<issues|pulls>/` 之后的**完整尾段**,
29
- 附件取 `/files/<issues|pulls>/` 之后的完整尾段;尾段首位的数字 ID 段不能省,漏掉会返回 `404 Resource not found`:
30
- - 原文 `.../-/imgs/issues/2101868753481326592/Lma2nfHjnchalMtHZcq4OA/6d7ab033-xxxx.png`
31
- - 正确 `2101868753481326592/Lma2nfHjnchalMtHZcq4OA/6d7ab033-xxxx.png` ✅
32
- - 漏 ID `Lma2nfHjnchalMtHZcq4OA/6d7ab033-xxxx.png` ❌ `404`
33
- - **CLI 内置鉴权**:手写 `Authorization: Bearer` 或 `token` 都不被识别,只会拿到 400
22
+ - `cnb issues get-imgs --img-path 图片路径` — 取正文里的图片
23
+ - `cnb issues get-files --file-path 附件路径` — 取正文里的附件
24
+
25
+ `--img-path` / `--file-path` 取 URL 的**完整尾段**,首位数字 ID 段不能省,漏掉返回 `404`:
26
+
27
+ - 原文 `.../-/imgs/issues/2101868753481326592/Lma2nfHjnchalMtHZcq4OA/6d7ab033-xxxx.png`
28
+ - 正确 `2101868753481326592/Lma2nfHjnchalMtHZcq4OA/6d7ab033-xxxx.png` ✅
29
+ - 漏 ID `Lma2nfHjnchalMtHZcq4OA/6d7ab033-xxxx.png` ❌ `404`
30
+
31
+ 鉴权由 `@cnbcool/cnb-cli`(命令 `cnb`)内置,执行命令即带登录态,
32
+ `get-imgs` / `get-files` 无需自己传鉴权头。
33
+
34
+ ## pull 访问
35
+
36
+ 编号同 `--repo` 一样默认从环境变量识别,无需传递;用 `--repo <组织/仓库>`、`--number <编号>`
37
+ 可覆盖,例:`cnb pulls get --repo x/y --number 97`。
38
+
39
+ - `cnb pulls get-imgs --img-path 图片路径` — 取正文里的图片
40
+ - `cnb pulls get-files --file-path 附件路径` — 取正文里的附件
41
+
42
+ `--img-path` / `--file-path` 与 issue 同规则:取 URL 的**完整尾段**,首位数字 ID 段不能省,
43
+ 漏掉返回 `404`(原文形如 `.../-/imgs/pulls/<数字 ID>/<key>/<file>`)。
44
+ 鉴权同样由 `cnb` 命令内置的登录态提供,无需自己传鉴权头。
34
45
 
35
46
  ## 常用链接
36
47
 
@@ -40,6 +51,6 @@ description: CNB 平台交互命令,支持代码仓库、Issue、PR、CI、制
40
51
 
41
52
  ## 更多 API
42
53
 
43
- 1. `<$CNB_CLI_CMD$> --help` 查看所有模块
44
- 2. `<$CNB_CLI_CMD$> <module> --help` 查看模块下的工具列表
45
- 3. `<$CNB_CLI_CMD$> <module> <tool> --help` 查看工具参数
54
+ 1. `cnb --help` 查看所有模块
55
+ 2. `cnb <module> --help` 查看模块下的工具列表
56
+ 3. `cnb <module> <tool> --help` 查看工具参数
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CNB_CLI_PROD_CMD = exports.CNB_CLI_DEV_CMD = void 0;
4
- exports.CNB_CLI_DEV_CMD = 'node scripts/core/index.js';
5
- exports.CNB_CLI_PROD_CMD = 'cnb';