@longzai-intelligence-cli/core 0.0.1
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/README.md +31 -0
- package/dist/index.d.ts +253 -0
- package/dist/index.js +1 -0
- package/dist/rolldown-runtime-BqT_7tdF.js +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @longzai-intelligence-cli/core
|
|
2
|
+
|
|
3
|
+
Longzai Intelligence 产品群级 CLI 基础设施核心包,收口 192 个子项目的 CLI 框架分化。
|
|
4
|
+
|
|
5
|
+
## 定位
|
|
6
|
+
|
|
7
|
+
该包面向所有 `longzai-intelligence-*` 子项目的 `apps/cli`,作为命令装配、参数解析、输出版式、交互层与版本注入的统一真源,替代各项目散落的自研 `cli/` 副本。
|
|
8
|
+
|
|
9
|
+
## 核心能力
|
|
10
|
+
|
|
11
|
+
- **createCli()**:基于 commander 的命令装配工厂,内置 `--version` / `--help`
|
|
12
|
+
- **输出版式**:`OutputFormatter` 支持 text / json 双格式 + table 渲染
|
|
13
|
+
- **交互层**:select / confirm / textInput / spinner / note(`@clack/prompts` 封装)
|
|
14
|
+
- **版本注入**:`getCliVersion()` 构建期 define 静态替换 + 开发期 package.json 兜底
|
|
15
|
+
|
|
16
|
+
## 使用示例
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createCli } from '@longzai-intelligence-cli/core';
|
|
20
|
+
|
|
21
|
+
const program = createCli({
|
|
22
|
+
name: 'lzi-my-app',
|
|
23
|
+
description: '我的 CLI 应用',
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
program.command('hello').action(() => {
|
|
27
|
+
console.log('Hello, Longzai Intelligence!');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await program.parseAsync(process.argv);
|
|
31
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import "./rolldown-runtime-BqT_7tdF.js";
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
//#region src/create-cli.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* createCli 工厂的配置选项
|
|
6
|
+
*/
|
|
7
|
+
type CreateCliOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* CLI 程序名称(如 `lzi-gateway`)
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* 程序描述(显示在 --help 顶部)
|
|
14
|
+
*/
|
|
15
|
+
description?: string;
|
|
16
|
+
/**
|
|
17
|
+
* 版本号(默认从构建期注入读取,无需手动传入)
|
|
18
|
+
*/
|
|
19
|
+
version?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* 创建 CLI 程序实例
|
|
23
|
+
*
|
|
24
|
+
* 基于 commander 封装,内置 `--version`(版本号由构建期 define 注入 + 开发期兜底)
|
|
25
|
+
* 与 `--help`,调用方只需注册具体子命令后 `parseAsync(process.argv)` 即可。
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const program = createCli({ name: 'lzi-my-app', description: '我的 CLI' });
|
|
30
|
+
* program.command('hello').action(() => console.log('hi'));
|
|
31
|
+
* await program.parseAsync(process.argv);
|
|
32
|
+
* ```;
|
|
33
|
+
*
|
|
34
|
+
* @param options - 工厂配置
|
|
35
|
+
* @returns commander Command 实例
|
|
36
|
+
*/
|
|
37
|
+
declare function createCli(options: CreateCliOptions): Command;
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/version/index.d.ts
|
|
40
|
+
/**
|
|
41
|
+
* 获取 CLI 版本号
|
|
42
|
+
*
|
|
43
|
+
* 解析顺序:
|
|
44
|
+
*
|
|
45
|
+
* 1. 构建期注入的 `process.env.CLI_VERSION`(lzi-builder define 静态替换)
|
|
46
|
+
* 2. 开发期兜底:从源码旁的 package.json 读取(src/../package.json)
|
|
47
|
+
* 3. 找不到时回退 '0.0.0'(极端沙箱场景),避免阻塞 --version 输出
|
|
48
|
+
*
|
|
49
|
+
* @returns CLI 版本号字符串
|
|
50
|
+
*/
|
|
51
|
+
declare const getCliVersion: () => string;
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/output/index.d.ts
|
|
54
|
+
/**
|
|
55
|
+
* 输出格式类型
|
|
56
|
+
*/
|
|
57
|
+
type OutputFormat = 'text' | 'json';
|
|
58
|
+
/**
|
|
59
|
+
* 输出格式化器
|
|
60
|
+
*
|
|
61
|
+
* 支持 text(带颜色的可读输出)与 json(机器可读的结构化输出)两种格式,
|
|
62
|
+
* 由 `parseOutputFormat` 从命令行 `--format` 参数解析得到。
|
|
63
|
+
*/
|
|
64
|
+
declare class OutputFormatter {
|
|
65
|
+
/**
|
|
66
|
+
* 当前输出格式
|
|
67
|
+
*/
|
|
68
|
+
private format;
|
|
69
|
+
/**
|
|
70
|
+
* 构造输出格式化器
|
|
71
|
+
*
|
|
72
|
+
* @param format - 输出格式,默认 text
|
|
73
|
+
*/
|
|
74
|
+
constructor(format?: OutputFormat);
|
|
75
|
+
/**
|
|
76
|
+
* 输出成功信息
|
|
77
|
+
*
|
|
78
|
+
* @param data - 要展示的数据
|
|
79
|
+
* @param message - 可选的自定义消息(仅 text 格式生效)
|
|
80
|
+
*/
|
|
81
|
+
success(data: unknown, message?: string): void;
|
|
82
|
+
/**
|
|
83
|
+
* 输出错误信息
|
|
84
|
+
*
|
|
85
|
+
* @param error - 错误对象或消息
|
|
86
|
+
*/
|
|
87
|
+
error(error: unknown): void;
|
|
88
|
+
/**
|
|
89
|
+
* 输出提示信息
|
|
90
|
+
*
|
|
91
|
+
* @param message - 提示消息
|
|
92
|
+
*/
|
|
93
|
+
info(message: string): void;
|
|
94
|
+
/**
|
|
95
|
+
* 输出警告信息
|
|
96
|
+
*
|
|
97
|
+
* @param message - 警告消息
|
|
98
|
+
*/
|
|
99
|
+
warn(message: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* 输出表格
|
|
102
|
+
*
|
|
103
|
+
* @param headers - 表头列表
|
|
104
|
+
* @param rows - 数据行列表
|
|
105
|
+
*/
|
|
106
|
+
table(headers: string[], rows: string[][]): void;
|
|
107
|
+
/**
|
|
108
|
+
* 格式化数据为字符串
|
|
109
|
+
*
|
|
110
|
+
* @param data - 原始数据
|
|
111
|
+
* @returns 字符串表示
|
|
112
|
+
*/
|
|
113
|
+
private formatData;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 解析输出格式参数
|
|
117
|
+
*
|
|
118
|
+
* @param format - 格式参数字符串
|
|
119
|
+
* @returns 输出格式类型
|
|
120
|
+
*/
|
|
121
|
+
declare function parseOutputFormat(format?: string): OutputFormat;
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/tui/index.d.ts
|
|
124
|
+
/**
|
|
125
|
+
* 选择选项
|
|
126
|
+
*/
|
|
127
|
+
type SelectOption = {
|
|
128
|
+
/**
|
|
129
|
+
* 选项值
|
|
130
|
+
*/
|
|
131
|
+
value: string;
|
|
132
|
+
/**
|
|
133
|
+
* 选项标签
|
|
134
|
+
*/
|
|
135
|
+
label: string;
|
|
136
|
+
/**
|
|
137
|
+
* 选项提示
|
|
138
|
+
*/
|
|
139
|
+
hint?: string;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* 文本输入选项
|
|
143
|
+
*/
|
|
144
|
+
type TextInputModule = {
|
|
145
|
+
/**
|
|
146
|
+
* 占位提示文本
|
|
147
|
+
*/
|
|
148
|
+
placeholder?: string;
|
|
149
|
+
/**
|
|
150
|
+
* 默认值
|
|
151
|
+
*/
|
|
152
|
+
defaultValue?: string;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* 确认选项
|
|
156
|
+
*/
|
|
157
|
+
type ConfirmOptions = {
|
|
158
|
+
/**
|
|
159
|
+
* 默认选中值
|
|
160
|
+
*/
|
|
161
|
+
defaultValue?: boolean;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* 加载动画控制器
|
|
165
|
+
*/
|
|
166
|
+
type SpinnerController = {
|
|
167
|
+
/**
|
|
168
|
+
* 启动动画
|
|
169
|
+
*
|
|
170
|
+
* @param message - 动画提示消息
|
|
171
|
+
*/
|
|
172
|
+
start(message: string): void;
|
|
173
|
+
/**
|
|
174
|
+
* 停止动画
|
|
175
|
+
*
|
|
176
|
+
* @param message - 结束提示消息
|
|
177
|
+
*/
|
|
178
|
+
stop(message: string): void;
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* 交互式选择
|
|
182
|
+
*
|
|
183
|
+
* @param message - 提示消息
|
|
184
|
+
* @param options - 选项列表
|
|
185
|
+
* @returns 选中的值
|
|
186
|
+
*/
|
|
187
|
+
declare function select(message: string, options: SelectOption[]): Promise<string>;
|
|
188
|
+
/**
|
|
189
|
+
* 交互式文本输入
|
|
190
|
+
*
|
|
191
|
+
* @param message - 提示消息
|
|
192
|
+
* @param options - 输入选项
|
|
193
|
+
* @returns 输入的文本
|
|
194
|
+
*/
|
|
195
|
+
declare function textInput(message: string, options?: TextInputModule): Promise<string>;
|
|
196
|
+
/**
|
|
197
|
+
* 交互式确认
|
|
198
|
+
*
|
|
199
|
+
* @param message - 提示消息
|
|
200
|
+
* @param options - 确认选项
|
|
201
|
+
* @returns 确认结果
|
|
202
|
+
*/
|
|
203
|
+
declare function confirm(message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
204
|
+
/**
|
|
205
|
+
* 创建加载动画
|
|
206
|
+
*
|
|
207
|
+
* @returns 加载动画控制器
|
|
208
|
+
*/
|
|
209
|
+
declare function spinner(): SpinnerController;
|
|
210
|
+
/**
|
|
211
|
+
* 显示提示信息框
|
|
212
|
+
*
|
|
213
|
+
* @param message - 信息内容
|
|
214
|
+
* @param title - 信息标题
|
|
215
|
+
*/
|
|
216
|
+
declare function note(message: string, title?: string): void;
|
|
217
|
+
/**
|
|
218
|
+
* 日志输出工具集
|
|
219
|
+
*/
|
|
220
|
+
declare const log: {
|
|
221
|
+
/**
|
|
222
|
+
* 输出信息级日志
|
|
223
|
+
*
|
|
224
|
+
* @param message - 日志消息
|
|
225
|
+
*/
|
|
226
|
+
info(message: string): void;
|
|
227
|
+
/**
|
|
228
|
+
* 输出成功级日志
|
|
229
|
+
*
|
|
230
|
+
* @param message - 日志消息
|
|
231
|
+
*/
|
|
232
|
+
success(message: string): void;
|
|
233
|
+
/**
|
|
234
|
+
* 输出警告级日志
|
|
235
|
+
*
|
|
236
|
+
* @param message - 日志消息
|
|
237
|
+
*/
|
|
238
|
+
warn(message: string): void;
|
|
239
|
+
/**
|
|
240
|
+
* 输出错误级日志
|
|
241
|
+
*
|
|
242
|
+
* @param message - 日志消息
|
|
243
|
+
*/
|
|
244
|
+
error(message: string): void;
|
|
245
|
+
/**
|
|
246
|
+
* 输出步骤级日志
|
|
247
|
+
*
|
|
248
|
+
* @param message - 日志消息
|
|
249
|
+
*/
|
|
250
|
+
step(message: string): void;
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
export { type ConfirmOptions, type CreateCliOptions, type OutputFormat, OutputFormatter, type SelectOption, type SpinnerController, type TextInputModule, confirm, createCli, getCliVersion, log, note, parseOutputFormat, select, spinner, textInput };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./rolldown-runtime-BqT_7tdF.js";import{Command as e}from"commander";import"node:fs";import"node:path";import"node:url";import{createNodeLogger as t}from"@longzai-intelligence-logger/node";import n from"picocolors";import*as r from"@clack/prompts";const i=()=>`0.0.1`;function a(t){let n=t.version??i(),r=new e;return r.name(t.name).version(n,`-v, --version`,`输出 ${t.name} 的版本号`),t.description&&r.description(t.description),r}const o=t(`@longzai-intelligence-cli/core/output`);var s=class{format;constructor(e=`text`){this.format=e}success(e,t){this.format===`json`?o.info(JSON.stringify({success:!0,data:e},null,2)):o.info(t?n.green(t):n.green(this.formatData(e)))}error(e){if(this.format===`json`){let t=e instanceof Error?{message:e.message,name:e.name}:{message:String(e)};o.info(JSON.stringify({success:!1,error:t},null,2))}else{let t=e instanceof Error?e.message:String(e);o.error(n.red(`错误: ${t}`))}}info(e){this.format===`json`?o.info(JSON.stringify({success:!0,message:e},null,2)):o.info(n.blue(e))}warn(e){this.format===`json`?o.info(JSON.stringify({success:!0,warning:e},null,2)):o.info(n.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});o.info(JSON.stringify({success:!0,data:n},null,2))}else{let r=e.map((e,n)=>Math.max(e.length,...t.map(e=>(e[n]??``).length)));o.info(e.map((e,t)=>n.bold(e.padEnd(r[t]??0))).join(` `)),o.info(r.map(e=>`-`.repeat(e??0)).join(` `));for(let e of t)o.info(e.map((e,t)=>(e??``).padEnd(r[t]??0)).join(` `))}}formatData(e){return typeof e==`string`?e:typeof e==`number`||typeof e==`boolean`?String(e):JSON.stringify(e,null,2)}};function c(e){return e===`json`?`json`:`text`}async function l(e,t){let n=await r.select({message:e,options:t.map(e=>({value:e.value,label:e.label,hint:e.hint}))});return r.isCancel(n)&&(r.cancel(`操作已取消`),process.exit(0)),typeof n==`string`?n:String(n)}async function u(e,t){let n=await r.text({message:e,placeholder:t?.placeholder,defaultValue:t?.defaultValue});return r.isCancel(n)&&(r.cancel(`操作已取消`),process.exit(0)),typeof n==`string`?n:String(n)}async function d(e,t){let n=await r.confirm({message:e,initialValue:t?.defaultValue??!1});return r.isCancel(n)&&(r.cancel(`操作已取消`),process.exit(0)),typeof n==`boolean`?n:!!n}function f(){let e=r.spinner();return{start(t){e.start(t)},stop(t){e.stop(t)}}}function p(e,t){r.note(e,t)}const m={info(e){r.log.info(n.blue(e))},success(e){r.log.success(n.green(e))},warn(e){r.log.warn(n.yellow(e))},error(e){r.log.error(n.red(e))},step(e){r.log.step(e)}};export{s as OutputFormatter,d as confirm,a as createCli,i as getCliVersion,m as log,p as note,c as parseOutputFormat,l as select,f as spinner,u as textInput};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"node:module";export{};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-cli/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"commander",
|
|
8
|
+
"infrastructure"
|
|
9
|
+
],
|
|
10
|
+
"license": "UNLICENSED",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "lzi-builder",
|
|
25
|
+
"build:prod": "NODE_ENV=production lzi-builder",
|
|
26
|
+
"prepublishOnly": "bun run build:prod",
|
|
27
|
+
"typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
|
|
28
|
+
"typecheck:app": "tsgo --noEmit -p tsconfig/app.json",
|
|
29
|
+
"typecheck:node": "tsgo --noEmit -p tsconfig/node.json",
|
|
30
|
+
"typecheck:test": "tsgo --noEmit -p tsconfig/test.json",
|
|
31
|
+
"lint": "oxlint && oxfmt --check",
|
|
32
|
+
"lint:fix": "oxlint --fix && oxfmt",
|
|
33
|
+
"test": "bun test",
|
|
34
|
+
"test:watch": "bun test --watch",
|
|
35
|
+
"test:coverage": "bun test --coverage",
|
|
36
|
+
"clean": "lzi-dev-cli clean"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@clack/prompts": "^0.11.0",
|
|
40
|
+
"@longzai-intelligence-builder/utils": "^0.2.13",
|
|
41
|
+
"@longzai-intelligence-logger/node": "^0.1.0",
|
|
42
|
+
"commander": "^14.0.0",
|
|
43
|
+
"picocolors": "^1.1.1"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {}
|
|
46
|
+
}
|