@apexhub/apex-ui-skill 0.0.0-stage → 1.2.0

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.
@@ -0,0 +1,340 @@
1
+ export const theme = {
2
+ brand: '#5e5ce5',
3
+ brandRgb: [94, 92, 229],
4
+ brandMutedRgb: [94, 92, 229],
5
+ }
6
+
7
+ import { LOGO_BLOCK, LOGO_COMPACT, LOGO_HUGE } from './logo.mjs'
8
+
9
+ const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
10
+
11
+ /**
12
+ * @returns {{ tty: boolean, color: boolean, width: number, plain: boolean }}
13
+ */
14
+ export function detectCapabilities({ plain = false } = {}) {
15
+ const tty = Boolean(process.stdin.isTTY && process.stdout.isTTY)
16
+ const noColor = process.env.NO_COLOR != null && process.env.NO_COLOR !== '0'
17
+ const dumb = process.env.TERM === 'dumb'
18
+ const color = tty && !noColor && !dumb
19
+ const width = Math.min(Math.max(process.stdout.columns || 60, 40), 80)
20
+ return { tty, color, width, plain: plain || !tty }
21
+ }
22
+
23
+ /** @param {boolean} color @param {string} text @param {'brand'|'dim'|'bold'|'reset'|'success'|'white'} style */
24
+ export function paint(color, text, style = 'reset') {
25
+ if (!color) return text
26
+ if (style === 'dim') return `\x1b[2m${text}\x1b[22m`
27
+ if (style === 'bold') return `\x1b[1m${text}\x1b[22m`
28
+ if (style === 'white') return `\x1b[1m\x1b[97m${text}\x1b[0m`
29
+ if (style === 'success') return `\x1b[32m${text}\x1b[39m`
30
+ if (style === 'brand') {
31
+ const [r, g, b] = theme.brandRgb
32
+ return `\x1b[38;2;${r};${g};${b}m${text}\x1b[39m`
33
+ }
34
+ return text
35
+ }
36
+
37
+ // logo 立体上色:填充块 █ 亮白,描边字符(╗╔╝╚═║ 等)暗灰,做出上亮下暗的 3D 层次。
38
+ const FILL_CHAR = '█'
39
+ const FILL_ANSI = '\x1b[1m\x1b[97m'
40
+ const EDGE_ANSI = '\x1b[38;5;240m'
41
+ const ANSI_RESET = '\x1b[0m'
42
+
43
+ /** @param {boolean} color @param {string} line */
44
+ function paintLogo(color, line) {
45
+ if (!color) return line
46
+ let out = ''
47
+ let i = 0
48
+ while (i < line.length) {
49
+ if (line[i] === ' ') {
50
+ out += ' '
51
+ i += 1
52
+ continue
53
+ }
54
+ const isFill = line[i] === FILL_CHAR
55
+ let j = i
56
+ while (j < line.length && line[j] !== ' ' && (line[j] === FILL_CHAR) === isFill) j += 1
57
+ out += `${isFill ? FILL_ANSI : EDGE_ANSI}${line.slice(i, j)}${ANSI_RESET}`
58
+ i = j
59
+ }
60
+ return out
61
+ }
62
+
63
+ /** @param {number} ms */
64
+ export function sleep(ms) {
65
+ return new Promise((resolve) => setTimeout(resolve, ms))
66
+ }
67
+
68
+ function hideCursor() {
69
+ process.stdout.write('\x1b[?25l')
70
+ }
71
+
72
+ function showCursor() {
73
+ process.stdout.write('\x1b[?25h')
74
+ }
75
+
76
+ function clearScreen() {
77
+ process.stdout.write('\x1b[H\x1b[J')
78
+ }
79
+
80
+ /**
81
+ * 共享的大 logo 头部。每一帧(进场 / 向导 / 安装 / 完成)都先画它,
82
+ * 这样 logo 始终钉在顶部、不会被后续清屏擦掉。
83
+ * @param {boolean} color
84
+ * @param {{ subtitle?: boolean }} [opts]
85
+ */
86
+ export function renderHeader(color, { subtitle = true } = {}) {
87
+ // 按终端宽度三档自适应:超宽=2x 大号,普通=block,窄=紧凑,避免换行错位
88
+ const cols = process.stdout.columns || 80
89
+ const logo = cols >= 112 ? LOGO_HUGE : cols >= 58 ? LOGO_BLOCK : LOGO_COMPACT
90
+ const lines = ['']
91
+ for (const row of logo) lines.push(` ${paintLogo(color, row)}`)
92
+ if (subtitle) {
93
+ lines.push('')
94
+ lines.push(` ${paint(color, 'AI assistant toolkit · Skills installer', 'dim')}`)
95
+ }
96
+ lines.push('')
97
+ return `${lines.join('\n')}\n`
98
+ }
99
+
100
+ /**
101
+ * @typedef {{ id: string, label: string, detail: string, skillName?: string, targetId?: string, skill?: object }} InstallTask
102
+ */
103
+
104
+ /**
105
+ * @param {{ color: boolean, width: number, version: string, plain?: boolean }} caps
106
+ */
107
+ export async function playIntro(caps) {
108
+ const { color, version } = caps
109
+
110
+ if (caps.plain) {
111
+ console.log(`\nApex UI Skills v${version}\n`)
112
+ return
113
+ }
114
+
115
+ hideCursor()
116
+ clearScreen()
117
+ process.stdout.write(renderHeader(color))
118
+
119
+ await withSpinner(caps, 'Loading skills manifest', async () => {
120
+ await sleep(420)
121
+ })
122
+
123
+ process.stdout.write(paint(color, ` v${version}`, 'dim'))
124
+ process.stdout.write('\n\n')
125
+ showCursor()
126
+ }
127
+
128
+ /**
129
+ * @param {{ color: boolean, plain?: boolean }} caps
130
+ * @param {string} message
131
+ * @param {() => Promise<void>} task
132
+ */
133
+ export async function withSpinner(caps, message, task) {
134
+ const { color, plain } = caps
135
+
136
+ if (plain) {
137
+ process.stdout.write(` ${message}…`)
138
+ await task()
139
+ process.stdout.write(' done\n')
140
+ return
141
+ }
142
+
143
+ let frame = 0
144
+ let done = false
145
+ const interval = setInterval(() => {
146
+ if (done) return
147
+ const icon = paint(color, SPINNER_FRAMES[frame % SPINNER_FRAMES.length], 'brand')
148
+ process.stdout.write(`\r\x1b[K ${icon} ${paint(color, message, 'dim')}`)
149
+ frame += 1
150
+ }, 80)
151
+
152
+ try {
153
+ await task()
154
+ } finally {
155
+ done = true
156
+ clearInterval(interval)
157
+ process.stdout.write(
158
+ `\r\x1b[K ${paint(color, '✔', 'success')} ${paint(color, message, 'dim')}\n`,
159
+ )
160
+ }
161
+ }
162
+
163
+ /**
164
+ * @typedef {{ id: string, label: string, detail: string }} InstallTaskLite
165
+ */
166
+
167
+ /**
168
+ * @param {{ color: boolean, plain?: boolean }} caps
169
+ * @param {InstallTaskLite[]} tasks
170
+ * @param {(task: InstallTaskLite) => Promise<void>} installOne
171
+ */
172
+ export async function runInstallSequence(caps, tasks, installOne) {
173
+ const { color, plain } = caps
174
+
175
+ if (plain || tasks.length === 0) {
176
+ for (const task of tasks) {
177
+ process.stdout.write(` · ${task.label} ${task.detail}\n`)
178
+ await installOne(task)
179
+ }
180
+ return
181
+ }
182
+
183
+ /** @type {Array<'pending'|'active'|'done'>} */
184
+ const states = tasks.map(() => 'pending')
185
+ let spinnerFrame = 0
186
+ let spinnerTimer = null
187
+
188
+ const statusIcon = (state) => {
189
+ if (state === 'done') return paint(color, '✔', 'success')
190
+ if (state === 'active') {
191
+ return paint(color, SPINNER_FRAMES[spinnerFrame % SPINNER_FRAMES.length], 'brand')
192
+ }
193
+ return paint(color, '○', 'dim')
194
+ }
195
+
196
+ const render = () => {
197
+ const lines = [paint(color, ' Installing…', 'bold'), '']
198
+
199
+ for (let i = 0; i < tasks.length; i += 1) {
200
+ const task = tasks[i]
201
+ const icon = statusIcon(states[i])
202
+ const label =
203
+ states[i] === 'active'
204
+ ? paint(color, task.label, 'bold')
205
+ : states[i] === 'done'
206
+ ? paint(color, task.label, 'dim')
207
+ : task.label
208
+ const detail = paint(color, task.detail, 'dim')
209
+ lines.push(` ${icon} ${label.padEnd(18)} ${detail}`)
210
+ }
211
+
212
+ lines.push('')
213
+ return lines.join('\n')
214
+ }
215
+
216
+ const refresh = () => {
217
+ // 每帧先重绘 logo 头部(不带副标题以省高度),logo 始终在顶部不消失
218
+ process.stdout.write('\x1b[H\x1b[J')
219
+ process.stdout.write(renderHeader(color, { subtitle: false }))
220
+ process.stdout.write(render())
221
+ }
222
+
223
+ hideCursor()
224
+ refresh()
225
+
226
+ spinnerTimer = setInterval(() => {
227
+ spinnerFrame += 1
228
+ if (states.some((s) => s === 'active')) refresh()
229
+ }, 80)
230
+
231
+ try {
232
+ for (let i = 0; i < tasks.length; i += 1) {
233
+ states[i] = 'active'
234
+ refresh()
235
+ await installOne(tasks[i])
236
+ states[i] = 'done'
237
+ refresh()
238
+ if (i < tasks.length - 1) await sleep(60)
239
+ }
240
+ } finally {
241
+ if (spinnerTimer) clearInterval(spinnerTimer)
242
+ showCursor()
243
+ process.stdout.write('\n')
244
+ }
245
+ }
246
+
247
+ /** @deprecated use runInstallSequence */
248
+ export async function runWithProgress(caps, label, task) {
249
+ await withSpinner(caps, label, task)
250
+ }
251
+
252
+ /**
253
+ * @param {{ color: boolean, width: number, plain?: boolean }} caps
254
+ * @param {{ skillCount: number, targetCount: number, ruleFiles: string[], skillEntries?: string[], interactive?: boolean, noProject?: boolean }} summary
255
+ */
256
+ export async function playSuccess(caps, summary) {
257
+ const { color, plain } = caps
258
+
259
+ if (plain) {
260
+ console.log('')
261
+ console.log('安装完成')
262
+ console.log(`已写入 ${summary.targetCount} 个 AI 目录 · ${summary.skillCount} 个 skill`)
263
+ if (summary.detected) console.log(`识别到的 AI 工具:${summary.detected}`)
264
+ for (const file of summary.ruleFiles) {
265
+ console.log(`· ${file}`)
266
+ }
267
+ console.log('')
268
+ if (!summary.interactive && summary.skillEntries?.length) {
269
+ // Usually read by the AI tool that ran the command: a skill installed mid-conversation is only
270
+ // picked up in a new conversation, so point it at the file to follow now.
271
+ console.log(
272
+ `AI 助手:新装的 skill 要到下一次对话才会自动加载。请现在读取 ${summary.skillEntries[0]},`,
273
+ )
274
+ console.log('按其中步骤继续用户的任务(首次接入默认走源码模式)。')
275
+ if (summary.noProject) {
276
+ console.log(
277
+ '当前目录还没有项目(没有 package.json):请按 references/source-install.md 第 2 步,先用大白话',
278
+ )
279
+ console.log(
280
+ '告诉用户要新建一个 React 项目,把它建在当前目录里(不要建在子文件夹),再安装 Apex UI 组件。',
281
+ )
282
+ }
283
+ } else {
284
+ console.log('下一步:在 AI 工具里开一个新对话即可生效。')
285
+ }
286
+ console.log('升级 skill:在项目根重新运行 npx @apexhub/apex-ui-skill')
287
+ console.log('')
288
+ return
289
+ }
290
+
291
+ hideCursor()
292
+ // 最终一帧:保留大 logo + 完成信息(清掉安装列表),退出后 logo 仍留在终端
293
+ process.stdout.write('\x1b[H\x1b[J')
294
+ process.stdout.write(renderHeader(color, { subtitle: false }))
295
+
296
+ const block = [
297
+ paint(color, ' ✔ 安装完成', 'success'),
298
+ '',
299
+ paint(
300
+ color,
301
+ ` 已写入 ${summary.targetCount} 个 AI 目录 · ${summary.skillCount} 个 skill`,
302
+ 'dim',
303
+ ),
304
+ ]
305
+
306
+ for (const file of summary.ruleFiles) {
307
+ block.push(paint(color, ` · ${file}`, 'dim'))
308
+ }
309
+
310
+ block.push('')
311
+ block.push(paint(color, ' 下一步:在 AI 工具里开一个新对话即可生效。', 'dim'))
312
+ block.push(paint(color, ' 升级:npx @apexhub/apex-ui-skill', 'dim'))
313
+ block.push('')
314
+
315
+ process.stdout.write(block.join('\n'))
316
+ process.stdout.write('\n')
317
+ showCursor()
318
+ }
319
+
320
+ export function printPlainHelp() {
321
+ console.log(`Apex UI Skills 安装器
322
+
323
+ npx @apexhub/apex-ui-skill 终端里:交互向导;AI 工具里(无终端):自动识别正在用的 AI 工具并安装
324
+ npx @apexhub/apex-ui-skill --list 列出 skill
325
+ npx @apexhub/apex-ui-skill --list-targets 列出 AI 目录
326
+ npx @apexhub/apex-ui-skill --target auto 自动识别:当前 AI 工具 + 项目里已有配置的工具 + Agents 通用
327
+ npx @apexhub/apex-ui-skill --skills apex-ui --target claude,cursor
328
+ npx @apexhub/apex-ui-skill --default --target claude
329
+ npx @apexhub/apex-ui-skill --all --target claude,cursor,copilot
330
+ npx @apexhub/apex-ui-skill --plain 关闭动效
331
+
332
+ 选项:
333
+ --skills <a,b> 要安装的 skill
334
+ --target <a,b> 目标 AI 目录 id;auto 表示自动识别
335
+ --default 安装所有 default skill
336
+ --all 安装全部 skill
337
+ --plain 纯文本模式
338
+ -h, --help 帮助
339
+ `)
340
+ }