@longzai-intelligence-cli/core 0.0.14 → 0.0.15

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.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import "./rolldown-runtime-BqT_7tdF.js";
2
2
  import { Command, Command as Command$1 } from "commander";
3
3
  import * as z from "zod";
4
- //#region src/contracts.d.ts
5
4
  /**
6
5
  * 能力契约定义
7
6
  *
@@ -211,8 +210,6 @@ type ProgressController = {
211
210
  * 输出格式类型
212
211
  */
213
212
  type OutputFormat = 'text' | 'json';
214
- //#endregion
215
- //#region src/provider.d.ts
216
213
  /**
217
214
  * 设置当前交互 provider(由 createCli 或消费方入口注入)
218
215
  *
@@ -237,8 +234,6 @@ declare function getInteractionProvider(): InteractionProvider;
237
234
  * @returns 当前生效的着色 provider
238
235
  */
239
236
  declare function getColorProvider(): ColorProvider;
240
- //#endregion
241
- //#region src/create-cli.d.ts
242
237
  /**
243
238
  * createCli 工厂的配置选项
244
239
  */
@@ -295,8 +290,6 @@ type CreateCliOptions = {
295
290
  * @returns commander Command 实例
296
291
  */
297
292
  declare function createCli(options: CreateCliOptions): Command$1;
298
- //#endregion
299
- //#region src/version.d.ts
300
293
  /**
301
294
  * 获取 CLI 版本号
302
295
  *
@@ -309,8 +302,6 @@ declare function createCli(options: CreateCliOptions): Command$1;
309
302
  * @returns CLI 版本号字符串
310
303
  */
311
304
  declare const getCliVersion: () => string;
312
- //#endregion
313
- //#region src/output.d.ts
314
305
  /**
315
306
  * 输出格式化器
316
307
  *
@@ -385,8 +376,6 @@ declare class OutputFormatter {
385
376
  * @returns 输出格式类型
386
377
  */
387
378
  declare function parseOutputFormat(format?: string): OutputFormat;
388
- //#endregion
389
- //#region src/tui.d.ts
390
379
  /**
391
380
  * 交互层模块函数
392
381
  *
@@ -468,8 +457,6 @@ declare const log: {
468
457
  */
469
458
  step(message: string): void;
470
459
  };
471
- //#endregion
472
- //#region src/progress.d.ts
473
460
  /**
474
461
  * 进度条能力(交互无关、平台无关)
475
462
  *
@@ -480,8 +467,6 @@ declare const log: {
480
467
  * @returns 进度条控制器
481
468
  */
482
469
  declare function progressBar(total?: number): ProgressController;
483
- //#endregion
484
- //#region src/log-format.d.ts
485
470
  /**
486
471
  * 日志格式化能力
487
472
  *
@@ -564,8 +549,6 @@ declare function formatLogEntry(entry: LogEntry): string;
564
549
  * @returns 扁平化后的键值对(所有值为 string)
565
550
  */
566
551
  declare function flattenObject(obj: Record<string, unknown>, prefix?: string): Record<string, string>;
567
- //#endregion
568
- //#region src/storage.d.ts
569
552
  /**
570
553
  * 通用 JSON 文件持久化
571
554
  *
@@ -649,8 +632,6 @@ declare class JsonStorage<T> {
649
632
  */
650
633
  update(predicate: Predicate<T>, updater: Updater<T>): boolean;
651
634
  }
652
- //#endregion
653
- //#region src/id.d.ts
654
635
  /**
655
636
  * 带前缀的唯一 ID 生成
656
637
  *
@@ -664,8 +645,6 @@ declare class JsonStorage<T> {
664
645
  * @returns 唯一 ID 字符串
665
646
  */
666
647
  declare function generateId(prefix: string): string;
667
- //#endregion
668
- //#region src/define-command.d.ts
669
648
  /**
670
649
  * 选项声明(commander 选项签名 + 是否带默认值)
671
650
  */
@@ -784,5 +763,4 @@ declare function resolveCommandParent(program: Command$1, parentName: string): C
784
763
  * @param spec - 命令参数规格
