@easyv/screen-cli 0.0.0-stage → 0.1.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 +283 -2
- package/bin/easyv-screen.js +2 -0
- package/dist/analyzer.js +130 -0
- package/dist/client.js +168 -0
- package/dist/commands/auth.js +285 -0
- package/dist/commands/component.js +197 -0
- package/dist/commands/data.js +147 -0
- package/dist/commands/debug.js +31 -0
- package/dist/commands/screen.js +199 -0
- package/dist/commands/skills.js +53 -0
- package/dist/config.js +421 -0
- package/dist/debug-query.js +224 -0
- package/dist/index.js +25 -0
- package/dist/output.js +7 -0
- package/package.json +36 -4
- package/skills/easyv-component-dev/SKILL.md +50 -0
- package/skills/easyv-component-dev/references/component-spec.md +123 -0
- package/skills/easyv-screen-build/SKILL.md +40 -0
- package/skills/easyv-screen-data/SKILL.md +53 -0
- package/skills/easyv-screen-debug/SKILL.md +49 -0
- package/skills/easyv-screen-edit/SKILL.md +59 -0
- package/skills/easyv-screen-shared/SKILL.md +32 -0
- package/spec/openapi.v1.yaml +2850 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerAuth = registerAuth;
|
|
4
|
+
exports.globalJson = globalJson;
|
|
5
|
+
const config_1 = require("../config");
|
|
6
|
+
const client_1 = require("../client");
|
|
7
|
+
const output_1 = require("../output");
|
|
8
|
+
function registerAuth(program) {
|
|
9
|
+
const auth = program.command('auth').description('登录与凭证');
|
|
10
|
+
auth.command('use')
|
|
11
|
+
.description('切换当前工作台下的登录')
|
|
12
|
+
.argument('<name>', '登录名称')
|
|
13
|
+
.action((name) => {
|
|
14
|
+
try {
|
|
15
|
+
(0, config_1.useKey)(name);
|
|
16
|
+
console.log(`已切换到「${name}」`);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
(0, client_1.exitOnError)(error);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
auth.command('list')
|
|
23
|
+
.description('列出当前工作台下的登录')
|
|
24
|
+
.action(function () {
|
|
25
|
+
try {
|
|
26
|
+
const target = (0, config_1.activeTarget)();
|
|
27
|
+
const keys = (0, config_1.listKeys)();
|
|
28
|
+
(0, output_1.emit)(globalJson(this), workspacePayload({ baseUrl: target?.baseUrl ?? null, keys }), () => {
|
|
29
|
+
if (target == null) {
|
|
30
|
+
console.log((0, config_1.missingWorkspaceMessage)());
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
console.log((0, config_1.hasMultipleWorkspaces)() ? `环境「${target.name}」 ${target.baseUrl}` : target.baseUrl);
|
|
34
|
+
if (keys.length === 0) {
|
|
35
|
+
console.log('这个地址下还没有登录');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
for (const key of keys)
|
|
39
|
+
console.log(`${key.current ? '* ' : ' '}${key.name} ${key.appId}`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
(0, client_1.exitOnError)(error);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
auth.command('remove')
|
|
47
|
+
.description('删除当前工作台下的一个登录')
|
|
48
|
+
.argument('<name>', '登录名称')
|
|
49
|
+
.action((name) => {
|
|
50
|
+
try {
|
|
51
|
+
(0, config_1.removeKey)(name);
|
|
52
|
+
console.log(`已删除「${name}」`);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
(0, client_1.exitOnError)(error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
auth.command('login')
|
|
59
|
+
.description('在浏览器确认后保存登录,过期后自动刷新')
|
|
60
|
+
.option('--name <name>', '保存时使用的名称', 'default')
|
|
61
|
+
.action(async (options) => {
|
|
62
|
+
try {
|
|
63
|
+
await deviceLogin(options.name);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
(0, client_1.exitOnError)(error);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
auth.command('status')
|
|
70
|
+
.description('查看工作台地址、当前登录,以及绑定的空间和团队')
|
|
71
|
+
.action(async function () {
|
|
72
|
+
try {
|
|
73
|
+
const target = (0, config_1.activeTarget)();
|
|
74
|
+
const keys = (0, config_1.listKeys)();
|
|
75
|
+
const binding = target?.token != null ? await fetchBinding() : null;
|
|
76
|
+
(0, output_1.emit)(globalJson(this), workspacePayload({
|
|
77
|
+
baseUrl: target?.baseUrl ?? null,
|
|
78
|
+
key: target?.keyName ?? null,
|
|
79
|
+
space: binding?.space ?? null,
|
|
80
|
+
team: binding?.team ?? null,
|
|
81
|
+
keys,
|
|
82
|
+
}), () => {
|
|
83
|
+
if ((0, config_1.hasMultipleWorkspaces)())
|
|
84
|
+
console.log(`环境:${target?.name ?? '未选择'}`);
|
|
85
|
+
console.log(`baseURL:${target?.baseUrl ?? '未配置'}`);
|
|
86
|
+
console.log(`当前登录:${target?.keyName ?? '未登录'}(这个地址下共 ${keys.length} 个)`);
|
|
87
|
+
console.log(`空间:${formatSide(binding?.space ?? null)}`);
|
|
88
|
+
console.log(`团队:${formatSide(binding?.team ?? null)}`);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
(0, client_1.exitOnError)(error);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
auth.command('logout')
|
|
96
|
+
.description('移除当前工作台下正在使用的登录')
|
|
97
|
+
.action(async () => {
|
|
98
|
+
try {
|
|
99
|
+
const target = (0, config_1.activeTarget)();
|
|
100
|
+
if (target?.keyName == null)
|
|
101
|
+
throw new Error('还没有登录');
|
|
102
|
+
if ((0, config_1.isSession)(target.credential))
|
|
103
|
+
await revokeQuietly((0, config_1.apiEndpoint)(target.baseUrl), target.credential.refreshToken);
|
|
104
|
+
const keyName = target.keyName;
|
|
105
|
+
const environment = target.name;
|
|
106
|
+
(0, config_1.removeKey)(keyName);
|
|
107
|
+
console.log((0, config_1.hasMultipleWorkspaces)()
|
|
108
|
+
? `已移除环境「${environment}」下的登录「${keyName}」`
|
|
109
|
+
: `已移除登录「${keyName}」`);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
(0, client_1.exitOnError)(error);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
const config = program.command('config').description('配置工作台地址。多个工作台时,登录状态互相隔离');
|
|
116
|
+
config.command('set')
|
|
117
|
+
.description('设置工作台地址。已有多个工作台时请改用 add 和 use')
|
|
118
|
+
.requiredOption('--base-url <url>', '工作台地址,例如 https://workspace.example.com 或 http://127.0.0.1:3000')
|
|
119
|
+
.action((options) => {
|
|
120
|
+
try {
|
|
121
|
+
const result = (0, config_1.setBaseUrl)(options.baseUrl);
|
|
122
|
+
if (result.created)
|
|
123
|
+
console.log(`已配置工作台 ${result.baseUrl}`);
|
|
124
|
+
else if (result.clearedKeys)
|
|
125
|
+
console.log(`已将工作台地址改为 ${result.baseUrl}。原有的登录已清除,请重新登录`);
|
|
126
|
+
else if (result.changed)
|
|
127
|
+
console.log(`已将工作台地址改为 ${result.baseUrl}`);
|
|
128
|
+
else
|
|
129
|
+
console.log(`工作台地址已经是 ${result.baseUrl}`);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
(0, client_1.exitOnError)(error);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
config.command('add')
|
|
136
|
+
.description('添加一个工作台地址')
|
|
137
|
+
.requiredOption('--name <name>', '环境名称,用于切换')
|
|
138
|
+
.requiredOption('--base-url <url>', '工作台地址,例如 https://workspace.example.com 或 http://127.0.0.1:3000')
|
|
139
|
+
.option('--use', '添加后立即切换到这个环境')
|
|
140
|
+
.action((options) => {
|
|
141
|
+
try {
|
|
142
|
+
(0, config_1.addEnvironment)({ name: options.name, baseUrl: options.baseUrl, select: options.use === true });
|
|
143
|
+
const current = (0, config_1.activeTarget)();
|
|
144
|
+
console.log(`已添加环境「${options.name}」${current?.name === options.name ? ',并切换过去' : ''}`);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
(0, client_1.exitOnError)(error);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
config.command('use')
|
|
151
|
+
.description('切换到另一个工作台,之后使用这个地址下的登录')
|
|
152
|
+
.argument('<name>', '环境名称')
|
|
153
|
+
.action((name) => {
|
|
154
|
+
try {
|
|
155
|
+
const env = (0, config_1.useEnvironment)(name);
|
|
156
|
+
console.log(`已切换到环境「${env.name}」 ${env.baseUrl}`);
|
|
157
|
+
console.log(env.currentKey != null ? `当前登录:${env.currentKey}` : '这个地址下还没有登录');
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
(0, client_1.exitOnError)(error);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
config.command('list')
|
|
164
|
+
.description('查看工作台地址。有多个工作台时列出全部环境')
|
|
165
|
+
.action(function () {
|
|
166
|
+
const workspace = (0, config_1.describeWorkspace)();
|
|
167
|
+
(0, output_1.emit)(globalJson(this), workspace, () => {
|
|
168
|
+
if (workspace.environments != null) {
|
|
169
|
+
for (const env of workspace.environments) {
|
|
170
|
+
const key = env.currentKey ?? '未登录';
|
|
171
|
+
console.log(`${env.current ? '* ' : ' '}${env.name} ${env.baseUrl} 登录 ${key}(${env.keys.length})`);
|
|
172
|
+
}
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (workspace.baseUrl == null) {
|
|
176
|
+
console.log((0, config_1.missingWorkspaceMessage)());
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const key = workspace.currentKey ?? '未登录';
|
|
180
|
+
console.log(`${workspace.baseUrl} 登录 ${key}(${workspace.keys.length})`);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
config.command('remove')
|
|
184
|
+
.description('删除工作台及其全部登录。有多个工作台时要写上环境名称')
|
|
185
|
+
.argument('[name]', '环境名称。只有一个工作台时可以不写')
|
|
186
|
+
.action((name) => {
|
|
187
|
+
try {
|
|
188
|
+
const multiple = (0, config_1.hasMultipleWorkspaces)();
|
|
189
|
+
const removed = (0, config_1.removeEnvironment)(name);
|
|
190
|
+
console.log(multiple ? `已删除环境「${removed}」及其登录` : '已删除工作台及其登录');
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
(0, client_1.exitOnError)(error);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
async function deviceLogin(keyName) {
|
|
198
|
+
const target = (0, config_1.activeTarget)();
|
|
199
|
+
if (target == null)
|
|
200
|
+
throw new client_1.ApiError('BAD_REQUEST', (0, config_1.missingWorkspaceMessage)(), 0);
|
|
201
|
+
const client = (0, client_1.createClient)();
|
|
202
|
+
const code = await client.request('POST', '/oauth/device', {
|
|
203
|
+
anonymous: true,
|
|
204
|
+
body: { client_id: client_1.CLI_CLIENT_ID },
|
|
205
|
+
});
|
|
206
|
+
const url = (0, config_1.joinBase)(target.baseUrl, code.verificationPath);
|
|
207
|
+
console.log('在浏览器打开下面的地址并确认授权:');
|
|
208
|
+
console.log(url);
|
|
209
|
+
const deadline = Date.now() + code.expires_in * 1000;
|
|
210
|
+
while (Date.now() < deadline) {
|
|
211
|
+
await sleep(code.interval * 1000);
|
|
212
|
+
try {
|
|
213
|
+
const session = await (0, client_1.exchangeToken)((0, config_1.apiEndpoint)(target.baseUrl), {
|
|
214
|
+
grant_type: client_1.DEVICE_GRANT,
|
|
215
|
+
device_code: code.device_code,
|
|
216
|
+
client_id: client_1.CLI_CLIENT_ID,
|
|
217
|
+
});
|
|
218
|
+
(0, config_1.addSession)(keyName, session);
|
|
219
|
+
const binding = await fetchBinding();
|
|
220
|
+
const where = (0, config_1.hasMultipleWorkspaces)() ? `环境「${target.name}」的「${keyName}」` : `「${keyName}」`;
|
|
221
|
+
console.log(`授权成功,已保存为${where}。访问令牌会自动刷新,绑定${formatSide(binding.space, '空间')}、${formatSide(binding.team, '团队')}`);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
if (error instanceof client_1.ApiError && error.errorCode === 'authorization_pending')
|
|
226
|
+
continue;
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
throw new client_1.ApiError('DEVICE_CODE_EXPIRED', '授权超时,请重新运行 easyv-screen auth login', 400);
|
|
231
|
+
}
|
|
232
|
+
async function revokeQuietly(baseUrl, refreshToken) {
|
|
233
|
+
try {
|
|
234
|
+
await fetch(`${baseUrl.replace(/\/$/, '')}/oauth/revoke`, {
|
|
235
|
+
method: 'POST',
|
|
236
|
+
headers: { 'content-type': 'application/json' },
|
|
237
|
+
body: JSON.stringify({ token: refreshToken, client_id: client_1.CLI_CLIENT_ID }),
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
// 网络失败时仍然清掉本地登录状态
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/** 向当前 Key 查询绑定的空间和团队。旧服务端没有名称时,name 为 null */
|
|
245
|
+
async function fetchBinding() {
|
|
246
|
+
const meta = await (0, client_1.createClient)().request('GET', '/meta');
|
|
247
|
+
const context = meta.context ?? {};
|
|
248
|
+
return {
|
|
249
|
+
space: { id: context.spaceId ?? null, name: nonempty(context.spaceName) },
|
|
250
|
+
team: { id: context.teamId ?? null, name: nonempty(context.teamName) },
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/** 只有一个工作台时不带环境名,多个工作台时带上 environment */
|
|
254
|
+
function workspacePayload(fields) {
|
|
255
|
+
if (!(0, config_1.hasMultipleWorkspaces)())
|
|
256
|
+
return fields;
|
|
257
|
+
return { environment: (0, config_1.activeTarget)()?.name ?? null, ...fields };
|
|
258
|
+
}
|
|
259
|
+
function nonempty(value) {
|
|
260
|
+
if (value == null || value.trim() === '')
|
|
261
|
+
return null;
|
|
262
|
+
return value;
|
|
263
|
+
}
|
|
264
|
+
function formatSide(side, label) {
|
|
265
|
+
const prefix = label ?? '';
|
|
266
|
+
if (side == null)
|
|
267
|
+
return `${prefix}未查询`;
|
|
268
|
+
if (side.name != null && side.id != null)
|
|
269
|
+
return `${prefix}「${side.name}」(${side.id})`;
|
|
270
|
+
if (side.name != null)
|
|
271
|
+
return `${prefix}「${side.name}」`;
|
|
272
|
+
return `${prefix} ${side.id ?? '未知'}`.trim();
|
|
273
|
+
}
|
|
274
|
+
function sleep(ms) {
|
|
275
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
276
|
+
}
|
|
277
|
+
function globalJson(command) {
|
|
278
|
+
let current = command;
|
|
279
|
+
while (current != null) {
|
|
280
|
+
if (current.opts().json === true)
|
|
281
|
+
return true;
|
|
282
|
+
current = current.parent;
|
|
283
|
+
}
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerComponent = registerComponent;
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const client_1 = require("../client");
|
|
8
|
+
const output_1 = require("../output");
|
|
9
|
+
const auth_1 = require("./auth");
|
|
10
|
+
const PROJECT_DIR = 'easyv-components';
|
|
11
|
+
function registerComponent(program) {
|
|
12
|
+
const component = program.command('component').description('组件:目录、schema,以及从零开发');
|
|
13
|
+
component.command('list')
|
|
14
|
+
.description('列出可用组件')
|
|
15
|
+
.option('--keyword <keyword>', '按名称或 module 过滤')
|
|
16
|
+
.option('--source <source>', 'platform 或 space')
|
|
17
|
+
.action(async function (options) {
|
|
18
|
+
try {
|
|
19
|
+
const result = await (0, client_1.createClient)().request('GET', '/components', {
|
|
20
|
+
query: { keyword: options.keyword },
|
|
21
|
+
});
|
|
22
|
+
const items = (result.components ?? []).filter(item => options.source == null || item.source === options.source);
|
|
23
|
+
(0, output_1.emit)((0, auth_1.globalJson)(this), { components: items }, () => {
|
|
24
|
+
for (const item of items)
|
|
25
|
+
console.log(`${item.source}\t${item.status}\t${item.module}@${item.version}\t${item.name}`);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
(0, client_1.exitOnError)(error);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
component.command('schema')
|
|
33
|
+
.description('查看组件的配置结构')
|
|
34
|
+
.argument('<module>')
|
|
35
|
+
.option('--version <version>')
|
|
36
|
+
.action(async function (moduleName, options) {
|
|
37
|
+
try {
|
|
38
|
+
const result = await (0, client_1.createClient)().request('GET', `/components/${moduleName}/schema`, {
|
|
39
|
+
query: { version: options.version },
|
|
40
|
+
});
|
|
41
|
+
(0, output_1.emit)((0, auth_1.globalJson)(this), result, () => console.log(JSON.stringify(result, null, 2)));
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
(0, client_1.exitOnError)(error);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
component.command('fields')
|
|
48
|
+
.description('按意图查组件配置字段,如 --keyword "图例 宽度";不传关键词只看分组')
|
|
49
|
+
.argument('<module>')
|
|
50
|
+
.option('--keyword <keyword>', '中文标签或路径,空格分隔的多个词需要同时命中')
|
|
51
|
+
.option('--version <version>')
|
|
52
|
+
.option('--limit <limit>', '最多返回多少条,默认 30')
|
|
53
|
+
.action(async function (moduleName, options) {
|
|
54
|
+
try {
|
|
55
|
+
const result = await (0, client_1.createClient)().request('GET', `/components/${moduleName}/config-fields`, {
|
|
56
|
+
query: { keyword: options.keyword, version: options.version, limit: options.limit },
|
|
57
|
+
});
|
|
58
|
+
(0, output_1.emit)((0, auth_1.globalJson)(this), result, () => console.log(JSON.stringify(result, null, 2)));
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
(0, client_1.exitOnError)(error);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
component.command('init')
|
|
65
|
+
.description('在当前目录创建或复用组件工程 ./easyv-components')
|
|
66
|
+
.action(() => {
|
|
67
|
+
const packageFile = (0, node_path_1.join)(PROJECT_DIR, 'package.json');
|
|
68
|
+
if ((0, node_fs_1.existsSync)(packageFile)) {
|
|
69
|
+
console.log(`${PROJECT_DIR} 已存在,直接使用`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(PROJECT_DIR, 'src', 'components'), { recursive: true });
|
|
73
|
+
(0, node_fs_1.writeFileSync)(packageFile, JSON.stringify({
|
|
74
|
+
name: 'easyv-components',
|
|
75
|
+
version: '0.1.0',
|
|
76
|
+
private: true,
|
|
77
|
+
scripts: { start: 'easyv dev' },
|
|
78
|
+
easyv: {
|
|
79
|
+
components: 'src/components',
|
|
80
|
+
builder: 'react-component-builder',
|
|
81
|
+
},
|
|
82
|
+
}, null, 2));
|
|
83
|
+
console.log(`已创建 ${PROJECT_DIR}。打包签名需要先配置 easyv-cli 的 Access ID(只需一次)`);
|
|
84
|
+
});
|
|
85
|
+
component.command('create')
|
|
86
|
+
.description('在组件工程里新增一个组件骨架,module 自动加 ai_ 前缀')
|
|
87
|
+
.argument('<name>', '组件名,如 kpi-card')
|
|
88
|
+
.option('--title <title>', '显示名')
|
|
89
|
+
.action((name, options) => {
|
|
90
|
+
const moduleName = name.startsWith('ai_') ? name : `ai_${name.replace(/-/g, '_')}`;
|
|
91
|
+
const dir = (0, node_path_1.join)(PROJECT_DIR, 'src', 'components', moduleName);
|
|
92
|
+
if ((0, node_fs_1.existsSync)(dir)) {
|
|
93
|
+
console.error(`组件 ${moduleName} 已存在`);
|
|
94
|
+
process.exit(2);
|
|
95
|
+
}
|
|
96
|
+
if (!(0, node_fs_1.existsSync)((0, node_path_1.join)(PROJECT_DIR, 'package.json'))) {
|
|
97
|
+
console.error('还没有组件工程。先运行 easyv-screen component init');
|
|
98
|
+
process.exit(2);
|
|
99
|
+
}
|
|
100
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(dir, 'config'), { recursive: true });
|
|
101
|
+
const config = {
|
|
102
|
+
base: { name: options.title ?? name, module_name: moduleName, version: '1.0.0', show: 1 },
|
|
103
|
+
width: 400,
|
|
104
|
+
height: 300,
|
|
105
|
+
configuration: [],
|
|
106
|
+
triggers: [{ name: '鼠标点击', value: 'click' }],
|
|
107
|
+
actions: [],
|
|
108
|
+
};
|
|
109
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'config', 'main.json'), JSON.stringify(config, null, 2));
|
|
110
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'config', 'main.data.json'), JSON.stringify({ data: [], fields: [] }, null, 2));
|
|
111
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(dir, 'index.jsx'), componentSource());
|
|
112
|
+
console.log(`已创建组件 ${moduleName},目录 ${dir}`);
|
|
113
|
+
});
|
|
114
|
+
component.command('build')
|
|
115
|
+
.description('用 easyv-cli 打包并签名组件')
|
|
116
|
+
.argument('<name>', '组件目录名')
|
|
117
|
+
.action((name) => {
|
|
118
|
+
const result = (0, node_child_process_1.spawnSync)('easyv', ['pack', name], { cwd: PROJECT_DIR, stdio: 'inherit' });
|
|
119
|
+
if (result.status !== 0)
|
|
120
|
+
process.exit(result.status ?? 1);
|
|
121
|
+
});
|
|
122
|
+
component.command('publish')
|
|
123
|
+
.description('打包并上传组件。版本不高于平台已有版本时自动升一个补丁号')
|
|
124
|
+
.argument('<name>', '组件目录名')
|
|
125
|
+
.action(async (name) => {
|
|
126
|
+
try {
|
|
127
|
+
await publishComponent(name);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
(0, client_1.exitOnError)(error);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async function publishComponent(name) {
|
|
135
|
+
const configFile = (0, node_path_1.join)(PROJECT_DIR, 'src', 'components', name, 'config', 'main.json');
|
|
136
|
+
if (!(0, node_fs_1.existsSync)(configFile))
|
|
137
|
+
throw new Error(`找不到组件 ${name}。先运行 easyv-screen component create`);
|
|
138
|
+
const config = JSON.parse((0, node_fs_1.readFileSync)(configFile, 'utf8'));
|
|
139
|
+
const remote = await remoteVersion(config.base.module_name);
|
|
140
|
+
if (remote != null && compareVersion(config.base.version, remote) <= 0) {
|
|
141
|
+
config.base.version = bumpVersion(remote);
|
|
142
|
+
(0, node_fs_1.writeFileSync)(configFile, JSON.stringify(config, null, 2));
|
|
143
|
+
console.log(`平台已有 ${remote},本地版本提升到 ${config.base.version}`);
|
|
144
|
+
}
|
|
145
|
+
const build = (0, node_child_process_1.spawnSync)('easyv', ['pack', name], { cwd: PROJECT_DIR, stdio: 'inherit' });
|
|
146
|
+
if (build.status !== 0)
|
|
147
|
+
process.exit(build.status ?? 1);
|
|
148
|
+
const constantFile = (0, node_path_1.join)(PROJECT_DIR, 'build', `${name}.${config.base.version}.constant`);
|
|
149
|
+
if (!(0, node_fs_1.existsSync)(constantFile))
|
|
150
|
+
throw new Error(`没有找到打包产物 ${constantFile}`);
|
|
151
|
+
const uploaded = await (0, client_1.createClient)().request('POST', '/components', {
|
|
152
|
+
file: {
|
|
153
|
+
field: 'file',
|
|
154
|
+
filename: `${name}.constant`,
|
|
155
|
+
contentType: 'application/octet-stream',
|
|
156
|
+
data: (0, node_fs_1.readFileSync)(constantFile),
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
console.log(`已上传 ${uploaded.module}@${uploaded.version}(${uploaded.status})`);
|
|
160
|
+
}
|
|
161
|
+
async function remoteVersion(moduleName) {
|
|
162
|
+
const result = await (0, client_1.createClient)().request('GET', '/components', {
|
|
163
|
+
query: { keyword: moduleName },
|
|
164
|
+
});
|
|
165
|
+
const matched = (result.components ?? []).find(item => item.module === moduleName && item.source === 'space');
|
|
166
|
+
return matched?.version;
|
|
167
|
+
}
|
|
168
|
+
function compareVersion(left, right) {
|
|
169
|
+
const a = left.split('.').map(Number);
|
|
170
|
+
const b = right.split('.').map(Number);
|
|
171
|
+
for (let index = 0; index < 3; index++) {
|
|
172
|
+
const diff = (a[index] ?? 0) - (b[index] ?? 0);
|
|
173
|
+
if (diff !== 0)
|
|
174
|
+
return diff;
|
|
175
|
+
}
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
|
178
|
+
function bumpVersion(version) {
|
|
179
|
+
const parts = version.split('.').map(Number);
|
|
180
|
+
parts[2] = (parts[2] ?? 0) + 1;
|
|
181
|
+
return parts.join('.');
|
|
182
|
+
}
|
|
183
|
+
function componentSource() {
|
|
184
|
+
return `import React from 'react';
|
|
185
|
+
|
|
186
|
+
export default function (props) {
|
|
187
|
+
const { left, top, width, height, id, data, configuration, emit } = props;
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<div className="__easyv-component" id={id} style={{ position: 'absolute', left, top, width, height }}>
|
|
191
|
+
{/* configuration 是按 main.json 的 configuration 归约后的配置 */}
|
|
192
|
+
{/* 触发事件:emit('click', data) */}
|
|
193
|
+
</div>
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
`;
|
|
197
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerData = registerData;
|
|
4
|
+
const client_1 = require("../client");
|
|
5
|
+
const output_1 = require("../output");
|
|
6
|
+
const auth_1 = require("./auth");
|
|
7
|
+
/**
|
|
8
|
+
* 数据源命令。
|
|
9
|
+
* 密码只接受环境变量或标准输入,不接受命令行参数,避免留在 shell 历史里。
|
|
10
|
+
*/
|
|
11
|
+
function registerData(program) {
|
|
12
|
+
const data = program.command('data').description('数据源');
|
|
13
|
+
const source = data.command('source').description('数据源连接');
|
|
14
|
+
source.command('list')
|
|
15
|
+
.description('列出数据源')
|
|
16
|
+
.option('--type <dataType>', '按类型过滤,如 mysql')
|
|
17
|
+
.action(async function (options) {
|
|
18
|
+
await run(this, () => (0, client_1.createClient)().request('GET', '/data-sources', { query: { dataType: options.type } }));
|
|
19
|
+
});
|
|
20
|
+
source.command('get')
|
|
21
|
+
.argument('<dataId>')
|
|
22
|
+
.description('查看数据源详情(密码已掩码)')
|
|
23
|
+
.action(async function (dataId) {
|
|
24
|
+
await run(this, () => (0, client_1.createClient)().request('GET', `/data-sources/${dataId}`));
|
|
25
|
+
});
|
|
26
|
+
source.command('create')
|
|
27
|
+
.description('新建数据源')
|
|
28
|
+
.requiredOption('--name <name>')
|
|
29
|
+
.requiredOption('--type <dataType>', '如 mysql、postgresql、api')
|
|
30
|
+
.option('--addr <addr>')
|
|
31
|
+
.option('--port <port>')
|
|
32
|
+
.option('--username <username>')
|
|
33
|
+
.option('--database <database>')
|
|
34
|
+
.option('--base-url <baseURL>', 'API、MQTT、WebSocket 的地址')
|
|
35
|
+
.option('--password-env <env>', '从环境变量读取密码')
|
|
36
|
+
.option('--password-stdin', '从标准输入读取密码')
|
|
37
|
+
.action(async function (options) {
|
|
38
|
+
try {
|
|
39
|
+
const body = await toInput(options);
|
|
40
|
+
const result = await (0, client_1.createClient)().request('POST', '/data-sources', { body });
|
|
41
|
+
(0, output_1.emit)((0, auth_1.globalJson)(this), result, () => console.log(JSON.stringify(result, null, 2)));
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
(0, client_1.exitOnError)(error);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
source.command('update')
|
|
48
|
+
.argument('<dataId>')
|
|
49
|
+
.description('更新数据源,不传密码则保留原密码')
|
|
50
|
+
.option('--name <name>')
|
|
51
|
+
.option('--addr <addr>')
|
|
52
|
+
.option('--port <port>')
|
|
53
|
+
.option('--username <username>')
|
|
54
|
+
.option('--database <database>')
|
|
55
|
+
.option('--base-url <baseURL>')
|
|
56
|
+
.option('--password-env <env>')
|
|
57
|
+
.option('--password-stdin')
|
|
58
|
+
.action(async function (dataId, options) {
|
|
59
|
+
try {
|
|
60
|
+
const body = await toInput(options);
|
|
61
|
+
const result = await (0, client_1.createClient)().request('PATCH', `/data-sources/${dataId}`, { body });
|
|
62
|
+
(0, output_1.emit)((0, auth_1.globalJson)(this), result, () => console.log('已更新'));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
(0, client_1.exitOnError)(error);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
source.command('delete')
|
|
69
|
+
.argument('<dataId>')
|
|
70
|
+
.description('删除数据源')
|
|
71
|
+
.action(async (dataId) => {
|
|
72
|
+
try {
|
|
73
|
+
await (0, client_1.createClient)().request('DELETE', `/data-sources/${dataId}`);
|
|
74
|
+
console.log('已删除');
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
(0, client_1.exitOnError)(error);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
source.command('test')
|
|
81
|
+
.argument('<dataId>')
|
|
82
|
+
.description('测试连接')
|
|
83
|
+
.action(async function (dataId) {
|
|
84
|
+
await run(this, () => (0, client_1.createClient)().request('POST', `/data-sources/${dataId}/test`));
|
|
85
|
+
});
|
|
86
|
+
source.command('tables')
|
|
87
|
+
.argument('<dataId>')
|
|
88
|
+
.description('列出表')
|
|
89
|
+
.action(async function (dataId) {
|
|
90
|
+
await run(this, () => (0, client_1.createClient)().request('GET', `/data-sources/${dataId}/tables`));
|
|
91
|
+
});
|
|
92
|
+
source.command('columns')
|
|
93
|
+
.argument('<dataId>')
|
|
94
|
+
.requiredOption('--table <table>', '表名')
|
|
95
|
+
.description('列出表字段')
|
|
96
|
+
.action(async function (dataId, options) {
|
|
97
|
+
await run(this, () => (0, client_1.createClient)().request('POST', `/data-sources/${dataId}/columns`, {
|
|
98
|
+
body: { tables: [{ tableName: options.table }] },
|
|
99
|
+
}));
|
|
100
|
+
});
|
|
101
|
+
source.command('preview')
|
|
102
|
+
.argument('<dataId>')
|
|
103
|
+
.description('预览数据')
|
|
104
|
+
.option('--sql <sql>', 'SQL 查询')
|
|
105
|
+
.action(async function (dataId, options) {
|
|
106
|
+
await run(this, () => (0, client_1.createClient)().request('POST', `/data-sources/${dataId}/preview`, {
|
|
107
|
+
body: { ...(options.sql != null ? { sql: options.sql } : {}) },
|
|
108
|
+
}));
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async function toInput(options) {
|
|
112
|
+
const password = await resolvePassword(options);
|
|
113
|
+
return {
|
|
114
|
+
...(options.name != null ? { name: options.name } : {}),
|
|
115
|
+
...(options.type != null ? { dataType: options.type } : {}),
|
|
116
|
+
...(options.addr != null ? { addr: options.addr } : {}),
|
|
117
|
+
...(options.port != null ? { port: Number(options.port) } : {}),
|
|
118
|
+
...(options.username != null ? { username: options.username } : {}),
|
|
119
|
+
...(options.database != null ? { database: options.database } : {}),
|
|
120
|
+
...(options.baseUrl != null ? { baseURL: options.baseUrl } : {}),
|
|
121
|
+
...(password != null ? { password } : {}),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
async function resolvePassword(options) {
|
|
125
|
+
if (options.passwordEnv != null) {
|
|
126
|
+
const value = process.env[options.passwordEnv];
|
|
127
|
+
if (value == null)
|
|
128
|
+
throw new Error(`环境变量 ${options.passwordEnv} 没有值`);
|
|
129
|
+
return value;
|
|
130
|
+
}
|
|
131
|
+
if (options.passwordStdin === true) {
|
|
132
|
+
const chunks = [];
|
|
133
|
+
for await (const chunk of process.stdin)
|
|
134
|
+
chunks.push(chunk);
|
|
135
|
+
return Buffer.concat(chunks).toString('utf8').trim();
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
async function run(command, call) {
|
|
140
|
+
try {
|
|
141
|
+
const result = await call();
|
|
142
|
+
(0, output_1.emit)((0, auth_1.globalJson)(command), result, () => console.log(JSON.stringify(result, null, 2)));
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
(0, client_1.exitOnError)(error);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerDebug = registerDebug;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const client_1 = require("../client");
|
|
6
|
+
const debug_query_1 = require("../debug-query");
|
|
7
|
+
const output_1 = require("../output");
|
|
8
|
+
const auth_1 = require("./auth");
|
|
9
|
+
function registerDebug(program) {
|
|
10
|
+
program.command('debug')
|
|
11
|
+
.description('查看一个组件的数据来源和交互配置')
|
|
12
|
+
.argument('<screenId>', '大屏 id,或本地文档 JSON 路径')
|
|
13
|
+
.argument('<key>', '组件 key,如 c-88')
|
|
14
|
+
.option('--trigger <trigger>', '只保留这种触发方式的事件,如 click')
|
|
15
|
+
.option('--event <eventId>', '只保留这个事件')
|
|
16
|
+
.action(async function (screenId, key, options) {
|
|
17
|
+
try {
|
|
18
|
+
const document = await loadDocument(screenId);
|
|
19
|
+
const result = (0, debug_query_1.queryComponent)(document, key, { trigger: options.trigger, eventId: options.event });
|
|
20
|
+
(0, output_1.emit)((0, auth_1.globalJson)(this), result, () => { });
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
(0, client_1.exitOnError)(error);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async function loadDocument(screenId) {
|
|
28
|
+
if (screenId.endsWith('.json'))
|
|
29
|
+
return JSON.parse((0, node_fs_1.readFileSync)(screenId, 'utf8'));
|
|
30
|
+
return await (0, client_1.createClient)().request('GET', `/screens/${screenId}/document`);
|
|
31
|
+
}
|