@easyv/screen-cli 0.1.0 → 0.1.2

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/config.js CHANGED
@@ -1,13 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MISSING_WORKSPACE = void 0;
3
4
  exports.isSession = isSession;
4
5
  exports.listEnvironments = listEnvironments;
6
+ exports.hasMultipleWorkspaces = hasMultipleWorkspaces;
7
+ exports.describeWorkspace = describeWorkspace;
8
+ exports.missingWorkspaceMessage = missingWorkspaceMessage;
5
9
  exports.activeTarget = activeTarget;
10
+ exports.joinBase = joinBase;
11
+ exports.apiEndpoint = apiEndpoint;
12
+ exports.setBaseUrl = setBaseUrl;
6
13
  exports.addEnvironment = addEnvironment;
7
14
  exports.useEnvironment = useEnvironment;
8
15
  exports.removeEnvironment = removeEnvironment;
9
- exports.updateEnvironmentWeb = updateEnvironmentWeb;
10
- exports.addKey = addKey;
11
16
  exports.addSession = addSession;
12
17
  exports.saveCurrentSession = saveCurrentSession;
13
18
  exports.useKey = useKey;
@@ -22,31 +27,104 @@ const CONFIG_FILE = (0, node_path_1.join)(HOME, 'config.json');
22
27
  const SECRET_FILE = (0, node_path_1.join)(HOME, 'credentials.json');
23
28
  const KEYCHAIN_SERVICE = 'easyv-screen';
24
29
  const USE_KEYCHAIN = (0, node_os_1.platform)() === 'darwin' && process.env['EASYV_SCREEN_HOME'] == null;
30
+ const OPEN_API_PATH = '/api/easyv/open/v1';
25
31
  function isSession(value) {
26
32
  return value != null && typeof value === 'object' && typeof value.accessToken === 'string' && typeof value.refreshToken === 'string';
27
33
  }
34
+ /** 只有一个工作台时对外不出现这个名字,多个工作台时才用来切换 */
35
+ const DEFAULT_ENVIRONMENT = 'default';
36
+ exports.MISSING_WORKSPACE = '还没有配置工作台。先运行 easyv-screen config set --base-url <地址>';
28
37
  function listEnvironments() {
29
38
  const config = loadConfig();
30
39
  return config.environments.map(env => ({
31
40
  name: env.name,
32
41
  baseUrl: env.baseUrl,
33
- web: env.web ?? null,
34
42
  current: env.name === config.current,
35
43
  keys: [...env.keys],
36
44
  currentKey: env.currentKey ?? null,
37
45
  }));
38
46
  }