785
764
  */
786
765
  declare function defineCommand<T extends z.ZodType>(program: Command$1, spec: CommandSpec<T>): void;
787
- //#endregion
788
766
  export { type ColorProvider, type Command, type CommandSpec, type ConfirmOptions, type CreateCliOptions, type InteractionProvider, JsonStorage, type LogEntry, type LogLevel, type LogToolset, type OptionDecl, type OutputFormat, OutputFormatter, type ProgressController, type SelectOption, type SpinnerController, type TextInputOptions, confirm, createCli, defineCommand, flattenObject, formatLevel, formatLogEntry, formatTimestamp, generateId, getCliVersion, getColorProvider, getInteractionProvider, log, note, parseOutputFormat, progressBar, resolveCommandParent, select, setColorProvider, setInteractionProvider, spinner, textInput };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import"./rolldown-runtime-BqT_7tdF.js";import{createNodeLogger as e}from"@longzai-intelligence-logger/node";import{Command as t}from"commander";import{existsSync as n,mkdirSync as r,readFileSync as i,writeFileSync as a}from"node:fs";import{dirname as o,join as s}from"node:path";import{fileURLToPath as c}from"node:url";import*as l from"zod";let u;function d(){return u??=e(`@longzai-intelligence-cli/core/provider`),u}const f={red:e=>e,green:e=>e,yellow:e=>e,blue:e=>e,cyan:e=>e,magenta:e=>e,gray:e=>e,bold:e=>e,dim:e=>e};let p={select:async(e,t)=>t[0]?.value??``,textInput:async(e,t)=>t?.defaultValue??``,confirm:async(e,t)=>t?.defaultValue??!1,spinner:()=>({start:e=>d().info(e),stop:e=>d().info(e)}),note:(e,t)=>{t?d().info(`${t}: ${e}`):d().info(e)},log:{info:e=>d().info(e),success:e=>d().info(e),warn:e=>d().warn(e),error:e=>d().error(e),step:e=>d().info(e)}},m=f;function h(e){p=e}function g(e){m=e}function _(){return p}function v(){return m}function y(e){return typeof e==`object`&&!!e&&(!(`name`in e)||typeof e.name==`string`)&&`version`in e&&typeof e.version==`string`}const b=()=>{let e=process.env.CLI_VERSION;if(typeof e==`string`&&e.length>0)return e;try{let e=s(o(c(import.meta.url)),`..`,`package.json`),t=JSON.parse(i(e,`utf-8`));if(y(t)&&t.name===`@longzai-intelligence-cli/core`)return t.version}catch{}return`0.0.0`};function x(e){let n=e.version??b();e.interaction&&h(e.interaction),e.color&&g(e.color);let r=new t;return r.name(e.name).version(n,`-v, --version`,`输出 ${e.name} 的版本号`),e.description&&r.description(e.description),r}let S;function C(){return S??=e(`@longzai-intelligence-cli/core/output`),S}var w=class{format;constructor(e=`text`){this.format=e}get colors(){return v()}success(e,t){this.format===`json`?C().info(JSON.stringify({success:!0,data:e},null,2)):C().info(t?this.colors.green(t):this.colors.green(this.formatData(e)))}error(e){if(this.format===`json`){let t=e instanceof Error?{message:e.message,name:e.name}:{message:String(e)};C().info(JSON.stringify({success:!1,error:t},null,2))}else{let t=e instanceof Error?e.message:String(e);C().error(this.colors.red(`错误: ${t}`))}}info(e){this.format===`json`?C().info(JSON.stringify({success:!0,message:e},null,2)):C().info(this.colors.blue(e))}warn(e){this.format===`json`?C().info(JSON.stringify({success:!0,warning:e},null,2)):C().info(this.colors.yellow(e))}table(e,t){if(this.format===`json`){let n=t.map(t=>{let n={};return e.forEach((e,r)=>{n[e]=t[r]??``}),n});C().info(JSON.stringify({success:!0,data:n},null,2))}else{let n=e.map((e,n)=>Math.min(60,Math.max(e.length,...t.map(e=>(e[n]??``).length))));C().info(e.map((e,t)=>this.colors.bold(e.padEnd(n[t]??0))).join(` `)),C().info(n.map(e=>`─`.repeat(e??0)).join(` `));for(let e of t)C().info(e.map((e,t)=>{let r=n[t]??0,i=e??``;return i.length>r?i.slice(0,Math.max(0,r-1))+`…`:i.padEnd(r)}).join(` `))}}formatData(e){return typeof e==`string`?e:typeof e==`number`||typeof e==`boolean`?String(e):JSON.stringify(e,null,2)}};function T(e){return e===`json`?`json`:`text`}async function E(e,t){return _().select(e,t)}async function D(e,t){return _().textInput(e,t)}async function O(e,t){return _().confirm(e,t)}function k(){return _().spinner()}function A(e,t){_().note(e,t)}const j={info(e){_().log.info(e)},success(e){_().log.success(e)},warn(e){_().log.warn(e)},error(e){_().log.error(e)},step(e){_().log.step(e)}};let M;function N(){return M??=e(`@longzai-intelligence-cli/core/progress`),M}function P(e=100){let t=-1;return{update(e,n){let r=Math.max(0,Math.min(100,e)),i=Math.round(r/100*30),a=`█`.repeat(i)+`░`.repeat(30-i);if(e!==t){t=e;let i=n?` ${n}`:``;process.stdout.write(`\r${v().blue(a)} ${r.toFixed(0)}%${i}`),r>=100&&process.stdout.write(`
1
+ import"./rolldown-runtime-BqT_7tdF.js";import{createNodeLogger as e}from"@longzai-intelligence-logger/node";import{Command as t}from"commander";import{existsSync as n,mkdirSync as r,readFileSync as i,writeFileSync as a}from"node:fs";import{dirname as o,join as s}from"node:path";import{fileURLToPath as c}from"node:url";import{ensureError as l}from"@longzai-intelligence-error/utils";import*as u from"zod";let d;function f(){return d??=e(`@longzai-intelligence-cli/core/provider`),d}const p={red:e=>e,green:e=>e,yellow:e=>e,blue:e=>e,cyan:e=>e,magenta:e=>e,gray:e=>e,bold:e=>e,dim:e=>e};let m={select:async(e,t)=>t[0]?.value??``,textInput:async(e,t)=>t?.defaultValue??``,confirm:async(e,t)=>t?.defaultValue??!1,spinner:()=>({start:e=>f().info(e),stop:e=>f().info(e)}),note:(e,t)=>{t?f().info(`${t}: ${e}`):f().info(e)},log:{info:e=>f().info(e),success:e=>f().info(e),warn:e=>f().warn(e),error:e=>f().error(e),step:e=>f().info(e)}},h=p;function g(e){m=e}function _(e){h=e}function v(){return m}function y(){return h}function b(e){return typeof e==`object`&&!!e&&(!(`name`in e)||typeof e.name==`string`)&&`version`in e&&typeof e.version==`string`}const x=()=>{let e=process.env.CLI_VERSION;if(typeof e==`string`&&e.length>0)return e;try{let e=s(o(c(import.meta.url)),`..`,`package.json`),t=JSON.parse(i(e,`utf-8`));if(b(t)&&t.name===`@longzai-intelligence-cli/core`)return t.version}catch{}return`0.0.0`};function S(e){let n=e.version??x();e.interaction&&g(e.interaction),e.color&&_(e.color);let r=new t;return r.name(e.name).version(n,`-v, --version`,`输出 ${e.name} 的版本号`),e.description&&r.description(e.description),r}let C;function w(){return C??=e(`@longzai-intelligence-cli/core/output`),C}var T=class{format;constructor(e=`text`){this.format=e}get colors(){return y()}success(e,t){this.format===`json`?w().info(JSON.stringify({success:!0,data:e},null,2)):w().info(t?this.colors.green(t):this.colors.green(this.formatData(e)))}error(e){if(this.format===`json`){let t=l(e),n={message:t.message,name:t.name};w().info(JSON.stringify({success:!1,error:n},null,2))}else{let t=l(e).message;w().error(this.colors.red(`错误: ${t}`))}}info(e){this.format===`json`?w().info(JSON.stringify({success:!0,message:e},null,2)):w().info(this.colors.blue(e))}warn(e){this.format===`json`?w().info(JSON.stringify({success:!0,warning:e},null,2)):w().info(this.colors.yellow(e))}table(e,t){if(this.format===`json`){let n=t.map(t=>{let n={};return e.forEach((e,r)=>{n[e]=t[r]??``}),n});w().info(JSON.stringify({success:!0,data:n},null,2))}else{let n=e.map((e,n)=>Math.min(60,Math.max(e.length,...t.map(e=>(e[n]??``).length))));w().info(e.map((e,t)=>this.colors.bold(e.padEnd(n[t]??0))).join(` `)),w().info(n.map(e=>`─`.repeat(e??0)).join(` `));for(let e of t)w().info(e.map((e,t)=>{let r=n[t]??0,i=e??``;return i.length>r?i.slice(0,Math.max(0,r-1))+`…`:i.padEnd(r)}).join(` `))}}formatData(e){return typeof e==`string`?e:typeof e==`number`||typeof e==`boolean`?String(e):JSON.stringify(e,null,2)}};function E(e){return e===`json`?`json`:`text`}async function D(e,t){return v().select(e,t)}async function O(e,t){return v().textInput(e,t)}async function k(e,t){return v().confirm(e,t)}function A(){return v().spinner()}function j(e,t){v().note(e,t)}const M={info(e){v().log.info(e)},success(e){v().log.success(e)},warn(e){v().log.warn(e)},error(e){v().log.error(e)},step(e){v().log.step(e)}};let N;function P(){return N??=e(`@longzai-intelligence-cli/core/progress`),N}function F(e=100){let t=-1;return{update(e,n){let r=Math.max(0,Math.min(100,e)),i=Math.round(r/100*30),a=`█`.repeat(i)+`░`.repeat(30-i);if(e!==t){t=e;let i=n?` ${n}`:``;process.stdout.write(`\r${y().blue(a)} ${r.toFixed(0)}%${i}`),r>=100&&process.stdout.write(`
2
2
  `)}},done(e){process.stdout.write(`
3
- `),e&&N().info(v().green(`✓ ${e}`))}}}const F={debug:`gray`,error:`red`,fatal:`red`,info:`green`,trace:`gray`,warn:`yellow`};function I(e){try{return new Date(e).toLocaleString(`zh-CN`,{day:`2-digit`,hour:`2-digit`,hour12:!1,minute:`2-digit`,month:`2-digit`,second:`2-digit`,year:`numeric`})}catch{return e}}function L(e){let t=e.toLowerCase(),n=e.toUpperCase().padEnd(5),r=F[t]??`green`;return v()[r](n)}function R(e){let t=v(),n=[];return e.timestamp!==void 0&&n.push(t.gray(I(e.timestamp))),n.push(L(e.level??`info`)),e.serviceName!==void 0&&e.serviceName!==``&&n.push(t.blue(`[${e.serviceName}]`)),n.push(e.message??String(e.message??``)),e.metadata!==void 0&&typeof e.metadata==`object`&&Object.keys(e.metadata).length>0&&n.push(t.gray(JSON.stringify(e.metadata))),n.join(` `)}function z(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function B(e,t=``){let n={};for(let[r,i]of Object.entries(e)){let e=t===``?r:`${t}.${r}`;z(i)?Object.assign(n,B(i,e)):n[e]=String(i??``)}return n}const V=l.array(l.unknown());function H(e){return V.safeParse(e).success}var U=class{filePath;constructor(e){this.filePath=e;let t=o(e);n(t)||r(t,{recursive:!0}),n(e)||a(e,`[]`,`utf-8`)}read(){try{let e=i(this.filePath,`utf-8`),t=JSON.parse(e);return H(t)?t:[]}catch{return[]}}write(e){let t=o(this.filePath);n(t)||r(t,{recursive:!0}),a(this.filePath,JSON.stringify(e,null,2),`utf-8`)}add(e){let t=this.read();t.push(e),this.write(t)}find(e){return this.read().find(e)}remove(e){let t=this.read(),n=t.filter(t=>!e(t));return n.length!==t.length&&(this.write(n),!0)}update(e,t){let n=this.read(),r=!1,i=n.map(n=>e(n)?(r=!0,t(n)):n);return r&&this.write(i),r}};function W(e){return`${e}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,8)}`}let G;function K(){return G??=e(`@longzai-intelligence-cli/core/define-command`),G}function q(e,t){let n=e.commands.find(e=>e.name()===t);return n===void 0?e.command(t):n}function J(e,t){if(t.length===1){let n=t[0];if(n===void 0)throw Error(`defineCommand: segments 为空,无法解析命令名`);return e.command(n)}let[n,...r]=t;if(n===void 0)throw Error(`defineCommand: segments 首段为空,无法解析父命令名`);let i=r.pop();if(i===void 0)throw Error(`defineCommand: segments 末段为空,无法解析子命令名`);let a=q(e,n);for(let e of r)a=q(a,e);return a.command(i)}function Y(e,n){let r=J(e,n.name.split(` `).filter(e=>e.length>0));n.description&&r.description(n.description);for(let e of n.arguments??[])e.description?r.argument(e.signature,e.description):r.argument(e.signature);for(let e of n.options??[])e.defaultValue===void 0?r.option(e.flags,e.description??``):r.option(e.flags,e.description??``,e.defaultValue);r.action(async(...e)=>{let r=e[e.length-1];if(!(r instanceof t))return;let i=r.opts(),a=r.args,o=n.schema.safeParse(i);if(!o.success){let e=o.error.issues.map(e=>e.message).join(`, `);K().error(v().red(`参数校验失败: ${e}`)),_().log.error(`参数校验失败: ${e}`),process.exit(1)}await n.action(o.data,a)})}export{U as JsonStorage,w as OutputFormatter,O as confirm,x as createCli,Y as defineCommand,B as flattenObject,L as formatLevel,R as formatLogEntry,I as formatTimestamp,W as generateId,b as getCliVersion,v as getColorProvider,_ as getInteractionProvider,j as log,A as note,T as parseOutputFormat,P as progressBar,q as resolveCommandParent,E as select,g as setColorProvider,h as setInteractionProvider,k as spinner,D as textInput};
3
+ `),e&&P().info(y().green(`✓ ${e}`))}}}const I={debug:`gray`,error:`red`,fatal:`red`,info:`green`,trace:`gray`,warn:`yellow`};function L(e){try{return new Date(e).toLocaleString(`zh-CN`,{day:`2-digit`,hour:`2-digit`,hour12:!1,minute:`2-digit`,month:`2-digit`,second:`2-digit`,year:`numeric`})}catch{return e}}function R(e){let t=e.toLowerCase(),n=e.toUpperCase().padEnd(5),r=I[t]??`green`;return y()[r](n)}function z(e){let t=y(),n=[];return e.timestamp!==void 0&&n.push(t.gray(L(e.timestamp))),n.push(R(e.level??`info`)),e.serviceName!==void 0&&e.serviceName!==``&&n.push(t.blue(`[${e.serviceName}]`)),n.push(e.message??String(e.message??``)),e.metadata!==void 0&&typeof e.metadata==`object`&&Object.keys(e.metadata).length>0&&n.push(t.gray(JSON.stringify(e.metadata))),n.join(` `)}function B(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function V(e,t=``){let n={};for(let[r,i]of Object.entries(e)){let e=t===``?r:`${t}.${r}`;B(i)?Object.assign(n,V(i,e)):n[e]=String(i??``)}return n}const H=u.array(u.unknown());function U(e){return H.safeParse(e).success}var W=class{filePath;constructor(e){this.filePath=e;let t=o(e);n(t)||r(t,{recursive:!0}),n(e)||a(e,`[]`,`utf-8`)}read(){try{let e=i(this.filePath,`utf-8`),t=JSON.parse(e);return U(t)?t:[]}catch{return[]}}write(e){let t=o(this.filePath);n(t)||r(t,{recursive:!0}),a(this.filePath,JSON.stringify(e,null,2),`utf-8`)}add(e){let t=this.read();t.push(e),this.write(t)}find(e){return this.read().find(e)}remove(e){let t=this.read(),n=t.filter(t=>!e(t));return n.length!==t.length&&(this.write(n),!0)}update(e,t){let n=this.read(),r=!1,i=n.map(n=>e(n)?(r=!0,t(n)):n);return r&&this.write(i),r}};function G(e){return`${e}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,8)}`}let K;function q(){return K??=e(`@longzai-intelligence-cli/core/define-command`),K}function J(e,t){let n=e.commands.find(e=>e.name()===t);return n===void 0?e.command(t):n}function Y(e,t){if(t.length===1){let n=t[0];if(n===void 0)throw Error(`defineCommand: segments 为空,无法解析命令名`);return e.command(n)}let[n,...r]=t;if(n===void 0)throw Error(`defineCommand: segments 首段为空,无法解析父命令名`);let i=r.pop();if(i===void 0)throw Error(`defineCommand: segments 末段为空,无法解析子命令名`);let a=J(e,n);for(let e of r)a=J(a,e);return a.command(i)}function X(e,n){let r=Y(e,n.name.split(` `).filter(e=>e.length>0));n.description&&r.description(n.description);for(let e of n.arguments??[])e.description?r.argument(e.signature,e.description):r.argument(e.signature);for(let e of n.options??[])e.defaultValue===void 0?r.option(e.flags,e.description??``):r.option(e.flags,e.description??``,e.defaultValue);r.action(async(...e)=>{let r=e[e.length-1];if(!(r instanceof t))return;let i=r.opts(),a=r.args,o=n.schema.safeParse(i);if(!o.success){let e=o.error.issues.map(e=>e.message).join(`, `);q().error(y().red(`参数校验失败: ${e}`)),v().log.error(`参数校验失败: ${e}`),process.exit(1)}await n.action(o.data,a)})}export{W as JsonStorage,T as OutputFormatter,k as confirm,S as createCli,X as defineCommand,V as flattenObject,R as formatLevel,z as formatLogEntry,L as formatTimestamp,G as generateId,x as getCliVersion,y as getColorProvider,v as getInteractionProvider,M as log,j as note,E as parseOutputFormat,F as progressBar,J as resolveCommandParent,D as select,_ as setColorProvider,g as setInteractionProvider,A as spinner,O as textInput};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longzai-intelligence-cli/core",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "private": false,
5
5
  "description": "Longzai Intelligence CLI 核心契约与平台无关能力",
6
6
  "keywords": [
@@ -33,14 +33,15 @@
33
33
  "typecheck:test": "lzi-tsgo typecheck tsconfig/test.json",
34
34
  "lint": "oxlint && oxfmt --check",
35
35
  "lint:fix": "oxlint --fix && oxfmt",
36
- "test": "bun test",
37
- "test:watch": "bun test --watch",
38
- "test:coverage": "bun test --coverage",
39
- "clean": "lzi-dev-cli clean"
36
+ "test": "lzi-bun test",
37
+ "test:watch": "lzi-bun test --watch",
38
+ "test:coverage": "lzi-bun test --coverage",
39
+ "clean": "lzi-dev-tools clean"
40
40
  },
41
41
  "dependencies": {
42
- "@longzai-intelligence-builder/utils": "^0.2.13",
43
- "@longzai-intelligence-logger/node": "^0.1.8",
42
+ "@longzai-intelligence-builder/utils": "^0.2.22",
43
+ "@longzai-intelligence-error/utils": "^0.2.0",
44
+ "@longzai-intelligence-logger/node": "^0.1.11",
44
45
  "commander": "^14.0.0",
45
46
  "zod": "^4.4.3"
46
47
  },