@coda-console/plugin-check 0.8.26 → 0.8.28
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 +49 -42
- package/bin/check.mjs +3 -3
- package/check/coda/adapters/plugin-modules.mjs +2 -7
- package/check/coda/adapters/plugin-test-host.d.mts +0 -2
- package/check/coda/api-surface.txt +8 -2
- package/check/coda/builtin.json +44 -1400
- package/check/coda/components/plugins/plugin-extensions.mjs +7 -6
- package/check/coda/components/plugins/plugin-rules.mjs +6 -44
- package/check/coda/components/plugins/plugin-source-rules.mjs +19 -15
- package/check/coda/contract.json +10 -13
- package/check/index.mjs +5 -4
- package/check/leftovers.mjs +1 -1
- package/check/manifest.mjs +7 -9
- package/check/module.mjs +5 -5
- package/check/report.mjs +1 -1
- package/check/resources.mjs +99 -0
- package/package.json +5 -4
- package/templates/resource/package.json +13 -10
- package/templates/resource/scripts/act.mjs +20 -0
- package/templates/resource/scripts/{resources.mjs → resource.mjs} +10 -5
- package/check/coda/resources/plugins/resources/panels/panels.mjs +0 -313
- package/check/coda/resources/plugins/resources/panels/rules.mjs +0 -232
- package/check/coda/resources/plugins/resources/panels/sign-in.mjs +0 -122
- package/check/panel.mjs +0 -78
- package/templates/resource/scripts/mark.mjs +0 -6
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { lastLine, settle, startPluginCommand } from '../../../../adapters/plugin-command.mjs';
|
|
3
|
-
import { PanelError } from './rules.mjs';
|
|
4
|
-
|
|
5
|
-
const check = (condition, code, message) => { if (!condition) throw new PanelError(code, message); };
|
|
6
|
-
|
|
7
|
-
const ansi = /\x1b\[[0-9;]*[A-Za-z]/g;
|
|
8
|
-
// A device-code login prints a verification URL that carries the code (`grok login --device-auth`, `kimi login`), or the
|
|
9
|
-
// code alone on a line after it. Only whole lines are read: output arrives in pieces, and a URL cut short would carry a
|
|
10
|
-
// code cut short. Codex uses a 4-5 code; Grok and Kimi use 4-4.
|
|
11
|
-
const codeFormat = /^[A-Z0-9]{4}-[A-Z0-9]{4,5}$/;
|
|
12
|
-
export function deviceCode(text) {
|
|
13
|
-
const plain = text.slice(0, text.lastIndexOf('\n') + 1).replace(ansi, '');
|
|
14
|
-
const url = plain.match(/https:\/\/\S+/)?.[0] ?? null;
|
|
15
|
-
let code = null;
|
|
16
|
-
try { code = url && new URL(url).searchParams.get('user_code'); } catch { return null; }
|
|
17
|
-
code ||= plain.match(/^\s*([A-Z0-9]{4}-[A-Z0-9]{4,5})\s*$/m)?.[1] ?? null;
|
|
18
|
-
return url && code && codeFormat.test(code) ? { url, code } : null;
|
|
19
|
-
}
|
|
20
|
-
// A login that asks for the authorization code its page shows prints the page's link and waits: the first whole line's
|
|
21
|
-
// link is it. Checking that the code belongs to this login is the login's own job.
|
|
22
|
-
export function authorizationLink(text) {
|
|
23
|
-
const url = text.slice(0, text.lastIndexOf('\n') + 1).replace(ansi, '').match(/https:\/\/\S+/)?.[0];
|
|
24
|
-
try { return url ? { url: new URL(url).href, input: 'authorizationCode' } : null; } catch { return null; }
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Signing in from Coda through a plugin's panel (docs/plugins.md「面板的登录」, panels.mjs): `login` is the
|
|
28
|
-
// panel's own — its login command, whether what it signs in to is there (`installed()`), the command that installs it, and
|
|
29
|
-
// how the link and code are read from what the login prints (`readCode`); `input`, whether the login asks for the
|
|
30
|
-
// authorization code its page shows, which goes to its standard input. Coda passes the link and code on; the command writes
|
|
31
|
-
// and renews the credentials, which Coda never reads.
|
|
32
|
-
// One login or install of each at a time: any page starting it again sees the one under way, and every page is told as it
|
|
33
|
-
// moves (`onChange`). Neither outlives the module that started it (`close` ends it), so neither is kept for after a restart.
|
|
34
|
-
// Both run as every plugin command does (adapters/plugin-command.mjs, docs/plugins.md「命令的约定」), through `start`, from
|
|
35
|
-
// the plugin's directory: in a process group of their own, cancelled with TERM and made to end past the grace. Neither
|
|
36
|
-
// answers: what they print is read for the link and code, or for why they failed, and ending with 0 is all of it.
|
|
37
|
-
export class SignIn {
|
|
38
|
-
constructor(login, { start, env = () => process.env, onChange = () => {}, onSignedIn = () => {}, onInstalled = () => {}, timeoutMs = 15 * 60 * 1000, startTimeoutMs = 20000, installTimeoutMs = 10 * 60 * 1000 }) {
|
|
39
|
-
this.login = login; this.processStart = start; this.env = env; this.onChange = onChange; this.onSignedIn = onSignedIn; this.onInstalled = onInstalled;
|
|
40
|
-
this.timeoutMs = timeoutMs; this.startTimeoutMs = startTimeoutMs; this.installTimeoutMs = installTimeoutMs;
|
|
41
|
-
this._state = { state: 'idle' }; this.child = null;
|
|
42
|
-
}
|
|
43
|
-
get state() { return this._state; }
|
|
44
|
-
set state(value) { this._state = value; this.onChange(); }
|
|
45
|
-
get available() { return this.login.installed(); }
|
|
46
|
-
view() { return { provider: this.login.provider, name: this.login.name, installed: this.available,
|
|
47
|
-
...(this.login.note ? { note: this.login.note } : {}), ...this.state }; }
|
|
48
|
-
get busy() { return ['starting', 'waiting', 'installing'].includes(this.state.state); }
|
|
49
|
-
spawn(command, { open = false, tail = false, timeoutMs, onOutput }) {
|
|
50
|
-
const env = this.env();
|
|
51
|
-
return startPluginCommand(command, { start: this.processStart, cwd: env.CODA_PLUGIN_PATH, env, open, tail, timeoutMs, onOutput });
|
|
52
|
-
}
|
|
53
|
-
// Starts the installer and answers at once: clients follow a download as they follow a waiting login.
|
|
54
|
-
install() {
|
|
55
|
-
if (this.busy) return this.view();
|
|
56
|
-
if (this.available) { this.state = { state: 'idle' }; return this.view(); }
|
|
57
|
-
if (!this.login.install) { this.state = { state: 'failed', error: `这台机器没有安装 ${this.login.name},Coda 不能代为安装` }; return this.view(); }
|
|
58
|
-
// An installer may say much on its way: the last of it is kept, and it is never stopped for that.
|
|
59
|
-
const child = this.spawn(this.login.install, { tail: true, timeoutMs: this.installTimeoutMs });
|
|
60
|
-
this.child = child;
|
|
61
|
-
this.state = { state: 'installing' };
|
|
62
|
-
child.done.then(ran => {
|
|
63
|
-
if (this.child === child) {
|
|
64
|
-
this.child = null;
|
|
65
|
-
const { error } = settle(ran, { answers: false, what: '安装' });
|
|
66
|
-
// Asked anew: what was there a moment ago is not what the installer left.
|
|
67
|
-
this.state = !error && this.login.installed(true) ? { state: 'idle' } : { state: 'failed', error: error?.message ?? '安装没有完成' };
|
|
68
|
-
if (this.state.state === 'idle') this.onInstalled();
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
return this.view();
|
|
72
|
-
}
|
|
73
|
-
// Resolves once the link and code are known, or the login has failed.
|
|
74
|
-
start() {
|
|
75
|
-
if (this.busy) return this.ready ?? Promise.resolve(this.view());
|
|
76
|
-
if (!this.available) { this.state = { state: 'failed', error: `这台机器没有安装 ${this.login.name}` }; return Promise.resolve(this.view()); }
|
|
77
|
-
const attemptId = randomUUID();
|
|
78
|
-
let output = '', silent = false, settleReady;
|
|
79
|
-
const ready = new Promise(resolve => { settleReady = resolve; });
|
|
80
|
-
const done = () => { settleReady(this.view()); if (this.ready === ready) this.ready = null; };
|
|
81
|
-
const read = chunk => {
|
|
82
|
-
output = (output + chunk).slice(-8000);
|
|
83
|
-
const found = this.state.state === 'starting' && this.child === child && this.login.readCode(output);
|
|
84
|
-
if (found) { this.state = { state: 'waiting', ...found, ...(this.login.input ? { attemptId } : {}) }; done(); }
|
|
85
|
-
};
|
|
86
|
-
const child = this.spawn(this.login.command, { open: this.login.input, timeoutMs: this.timeoutMs, onOutput: read });
|
|
87
|
-
this.child = child;
|
|
88
|
-
this.ready = ready;
|
|
89
|
-
this.state = { state: 'starting' };
|
|
90
|
-
const startTimer = setTimeout(() => { if (this.child === child && this.state.state === 'starting') { silent = true; child.stop(); } }, this.startTimeoutMs);
|
|
91
|
-
child.done.then(ran => {
|
|
92
|
-
clearTimeout(startTimer);
|
|
93
|
-
if (this.child !== child) return done(); // Cancelled: the state is already idle.
|
|
94
|
-
this.child = null;
|
|
95
|
-
const { error } = settle(ran, { answers: false, what: '登录' });
|
|
96
|
-
if (!error) { this.state = { state: 'idle' }; this.onSignedIn(); }
|
|
97
|
-
else if (ran.timedOut) this.state = { state: 'expired' };
|
|
98
|
-
// Stopped for printing no link in time, it failed as it says, not as one cancelled.
|
|
99
|
-
else if (this.state.state !== 'idle') this.state = { state: 'failed', error: silent ? lastLine(output) || '登录没有完成' : error.message };
|
|
100
|
-
done();
|
|
101
|
-
});
|
|
102
|
-
return ready;
|
|
103
|
-
}
|
|
104
|
-
submit({ attemptId, code } = {}) {
|
|
105
|
-
check(this.login.input && this.child && this.state.state === 'waiting' && attemptId === this.state.attemptId,
|
|
106
|
-
'invalid_input', '此登录已结束,请重新登录');
|
|
107
|
-
check(typeof code === 'string' && code.length <= 4096, 'invalid_input', '请粘贴完整的授权码');
|
|
108
|
-
if (!this.state.submitted) {
|
|
109
|
-
this.child.write(`${code.trim()}\n`);
|
|
110
|
-
this.state = { ...this.state, submitted: true };
|
|
111
|
-
}
|
|
112
|
-
return this.view();
|
|
113
|
-
}
|
|
114
|
-
cancel() {
|
|
115
|
-
const child = this.child; this.child = null;
|
|
116
|
-
child?.stop();
|
|
117
|
-
this.state = { state: 'idle' };
|
|
118
|
-
return this.view();
|
|
119
|
-
}
|
|
120
|
-
// `signal`: KILL as the module goes (the plugin host stopping), when nothing would end one that does not stop when asked.
|
|
121
|
-
close(signal = 'SIGTERM') { if (signal === 'SIGKILL') this.child?.kill(); else this.child?.stop(); this.child = null; }
|
|
122
|
-
}
|
package/check/panel.mjs
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
// A resource type (or a panel before them) at the handshake level: read once by Coda's own resources module
|
|
2
|
-
// (check/coda/resources/plugins/resources/panels/, Panels), given what Coda's plugin host gives it
|
|
3
|
-
// (components/plugins/plugin-host.mjs): the installed plugins as it watches them, here the one checked, and each one's
|
|
4
|
-
// environment, as Coda gives every process of the plugin's.
|
|
5
|
-
// Its actions are not run: a button may change things (switch an account, spend a reset), and only a person presses it.
|
|
6
|
-
import { dirname } from 'node:path';
|
|
7
|
-
import { cleared } from './coda/components/plugins/plugin-rules.mjs';
|
|
8
|
-
import { ENVELOPE } from './coda/adapters/call-limit.mjs';
|
|
9
|
-
import { runPluginCommand } from './coda/adapters/plugin-command.mjs';
|
|
10
|
-
import { Panels } from './coda/resources/plugins/resources/panels/panels.mjs';
|
|
11
|
-
import { panelItems, resourceItems } from './coda/resources/plugins/resources/panels/rules.mjs';
|
|
12
|
-
import { closing, temporary } from './leftovers.mjs';
|
|
13
|
-
|
|
14
|
-
// The session a process would belong to: the host's belong to none.
|
|
15
|
-
const THREAD_ENV = 'CODA_THREAD_ID';
|
|
16
|
-
|
|
17
|
-
// The item types a reading holds, cards' items included.
|
|
18
|
-
const typesOf = items => items.flatMap(item => item.type === 'card' ? ['card', ...item.items.map(i => i.type)] : [item.type]);
|
|
19
|
-
// What a panel's command printed as Coda takes it (panelItems), by type; throws PanelError with where it went wrong. For the
|
|
20
|
-
// tests of a plugin's panel.
|
|
21
|
-
export const panelTypes = (output, panel, views = []) => typesOf(panelItems(output, panel, views));
|
|
22
|
-
// What a resource type's command printed as Coda takes it (resourceItems): its resources, each with only the keys it has and
|
|
23
|
-
// its values and buttons; throws PanelError with where it went wrong. For the tests of a plugin's resources.
|
|
24
|
-
export const readResources = (output, type, views = []) => resourceItems(output, type, views);
|
|
25
|
-
// A reading of resources in a line: how many, and how many values and buttons they have.
|
|
26
|
-
const counted = resources => `${resources.length} 个资源,读数 ${resources.reduce((n, r) => n + r.values.length, 0)} 个,按钮 ${resources.reduce((n, r) => n + r.actions.length, 0)} 个${
|
|
27
|
-
resources.some(r => r.error) ? `,${resources.filter(r => r.error).length} 个写了错误` : ''}`;
|
|
28
|
-
|
|
29
|
-
// `checked`: the plugin as the manifest level read it (manifest.mjs checkManifest). `limit`: how long a reading may take, as
|
|
30
|
-
// Panels has it unless given.
|
|
31
|
-
export async function checkPanel({ panel, checked, say, limit }) {
|
|
32
|
-
const { plugin } = checked;
|
|
33
|
-
// The plugin's state directory, which Coda keeps for it; here a temporary one, gone however the checker ends.
|
|
34
|
-
const state = temporary('coda-plugin-check-state-');
|
|
35
|
-
// The host's environment of a plugin (env): its processes', the backend's `node` first on the PATH, and the concurrency
|
|
36
|
-
// groups in use as it is asked for (none here).
|
|
37
|
-
const environment = async () => {
|
|
38
|
-
const env = { ...cleared(process.env), ...checked.env(state.dir) };
|
|
39
|
-
delete env[THREAD_ENV];
|
|
40
|
-
env.PATH = [dirname(process.execPath), env.PATH].filter(Boolean).join(':');
|
|
41
|
-
env.CODA_GROUPS_STATE = JSON.stringify({ now: Date.now(), groups: [] });
|
|
42
|
-
return env;
|
|
43
|
-
};
|
|
44
|
-
// The installed plugins as a module watches them (components/plugins/plugins.mjs offered).
|
|
45
|
-
const offered = { id: plugin.id, name: plugin.displayName, version: plugin.version, source: plugin.source, views: plugin.views.map(v => v.id), contributes: plugin.offers ?? {} };
|
|
46
|
-
const coda = { plugin: { state: null }, environment,
|
|
47
|
-
watch: async (name, told) => { told({ type: 'ADDED', id: offered.id, object: offered }); return { dispose() {} }; } };
|
|
48
|
-
// The command's own result, beside what the page shows of it: whether it ended well and what it printed was not a panel's.
|
|
49
|
-
let result = null;
|
|
50
|
-
const panels = new Panels(coda, { run: async (command, options) => (result = await runPluginCommand(command, options)), ...(limit ? { readTimeoutMs: limit } : {}) });
|
|
51
|
-
const done = closing(() => panels.close());
|
|
52
|
-
try {
|
|
53
|
-
await panels.start();
|
|
54
|
-
const started = Date.now(), reading = await panels.read(`${plugin.id}/${panel.id}`), took = ((Date.now() - started) / 1000).toFixed(1);
|
|
55
|
-
const kind = panel.format === 'resources' ? '资源' : '面板';
|
|
56
|
-
if (reading.error && result && result.code === 0 && !result.timedOut && !result.overflow) return say.fail('panel.output', `输出不合${kind}格式:资源页显示「${reading.error}」`);
|
|
57
|
-
if (reading.error) {
|
|
58
|
-
const why = !result ? '没有运行' : result.timedOut ? `${panels.readTimeoutMs / 1000} 秒没读完,Coda 停下这次读取` : result.overflow ? `标准输出超过 ${ENVELOPE / 1024 / 1024} MiB,Coda 停下这次读取`
|
|
59
|
-
: result.error ? '命令起不来' : `退出码 ${result.code}`;
|
|
60
|
-
return say.fail('panel.read', `读取没成功(${why}):资源页显示「${reading.error}」`);
|
|
61
|
-
}
|
|
62
|
-
const empty = `资源页不显示它${panel.signIn ? ',它出现在「添加资源」里' : ''}`;
|
|
63
|
-
if (panel.format === 'resources') {
|
|
64
|
-
if (!reading.resources.length) say.info('panel.read', `读出空的 resources(${took} 秒):${empty}`);
|
|
65
|
-
else say.pass('panel.read', `读取 ${took} 秒,输出合资源格式:${counted(reading.resources)}`);
|
|
66
|
-
} else {
|
|
67
|
-
const types = typesOf(reading.items);
|
|
68
|
-
const counts = Object.entries(Object.groupBy(types, t => t)).map(([t, list]) => `${t} ×${list.length}`).join(',');
|
|
69
|
-
if (!types.length) say.info('panel.read', `读出空的 items(${took} 秒):${empty}`);
|
|
70
|
-
else say.pass('panel.read', `读取 ${took} 秒,输出合面板格式:${counts}`);
|
|
71
|
-
}
|
|
72
|
-
if (Object.keys(panel.actions).length) say.skip('panel.actions', `动作 ${Object.keys(panel.actions).join('、')} 不运行:按钮可能改动东西,只由人点`);
|
|
73
|
-
} finally {
|
|
74
|
-
panels.close();
|
|
75
|
-
done();
|
|
76
|
-
state.remove();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
// 「记下此刻」按钮的动作:Coda 在点按钮(并确认)后运行它,退出码 0 就算成功,随后重读这个类型。
|
|
2
|
-
// 按钮带的 args 在 CODA_PANEL_ARGS 里(JSON),这个按钮没带。
|
|
3
|
-
import { writeFileSync } from 'node:fs';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
|
|
6
|
-
writeFileSync(join(process.env.CODA_PLUGIN_STATE, 'mark.json'), JSON.stringify({ at: new Date().toISOString() }));
|