@shiqianjiang/ceru-plugin-cli 0.3.6 → 0.3.8
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 +2 -0
- package/assets/core-contracts.js +1 -1
- package/assets/sandbox.js +27 -27
- package/dist/dev.js +89 -33
- package/dist/index.js +17 -30
- package/dist/init.d.ts +8 -0
- package/dist/init.js +71 -0
- package/dist/project.d.ts +1 -0
- package/dist/project.js +12 -0
- package/dist/tui.d.ts +52 -0
- package/dist/tui.js +261 -0
- package/package.json +3 -3
- package/templates/connected-library/js/package.json +2 -2
- package/templates/connected-library/ts/package.json +2 -2
- package/templates/guest-adapter/js/package.json +2 -2
- package/templates/guest-adapter/ts/package.json +2 -2
- package/templates/importer/js/package.json +2 -2
- package/templates/importer/ts/package.json +2 -2
- package/templates/react/js/package.json +2 -2
- package/templates/react/ts/package.json +2 -2
- package/templates/snapshot.json +1 -1
- package/templates/source/js/package.json +2 -2
- package/templates/source/js/src/index.js +9 -1
- package/templates/source/ts/package.json +2 -2
- package/templates/source/ts/src/index.ts +9 -1
- package/templates/vue/js/package.json +2 -2
- package/templates/vue/ts/package.json +2 -2
- package/templates/vue-tsx/js/package.json +2 -2
- package/templates/vue-tsx/ts/package.json +2 -2
- package/templates/web-dist/js/package.json +2 -2
- package/templates/web-dist/ts/package.json +2 -2
- package/templates/web-surface/js/package.json +2 -2
- package/templates/web-surface/ts/package.json +2 -2
package/dist/dev.js
CHANGED
|
@@ -15,6 +15,86 @@ import { readArtifact, parseJsonStrict, resolveArtifactConfig, } from '@shiqianj
|
|
|
15
15
|
import { buildProject } from './project.js';
|
|
16
16
|
import { DevelopmentStorage } from './storage.js';
|
|
17
17
|
const asset = (name) => fileURLToPath(new URL('../assets/' + name, import.meta.url));
|
|
18
|
+
const developmentServiceMethods = {
|
|
19
|
+
account: ['getSession', 'getProfile'],
|
|
20
|
+
app: ['getInfo'],
|
|
21
|
+
player: ['getState'],
|
|
22
|
+
queue: ['get'],
|
|
23
|
+
history: ['list'],
|
|
24
|
+
downloads: ['list'],
|
|
25
|
+
localMusic: ['list'],
|
|
26
|
+
settings: ['get'],
|
|
27
|
+
rooms: ['getState'],
|
|
28
|
+
devices: ['list'],
|
|
29
|
+
};
|
|
30
|
+
const hostServiceNames = [
|
|
31
|
+
'account',
|
|
32
|
+
'app',
|
|
33
|
+
'library',
|
|
34
|
+
'player',
|
|
35
|
+
'queue',
|
|
36
|
+
'favorites',
|
|
37
|
+
'history',
|
|
38
|
+
'downloads',
|
|
39
|
+
'files',
|
|
40
|
+
'clipboard',
|
|
41
|
+
'localMusic',
|
|
42
|
+
'settings',
|
|
43
|
+
'window',
|
|
44
|
+
'hotkeys',
|
|
45
|
+
'sharing',
|
|
46
|
+
'rooms',
|
|
47
|
+
'devices',
|
|
48
|
+
'ai',
|
|
49
|
+
'tasks',
|
|
50
|
+
];
|
|
51
|
+
const developmentAvailability = (service) => {
|
|
52
|
+
const methods = developmentServiceMethods[service] ?? [];
|
|
53
|
+
return {
|
|
54
|
+
service,
|
|
55
|
+
version: '1.0.0',
|
|
56
|
+
available: methods.length > 0,
|
|
57
|
+
...(methods.length ? { methods } : { reason: 'host-not-connected' }),
|
|
58
|
+
permissionGroups: [],
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const developmentServiceValue = (method, args) => {
|
|
62
|
+
if (method === 'services.account.getSession')
|
|
63
|
+
return { loggedIn: false, profile: null };
|
|
64
|
+
if (method === 'services.account.getProfile')
|
|
65
|
+
return null;
|
|
66
|
+
if (method === 'services.app.getInfo')
|
|
67
|
+
return {
|
|
68
|
+
name: 'Ceru Plugin Development Host',
|
|
69
|
+
version: '0.3.6',
|
|
70
|
+
platform: process.platform,
|
|
71
|
+
locale: 'zh-CN',
|
|
72
|
+
theme: 'light',
|
|
73
|
+
hostApi: '2.0.0',
|
|
74
|
+
};
|
|
75
|
+
if (method === 'services.player.getState')
|
|
76
|
+
return {
|
|
77
|
+
status: 'idle',
|
|
78
|
+
track: null,
|
|
79
|
+
positionMs: 0,
|
|
80
|
+
durationMs: 0,
|
|
81
|
+
volume: 1,
|
|
82
|
+
muted: false,
|
|
83
|
+
repeat: 'off',
|
|
84
|
+
shuffle: false,
|
|
85
|
+
};
|
|
86
|
+
if (method === 'services.queue.get')
|
|
87
|
+
return { items: [], currentIndex: -1, revision: 'development-empty' };
|
|
88
|
+
if (method === 'services.history.list' || method === 'services.localMusic.list')
|
|
89
|
+
return { items: [], totalEstimate: 0 };
|
|
90
|
+
if (method === 'services.downloads.list' || method === 'services.devices.list')
|
|
91
|
+
return [];
|
|
92
|
+
if (method === 'services.settings.get')
|
|
93
|
+
return Object.fromEntries((Array.isArray(args[0]) ? args[0] : []).map((key) => [key, null]));
|
|
94
|
+
if (method === 'services.rooms.getState')
|
|
95
|
+
return { joined: false };
|
|
96
|
+
return undefined;
|
|
97
|
+
};
|
|
18
98
|
function mergeConfig(base, override) {
|
|
19
99
|
const result = structuredClone(base);
|
|
20
100
|
for (const [key, value] of Object.entries(override))
|
|
@@ -463,48 +543,24 @@ export async function runDev(project, options = {}) {
|
|
|
463
543
|
}
|
|
464
544
|
if (input.method === 'services.capabilities.list') {
|
|
465
545
|
json(200, {
|
|
466
|
-
value:
|
|
467
|
-
'account',
|
|
468
|
-
'library',
|
|
469
|
-
'player',
|
|
470
|
-
'queue',
|
|
471
|
-
'favorites',
|
|
472
|
-
'history',
|
|
473
|
-
'downloads',
|
|
474
|
-
'files',
|
|
475
|
-
'clipboard',
|
|
476
|
-
'localMusic',
|
|
477
|
-
'settings',
|
|
478
|
-
'window',
|
|
479
|
-
'hotkeys',
|
|
480
|
-
'sharing',
|
|
481
|
-
'rooms',
|
|
482
|
-
'devices',
|
|
483
|
-
'ai',
|
|
484
|
-
'tasks',
|
|
485
|
-
].map((service) => ({
|
|
486
|
-
service,
|
|
487
|
-
version: '1.0.0',
|
|
488
|
-
available: false,
|
|
489
|
-
reason: 'host-not-connected',
|
|
490
|
-
permissionGroups: [],
|
|
491
|
-
})),
|
|
546
|
+
value: hostServiceNames.map(developmentAvailability),
|
|
492
547
|
});
|
|
493
548
|
return;
|
|
494
549
|
}
|
|
495
550
|
if (input.method === 'services.capabilities.get') {
|
|
496
551
|
const service = String(data.args?.[0] ?? '');
|
|
497
552
|
json(200, {
|
|
498
|
-
value:
|
|
499
|
-
service,
|
|
500
|
-
version: '1.0.0',
|
|
501
|
-
available: false,
|
|
502
|
-
reason: 'host-not-connected',
|
|
503
|
-
permissionGroups: [],
|
|
504
|
-
},
|
|
553
|
+
value: developmentAvailability(service),
|
|
505
554
|
});
|
|
506
555
|
return;
|
|
507
556
|
}
|
|
557
|
+
if (input.method.startsWith('services.')) {
|
|
558
|
+
const value = developmentServiceValue(input.method, data.args ?? []);
|
|
559
|
+
if (value === undefined)
|
|
560
|
+
throw new Error('此独立开发 Host 尚未连接该宿主服务;请先通过 capabilities.get() 检查 methods,并在正式桌面 Host 中验证。');
|
|
561
|
+
json(200, { value });
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
508
564
|
if (input.method === 'http.request') {
|
|
509
565
|
const declaration = artifact.header.manifest.permissions?.find((p) => p.key === data.permissionKey && p.name === 'network.request');
|
|
510
566
|
const grant = declaration && grants.get(declaration.key);
|
package/dist/index.js
CHANGED
|
@@ -2,10 +2,11 @@ import { parseArgs } from 'node:util';
|
|
|
2
2
|
import { readFileSync } from 'node:fs';
|
|
3
3
|
import { readFile, stat } from 'node:fs/promises';
|
|
4
4
|
import { resolve, extname } from 'node:path';
|
|
5
|
-
import { createInterface } from 'node:readline/promises';
|
|
6
5
|
import { createTemplate, generateSigningKeys, parseJsonStrict, PreparedIssuer, readArtifact, signArtifact, } from '@shiqianjiang/ceru-plugin-issuer';
|
|
7
6
|
import { buildProject, scaffoldProject, TEMPLATES, writeAtomic } from './project.js';
|
|
8
7
|
import { runDev } from './dev.js';
|
|
8
|
+
import { printInitSummary, promptInit } from './init.js';
|
|
9
|
+
import { PromptCancelled } from './tui.js';
|
|
9
10
|
export { buildProject, loadProject, scaffoldProject, TEMPLATES } from './project.js';
|
|
10
11
|
const VERSION = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
|
|
11
12
|
const HELP = 'Ceru Plugin CLI ' +
|
|
@@ -81,42 +82,28 @@ export async function runCli(argv = process.argv.slice(2)) {
|
|
|
81
82
|
};
|
|
82
83
|
if (command === 'init') {
|
|
83
84
|
let destination = positionals[1];
|
|
84
|
-
if (!destination && process.stdin.isTTY) {
|
|
85
|
-
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
86
|
-
try {
|
|
87
|
-
destination = (await rl.question('项目目录 [my-ceru-plugin]: ')).trim() || 'my-ceru-plugin';
|
|
88
|
-
}
|
|
89
|
-
finally {
|
|
90
|
-
rl.close();
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (!destination)
|
|
94
|
-
throw new Error('Specify a project directory');
|
|
95
85
|
let template = flags.template;
|
|
96
86
|
let language = flags.lang;
|
|
97
|
-
|
|
98
|
-
|
|
87
|
+
const interactive = Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
88
|
+
if (interactive && (!destination || !template || !language)) {
|
|
99
89
|
try {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (!template)
|
|
105
|
-
throw new Error('Invalid template selection');
|
|
106
|
-
}
|
|
107
|
-
if (!language)
|
|
108
|
-
language = (await rl.question('语言 ts / js [ts]: ')).trim() || 'ts';
|
|
90
|
+
const answers = await promptInit({ input: process.stdin, output: process.stdout }, { destination, template, language }, VERSION);
|
|
91
|
+
destination = answers.destination;
|
|
92
|
+
template = answers.template;
|
|
93
|
+
language = answers.language;
|
|
109
94
|
}
|
|
110
|
-
|
|
111
|
-
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (error instanceof PromptCancelled) {
|
|
97
|
+
process.exitCode = 130;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
112
101
|
}
|
|
113
102
|
}
|
|
103
|
+
if (!destination)
|
|
104
|
+
throw new Error('Specify a project directory');
|
|
114
105
|
const path = await scaffoldProject(destination, { template, language });
|
|
115
|
-
|
|
116
|
-
path +
|
|
117
|
-
'\n\nNext:\n cd ' +
|
|
118
|
-
JSON.stringify(path) +
|
|
119
|
-
'\n npm install\n npm run build\n\nShare only dist/plugin.js. A v2-compatible Host is required.');
|
|
106
|
+
printInitSummary(process.stdout, path, interactive);
|
|
120
107
|
return;
|
|
121
108
|
}
|
|
122
109
|
if (command === 'build') {
|
package/dist/init.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type TuiOutput, type TuiStreams } from './tui.js';
|
|
2
|
+
export interface InitAnswers {
|
|
3
|
+
destination?: string;
|
|
4
|
+
template?: string;
|
|
5
|
+
language?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function promptInit(streams: TuiStreams, defaults: InitAnswers, version: string): Promise<Required<InitAnswers>>;
|
|
8
|
+
export declare function printInitSummary(output: TuiOutput, path: string, interactive: boolean): void;
|
package/dist/init.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// init 交互向导:目录、模板、语言三个问题,以及创建后的结果卡片。
|
|
2
|
+
// 只在交互终端启用;非交互路径继续用 index.ts 的纯文本输出,保证脚本可用。
|
|
3
|
+
import { readdir } from 'node:fs/promises';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
import { TEMPLATE_INFO, TEMPLATES } from './project.js';
|
|
6
|
+
import { createStyle, promptSelect, promptText } from './tui.js';
|
|
7
|
+
const LANGUAGES = [
|
|
8
|
+
{ value: 'ts', label: 'TypeScript', hint: '构建前做类型检查' },
|
|
9
|
+
{ value: 'js', label: 'JavaScript', hint: '不检查类型,直接构建' },
|
|
10
|
+
];
|
|
11
|
+
export async function promptInit(streams, defaults, version) {
|
|
12
|
+
const style = createStyle(streams.output);
|
|
13
|
+
streams.output.write('\n ' +
|
|
14
|
+
style.bold(style.accent('澜音插件脚手架')) +
|
|
15
|
+
' ' +
|
|
16
|
+
style.dim('v' + version) +
|
|
17
|
+
'\n ' +
|
|
18
|
+
style.dim('生成单文件插件工程 · TypeScript / JavaScript · Vue / React') +
|
|
19
|
+
'\n\n');
|
|
20
|
+
const destination = defaults.destination ??
|
|
21
|
+
(await promptText(streams, {
|
|
22
|
+
message: '插件目录',
|
|
23
|
+
initial: 'my-ceru-plugin',
|
|
24
|
+
validate: async (value) => {
|
|
25
|
+
if (!value)
|
|
26
|
+
return '请输入目录名';
|
|
27
|
+
try {
|
|
28
|
+
if ((await readdir(resolve(value))).length)
|
|
29
|
+
return '目标目录非空:' + resolve(value);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// 目录不存在时会新建,直接通过。
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
},
|
|
36
|
+
}));
|
|
37
|
+
const template = defaults.template ??
|
|
38
|
+
(await promptSelect(streams, {
|
|
39
|
+
message: '选择模板',
|
|
40
|
+
choices: TEMPLATES.map((name) => ({ value: name, label: name, hint: TEMPLATE_INFO[name] })),
|
|
41
|
+
}));
|
|
42
|
+
const language = defaults.language ??
|
|
43
|
+
(await promptSelect(streams, {
|
|
44
|
+
message: '选择语言',
|
|
45
|
+
choices: LANGUAGES,
|
|
46
|
+
}));
|
|
47
|
+
return { destination, template, language };
|
|
48
|
+
}
|
|
49
|
+
export function printInitSummary(output, path, interactive) {
|
|
50
|
+
if (!interactive) {
|
|
51
|
+
output.write('Created ' +
|
|
52
|
+
path +
|
|
53
|
+
'\n\nNext:\n cd ' +
|
|
54
|
+
JSON.stringify(path) +
|
|
55
|
+
'\n npm install\n npm run build\n\nShare only dist/plugin.js. A v2-compatible Host is required.\n');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const style = createStyle(output);
|
|
59
|
+
const steps = [
|
|
60
|
+
['cd ' + JSON.stringify(path), ''],
|
|
61
|
+
['npm install', '安装模板依赖'],
|
|
62
|
+
['npm run dev', '启动 Electron 调试工作台'],
|
|
63
|
+
['npm run build', '产出 dist/plugin.js'],
|
|
64
|
+
];
|
|
65
|
+
const width = Math.max(...steps.map(([command]) => command.length));
|
|
66
|
+
output.write('\n ' + style.success('✔') + ' ' + style.bold('已创建') + ' ' + style.accent(path) + '\n');
|
|
67
|
+
output.write('\n ' + style.bold('下一步') + '\n');
|
|
68
|
+
for (const [command, note] of steps)
|
|
69
|
+
output.write(' ' + style.accent(command.padEnd(width)) + (note ? ' ' + style.dim(note) : '') + '\n');
|
|
70
|
+
output.write('\n ' + style.dim('只分发 dist/plugin.js;安装需要 v2 兼容的澜音 Host。') + '\n\n');
|
|
71
|
+
}
|
package/dist/project.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ export declare function buildProject(project?: string, options?: {
|
|
|
31
31
|
}>;
|
|
32
32
|
export declare const TEMPLATES: readonly ["source", "connected-library", "importer", "guest-adapter", "web-surface", "vue", "vue-tsx", "react", "web-dist"];
|
|
33
33
|
export type TemplateName = (typeof TEMPLATES)[number];
|
|
34
|
+
export declare const TEMPLATE_INFO: Record<TemplateName, string>;
|
|
34
35
|
export declare function scaffoldProject(destination: string, options?: {
|
|
35
36
|
template?: string;
|
|
36
37
|
language?: string;
|
package/dist/project.js
CHANGED
|
@@ -494,6 +494,18 @@ export const TEMPLATES = [
|
|
|
494
494
|
'react',
|
|
495
495
|
'web-dist',
|
|
496
496
|
];
|
|
497
|
+
// 与根 README 的模板表保持一致;Record 保证新增模板必须补说明。
|
|
498
|
+
export const TEMPLATE_INFO = {
|
|
499
|
+
source: '本地搜索 demo、标准 Provider、结构化解析结果',
|
|
500
|
+
'connected-library': '连接表单与状态更新 demo',
|
|
501
|
+
importer: '曲目去重与导入计划预览',
|
|
502
|
+
'guest-adapter': '父插件与 Guest bootstrap 的边界',
|
|
503
|
+
'web-surface': '普通 DOM 页面',
|
|
504
|
+
vue: 'Vue 单文件组件、scoped CSS',
|
|
505
|
+
'vue-tsx': 'Vue TSX 组件',
|
|
506
|
+
react: 'React TSX/JSX、Hooks',
|
|
507
|
+
'web-dist': '封装已构建的本地 index.html、脚本和样式',
|
|
508
|
+
};
|
|
497
509
|
export async function scaffoldProject(destination, options = {}) {
|
|
498
510
|
const template = options.template ?? 'source';
|
|
499
511
|
const language = options.language ?? 'ts';
|
package/dist/tui.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface TuiInput {
|
|
2
|
+
isTTY?: boolean;
|
|
3
|
+
setRawMode?(mode: boolean): void;
|
|
4
|
+
resume?(): void;
|
|
5
|
+
pause?(): void;
|
|
6
|
+
on(event: string, listener: (...args: any[]) => void): unknown;
|
|
7
|
+
off(event: string, listener: (...args: any[]) => void): unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface TuiOutput {
|
|
10
|
+
isTTY?: boolean;
|
|
11
|
+
columns?: number;
|
|
12
|
+
hasColors?(count?: number): boolean;
|
|
13
|
+
write(chunk: string): unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface TuiStreams {
|
|
16
|
+
input: TuiInput;
|
|
17
|
+
output: TuiOutput;
|
|
18
|
+
}
|
|
19
|
+
export interface Key {
|
|
20
|
+
name?: string;
|
|
21
|
+
ctrl?: boolean;
|
|
22
|
+
meta?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare class PromptCancelled extends Error {
|
|
25
|
+
constructor();
|
|
26
|
+
}
|
|
27
|
+
export interface Style {
|
|
28
|
+
bold(text: string): string;
|
|
29
|
+
dim(text: string): string;
|
|
30
|
+
accent(text: string): string;
|
|
31
|
+
success(text: string): string;
|
|
32
|
+
error(text: string): string;
|
|
33
|
+
}
|
|
34
|
+
export declare function createStyle(output: TuiOutput): Style;
|
|
35
|
+
export interface TextOptions {
|
|
36
|
+
message: string;
|
|
37
|
+
/** 直接回车时采用的值,显示为灰色占位。 */
|
|
38
|
+
initial?: string;
|
|
39
|
+
validate?(value: string): string | undefined | Promise<string | undefined>;
|
|
40
|
+
}
|
|
41
|
+
export declare function promptText(streams: TuiStreams, options: TextOptions): Promise<string>;
|
|
42
|
+
export interface Choice<T> {
|
|
43
|
+
value: T;
|
|
44
|
+
label: string;
|
|
45
|
+
hint?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface SelectOptions<T> {
|
|
48
|
+
message: string;
|
|
49
|
+
choices: Choice<T>[];
|
|
50
|
+
initial?: number;
|
|
51
|
+
}
|
|
52
|
+
export declare function promptSelect<T>(streams: TuiStreams, options: SelectOptions<T>): Promise<T>;
|
package/dist/tui.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// 无依赖的终端界面基础件:颜色降级、行内输入、方向键选择与取消处理。
|
|
2
|
+
// 仅由 init 向导在 TTY 中调用,调用方负责判断是否具备交互条件。
|
|
3
|
+
// 颜色按 NO_COLOR / FORCE_COLOR / 终端能力降级,非 TTY 输出退化为纯文本。
|
|
4
|
+
import { emitKeypressEvents } from 'node:readline';
|
|
5
|
+
export class PromptCancelled extends Error {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Prompt cancelled');
|
|
8
|
+
this.name = 'PromptCancelled';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const CLEAR_LINE = '\r\u001b[2K';
|
|
12
|
+
const CLEAR_DOWN = '\u001b[0J';
|
|
13
|
+
const RESET = '\u001b[0m';
|
|
14
|
+
// 终端显示宽度:忽略 ANSI 序列,全角字符按两列计。
|
|
15
|
+
function displayWidth(text) {
|
|
16
|
+
let width = 0;
|
|
17
|
+
for (const char of text.replace(/\u001b\[[0-9;]*[A-Za-z]/g, '')) {
|
|
18
|
+
const code = char.codePointAt(0) ?? 0;
|
|
19
|
+
const wide = (code >= 0x1100 && code <= 0x115f) ||
|
|
20
|
+
(code >= 0x2e80 && code <= 0xa4cf) ||
|
|
21
|
+
(code >= 0xac00 && code <= 0xd7a3) ||
|
|
22
|
+
(code >= 0xf900 && code <= 0xfaff) ||
|
|
23
|
+
(code >= 0xfe30 && code <= 0xfe6f) ||
|
|
24
|
+
(code >= 0xff00 && code <= 0xff60) ||
|
|
25
|
+
(code >= 0xffe0 && code <= 0xffe6);
|
|
26
|
+
width += wide ? 2 : 1;
|
|
27
|
+
}
|
|
28
|
+
return width;
|
|
29
|
+
}
|
|
30
|
+
// 窄终端下截断长文本,避免行内重绘错位。
|
|
31
|
+
function clampText(text, max) {
|
|
32
|
+
if (max === undefined || displayWidth(text) <= max)
|
|
33
|
+
return text;
|
|
34
|
+
let result = '';
|
|
35
|
+
for (const char of text) {
|
|
36
|
+
if (displayWidth(result + char) > max - 1)
|
|
37
|
+
break;
|
|
38
|
+
result += char;
|
|
39
|
+
}
|
|
40
|
+
return result + '…';
|
|
41
|
+
}
|
|
42
|
+
// 16 色码,以及 256 色终端上更柔和的等价色。
|
|
43
|
+
const PALETTE = {
|
|
44
|
+
16: { accent: '36', success: '32', error: '31', dim: '90' },
|
|
45
|
+
256: { accent: '38;5;45', success: '38;5;42', error: '38;5;203', dim: '38;5;245' },
|
|
46
|
+
};
|
|
47
|
+
function colorDepth(output) {
|
|
48
|
+
if ('NO_COLOR' in process.env)
|
|
49
|
+
return 0;
|
|
50
|
+
const forced = process.env.FORCE_COLOR;
|
|
51
|
+
if (forced === '0')
|
|
52
|
+
return 0;
|
|
53
|
+
if (forced)
|
|
54
|
+
return forced === '1' ? 16 : 256;
|
|
55
|
+
if (!output.isTTY || process.env.TERM === 'dumb')
|
|
56
|
+
return 0;
|
|
57
|
+
return output.hasColors?.(256) ? 256 : 16;
|
|
58
|
+
}
|
|
59
|
+
export function createStyle(output) {
|
|
60
|
+
const depth = colorDepth(output);
|
|
61
|
+
const codes = depth === 256 ? PALETTE[256] : PALETTE[16];
|
|
62
|
+
const paint = (code) => (text) => depth === 0 ? text : '\u001b[' + code + 'm' + text + RESET;
|
|
63
|
+
return {
|
|
64
|
+
bold: paint('1'),
|
|
65
|
+
dim: paint(codes.dim),
|
|
66
|
+
accent: paint(codes.accent),
|
|
67
|
+
success: paint(codes.success),
|
|
68
|
+
error: paint(codes.error),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// 接管按键:进入原始模式并监听 keypress;返回的清理函数恢复终端状态。
|
|
72
|
+
function listen(streams, onKey) {
|
|
73
|
+
const { input } = streams;
|
|
74
|
+
const raw = input.isTTY === true && typeof input.setRawMode === 'function';
|
|
75
|
+
emitKeypressEvents(input);
|
|
76
|
+
if (raw)
|
|
77
|
+
input.setRawMode?.(true);
|
|
78
|
+
input.resume?.();
|
|
79
|
+
const handler = (text, key) => onKey(typeof text === 'string' ? text : '', key ?? {});
|
|
80
|
+
input.on('keypress', handler);
|
|
81
|
+
let closed = false;
|
|
82
|
+
const close = () => {
|
|
83
|
+
if (closed)
|
|
84
|
+
return;
|
|
85
|
+
closed = true;
|
|
86
|
+
input.off('keypress', handler);
|
|
87
|
+
if (raw)
|
|
88
|
+
input.setRawMode?.(false);
|
|
89
|
+
input.pause?.();
|
|
90
|
+
process.off('exit', close);
|
|
91
|
+
};
|
|
92
|
+
process.once('exit', close);
|
|
93
|
+
return close;
|
|
94
|
+
}
|
|
95
|
+
export async function promptText(streams, options) {
|
|
96
|
+
const style = createStyle(streams.output);
|
|
97
|
+
const suffix = options.initial ? ' ' + style.dim('(' + options.initial + ')') : '';
|
|
98
|
+
const head = ' ' +
|
|
99
|
+
style.accent('?') +
|
|
100
|
+
' ' +
|
|
101
|
+
style.bold(options.message) +
|
|
102
|
+
suffix +
|
|
103
|
+
' ' +
|
|
104
|
+
style.accent('›') +
|
|
105
|
+
' ';
|
|
106
|
+
let value = '';
|
|
107
|
+
const write = (line) => streams.output.write(CLEAR_LINE + line);
|
|
108
|
+
// 已输入内容占满一行时不再覆盖本行,避免折行残影。
|
|
109
|
+
const collapse = (line) => streams.output.write((streams.output.columns === undefined || displayWidth(head + value) < streams.output.columns
|
|
110
|
+
? CLEAR_LINE
|
|
111
|
+
: '\n') + line);
|
|
112
|
+
return await new Promise((resolve, reject) => {
|
|
113
|
+
let settled = false;
|
|
114
|
+
let stop = () => { };
|
|
115
|
+
const finish = (done) => {
|
|
116
|
+
if (settled)
|
|
117
|
+
return;
|
|
118
|
+
settled = true;
|
|
119
|
+
stop();
|
|
120
|
+
done();
|
|
121
|
+
};
|
|
122
|
+
const cancel = () => finish(() => {
|
|
123
|
+
collapse(' ' + style.error('■') + ' ' + style.dim('已取消') + '\n');
|
|
124
|
+
reject(new PromptCancelled());
|
|
125
|
+
});
|
|
126
|
+
const submit = async () => {
|
|
127
|
+
const answer = (value.trim() || options.initial || '').trim();
|
|
128
|
+
let problem;
|
|
129
|
+
try {
|
|
130
|
+
problem = await options.validate?.(answer);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
problem = error instanceof Error ? error.message : String(error);
|
|
134
|
+
}
|
|
135
|
+
if (problem) {
|
|
136
|
+
value = '';
|
|
137
|
+
streams.output.write('\n' + ' ' + style.error('✖') + ' ' + style.dim(problem) + '\n' + head);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
finish(() => {
|
|
141
|
+
collapse(' ' +
|
|
142
|
+
style.success('✔') +
|
|
143
|
+
' ' +
|
|
144
|
+
style.dim(options.message) +
|
|
145
|
+
' ' +
|
|
146
|
+
style.bold(answer) +
|
|
147
|
+
'\n');
|
|
148
|
+
resolve(answer);
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
stop = listen(streams, (text, key) => {
|
|
152
|
+
if (key.ctrl && key.name === 'c')
|
|
153
|
+
return cancel();
|
|
154
|
+
if (key.name === 'escape')
|
|
155
|
+
return cancel();
|
|
156
|
+
if (key.name === 'return' || key.name === 'enter') {
|
|
157
|
+
void submit();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (key.name === 'backspace') {
|
|
161
|
+
value = Array.from(value).slice(0, -1).join('');
|
|
162
|
+
write(head + value);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (key.ctrl && key.name === 'u') {
|
|
166
|
+
value = '';
|
|
167
|
+
write(head + value);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (!text || key.ctrl || key.meta || /[\u0000-\u001f\u007f]/.test(text))
|
|
171
|
+
return;
|
|
172
|
+
value += text;
|
|
173
|
+
write(head + value);
|
|
174
|
+
});
|
|
175
|
+
write(head + value);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
export async function promptSelect(streams, options) {
|
|
179
|
+
const style = createStyle(streams.output);
|
|
180
|
+
const choices = options.choices;
|
|
181
|
+
if (!choices.length)
|
|
182
|
+
throw new Error('promptSelect requires at least one choice');
|
|
183
|
+
const width = Math.max(...choices.map((choice) => choice.label.length));
|
|
184
|
+
let active = Math.min(Math.max(options.initial ?? 0, 0), choices.length - 1);
|
|
185
|
+
const columns = streams.output.columns;
|
|
186
|
+
const message = ' ' + style.accent('?') + ' ' + style.bold(options.message);
|
|
187
|
+
const hint = '↑/↓ 移动 · 数字键跳转 · Enter 确认 · Ctrl+C 取消';
|
|
188
|
+
const head = columns !== undefined && displayWidth(message + ' ' + hint) >= columns
|
|
189
|
+
? message
|
|
190
|
+
: message + ' ' + style.dim(hint);
|
|
191
|
+
const hintWidth = columns === undefined ? undefined : columns - 6 - String(choices.length).length - width;
|
|
192
|
+
const rows = () => choices.map((choice, index) => {
|
|
193
|
+
const current = index === active;
|
|
194
|
+
const label = choice.label.padEnd(width);
|
|
195
|
+
const note = choice.hint && (hintWidth ?? 4) >= 4 ? clampText(choice.hint, hintWidth) : '';
|
|
196
|
+
return (' ' +
|
|
197
|
+
(current ? style.accent('❯') : ' ') +
|
|
198
|
+
' ' +
|
|
199
|
+
style.dim(String(index + 1) + '.') +
|
|
200
|
+
' ' +
|
|
201
|
+
(current ? style.bold(style.accent(label)) : label) +
|
|
202
|
+
(note ? ' ' + style.dim(note) : ''));
|
|
203
|
+
});
|
|
204
|
+
let drawn = false;
|
|
205
|
+
const draw = () => {
|
|
206
|
+
if (drawn)
|
|
207
|
+
streams.output.write('\u001b[' + choices.length + 'A');
|
|
208
|
+
streams.output.write(rows()
|
|
209
|
+
.map((row) => CLEAR_LINE + row + '\n')
|
|
210
|
+
.join(''));
|
|
211
|
+
drawn = true;
|
|
212
|
+
};
|
|
213
|
+
streams.output.write(head + '\n');
|
|
214
|
+
draw();
|
|
215
|
+
return await new Promise((resolve, reject) => {
|
|
216
|
+
let settled = false;
|
|
217
|
+
let stop = () => { };
|
|
218
|
+
const finish = (done) => {
|
|
219
|
+
if (settled)
|
|
220
|
+
return;
|
|
221
|
+
settled = true;
|
|
222
|
+
stop();
|
|
223
|
+
streams.output.write('\u001b[' + (choices.length + 1) + 'A' + CLEAR_DOWN);
|
|
224
|
+
done();
|
|
225
|
+
};
|
|
226
|
+
const cancel = () => finish(() => {
|
|
227
|
+
streams.output.write(' ' + style.error('■') + ' ' + style.dim('已取消') + '\n');
|
|
228
|
+
reject(new PromptCancelled());
|
|
229
|
+
});
|
|
230
|
+
const confirm = (index) => finish(() => {
|
|
231
|
+
streams.output.write(' ' +
|
|
232
|
+
style.success('✔') +
|
|
233
|
+
' ' +
|
|
234
|
+
style.dim(options.message) +
|
|
235
|
+
' ' +
|
|
236
|
+
style.bold(choices[index].label) +
|
|
237
|
+
'\n');
|
|
238
|
+
resolve(choices[index].value);
|
|
239
|
+
});
|
|
240
|
+
const move = (delta) => {
|
|
241
|
+
active = (active + delta + choices.length) % choices.length;
|
|
242
|
+
draw();
|
|
243
|
+
};
|
|
244
|
+
stop = listen(streams, (text, key) => {
|
|
245
|
+
if (key.ctrl && key.name === 'c')
|
|
246
|
+
return cancel();
|
|
247
|
+
if (key.name === 'escape')
|
|
248
|
+
return cancel();
|
|
249
|
+
if (key.name === 'up' || key.name === 'k')
|
|
250
|
+
return move(-1);
|
|
251
|
+
if (key.name === 'down' || key.name === 'j')
|
|
252
|
+
return move(1);
|
|
253
|
+
if (key.name === 'return' || key.name === 'enter')
|
|
254
|
+
return confirm(active);
|
|
255
|
+
if (/^[1-9]$/.test(text) && Number(text) <= choices.length) {
|
|
256
|
+
active = Number(text) - 1;
|
|
257
|
+
draw();
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shiqianjiang/ceru-plugin-cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "TypeScript scaffolding, single-file build, watch, validation, signing and issuance for Ceru plugins",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@babel/core": "^7.29.7",
|
|
35
|
-
"@shiqianjiang/ceru-plugin-issuer": "0.3.
|
|
36
|
-
"@shiqianjiang/ceru-plugin-sdk": "0.3.
|
|
35
|
+
"@shiqianjiang/ceru-plugin-issuer": "0.3.8",
|
|
36
|
+
"@shiqianjiang/ceru-plugin-sdk": "0.3.8",
|
|
37
37
|
"@vue/babel-plugin-jsx": "^2.0.1",
|
|
38
38
|
"@vue/compiler-sfc": "3.5.43",
|
|
39
39
|
"axios": "^1.20.0",
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"preview": "ceru-plugin preview dist/plugin.js"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@shiqianjiang/ceru-plugin-sdk": "^0.3.
|
|
14
|
+
"@shiqianjiang/ceru-plugin-sdk": "^0.3.8"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@shiqianjiang/ceru-plugin-cli": "^0.3.
|
|
17
|
+
"@shiqianjiang/ceru-plugin-cli": "^0.3.8",
|
|
18
18
|
"typescript": "~5.9.3",
|
|
19
19
|
"electron": "^44.4.2",
|
|
20
20
|
"prettier": "^3.6.2"
|