@cnbcool/cnb-cli 1.21.1 → 1.21.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.
- package/dist/index.js +2 -2
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -142,7 +142,7 @@ PR
|
|
|
142
142
|
1. 先调 cnb_status 检查登录(传 cwd = 工作区路径,平台按该目录的 git remote 推断);未登录调 cnb_login,把返回的授权链接发给用户,用户授权后再调 cnb_status 确认。
|
|
143
143
|
2. 所有 \`cnb ...\` 命令都通过 cnb_run 执行,不要在 shell 里执行 cnb,也不要安装 cli;同样传 cwd,仓库和平台都会自动推断。
|
|
144
144
|
3. cnb_run 的工具描述里有常用命令速查,照着直接调用;速查没有的命令再用 --help 查。`,Yi=(e,i=!1)=>({content:[{type:"text",text:e}],...i?{isError:!0}:{}});function Uj(e){if(!e.length)return'args 不能为空,可先传 ["--help"] 查看可用命令。';let i=e.find((r)=>!r.startsWith("-"));if(i&&_j[i])return _j[i];return null}function jge(e){return e.filter((i)=>!i.startsWith("-"))}function Ij(e,i){let r=mge[e];if(!r||!i)return!1;if(r[i])return Boolean(r[i].repo);let n=(Uge[e]||[]).find((t)=>t.shortName===i);if(!n)return!1;if(n.realTool.startsWith("__"))return!0;return Boolean(r[n.realTool]?.repo)}function Jge(){let e=process.argv[1];if(!e||/[\\/]\$bunfs[\\/]|~BUN/.test(e))return[process.execPath];return[process.execPath,e]}function rP(e){if(!e||typeof e!=="string")return null;let i,r=e.trim();if(/(^|\/)\.\.(\/|$)/.test(r))return null;if(/^[^@/]+@[^:/]+:/.test(r)&&!/^[a-z]+:\/\//i.test(r))i=r.slice(r.indexOf(":")+1);else{let t;try{t=new URL(r)}catch{return null}if(!/^(https?|ssh)$/i.test(t.protocol.replace(":","")))return null;i=t.pathname}if(/(^|\/)\.\.(\/|$)/.test(i))return null;i=i.replace(/^\/+/,"").replace(/\/+$/,"").replace(/\.git$/i,"");let n=i.split("/");if(n.length<2)return null;if(!n.every((t)=>/^[\w.-]+$/.test(t)&&t!=="."&&t!==".."))return null;return n.join("/")}var eP=new Map;async function Dge(e){if(eP.has(e))return eP.get(e);let i=(...n)=>new Promise((t)=>{(0,mj.execFile)("git",n,{cwd:e,timeout:5000,encoding:"utf8"},(p,y)=>t(p?"":String(y).trim()))}),r=rP(await i("config","--get","remote.origin.url"));if(!r){let t=(await i("remote","-v")).split(`
|
|
145
|
-
`).map((p)=>p.split(/\s+/)[1]).find(Boolean);r=rP(t)}return eP.set(e,r),r}function Mge(e,{cwd:i,env:r}){let[n,...t]=Jge();return new Promise((p)=>{let y="",d=!1,l=(a)=>{if(d)return;if(y+=a,y.length>iP)y=y.slice(0,iP),d=!0},c=(0,mj.spawn)(n,[...t,...e],{cwd:i,env:r,stdio:["ignore","pipe","pipe"],windowsHide:!0}),u=setTimeout(()=>c.kill("SIGTERM"),Nge);c.stdout.setEncoding("utf8").on("data",l),c.stderr.setEncoding("utf8").on("data",l),c.on("error",(a)=>{clearTimeout(u),p({code:-1,out:`启动 cnb 失败:${a.message}`,truncated:d})}),c.on("close",(a,g)=>{clearTimeout(u),p({code:a??-1,signal:g,out:y.trim(),truncated:d})})})}function Xge(e,i,r){let n={access_token:r.access_token,expires_at:Math.floor(Date.now()/1000)+r.expires_in,platform_url:e,client_id:i,login_host:`https://api.${new URL(e).host}`};if(r.refresh_token)n.refresh_token=r.refresh_token;(0,gb.saveToken)(n)}function Nj(e,i){let r=process.cwd();try{process.chdir(i)}catch{return(0,wj.resolvePlatformUrl)(e)}try{return(0,wj.resolvePlatformUrl)(e)}finally{process.chdir(r)}}function Wj(e={}){let i=new Map,r=async(c)=>{if(!i.has(c))i.set(c,Nj(e.host,c));return i.get(c)},n=process.env.OAUTH2_CLIENT_ID||"cnb_cli",t=null,p=null,y=new Age.McpServer({name:"cnb",version:"1.21.
|
|
145
|
+
`).map((p)=>p.split(/\s+/)[1]).find(Boolean);r=rP(t)}return eP.set(e,r),r}function Mge(e,{cwd:i,env:r}){let[n,...t]=Jge();return new Promise((p)=>{let y="",d=!1,l=(a)=>{if(d)return;if(y+=a,y.length>iP)y=y.slice(0,iP),d=!0},c=(0,mj.spawn)(n,[...t,...e],{cwd:i,env:r,stdio:["ignore","pipe","pipe"],windowsHide:!0}),u=setTimeout(()=>c.kill("SIGTERM"),Nge);c.stdout.setEncoding("utf8").on("data",l),c.stderr.setEncoding("utf8").on("data",l),c.on("error",(a)=>{clearTimeout(u),p({code:-1,out:`启动 cnb 失败:${a.message}`,truncated:d})}),c.on("close",(a,g)=>{clearTimeout(u),p({code:a??-1,signal:g,out:y.trim(),truncated:d})})})}function Xge(e,i,r){let n={access_token:r.access_token,expires_at:Math.floor(Date.now()/1000)+r.expires_in,platform_url:e,client_id:i,login_host:`https://api.${new URL(e).host}`};if(r.refresh_token)n.refresh_token=r.refresh_token;(0,gb.saveToken)(n)}function Nj(e,i){let r=process.cwd();try{process.chdir(i)}catch{return(0,wj.resolvePlatformUrl)(e)}try{return(0,wj.resolvePlatformUrl)(e)}finally{process.chdir(r)}}function Wj(e={}){let i=new Map,r=async(c)=>{if(!i.has(c))i.set(c,Nj(e.host,c));return i.get(c)},n=process.env.OAUTH2_CLIENT_ID||"cnb_cli",t=null,p=null,y=new Age.McpServer({name:"cnb",version:"1.21.2"},{instructions:Oge});y.registerTool("cnb_run",{title:"执行 cnb 命令",description:Wge,inputSchema:{args:bb.z.array(bb.z.string()).describe("`cnb` 之后的参数数组"),cwd:bb.z.string().optional().describe("执行目录(绝对路径),传工作区路径:用于按 git remote 推断仓库(可省略 --repo)与平台,并解析 --file、--body-file 等相对路径")}},async({args:c,cwd:u})=>{let a=Uj(c);if(a)return Yi(a,!0);if(u&&!$ge.default.statSync(u,{throwIfNoEntry:!1})?.isDirectory())return Yi(`cwd 不存在或不是目录:${u}`,!0);let g=u||process.cwd(),s=await r(g),$={...process.env,CNB_API_ENDPOINT:`https://api.${new URL(s).host}`},f=[...c];if(!$.CNB_REPO_SLUG&&!c.includes("--repo")){let[m,U]=jge(c);if(Ij(m,U)){let O=await Dge(g);if(!O)return Yi(`当前目录(${g})不是 git 仓库,无法推断仓库,请显式传 --repo 组织/仓库。`,!0);$.CNB_REPO_SLUG=O,f.push("--repo",O)}}let A=await Mge(f,{cwd:g,env:$}),_=A.out||"(无输出)";if(A.truncated)_+=`
|
|
146
146
|
|
|
147
147
|
[输出超过 ${iP/1024}KB 已截断,可去掉 --verbose 或分页查询]`;if(A.code!==0)return Yi(`${_}
|
|
148
148
|
|
|
@@ -417,4 +417,4 @@ PR 页面路径: ${t}
|
|
|
417
417
|
`),(0,sMe.tryOpenBrowser)(p),console.log("(已尝试自动打开浏览器,若未打开请手动复制上方链接)"),r.debug)console.log("[Debug] 设备注册详情:"),console.log(` verification_uri : ${t.verification_uri}`),console.log(` verification_uri_complete : ${t.verification_uri_complete}`),console.log(` user_code : ${t.user_code}`),console.log(` device_code : ${(0,vp.maskToken)(t.device_code)}`),console.log(` expires_in : ${t.expires_in}s`),console.log(` interval : ${t.interval}s`);console.log("正在等待授权...");let y=await(0,vp.pollToken)(r,t,n.signal);if(console.log(`
|
|
418
418
|
登录成功 \uD83C\uDF89`),r.debug){if(console.log(` token_type : ${y.token_type}`),console.log(` expires_in : ${y.expires_in}`),console.log(` scope : ${y.scope||""}`),console.log(` access_token : ${(0,vp.maskToken)(y.access_token)}`),y.refresh_token)console.log(` refresh_token : ${(0,vp.maskToken)(y.refresh_token)}`);if(y.id_token)console.log(` id_token : ${(0,vp.maskToken)(y.id_token)}`)}let d=Math.floor(Date.now()/1000),c=`https://api.${new URL(r.platformURL).host}`,u={access_token:y.access_token,expires_at:d+y.expires_in,platform_url:r.platformURL,client_id:r.clientID,login_host:c};if(y.refresh_token)u.refresh_token=y.refresh_token;if((0,vp.saveToken)(u),r.debug)console.log(`Token 已保存到 ${(0,vp.getTokenPath)()}`)}catch(t){if(console.error(""),(0,_S.isNetworkError)(t))(0,_S.printFetchError)(t,"登录失败"),console.error("提示:若网络需要代理,请先设置 HTTPS_PROXY 后重试;也可用 --debug 查看请求详情。");else console.error(`登录失败: ${t?.message||t}`);process.exit(1)}})}});var US=o((qz)=>{Object.defineProperty(qz,"__esModule",{value:!0});qz.registerLogoutCommand=zMe;var fMe=KMe(require("fs")),PMe=td(),$Me=pd(),AMe=jl();function KMe(e){return e&&e.__esModule?e:{default:e}}function zMe(e){e.command("logout").description("退出登录,删除本地保存的 access_token").option("--host <string>","只退出指定平台域名的登录态(如 cnb.cool),不传则退出全部域名的登录态").option("--debug","打印调试信息",!1).helpOption("-h, --help","显示帮助文档").action(async(i)=>{let r=(0,PMe.getTokenPath)();if(i.host){if((0,AMe.removeToken)(i.host))console.log(`已成功退出 ${i.host} 的登录。`);else console.log(`${i.host} 当前未登录,无需退出。`);return}let{auths:n}=(0,$Me.readTokenFile)(),t=Object.keys(n);if(!t.length){console.log("当前未登录,无需退出。");return}if(i.debug){console.log(`Token 文件路径: ${r}`);for(let p of t)console.log(`${p}: Platform URL=${n[p].platform_url}, Client ID=${n[p].client_id}`)}try{fMe.default.unlinkSync(r),console.log(`已成功退出全部登录(${t.join("、")})。`)}catch(p){console.error(`退出登录失败: ${p.message}`),process.exit(1)}})}});var IS=o((wz)=>{Object.defineProperty(wz,"__esModule",{value:!0});wz.registerStatusCommand=UMe;var qMe=ob(),wMe=ub(),_Me=td(),mMe={workbuddy:"WorkBuddy 远程获取",codebuddy_env:"CNB_TOKEN_FOR_CODEBUDDY 环境变量",env:"CNB_TOKEN 环境变量"};function UMe(e){e.command("status").description("检查当前登录状态(token 是否存在、是否过期)").option("--host <string>","查看指定平台域名的登录态(如 cnb.cool),不传则按当前仓库 git remote、CNB_API_ENDPOINT 的顺序推断,都没有对应登录态时取最近一次保存的").option("--debug","打印调试信息",!1).helpOption("-h, --help","显示帮助文档").action(async(i)=>{let r=await(0,qMe.resolveTokenSource)(i.host);if(r.source==="none"){console.log(r.reason?`⚠️ ${r.reason}`:"⚠️ 未登录,请执行 `cnb login` 进行授权。");return}if(r.source!=="file"){if(console.log("✅ 已登录。"),i.debug)console.log(` Token 来源: ${mMe[r.source]}`);return}let n=(0,_Me.getTokenPath)(),t=r.store;if(i.debug)console.log(`Token 文件路径 : ${n}`),console.log(`Platform URL : ${t.platform_url||"未知"}`),console.log(`Client ID : ${t.client_id||"未知"}`),console.log(`Expires At : ${new Date(t.expires_at*1000).toLocaleString()}`);if(Math.floor(Date.now()/1000)>=t.expires_at)if(t.refresh_token){if(i.debug)console.log("Token 已过期,尝试使用 refresh_token 刷新...");try{if(await(0,wMe.refreshAccessToken)(t),console.log("✅ 已登录。"),i.debug)console.log(" (Token 已自动刷新)")}catch(d){if(i.debug)console.log(`刷新失败: ${d.message}`);console.log("⚠️ 未登录,请执行 `cnb login` 重新授权。")}}else console.log("⚠️ 未登录,请执行 `cnb login` 重新授权。");else console.log("✅ 已登录。")})}});var NS=o((_z)=>{Object.defineProperty(_z,"__esModule",{value:!0});_z.registerGitCredentialCommand=OMe;var IMe=fb(),NMe=nd(),WMe=8000;function OMe(e){e.command("git-credential <action>").description("作为 git credential helper,向 git 提供 CNB 凭据(仅供 git 内部调用)").allowUnknownOption().allowExcessArguments().helpOption("-h, --help","显示帮助文档").action(async(i,r,n)=>{let t=n.args.slice(0,-1);if(t.length>0)console.error(`[git-credential]: custom args: ${t.join(" ")}`);if(i==="store"||i==="erase")console.error(`[git-credential]: ${i} ignored`),process.exit(0);if(i!=="get")console.error(`[git-credential]: unknown action: ${i}`),process.exit(1);try{let p=await MMe(WMe),y=XMe(p,"=");DMe(y),await jMe(y),process.exit(0)}catch(p){console.error(p?.message?p.message:String(p)),process.exit(2)}})}async function jMe(e){let{host:i}=e;console.error(`[git-credential]: for ${e.protocol}://${e.host}/${e.path??""}`);let r=(0,NMe.getGitRemoteHosts)();if(!i||r.length===0||!r.includes(i))throw Error(`unknown host: ${i}`);let n=await JMe(i);process.stdout.write(`username=${n.username}
|
|
419
419
|
`),process.stdout.write(`password=${n.password}
|
|
420
|
-
`),console.error("[git-credential]: done")}async function JMe(e){return{username:"cnb",password:await(0,IMe.resolveToken)(e)}}function DMe(e){if(!e.path)return;if(e.path.endsWith(".git"))e.path=e.path.slice(0,-4);else if(e.path.endsWith(".git/info/lfs"))e.path=e.path.slice(0,-13)}function MMe(e){return new Promise((i,r)=>{let n=[],t=!1,p=()=>{if(t)return;t=!0,clearTimeout(y),i(Buffer.concat(n).toString("utf-8"))},y=setTimeout(()=>{if(t)return;t=!0,console.error("[git-credential]: timeout occurred"),r(Error("stdin timeout"))},e);process.stdin.on("data",(d)=>{if(n.push(d),d.length===1&&d[0]===10)p()}),process.stdin.on("end",()=>{p()}),process.stdin.on("error",(d)=>{if(t)return;t=!0,clearTimeout(y),r(d)})})}function XMe(e,i){return e.split(/(?:\r\n|\r|\n)/).filter(Boolean).map((r)=>{let n=r.indexOf(i),t=n>=0?r.substring(0,n).trim():r.trim(),p=n>=0?r.substring(n+1).trim():"";return{key:t,value:p}}).reduce((r,{key:n,value:t})=>{if(n)r[n]=t;return r},{})}});var WS=o((mz)=>{Object.defineProperty(mz,"__esModule",{value:!0});Object.defineProperty(mz,"parseArguments",{enumerable:!0,get:function(){return VMe.parseUnknownOptions}});var YMe=db(),QMe=jj(),SMe=zS(),FMe=qS(),GMe=mS(),BMe=US(),LMe=IS(),HMe=NS(),TMe=Cf(),VMe=$P();(0,TMe.applySslNoVerify)();var fp=new YMe.Command;fp.name("cnb").description("CNB OpenAPI 命令行工具").version("1.21.
|
|
420
|
+
`),console.error("[git-credential]: done")}async function JMe(e){return{username:"cnb",password:await(0,IMe.resolveToken)(e)}}function DMe(e){if(!e.path)return;if(e.path.endsWith(".git"))e.path=e.path.slice(0,-4);else if(e.path.endsWith(".git/info/lfs"))e.path=e.path.slice(0,-13)}function MMe(e){return new Promise((i,r)=>{let n=[],t=!1,p=()=>{if(t)return;t=!0,clearTimeout(y),i(Buffer.concat(n).toString("utf-8"))},y=setTimeout(()=>{if(t)return;t=!0,console.error("[git-credential]: timeout occurred"),r(Error("stdin timeout"))},e);process.stdin.on("data",(d)=>{if(n.push(d),d.length===1&&d[0]===10)p()}),process.stdin.on("end",()=>{p()}),process.stdin.on("error",(d)=>{if(t)return;t=!0,clearTimeout(y),r(d)})})}function XMe(e,i){return e.split(/(?:\r\n|\r|\n)/).filter(Boolean).map((r)=>{let n=r.indexOf(i),t=n>=0?r.substring(0,n).trim():r.trim(),p=n>=0?r.substring(n+1).trim():"";return{key:t,value:p}}).reduce((r,{key:n,value:t})=>{if(n)r[n]=t;return r},{})}});var WS=o((mz)=>{Object.defineProperty(mz,"__esModule",{value:!0});Object.defineProperty(mz,"parseArguments",{enumerable:!0,get:function(){return VMe.parseUnknownOptions}});var YMe=db(),QMe=jj(),SMe=zS(),FMe=qS(),GMe=mS(),BMe=US(),LMe=IS(),HMe=NS(),TMe=Cf(),VMe=$P();(0,TMe.applySslNoVerify)();var fp=new YMe.Command;fp.name("cnb").description("CNB OpenAPI 命令行工具").version("1.21.2","--version","显示版本号").allowUnknownOption().allowExcessArguments().helpOption("-h, --help","显示帮助文档").option("-s, --short","显示当前仓库的快捷命令").addHelpText("after",(0,QMe.getExtraHelpText)());(0,GMe.registerLoginCommand)(fp);(0,BMe.registerLogoutCommand)(fp);(0,LMe.registerStatusCommand)(fp);(0,HMe.registerGitCredentialCommand)(fp);(0,SMe.registerModuleCommands)(fp);(0,FMe.registerFallbackAction)(fp);fp.parseAsync(process.argv)});if(process.argv[2]==="mcp")Oj().runMcpCommand(process.argv.slice(3));else WS();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cnbcool/cnb-cli",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.2",
|
|
4
4
|
"description": "CNB OpenAPI 命令行工具,基于 CNB 平台 Swagger 自动生成,支持 Issues、PR、Git、组织管理等全部 API 操作",
|
|
5
5
|
"main": "skills/cnb-api/scripts/core/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"author": "",
|
|
43
43
|
"license": "MIT",
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@cnbcool/cnb-api-generate": "^2.
|
|
45
|
+
"@cnbcool/cnb-api-generate": "^2.23.1",
|
|
46
46
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
47
47
|
"bun": "^1.3.9",
|
|
48
48
|
"skills": "^1.4.4",
|