@longzai-intelligence-cli/core 0.0.7 → 0.0.9
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 +104 -1
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -362,6 +362,9 @@ declare class OutputFormatter {
|
|
|
362
362
|
/**
|
|
363
363
|
* 输出表格
|
|
364
364
|
*
|
|
365
|
+
* text 模式下渲染对齐表格(表头加粗 + 分隔线 + 列对齐 + 超长截断),
|
|
366
|
+
* json 模式下输出对象数组。
|
|
367
|
+
*
|
|
365
368
|
* @param headers - 表头列表
|
|
366
369
|
* @param rows - 数据行列表
|
|
367
370
|
*/
|
|
@@ -561,6 +564,106 @@ declare function formatLogEntry(entry: LogEntry): string;
|
|
|
561
564
|
*/
|
|
562
565
|
declare function flattenObject(obj: Record<string, unknown>, prefix?: string): Record<string, string>;
|
|
563
566
|
//#endregion
|
|
567
|
+
//#region src/storage.d.ts
|
|
568
|
+
/**
|
|
569
|
+
* 通用 JSON 文件持久化
|
|
570
|
+
*
|
|
571
|
+
* 提供 JSON 文件读写、增删改查的通用存储能力,消除各 CLI 项目重复实现的 JsonStorage。
|
|
572
|
+
* 基于 node:fs,无交互/着色依赖,属于 core 的平台无关能力。
|
|
573
|
+
*/
|
|
574
|
+
/**
|
|
575
|
+
* 记录匹配谓词(用于 find / remove / update 定位目标记录)
|
|
576
|
+
*
|
|
577
|
+
* @typeParam T - 记录类型
|
|
578
|
+
*/
|
|
579
|
+
type Predicate<T> = (item: T) => boolean;
|
|
580
|
+
/**
|
|
581
|
+
* 记录更新函数(接收匹配记录,返回更新后的新记录)
|
|
582
|
+
*
|
|
583
|
+
* @typeParam T - 记录类型
|
|
584
|
+
*/
|
|
585
|
+
type Updater<T> = (item: T) => T;
|
|
586
|
+
/**
|
|
587
|
+
* 通用 JSON 文件存储
|
|
588
|
+
*
|
|
589
|
+
* 自动创建目录与初始化文件(`[]`),read 容错(损坏文件回退空数组)。
|
|
590
|
+
*
|
|
591
|
+
* @typeParam T - 记录类型
|
|
592
|
+
*/
|
|
593
|
+
declare class JsonStorage<T> {
|
|
594
|
+
/**
|
|
595
|
+
* 存储文件绝对路径
|
|
596
|
+
*/
|
|
597
|
+
private readonly filePath;
|
|
598
|
+
/**
|
|
599
|
+
* 构造 JSON 存储
|
|
600
|
+
*
|
|
601
|
+
* 自动创建目录与初始化文件(如果不存在则写入 `[]`)。
|
|
602
|
+
*
|
|
603
|
+
* @param filePath - 存储文件绝对路径
|
|
604
|
+
*/
|
|
605
|
+
constructor(filePath: string);
|
|
606
|
+
/**
|
|
607
|
+
* 读取全部记录
|
|
608
|
+
*
|
|
609
|
+
* 文件不存在或解析失败时返回空数组(容错)。
|
|
610
|
+
*
|
|
611
|
+
* @returns 记录数组
|
|
612
|
+
*/
|
|
613
|
+
read(): T[];
|
|
614
|
+
/**
|
|
615
|
+
* 写入全部记录
|
|
616
|
+
*
|
|
617
|
+
* 每次写入确保目录存在。
|
|
618
|
+
*
|
|
619
|
+
* @param data - 记录数组
|
|
620
|
+
*/
|
|
621
|
+
write(data: T[]): void;
|
|
622
|
+
/**
|
|
623
|
+
* 添加一条记录
|
|
624
|
+
*
|
|
625
|
+
* @param item - 待添加的记录
|
|
626
|
+
*/
|
|
627
|
+
add(item: T): void;
|
|
628
|
+
/**
|
|
629
|
+
* 查找第一条匹配的记录
|
|
630
|
+
*
|
|
631
|
+
* @param predicate - 匹配条件
|
|
632
|
+
* @returns 匹配的记录或 undefined
|
|
633
|
+
*/
|
|
634
|
+
find(predicate: Predicate<T>): T | undefined;
|
|
635
|
+
/**
|
|
636
|
+
* 删除所有匹配的记录
|
|
637
|
+
*
|
|
638
|
+
* @param predicate - 匹配条件
|
|
639
|
+
* @returns 是否实际删除了记录
|
|
640
|
+
*/
|
|
641
|
+
remove(predicate: Predicate<T>): boolean;
|
|
642
|
+
/**
|
|
643
|
+
* 更新第一条匹配的记录
|
|
644
|
+
*
|
|
645
|
+
* @param predicate - 匹配条件
|
|
646
|
+
* @param updater - 更新函数(返回新记录)
|
|
647
|
+
* @returns 是否实际更新了记录
|
|
648
|
+
*/
|
|
649
|
+
update(predicate: Predicate<T>, updater: Updater<T>): boolean;
|
|
650
|
+
}
|
|
651
|
+
//#endregion
|
|
652
|
+
//#region src/id.d.ts
|
|
653
|
+
/**
|
|
654
|
+
* 带前缀的唯一 ID 生成
|
|
655
|
+
*
|
|
656
|
+
* 格式:`<prefix>_<timestamp36>_<random6>`
|
|
657
|
+
* 消除各 CLI 项目重复实现的 generateId。
|
|
658
|
+
*/
|
|
659
|
+
/**
|
|
660
|
+
* 生成带前缀的唯一 ID
|
|
661
|
+
*
|
|
662
|
+
* @param prefix - ID 前缀(如 `'course'`、`'meeting'`)
|
|
663
|
+
* @returns 唯一 ID 字符串
|
|
664
|
+
*/
|
|
665
|
+
declare function generateId(prefix: string): string;
|
|
666
|
+
//#endregion
|
|
564
667
|
//#region src/define-command.d.ts
|
|
565
668
|
/**
|
|
566
669
|
* 选项声明(commander 选项签名 + 是否带默认值)
|
|
@@ -665,4 +768,4 @@ type CommandSpec<T extends z.ZodType> = {
|
|
|
665
768
|
*/
|
|
666
769
|
declare function defineCommand<T extends z.ZodType>(program: Command$1, spec: CommandSpec<T>): void;
|
|
667
770
|
//#endregion
|
|
668
|
-
export { type ColorProvider, type Command, type CommandSpec, type ConfirmOptions, type CreateCliOptions, type InteractionProvider, 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, getCliVersion, getColorProvider, getInteractionProvider, log, note, parseOutputFormat, progressBar, select, setColorProvider, setInteractionProvider, spinner, textInput };
|
|
771
|
+
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, 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"node:fs";import"node:path";import"node:url";const
|
|
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}from"node:path";import"node:url";import*as s from"zod";const c=e(`@longzai-intelligence-cli/core/provider`),l={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 u={select:async(e,t)=>t[0]?.value??``,textInput:async(e,t)=>t?.defaultValue??``,confirm:async(e,t)=>t?.defaultValue??!1,spinner:()=>({start:e=>c.info(e),stop:e=>c.info(e)}),note:(e,t)=>{t?c.info(`${t}: ${e}`):c.info(e)},log:{info:e=>c.info(e),success:e=>c.info(e),warn:e=>c.warn(e),error:e=>c.error(e),step:e=>c.info(e)}},d=l;function f(e){u=e}function p(e){d=e}function m(){return u}function h(){return d}const g=()=>`0.0.9`;function _(e){let n=e.version??g();e.interaction&&f(e.interaction),e.color&&p(e.color);let r=new t;return r.name(e.name).version(n,`-v, --version`,`输出 ${e.name} 的版本号`),e.description&&r.description(e.description),r}const v=e(`@longzai-intelligence-cli/core/output`);var y=class{format;constructor(e=`text`){this.format=e}get colors(){return h()}success(e,t){this.format===`json`?v.info(JSON.stringify({success:!0,data:e},null,2)):v.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)};v.info(JSON.stringify({success:!1,error:t},null,2))}else{let t=e instanceof Error?e.message:String(e);v.error(this.colors.red(`错误: ${t}`))}}info(e){this.format===`json`?v.info(JSON.stringify({success:!0,message:e},null,2)):v.info(this.colors.blue(e))}warn(e){this.format===`json`?v.info(JSON.stringify({success:!0,warning:e},null,2)):v.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});v.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))));v.info(e.map((e,t)=>this.colors.bold(e.padEnd(n[t]??0))).join(` `)),v.info(n.map(e=>`─`.repeat(e??0)).join(` `));for(let e of t)v.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 b(e){return e===`json`?`json`:`text`}async function x(e,t){return m().select(e,t)}async function S(e,t){return m().textInput(e,t)}async function C(e,t){return m().confirm(e,t)}function w(){return m().spinner()}function T(e,t){m().note(e,t)}const E={info(e){m().log.info(e)},success(e){m().log.success(e)},warn(e){m().log.warn(e)},error(e){m().log.error(e)},step(e){m().log.step(e)}},D=e(`@longzai-intelligence-cli/core/progress`);function O(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${h().blue(a)} ${r.toFixed(0)}%${i}`),r>=100&&process.stdout.write(`
|
|
2
2
|
`)}},done(e){process.stdout.write(`
|
|
3
|
-
`),e&&
|
|
3
|
+
`),e&&D.info(h().green(`✓ ${e}`))}}}const k={debug:`gray`,error:`red`,fatal:`red`,info:`green`,trace:`gray`,warn:`yellow`};function A(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 j(e){let t=e.toLowerCase(),n=e.toUpperCase().padEnd(5),r=k[t]??`green`;return h()[r](n)}function M(e){let t=h(),n=[];return e.timestamp!==void 0&&n.push(t.gray(A(e.timestamp))),n.push(j(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 N(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function P(e,t=``){let n={};for(let[r,i]of Object.entries(e)){let e=t===``?r:`${t}.${r}`;N(i)?Object.assign(n,P(i,e)):n[e]=String(i??``)}return n}const F=s.array(s.unknown());function I(e){return F.safeParse(e).success}var L=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 I(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?!1:(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 R(e){return`${e}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,8)}`}const z=e(`@longzai-intelligence-cli/core/define-command`);function B(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=e.commands.find(e=>e.name()===n);a===void 0&&(a=e.command(n));for(let e of r){let t=a;a=t.commands.find(t=>t.name()===e)??t.command(e)}return a.command(i)}function V(e,n){let r=B(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(`, `);z.error(h().red(`参数校验失败: ${e}`)),m().log.error(`参数校验失败: ${e}`),process.exit(1)}await n.action(o.data,a)})}export{L as JsonStorage,y as OutputFormatter,C as confirm,_ as createCli,V as defineCommand,P as flattenObject,j as formatLevel,M as formatLogEntry,A as formatTimestamp,R as generateId,g as getCliVersion,h as getColorProvider,m as getInteractionProvider,E as log,T as note,b as parseOutputFormat,O as progressBar,x as select,p as setColorProvider,f as setInteractionProvider,w as spinner,S as textInput};
|