47
+ function hasMultipleWorkspaces() {
48
+ return loadConfig().environments.length > 1;
49
+ }
50
+ function describeWorkspace() {
51
+ const environments = listEnvironments();
52
+ const current = environments.find(env => env.current);
53
+ if (environments.length <= 1) {
54
+ const only = current ?? environments[0];
55
+ return {
56
+ baseUrl: only?.baseUrl ?? null,
57
+ currentKey: only?.currentKey ?? null,
58
+ keys: only?.keys ?? [],
59
+ };
60
+ }
61
+ return {
62
+ baseUrl: current?.baseUrl ?? null,
63
+ currentKey: current?.currentKey ?? null,
64
+ keys: current?.keys ?? [],
65
+ current: current?.name ?? null,
66
+ environments,
67
+ };
68
+ }
69
+ /** 还没配地址,或有多个工作台但没选定当前项时,告诉调用方下一步该运行什么 */
70
+ function missingWorkspaceMessage() {
71
+ const config = loadConfig();
72
+ if (config.environments.length > 1)
73
+ return '还没有选择工作台。运行 easyv-screen config use <名称>';
74
+ return exports.MISSING_WORKSPACE;
75
+ }
39
76
  function activeTarget() {
40
77
  const config = loadConfig();
41
78
  const env = config.environments.find(item => item.name === config.current);
42
79
  if (env == null)
43
80
  return undefined;
44
81
  const credential = env.currentKey == null ? undefined : readSecrets()[env.name]?.[env.currentKey];
45
- const token = credential == null ? undefined : isSession(credential) ? credential.accessToken : credential;
46
- return { name: env.name, baseUrl: env.baseUrl, web: env.web, keyName: env.currentKey, token, credential };
82
+ const token = isSession(credential) ? credential.accessToken : undefined;
83
+ return { name: env.name, baseUrl: env.baseUrl, keyName: env.currentKey, token, credential };
84
+ }
85
+ /** 把路径接到工作台地址后面 */
86
+ function joinBase(baseUrl, path) {
87
+ const base = trimSlash(baseUrl);
88
+ return path.startsWith('/') ? `${base}${path}` : `${base}/${path}`;
89
+ }
90
+ /** 接口根路径。配置里只存工作台地址,请求时补上固定前缀 */
91
+ function apiEndpoint(baseUrl) {
92
+ return joinBase(baseUrl, OPEN_API_PATH);
93
+ }
94
+ /**
95
+ * 设置唯一的工作台地址。没有地址时记入名为 default 的一项;已有一项时改这一项的地址。
96
+ * 地址变了就清掉原来的 Key,因为它们属于旧工作台。已有多项时拒绝,改由 add / use 指定名称。
97
+ */
98
+ function setBaseUrl(url) {
99
+ const baseUrl = normalizeBaseUrl(url);
100
+ const config = loadConfig();
101
+ if (config.environments.length > 1)
102
+ throw new Error('已有多个工作台。添加用 easyv-screen config add --name <名称> --base-url <地址>,切换用 easyv-screen config use <名称>');
103
+ if (config.environments.length === 0) {
104
+ config.environments.push({ name: DEFAULT_ENVIRONMENT, baseUrl, keys: [] });
105
+ config.current = DEFAULT_ENVIRONMENT;
106
+ saveConfig(config);
107
+ return { baseUrl, created: true, changed: true, clearedKeys: false };
108
+ }
109
+ const env = config.environments[0];
110
+ if (env.baseUrl === baseUrl) {
111
+ if (config.current !== env.name) {
112
+ config.current = env.name;
113
+ saveConfig(config);
114
+ }
115
+ return { baseUrl, created: false, changed: false, clearedKeys: false };
116
+ }
117
+ const hadKeys = env.keys.length > 0;
118
+ env.baseUrl = baseUrl;
119
+ env.keys = [];
120
+ delete env.currentKey;
121
+ config.current = env.name;
122
+ saveConfig(config);
123
+ const clearedSecrets = clearEnvironmentSecrets(env.name);
124
+ return { baseUrl, created: false, changed: true, clearedKeys: hadKeys || clearedSecrets };
47
125
  }
48
126
  /**
49
- * 新增一个 baseURL。同名或同一个地址已存在时拒绝,避免把 Key 拆到两处。
127
+ * 新增一个工作台地址。同名或同一个地址已存在时拒绝,避免把 Key 拆到两处。
50
128
  * `select` 为 true 时立刻切过去。
51
129
  */
