@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.
@@ -7,46 +7,32 @@ const client_1 = require("../client");
7
7
  const output_1 = require("../output");
8
8
  function registerAuth(program) {
9
9
  const auth = program.command('auth').description('登录与凭证');
10
- auth.command('add')
11
- .description('给当前 baseURL 添加一把 API Key,并切换到它')
12
- .requiredOption('--name <name>', '这把 Key 的名称,只在当前环境下有效')
13
- .requiredOption('--token <token>', 'API Key,格式 appId.secret')
14
- .action((options) => {
15
- try {
16
- (0, config_1.addKey)(options.name, options.token);
17
- const current = (0, config_1.activeTarget)();
18
- console.log(`已在环境「${current?.name}」下保存 Key「${options.name}」并切换过去`);
19
- }
20
- catch (error) {
21
- (0, client_1.exitOnError)(error);
22
- }
23
- });
24
10
  auth.command('use')
25
- .description('切换当前 baseURL 下的 API Key')
26
- .argument('<name>', 'Key 名称')
11
+ .description('切换当前工作台下的登录')
12
+ .argument('<name>', '登录名称')
27
13
  .action((name) => {
28
14
  try {
29
15
  (0, config_1.useKey)(name);
30
- console.log(`已切换到 Key「${name}」`);
16
+ console.log(`已切换到「${name}」`);
31
17
  }
32
18
  catch (error) {
33
19
  (0, client_1.exitOnError)(error);
34
20
  }
35
21
  });
36
22
  auth.command('list')
37
- .description('列出当前 baseURL 下的 API Key')
23
+ .description('列出当前工作台下的登录')
38
24
  .action(function () {
39
25
  try {
40
26
  const target = (0, config_1.activeTarget)();
41
27
  const keys = (0, config_1.listKeys)();
42
- (0, output_1.emit)(globalJson(this), { environment: target?.name ?? null, baseUrl: target?.baseUrl ?? null, keys }, () => {
28
+ (0, output_1.emit)(globalJson(this), workspacePayload({ baseUrl: target?.baseUrl ?? null, keys }), () => {
43
29
  if (target == null) {
44
- console.log('还没有选择环境');
30
+ console.log((0, config_1.missingWorkspaceMessage)());
45
31
  return;
46
32
  }
47
- console.log(`环境「${target.name}」 ${target.baseUrl}`);
33
+ console.log((0, config_1.hasMultipleWorkspaces)() ? `环境「${target.name}」 ${target.baseUrl}` : target.baseUrl);
48
34
  if (keys.length === 0) {
49
- console.log('这个地址下还没有 API Key');
35
+ console.log('这个地址下还没有登录');
50
36
  return;
51
37
  }
52
38
  for (const key of keys)
@@ -58,28 +44,22 @@ function registerAuth(program) {
58
44
  }
59
45
  });
60
46
  auth.command('remove')
61
- .description('删除当前 baseURL 下的一把 API Key')
62
- .argument('<name>', 'Key 名称')
47
+ .description('删除当前工作台下的一个登录')
48
+ .argument('<name>', '登录名称')
63
49
  .action((name) => {
64
50
  try {
65
51
  (0, config_1.removeKey)(name);
66
- console.log(`已删除 Key「${name}」`);
52
+ console.log(`已删除「${name}」`);
67
53
  }
68
54
  catch (error) {
69
55
  (0, client_1.exitOnError)(error);
70
56
  }
71
57
  });
72
58
  auth.command('login')
73
- .description('设备码登录:在浏览器确认后保存登录状态,访问令牌过期会自动刷新')
74
- .option('--name <name>', '保存时使用的 Key 名称', 'default')
75
- .option('--with-token <token>', '直接使用已有的 API Key(appId.secret)')
76
- .action(async function (options) {
59
+ .description('在浏览器确认后保存登录,过期后自动刷新')
60
+ .option('--name <name>', '保存时使用的名称', 'default')
61
+ .action(async (options) => {
77
62
  try {
78
- if (options.withToken != null) {
79
- (0, config_1.addKey)(options.name, options.withToken);
80
- console.log(`已在当前环境下保存 Key「${options.name}」`);
81
- return;
82
- }
83
63
  await deviceLogin(options.name);
84
64
  }
85
65
  catch (error) {
@@ -87,25 +67,23 @@ function registerAuth(program) {
87
67
  }
88
68
  });
89
69
  auth.command('status')
90
- .description('查看当前环境、Key,以及绑定的空间和团队')
70
+ .description('查看工作台地址、当前登录,以及绑定的空间和团队')
91
71
  .action(async function () {
92
72
  try {
93
73
  const target = (0, config_1.activeTarget)();
94
74
  const keys = (0, config_1.listKeys)();
95
75
  const binding = target?.token != null ? await fetchBinding() : null;
96
- (0, output_1.emit)(globalJson(this), {
97
- environment: target?.name ?? null,
76
+ (0, output_1.emit)(globalJson(this), workspacePayload({
98
77
  baseUrl: target?.baseUrl ?? null,
99
- web: target?.web ?? null,
100
78
  key: target?.keyName ?? null,
101
79
  space: binding?.space ?? null,
102
80
  team: binding?.team ?? null,
103
81
  keys,
104
- }, () => {
105
- console.log(`环境:${target?.name ?? '未选择'}`);
82
+ }), () => {
83
+ if ((0, config_1.hasMultipleWorkspaces)())
84
+ console.log(`环境:${target?.name ?? '未选择'}`);
106
85
  console.log(`baseURL:${target?.baseUrl ?? '未配置'}`);
107
- console.log(`Web 地址:${target?.web ?? '未配置'}`);
108
- console.log(`当前 Key:${target?.keyName ?? '未选择'}(这个地址下共 ${keys.length} 把)`);
86
+ console.log(`当前登录:${target?.keyName ?? '未登录'}(这个地址下共 ${keys.length} 个)`);
109
87
  console.log(`空间:${formatSide(binding?.space ?? null)}`);
110
88
  console.log(`团队:${formatSide(binding?.team ?? null)}`);
111
89
  });
@@ -115,31 +93,53 @@ function registerAuth(program) {
115
93
  }
116
94
  });
117
95
  auth.command('logout')
118
- .description('移除当前 baseURL 下正在使用的 API Key 或登录状态')
96
+ .description('移除当前工作台下正在使用的登录')
119
97
  .action(async () => {
120
98
  try {
121
99
  const target = (0, config_1.activeTarget)();
122
100
  if (target?.keyName == null)
123
- throw new Error('当前环境没有选中的 API Key');
101
+ throw new Error('还没有登录');
124
102
  if ((0, config_1.isSession)(target.credential))
125
- await revokeQuietly(target.baseUrl, target.credential.refreshToken);
126
- (0, config_1.removeKey)(target.keyName);
127
- console.log(`已移除环境「${target.name}」下的 Key「${target.keyName}」`);
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}`);
128
130
  }
129
131
  catch (error) {
130
132
  (0, client_1.exitOnError)(error);
131
133
  }
132
134
  });
133
- const config = program.command('config').description('管理 baseURL,每个地址下的 API Key 互相隔离');
134
135
  config.command('add')
135
- .description('添加一个 baseURL')
136
+ .description('添加一个工作台地址')
136
137
  .requiredOption('--name <name>', '环境名称,用于切换')
137
- .requiredOption('--base-url <url>', 'OpenAPI 地址,形如 http://127.0.0.1:9800/api/easyv/open/v1')
138
- .option('--web <url>', 'Web 站点地址,用于授权页和预览页')
138
+ .requiredOption('--base-url <url>', '工作台地址,例如 https://workspace.example.com 或 http://127.0.0.1:3000')
139
139
  .option('--use', '添加后立即切换到这个环境')
140
140
  .action((options) => {
141
141
  try {
142
- (0, config_1.addEnvironment)({ name: options.name, baseUrl: options.baseUrl, web: options.web, select: options.use === true });
142
+ (0, config_1.addEnvironment)({ name: options.name, baseUrl: options.baseUrl, select: options.use === true });
143
143
  const current = (0, config_1.activeTarget)();
144
144
  console.log(`已添加环境「${options.name}」${current?.name === options.name ? ',并切换过去' : ''}`);
145
145
  }
@@ -148,52 +148,46 @@ function registerAuth(program) {
148
148
  }
149
149
  });
150
150
  config.command('use')
151
- .description('切换当前 baseURL,之后只能使用这个地址下的 API Key')
151
+ .description('切换到另一个工作台,之后使用这个地址下的登录')
152
152
  .argument('<name>', '环境名称')
153
153
  .action((name) => {
154
154
  try {
155
155
  const env = (0, config_1.useEnvironment)(name);
156
156
  console.log(`已切换到环境「${env.name}」 ${env.baseUrl}`);
157
- console.log(env.currentKey != null ? `当前 Key:${env.currentKey}` : '这个地址下还没有选择 API Key');
157
+ console.log(env.currentKey != null ? `当前登录:${env.currentKey}` : '这个地址下还没有登录');
158
158
  }
159
159
  catch (error) {
160
160
  (0, client_1.exitOnError)(error);
161
161
  }
162
162
  });
163
163
  config.command('list')
164
- .description('列出全部 baseURL')
164
+ .description('查看工作台地址。有多个工作台时列出全部环境')
165
165
  .action(function () {
166
- const environments = (0, config_1.listEnvironments)();
167
- (0, output_1.emit)(globalJson(this), { environments }, () => {
168
- if (environments.length === 0) {
169
- console.log('还没有环境。运行 easyv-screen config add --name <名称> --base-url <地址>');
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
+ }
170
173
  return;
171
174
  }
172
- for (const env of environments) {
173
- const key = env.currentKey ?? '未选择 Key';
174
- console.log(`${env.current ? '* ' : ' '}${env.name} ${env.baseUrl} Key ${key}(${env.keys.length})`);
175
+ if (workspace.baseUrl == null) {
176
+ console.log((0, config_1.missingWorkspaceMessage)());
177
+ return;
175
178
  }
179
+ const key = workspace.currentKey ?? '未登录';
180
+ console.log(`${workspace.baseUrl} 登录 ${key}(${workspace.keys.length})`);
176
181
  });
177
182
  });
178
183
  config.command('remove')
179
- .description('删除一个 baseURL 及其全部 API Key')
180
- .argument('<name>', '环境名称')
184
+ .description('删除工作台及其全部登录。有多个工作台时要写上环境名称')
185
+ .argument('[name]', '环境名称。只有一个工作台时可以不写')
181
186
  .action((name) => {
182
187
  try {
183
- (0, config_1.removeEnvironment)(name);
184
- console.log(`已删除环境「${name}」及其 API Key`);
185
- }
186
- catch (error) {
187
- (0, client_1.exitOnError)(error);
188
- }
189
- });
190
- config.command('web')
191
- .description('设置当前 baseURL 对应的 Web 站点地址')
192
- .argument('<url>')
193
- .action((url) => {
194
- try {
195
- (0, config_1.updateEnvironmentWeb)(url);
196
- console.log('已更新当前环境的 Web 地址');
188
+ const multiple = (0, config_1.hasMultipleWorkspaces)();
189
+ const removed = (0, config_1.removeEnvironment)(name);
190
+ console.log(multiple ? `已删除环境「${removed}」及其登录` : '已删除工作台及其登录');
197
191
  }
198
192
  catch (error) {
199
193
  (0, client_1.exitOnError)(error);
@@ -203,29 +197,28 @@ function registerAuth(program) {
203
197
  async function deviceLogin(keyName) {
204
198
  const target = (0, config_1.activeTarget)();
205
199
  if (target == null)
206
- throw new client_1.ApiError('BAD_REQUEST', '还没有选择环境。先运行 easyv-screen config add --name <名称> --base-url <地址> --web <站点>', 0);
207
- if (target.web == null)
208
- throw new client_1.ApiError('BAD_REQUEST', '设备码登录需要 Web 地址。运行 easyv-screen config web <站点地址>', 0);
200
+ throw new client_1.ApiError('BAD_REQUEST', (0, config_1.missingWorkspaceMessage)(), 0);
209
201
  const client = (0, client_1.createClient)();
210
202
  const code = await client.request('POST', '/oauth/device', {
211
203
  anonymous: true,
212
204
  body: { client_id: client_1.CLI_CLIENT_ID },
213
205
  });
214
- const url = `${target.web.replace(/\/$/, '')}${code.verificationPath}`;
206
+ const url = (0, config_1.joinBase)(target.baseUrl, code.verificationPath);
215
207
  console.log('在浏览器打开下面的地址并确认授权:');
216
208
  console.log(url);
217
209
  const deadline = Date.now() + code.expires_in * 1000;
218
210
  while (Date.now() < deadline) {
219
211
  await sleep(code.interval * 1000);
220
212
  try {
221
- const session = await (0, client_1.exchangeToken)(target.baseUrl, {
213
+ const session = await (0, client_1.exchangeToken)((0, config_1.apiEndpoint)(target.baseUrl), {
222
214
  grant_type: client_1.DEVICE_GRANT,
223
215
  device_code: code.device_code,
224
216
  client_id: client_1.CLI_CLIENT_ID,
225
217
  });
226
218
  (0, config_1.addSession)(keyName, session);
227
219
  const binding = await fetchBinding();
228
- console.log(`授权成功,已保存为环境「${target.name}」的「${keyName}」。访问令牌会自动刷新,绑定${formatSide(binding.space, '空间')}、${formatSide(binding.team, '团队')}`);
220
+ const where = (0, config_1.hasMultipleWorkspaces)() ? `环境「${target.name}」的「${keyName}」` : `「${keyName}」`;
221
+ console.log(`授权成功,已保存为${where}。访问令牌会自动刷新,绑定${formatSide(binding.space, '空间')}、${formatSide(binding.team, '团队')}`);
229
222
  return;
230
223
  }
231
224
  catch (error) {
@@ -257,6 +250,12 @@ async function fetchBinding() {
257
250
  team: { id: context.teamId ?? null, name: nonempty(context.teamName) },
258
251
  };
259
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
+ }
260
259
  function nonempty(value) {
261
260
  if (value == null || value.trim() === '')
262
261
  return null;
@@ -2,48 +2,28 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.registerDebug = registerDebug;
4
4
  const node_fs_1 = require("node:fs");
5
- const analyzer_1 = require("../analyzer");
6
5
  const client_1 = require("../client");
6
+ const debug_query_1 = require("../debug-query");
7
7
  const output_1 = require("../output");
8
8
  const auth_1 = require("./auth");
9
9
  function registerDebug(program) {
10
- const debug = program.command('debug').description('排查大屏问题');
11
- debug.command('inspect')
12
- .description('查看一个组件的显隐、交互和数据状态')
13
- .argument('<screenId>')
10
+ program.command('debug')
11
+ .description('查看一个组件的数据来源和交互配置')
12
+ .argument('<screenId>', '大屏 id,或本地文档 JSON 路径')
14
13
  .argument('<key>', '组件 key,如 c-88')
15
- .action(async function (screenId, key) {
16
- await report(this, screenId, document => (0, analyzer_1.inspect)(document, key).findings);
17
- });
18
- debug.command('why-hidden')
19
- .description('为什么一个组件不显示')
20
- .argument('<screenId>')
21
- .argument('<key>')
22
- .action(async function (screenId, key) {
23
- await report(this, screenId, document => (0, analyzer_1.whyHidden)(document, key));
24
- });
25
- debug.command('why-no-response')
26
- .description('为什么点击一个组件没有反应')
27
- .argument('<screenId>')
28
- .argument('<key>')
29
- .option('--trigger <trigger>', '触发方式', 'click')
14
+ .option('--trigger <trigger>', '只保留这种触发方式的事件,如 click')
15
+ .option('--event <eventId>', '只保留这个事件')
30
16
  .action(async function (screenId, key, options) {
31
- await report(this, screenId, document => (0, analyzer_1.whyNoResponse)(document, key, options.trigger));
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
+ }
32
25
  });
33
26
  }
34
- async function report(command, screenId, analyze) {
35
- try {
36
- const document = await loadDocument(screenId);
37
- const findings = analyze(document);
38
- (0, output_1.emit)((0, auth_1.globalJson)(command), { findings }, () => {
39
- for (const finding of findings)
40
- console.log(`[${finding.level}] ${finding.code}: ${finding.message}`);
41
- });
42
- }
43
- catch (error) {
44
- (0, client_1.exitOnError)(error);
45
- }
46
- }
47
27
  async function loadDocument(screenId) {
48
28
  if (screenId.endsWith('.json'))
49
29
  return JSON.parse((0, node_fs_1.readFileSync)(screenId, 'utf8'));
@@ -153,10 +153,10 @@ function registerScreen(program) {
153
153
  }
154
154
  async function takeSnapshot(screenId, output) {
155
155
  const target = (0, config_1.activeTarget)();
156
- if (target?.web == null)
157
- throw new Error('截图需要 Web 地址。运行 easyv-screen config add 时带上 --web,或 easyv-screen config web <站点地址>');
156
+ if (target == null)
157
+ throw new Error((0, config_1.missingWorkspaceMessage)());
158
158
  const issued = await (0, client_1.createClient)().request('POST', `/screens/${screenId}/preview-token`);
159
- const url = `${target.web.replace(/\/$/, '')}${issued.previewPath}`;
159
+ const url = (0, config_1.joinBase)(target.baseUrl, issued.previewPath);
160
160
  let chromium;
161
161
  try {
162
162
  chromium = require('playwright').chromium;