11import { scanCommandPath , parseFlags } from "./args.ts" ;
2- import { registry } from "./registry.ts" ;
2+ import { createRegistry , getActiveRegistry } from "./registry.ts" ;
3+ import { loadCommandCatalog } from "./load-commands.ts" ;
34import {
45 GLOBAL_OPTIONS ,
56 loadConfig ,
@@ -21,32 +22,8 @@ import {
2122 setExecutingCommandPath ,
2223} from "./utils/command-help.ts" ;
2324
24- registerCommandHelpPrinter ( ( commandPath , out ) => {
25- const a = process . argv . slice ( 2 ) ;
26- const ri = a . indexOf ( "--region" ) ;
27- const region = ( ( ri >= 0 && a [ ri + 1 ] ) ||
28- process . env . DASHSCOPE_REGION ||
29- readConfigFile ( ) . region ||
30- "cn" ) as Region ;
31- registry . printHelp ( commandPath , out , region ) ;
32- } ) ;
33-
34- // 优雅处理 Ctrl+C
35- // 退出前尝试 best-effort 刷出埋点,让去抖队列中 / 在途的 fetch 请求有机会
36- // 落网络;flush 与较短超时 race,保证 SIGINT 仍然响应及时。
37- process . on ( "SIGINT" , ( ) => {
38- process . stderr . write ( "\nInterrupted. Exiting.\n" ) ;
39- void flushTelemetry ( 500 ) . finally ( ( ) => process . exit ( 130 ) ) ;
40- } ) ;
41-
42- // 优雅处理 stdout EPIPE(例如管道到提前退出的 `mpv`)
43- process . stdout . on ( "error" , ( e : NodeJS . ErrnoException ) => {
44- if ( e . code === "EPIPE" ) process . exit ( 0 ) ;
45- else throw e ;
46- } ) ;
47-
4825// 自己接管鉴权 或 根本不需要 API key 的命令
49- const NO_AUTH_SETUP = [
26+ const BUILTIN_NO_AUTH_SETUP : string [ ] [ ] = [
5027 [ "auth" , "login" ] ,
5128 [ "auth" , "logout" ] ,
5229 [ "config" , "show" ] ,
@@ -60,9 +37,31 @@ const NO_AUTH_SETUP = [
6037 [ "app" , "list" ] ,
6138 [ "console" , "call" ] ,
6239 [ "usage" , "free" ] ,
40+ [ "plugins" , "list" ] ,
41+ [ "plugins" , "install" ] ,
42+ [ "plugins" , "link" ] ,
43+ [ "plugins" , "remove" ] ,
6344] ;
6445
46+ function matchesNoAuthSetup ( commandPath : string [ ] , rules : string [ ] [ ] ) : boolean {
47+ return rules . some ( ( cmd ) => cmd . every ( ( c , i ) => commandPath [ i ] === c ) ) ;
48+ }
49+
6550async function main ( ) {
51+ const registry = await createRegistry ( ) ;
52+ const catalog = await loadCommandCatalog ( ) ;
53+ const noAuthSetup = [ ...BUILTIN_NO_AUTH_SETUP , ...catalog . noAuthSetup ] ;
54+
55+ registerCommandHelpPrinter ( ( commandPath , out ) => {
56+ const a = process . argv . slice ( 2 ) ;
57+ const ri = a . indexOf ( "--region" ) ;
58+ const region = ( ( ri >= 0 && a [ ri + 1 ] ) ||
59+ process . env . DASHSCOPE_REGION ||
60+ readConfigFile ( ) . region ||
61+ "cn" ) as Region ;
62+ getActiveRegistry ( ) . printHelp ( commandPath , out , region ) ;
63+ } ) ;
64+
6665 const argv = process . argv . slice ( 2 ) ;
6766
6867 if ( argv . includes ( "--version" ) || argv . includes ( "-v" ) ) {
@@ -122,7 +121,7 @@ async function main() {
122121 config . clientName = "bailian-cli" ;
123122 config . clientVersion = CLI_VERSION ;
124123
125- const needsAuthSetup = ! NO_AUTH_SETUP . some ( ( cmd ) => cmd . every ( ( c , i ) => commandPath [ i ] === c ) ) ;
124+ const needsAuthSetup = ! matchesNoAuthSetup ( commandPath , noAuthSetup ) ;
126125 if ( needsAuthSetup ) {
127126 await ensureApiKey ( config ) ;
128127 try {
@@ -168,10 +167,16 @@ async function main() {
168167 await flushTelemetry ( 1000 ) ;
169168}
170169
170+ process . on ( "SIGINT" , ( ) => {
171+ process . stderr . write ( "\nInterrupted. Exiting.\n" ) ;
172+ void flushTelemetry ( 500 ) . finally ( ( ) => process . exit ( 130 ) ) ;
173+ } ) ;
174+
175+ process . stdout . on ( "error" , ( e : NodeJS . ErrnoException ) => {
176+ if ( e . code === "EPIPE" ) process . exit ( 0 ) ;
177+ else throw e ;
178+ } ) ;
179+
171180main ( ) . catch ( ( err ) => {
172- // 在 handleError() 调用 process.exit() 之前刷出在途埋点。
173- // 命令抛出的错误已被 trackCommandExecution 的 finally 块记录,
174- // 但底层 tracker 有 ~500ms 的发送去抖。不主动 flush 的话,
175- // 错误事件会随进程退出丢掉。
176181 void flushTelemetry ( 1000 ) . finally ( ( ) => handleError ( err ) ) ;
177182} ) ;
0 commit comments