52
130
  function addEnvironment(input) {
@@ -57,9 +135,12 @@ function addEnvironment(input) {
57
135
  if (sameName != null)
58
136
  throw new Error(`环境「${name}」已存在。切换用 easyv-screen config use ${name}`);
59
137
  const sameUrl = config.environments.find(item => item.baseUrl === baseUrl);
60
- if (sameUrl != null)
138
+ if (sameUrl != null) {
139
+ if (config.environments.length === 1)
140
+ throw new Error('这个地址已经是当前工作台。要添加另一个工作台,请换一个地址');
61
141
  throw new Error(`这个地址已经在环境「${sameUrl.name}」里。切换用 easyv-screen config use ${sameUrl.name}`);
62
- const created = { name, baseUrl, keys: [], ...(input.web != null ? { web: trimSlash(input.web) } : {}) };
142
+ }
143
+ const created = { name, baseUrl, keys: [] };
63
144
  config.environments.push(created);
64
145
  if (input.select === true || config.current == null)
65
146
  config.current = name;
@@ -73,30 +154,23 @@ function useEnvironment(name) {
73
154
  saveConfig(config);
74
155
  return env;
75
156
  }
157
+ /** 删除工作台。只有一个时可以不写名称;有多个时必须带上环境名 */
76
158
  function removeEnvironment(name) {
77
- const target = normalizeName(name);
78
159
  const config = loadConfig();
79
- if (!config.environments.some(item => item.name === target))
80
- throw new Error(`没有环境「${target}」`);
160
+ const target = resolveRemoveTarget(config, name);
161
+ if (!config.environments.some(item => item.name === target)) {
162
+ if (config.environments.length > 1) {
163
+ const names = config.environments.map(item => item.name).join('、');
164
+ throw new Error(`没有环境「${target}」。现有环境:${names}`);
165
+ }
166
+ throw new Error(config.environments.length === 0 ? exports.MISSING_WORKSPACE : `没有名为「${target}」的工作台`);
167
+ }
81
168
  config.environments = config.environments.filter(item => item.name !== target);
82
169
  if (config.current === target)
83
- config.current = undefined;
170
+ config.current = config.environments.length === 1 ? config.environments[0].name : undefined;
84
171
  saveConfig(config);
85
- const secrets = readSecrets();
86
- delete secrets[target];
87
- writeSecrets(secrets);
88
- }
89
- function updateEnvironmentWeb(web) {
90
- const config = loadConfig();
91
- const env = requireCurrent(config);
92
- env.web = trimSlash(web);
93
- saveConfig(config);
94
- }
95
- function addKey(name, token) {
96
- const secret = token.trim();
97
- if (!secret.includes('.'))
98
- throw new Error('API Key 的格式是 appId.secret');
99
- writeCredential(name, secret);
172
+ clearEnvironmentSecrets(target);
173
+ return target;
100
174
  }
101
175
  /** 设备码登录成功后保存。之后访问令牌过期会就地换成新的 */
102
176
  function addSession(name, session) {
@@ -106,7 +180,7 @@ function saveCurrentSession(session) {
106
180
  const config = loadConfig();
107
181
  const env = requireCurrent(config);
108
182
  if (env.currentKey == null)
109
- throw new Error('当前环境没有选中的登录状态');
183
+ throw new Error('还没有选中的登录状态');
110
184
  const secrets = readSecrets();
111
185
  secrets[env.name] = { ...secrets[env.name], [env.currentKey]: session };
112
186
  writeSecrets(secrets);
@@ -162,48 +236,93 @@ function writeCredential(name, credential) {
162
236
  function requireCurrent(config) {
163
237
  const env = config.environments.find(item => item.name === config.current);
164
238
  if (env == null)
165
- throw new Error('还没有选择环境。先运行 easyv-screen config add --name <名称> --base-url <地址>');
239
+ throw new Error(missingWorkspaceMessage());
166
240
  return env;
167
241
  }
242
+ function resolveRemoveTarget(config, name) {
243
+ const given = name?.trim() ?? '';
244
+ if (given.length > 0)
245
+ return normalizeName(given);
246
+ if (config.environments.length === 1)
247
+ return config.environments[0].name;
248
+ if (config.environments.length === 0)
249
+ throw new Error(exports.MISSING_WORKSPACE);
250
+ throw new Error('有多个工作台。请指定要删除的环境:easyv-screen config remove <名称>');
251
+ }
252
+ function clearEnvironmentSecrets(name) {
253
+ const secrets = readSecrets();
254
+ if (secrets[name] == null)
255
+ return false;
256
+ delete secrets[name];
257
+ writeSecrets(secrets);
258
+ return true;
259
+ }
168
260
  function requireEnvironment(config, name) {
169
261
  const env = config.environments.find(item => item.name === name);
170
262
  if (env == null) {
263
+ if (config.environments.length <= 1)
264
+ throw new Error(config.environments.length === 0 ? exports.MISSING_WORKSPACE : '当前只有一个工作台。要再添加一个才需要按名称切换:easyv-screen config add --name <名称> --base-url <地址>');
171
265
  const names = config.environments.map(item => item.name).join('、');
172
- throw new Error(names.length > 0 ? `没有环境「${name}」。现有环境:${names}` : `没有环境「${name}」`);
266
+ throw new Error(`没有环境「${name}」。现有环境:${names}`);
173
267
  }
174
268
  return env;
175
269
  }
176
270
  function missingKeyMessage(env, keyName) {
177
271
  const available = env.keys.length > 0 ? env.keys.join('、') : '(空)';
178
- return `环境「${env.name}」下没有 Key「${keyName}」。这个地址下可用的 Key:${available}`;
272
+ if (loadConfig().environments.length <= 1)
273
+ return `没有登录「${keyName}」。已有登录:${available}`;
274
+ return `环境「${env.name}」下没有登录「${keyName}」。这个地址下已有登录:${available}`;
179
275
  }
180
276
  function loadConfig() {
277
+ return selectOnlyEnvironment(parseConfig());
278
+ }
279
+ function parseConfig() {
181
280
  const parsed = readJson(CONFIG_FILE);
182
281
  if (parsed == null)
183
282
  return { environments: [] };
184
283
  if (Array.isArray(parsed.environments))
185
- return { current: parsed.current, environments: parsed.environments };
186
- // 旧版本只存了一个地址和一把 Key,读到时收成一个环境
284
+ return withoutWeb(parsed.current, parsed.environments);
285
+ // 旧版本只存了一个地址,读到时收成一个环境。web 字段不再使用
187
286
  if (typeof parsed.endpoint === 'string' && parsed.endpoint.length > 0)
188
- return migrateLegacy(parsed.endpoint, parsed.web);
287
+ return migrateLegacy(parsed.endpoint);
189
288
  return { environments: [] };
190
289
  }
191
- function migrateLegacy(endpoint, web) {
290
+ /** 只剩一个工作台时,它就是当前项,调用方不用再选一次 */
291
+ function selectOnlyEnvironment(config) {
292
+ if (config.environments.length !== 1)
293
+ return config;
294
+ const only = config.environments[0];
295
+ if (config.current === only.name)
296
+ return config;
297
+ config.current = only.name;
298
+ saveConfig(config);
299
+ return config;
300
+ }
301
+ /** 丢掉已废弃的 web 字段,baseURL 保持原样 */
302
+ function withoutWeb(current, environments) {
303
+ const hasWeb = environments.some(env => env.web != null);
304
+ const next = environments.map(env => ({
305
+ name: env.name,
306
+ baseUrl: env.baseUrl,
307
+ keys: env.keys ?? [],
308
+ ...(env.currentKey != null ? { currentKey: env.currentKey } : {}),
309
+ }));
310
+ const config = { current, environments: next };
311
+ if (hasWeb)
312
+ saveConfig(config);
313
+ return config;
314
+ }
315
+ function migrateLegacy(endpoint) {
192
316
  const name = 'default';
193
- const legacyToken = readLegacyToken();
194
317
  const config = {
195
318
  current: name,
196
319
  environments: [{
197
320
  name,
198
321
  baseUrl: normalizeBaseUrl(endpoint),
199
- keys: legacyToken != null ? [name] : [],
200
- ...(web != null ? { web: trimSlash(web) } : {}),
201
- ...(legacyToken != null ? { currentKey: name } : {}),
322
+ keys: [],
202
323
  }],
203
324
  };
204
325
  saveConfig(config);
205
- if (legacyToken != null)
206
- writeSecrets({ [name]: { [name]: legacyToken } });
207
326
  return config;
208
327
  }
209
328
  function saveConfig(config) {
@@ -227,18 +346,6 @@ function writeSecrets(secrets) {
227
346
  (0, node_fs_1.writeFileSync)(SECRET_FILE, text);
228
347
  (0, node_fs_1.chmodSync)(SECRET_FILE, 0o600);
229
348
  }
230
- /** 旧版钥匙串里存的是单条 token,不是 JSON */
231
- function readLegacyToken() {
232
- const candidates = [USE_KEYCHAIN ? keychainGet() : undefined, readText((0, node_path_1.join)(HOME, 'credentials'))];
233
- for (const raw of candidates) {
234
- if (raw == null || raw.startsWith('{'))
235
- continue;
236
- const token = raw.trim();
237
- if (token.includes('.'))
238
- return token;
239
- }
240
- return undefined;
241
- }
242
349
  function parseSecret(raw) {
243
350
  if (raw == null || !raw.startsWith('{'))
244
351
  return undefined;
@@ -278,7 +385,7 @@ function normalizeName(name) {
278
385
  function normalizeBaseUrl(url) {
279
386
  const trimmed = trimSlash(url.trim());
280
387
  if (!/^https?:\/\//.test(trimmed))
281
- throw new Error('baseURL 需要以 http:// 或 https:// 开头');
388
+ throw new Error('baseURL 需要以 http:// 或 https:// 开头,例如 https://workspace.example.com');
282
389
  return trimmed;
283
390
  }
284
391
  function trimSlash(url) {
@@ -288,9 +395,8 @@ function labelOf(credential) {
288
395
  if (credential == null)
289
396
  return '';
290
397
  if (isSession(credential))
291
- return '设备登录';
292
- const dot = credential.indexOf('.');
293
- return dot > 0 ? credential.slice(0, dot) : credential;
398
+ return '已登录';
399
+ return '需重新登录';
294
400
  }
295
401
  function keychainGet() {
296
402
  try {
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ /**
3
+ * 从大屏文档里抽出一个组件的数据来源和交互配置。
4
+ * 只做提取和引用解析,不判断显示或点击是否正常。
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.queryComponent = queryComponent;
8
+ const REF_RULES = [
9
+ { type: 'component', source: String.raw `\$super\(component\(([^)]+)\)\)` },
10
+ { type: 'component', source: String.raw `\$container_\w+\(component\(([^)]+)\)\)` },
11
+ { type: 'component', source: String.raw `\$component\(([^)]+)\)` },
12
+ { type: 'group', source: String.raw `\$group\(([^)]+)\)` },
13
+ { type: 'panel', source: String.raw `\$panel\(([^)]+)\)` },
14
+ { type: 'container', source: String.raw `\$container\(([^)]+)\)` },
15
+ { type: 'state', source: String.raw `\$state\(([^)]+)\)` },
16
+ ];
17
+ /** 读取一个组件的数据绑定,以及指向别人 / 被别人指向的事件。 */
18
+ function queryComponent(document, key, filter = {}) {
19
+ const index = indexDocument(document.elements);
20
+ const target = index.all.find(item => item.key === key);
21
+ if (target == null)
22
+ throw new Error(`文档里没有 ${key}`);
23
+ const outgoing = readEvents(target.element).filter(event => matches(event, filter));
24
+ const incoming = index.all
25
+ .filter(item => item !== target)
26
+ .flatMap(item => readEvents(item.element)
27
+ .filter(event => matches(event, filter) && targetsKey(event, key, index))
28
+ .map(event => ({ ...describe(item), event })));
29
+ const refTexts = new Set();
30
+ collectRefStrings(outgoing, refTexts);
31
+ collectRefStrings(incoming, refTexts);
32
+ const refs = {};
33
+ for (const ref of [...refTexts].sort())
34
+ refs[ref] = resolveRef(ref, index);
35
+ return {
36
+ ...describe(target),
37
+ data: {
38
+ dataBinding: target.element['dataBinding'] ?? null,
39
+ staticData: target.element['staticData'] ?? null,
40
+ },
41
+ outgoing,
42
+ incoming,
43
+ refs,
44
+ };
45
+ }
46
+ function indexDocument(elements) {
47
+ const index = { elements: new Map(), states: new Map(), all: [] };
48
+ walkElements(elements, undefined, index);
49
+ return index;
50
+ }
51
+ function walkElements(elements, location, index) {
52
+ if (!isRecord(elements))
53
+ return;
54
+ for (const [key, value] of Object.entries(elements)) {
55
+ if (!isRecord(value))
56
+ continue;
57
+ const located = { key, element: value, ...(location != null ? { location } : {}) };
58
+ index.all.push(located);
59
+ if (!index.elements.has(key))
60
+ index.elements.set(key, located);
61
+ const states = value['states'];
62
+ if (!Array.isArray(states))
63
+ continue;
64
+ const parentKind = typeof value['kind'] === 'string' ? value['kind'] : 'panel';
65
+ for (const state of states) {
66
+ if (!isRecord(state) || typeof state['key'] !== 'string')
67
+ continue;
68
+ const stateName = typeof state['name'] === 'string' ? state['name'] : undefined;
69
+ if (!index.states.has(state['key'])) {
70
+ index.states.set(state['key'], {
71
+ key: state['key'],
72
+ parentKey: key,
73
+ ...(stateName != null ? { name: stateName } : {}),
74
+ });
75
+ }
76
+ walkElements(state['elements'], {
77
+ parentKey: key,
78
+ parentKind,
79
+ stateKey: state['key'],
80
+ ...(stateName != null ? { stateName } : {}),
81
+ }, index);
82
+ }
83
+ }
84
+ }
85
+ function describe(located) {
86
+ const described = {
87
+ key: located.key,
88
+ kind: typeof located.element['kind'] === 'string' ? located.element['kind'] : 'unknown',
89
+ };
90
+ copyString(described, 'name', located.element['name']);
91
+ copyString(described, 'module', located.element['module']);
92
+ copyString(described, 'version', located.element['version']);
93
+ if (located.location != null)
94
+ described['location'] = located.location;
95
+ return described;
96
+ }
97
+ function readEvents(element) {
98
+ const interactions = element['interactions'];
99
+ if (!isRecord(interactions) || !Array.isArray(interactions['events']))
100
+ return [];
101
+ return interactions['events'];
102
+ }
103
+ function matches(event, filter) {
104
+ if (filter.trigger == null && filter.eventId == null)
105
+ return true;
106
+ if (!isRecord(event))
107
+ return false;
108
+ if (filter.trigger != null && event['trigger'] !== filter.trigger)
109
+ return false;
110
+ if (filter.eventId != null && String(event['id'] ?? '') !== filter.eventId)
111
+ return false;
112
+ return true;
113
+ }
114
+ function targetsKey(event, key, index) {
115
+ if (!isRecord(event) || !Array.isArray(event['actions']))
116
+ return false;
117
+ return event['actions'].some((action) => {
118
+ if (!isRecord(action))
119
+ return false;
120
+ return actionTargets(action['component']).some(raw => extractRefs(raw).some(ref => resolveRef(ref, index).key === key));
121
+ });
122
+ }
123
+ function actionTargets(component) {
124
+ if (typeof component === 'string')
125
+ return [component];
126
+ if (!Array.isArray(component))
127
+ return [];
128
+ return component.filter((item) => typeof item === 'string');
129
+ }
130
+ function collectRefStrings(value, into) {
131
+ if (typeof value === 'string') {
132
+ for (const ref of extractRefs(value))
133
+ into.add(ref);
134
+ return;
135
+ }
136
+ if (Array.isArray(value)) {
137
+ for (const item of value)
138
+ collectRefStrings(item, into);
139
+ return;
140
+ }
141
+ if (isRecord(value)) {
142
+ for (const item of Object.values(value))
143
+ collectRefStrings(item, into);
144
+ }
145
+ }
146
+ function extractRefs(text) {
147
+ const refs = [];
148
+ for (const rule of REF_RULES) {
149
+ for (const match of text.matchAll(new RegExp(rule.source, 'g'))) {
150
+ const id = match[1];
151
+ if (id != null && id.length > 0)
152
+ refs.push(`$${rule.type}(${id})`);
153
+ }
154
+ }
155
+ return refs;
156
+ }
157
+ function resolveRef(ref, index) {
158
+ const match = /^\$(component|group|panel|container|state)\(([^)]+)\)$/.exec(ref);
159
+ if (match == null)
160
+ return { found: false };
161
+ const type = match[1] ?? '';
162
+ const id = match[2] ?? '';
163
+ if (type === 'state') {
164
+ const state = index.states.get(id);
165
+ if (state == null)
166
+ return { found: false };
167
+ return {
168
+ key: state.key,
169
+ kind: 'state',
170
+ found: true,
171
+ parentKey: state.parentKey,
172
+ ...(state.name != null ? { name: state.name } : {}),
173
+ };
174
+ }
175
+ const key = guessKey(type, id, index);
176
+ const located = key != null ? index.elements.get(key) : undefined;
177
+ if (located == null)
178
+ return { ...(key != null ? { key } : {}), found: false };
179
+ const resolved = { key: located.key, kind: describe(located)['kind'], found: true };
180
+ copyString(resolved, 'name', located.element['name']);
181
+ copyString(resolved, 'module', located.element['module']);
182
+ return resolved;
183
+ }
184
+ function guessKey(type, id, index) {
185
+ if (index.elements.has(id))
186
+ return id;
187
+ if (type === 'component') {
188
+ if (/^\d+$/.test(id))
189
+ return `c-${id}`;
190
+ if (id.startsWith('super_'))
191
+ return `u-${id.slice('super_'.length)}`;
192
+ return `c-${id}`;
193
+ }
194
+ if (type === 'group') {
195
+ const raw = id.startsWith('group_') ? id.slice('group_'.length) : id;
196
+ return `g-${sanitize(raw)}`;
197
+ }
198
+ if (type === 'panel') {
199
+ if (id.startsWith('panel_'))
200
+ return `p-${id.slice('panel_'.length)}`;
201
+ if (id.startsWith('ref_'))
202
+ return `rp-${id.slice('ref_'.length)}`;
203
+ if (/^\d+$/.test(id))
204
+ return `p-${id}`;
205
+ return undefined;
206
+ }
207
+ if (type === 'container') {
208
+ if (id.startsWith('container_'))
209
+ return `k-${id.slice('container_'.length)}`;
210
+ if (/^\d+$/.test(id))
211
+ return `k-${id}`;
212
+ }
213
+ return undefined;
214
+ }
215
+ function sanitize(value) {
216
+ return value.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/-+/g, '-');
217
+ }
218
+ function copyString(target, field, value) {
219
+ if (typeof value === 'string')
220
+ target[field] = value;
221
+ }
222
+ function isRecord(value) {
223
+ return typeof value === 'object' && value != null && !Array.isArray(value);
224
+ }
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_fs_1 = require("node:fs");
4
+ const node_path_1 = require("node:path");
3
5
  const commander_1 = require("commander");
4
6
  const auth_1 = require("./commands/auth");
5
7
  const component_1 = require("./commands/component");
@@ -7,11 +9,12 @@ const data_1 = require("./commands/data");
7
9
  const debug_1 = require("./commands/debug");
8
10
  const screen_1 = require("./commands/screen");
9
11
  const skills_1 = require("./commands/skills");
12
+ const { version } = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(__dirname, '..', 'package.json'), 'utf8'));
10
13
  const program = new commander_1.Command();
11
14
  program
12
15
  .name('easyv-screen')
13
16
  .description('用命令驱动 EasyV 大屏:搭建、修改、排查,以及从零开发组件')
14
- .version('0.1.0')
17
+ .version(version)
15
18
  .option('--json', '输出 JSON。现在默认就是 JSON,这个参数可以不写');
16
19
  (0, auth_1.registerAuth)(program);
17
20
  (0, screen_1.registerScreen)(program);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyv/screen-cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "用自然语言驱动 EasyV 大屏的命令行工具:搭建、修改、排查,以及从零开发组件",
5
5
  "bin": {
6
6
  "easyv-screen": "bin/easyv-screen.js"
@@ -1,35 +1,49 @@
1
1
  ---
2
2
  name: easyv-screen-debug
3
- description: 排查 EasyV 大屏的问题:一个组件为什么不显示、点击为什么没反应、数据为什么没绑上。基于大屏文档做确定性分析并给出结论。当用户反馈大屏显示或交互异常时使用。
3
+ description: 排查 EasyV 大屏的问题。读取一个组件的数据来源和交互配置(它触发了谁、谁触发了它),再根据这些配置判断原因。当用户反馈大屏显示、交互或数据异常时使用。
4
4
  ---
5
5
 
6
6
  # 排查大屏
7
7
 
8
- 这些命令只根据大屏文档下结论,能定位配置层面的原因。数据没返回、脚本报错这类运行时问题不在这里,要说明无法从文档判断。
8
+ `easyv-screen debug` 只取出一个组件的配置,不下结论。拿到数据后自己判断原因,再用一句人话告诉用户。
9
+
10
+ 下面这些已经有别的命令,不要再用 debug 去要:
11
+
12
+ - 显隐、位置、完整属性树:`easyv-screen screen document get <screenId>`
13
+ - 数据源连接详情、表和预览:拿到 `data_id` 后走 `easyv-screen-data`
14
+ - 组件能发出的事件、能接收的动作:`easyv-screen component schema <module>`
9
15
 
10
16
  ## 命令
11
17
 
12
- - `easyv-screen debug inspect <id> <key> --json`:汇总一个组件的显隐、交互和数据状态。
13
- - `easyv-screen debug why-hidden <id> <key> --json`:为什么不显示。
14
- - `easyv-screen debug why-no-response <id> <key> --json`:为什么点击没反应。其它触发方式加 `--trigger mouseEnter`。
18
+ ```
19
+ easyv-screen debug <screenId> <key>
20
+ ```
21
+
22
+ `<screenId>` 是大屏 id,也可以是一份文档 JSON 的路径。
23
+
24
+ `<key>` 是组件在文档里的 key,以 `c-` 开头。平台上已有的组件是 `c-` 加组件数字 id,例如组件 `88` 写成 `c-88`。通过命令新建并指定了 key 的组件,用当时写入的那个 key,例如 `c-kpi-1`,横线后面不一定是数字 id。
25
+
26
+ 过滤事件时加上,两个条件同时生效:
27
+
28
+ - `--trigger <trigger>`:只保留这种触发方式,例如 `click`、`mouseEnter`
29
+ - `--event <eventId>`:只保留这一个事件
30
+
31
+ ## 返回里有什么
15
32
 
16
- `<id>` 也可以是一份文档 JSON 文件的路径,便于离线分析。
33
+ - `data.dataBinding`:数据源绑定。不为 `null` 时,`dataType` 是类型,`data.data_id` 是数据源 id,其余字段是查询配置(如 `sql`)。为 `null` 表示当前用静态数据。
34
+ - `data.staticData`:文档里的静态数据。绑了数据源时它也可能还在。
35
+ - `outgoing`:这个组件自己的事件,结构与文档里的 `interactions.events` 相同。动作的 `component` 是它指向的目标。
36
+ - `incoming`:别的组件里,动作 `component` 指向它的事件。每项的 `key` 是来源组件。
37
+ - `refs`:上面这些事件里出现的 `$component(...)` 等引用,解析成文档 key、名称和 `found`。`found` 为 false 表示文档里没有这个对象。
38
+ - `location`:组件在某个面板或容器的状态里时才有。没有该字段就是在大屏根上。
17
39
 
18
- ## 怎么读结论
40
+ ## 怎么判断
19
41
 
20
- 每条 finding 有 `level` 和 `code`:
42
+ 按用户的具体问题看对应的字段,不要套固定的故障类型。
21
43
 
22
- | code | 含义 | 处理 |
23
- | --- | --- | --- |
24
- | `SELF_HIDDEN` | 组件自己被隐藏 | 问用户是否改为显示 |
25
- | `ANCESTOR_HIDDEN` | 被某个上层分组隐藏 | 指出是哪一层,问用户改哪一层 |
26
- | `NOT_IN_LAYERS` | 不在图层树里,不会渲染 | 需要把它加回图层 |
27
- | `NO_EVENT` | 没有配置对应的触发事件 | 问用户想要什么效果,再用 `addComponentEvent` 补 |
28
- | `NO_ACTION` | 事件没有动作 | 补动作 |
29
- | `NO_TARGET` | 动作没有目标组件 | 补目标 |
30
- | `TARGET_MISSING` | 动作指向的组件已不存在 | 事件悬空,删除或改指向 |
31
- | `TARGET_HIDDEN` | 目标组件存在但不可见 | 说明是目标被隐藏,点了也不会有可见变化 |
32
- | `DATA_ID_MISSING` | 声明了数据绑定但没有数据源 | 交给 `easyv-screen-data` 重新绑定 |
44
+ - 点击没反应:看 `outgoing` 里有没有该触发方式、动作有没有目标、`refs` 里目标是否 `found`。只关心一种触发就加 `--trigger`。
45
+ - 被别的组件影响:看 `incoming`,来源是每一项的 `key`。
46
+ - 数据不对:先看 `data`。绑定了数据源再按 `data_id` 查连接和预览。
47
+ - 不显示:debug 不返回显隐。用 `screen document get` 看该 key 和祖先图层的 `visible`。
33
48
 
34
- 先用 `inspect` 看全貌,再针对用户的具体问题用 `why-hidden` 或 `why-no-response`。
35
- 把结论用一句人话告诉用户,并给出要不要修复的选项。
49
+ 接口没返回、脚本报错这类运行时问题,文档里看不出来,直接说明。