@sdeck/plugin 0.0.0-stage → 0.1.0
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/bridge.d.ts +42 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/connection.d.ts +44 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3352 -0
- package/dist/index.js.map +37 -0
- package/dist/manifest.d.ts +45 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/plugin.d.ts +304 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/settings.d.ts +107 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.d.ts.map +1 -0
- package/package.json +41 -4
- package/src/bridge.ts +128 -0
- package/src/connection.ts +206 -0
- package/src/index.ts +83 -0
- package/src/manifest.ts +85 -0
- package/src/plugin.ts +541 -0
- package/src/settings.ts +232 -0
- package/src/shared.ts +34 -0
- package/README.md +0 -3
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import type { SlotInfo } from "@sdeck/app";
|
|
2
|
+
import type { ExecSource, StateValueType } from "@sdeck/engine";
|
|
3
|
+
import type { Look, NavigateTarget } from "@sdeck/schema";
|
|
4
|
+
import type { ComponentType } from "react";
|
|
5
|
+
import {
|
|
6
|
+
checkSchema,
|
|
7
|
+
type InferSettings,
|
|
8
|
+
type SelectOption,
|
|
9
|
+
type SettingsSchema,
|
|
10
|
+
} from "./settings.ts";
|
|
11
|
+
|
|
12
|
+
/** Bumped when the plugin contract changes incompatibly. Hosts refuse plugins built for another. */
|
|
13
|
+
export const PLUGIN_API_VERSION = 2;
|
|
14
|
+
|
|
15
|
+
/** Where an action can be placed. */
|
|
16
|
+
export type SlotKind = "key" | "sensor" | "window";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a plugin declares it does. Shown to the user before enabling it. Plugins run in the host
|
|
20
|
+
* process, so this is information, not a sandbox.
|
|
21
|
+
*/
|
|
22
|
+
export type Permission =
|
|
23
|
+
| "shell"
|
|
24
|
+
| "process"
|
|
25
|
+
| "filesystem"
|
|
26
|
+
| "network"
|
|
27
|
+
| "input"
|
|
28
|
+
| "audio"
|
|
29
|
+
| "clipboard";
|
|
30
|
+
|
|
31
|
+
export const PERMISSIONS: readonly Permission[] = [
|
|
32
|
+
"shell",
|
|
33
|
+
"process",
|
|
34
|
+
"filesystem",
|
|
35
|
+
"network",
|
|
36
|
+
"input",
|
|
37
|
+
"audio",
|
|
38
|
+
"clipboard",
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
/** Given to a plugin's `activate`, its actions, state providers and triggers. */
|
|
42
|
+
export interface PluginContext {
|
|
43
|
+
readonly pluginId: string;
|
|
44
|
+
/** Prefix of this plugin's state keys and events (`system` → `system.cpu`). */
|
|
45
|
+
readonly namespace: string;
|
|
46
|
+
/** A writable directory for this plugin's own files (created on first use). */
|
|
47
|
+
readonly dataDir: string;
|
|
48
|
+
/** Logs to the host, tagged with the plugin id. */
|
|
49
|
+
log(...args: unknown[]): void;
|
|
50
|
+
/** The plugin's own settings (secrets included), resolved against `PluginDef.settings`. */
|
|
51
|
+
settings(): Record<string, unknown>;
|
|
52
|
+
/** Called after the plugin's settings change; returns a function that stops. */
|
|
53
|
+
onSettings(listener: () => void): () => void;
|
|
54
|
+
/** Values the plugin keeps private between runs (tokens it obtained, for example). */
|
|
55
|
+
readonly secrets: {
|
|
56
|
+
get(key: string): string | undefined;
|
|
57
|
+
set(key: string, value: string | undefined): Promise<void>;
|
|
58
|
+
};
|
|
59
|
+
/** Shown with the plugin in the UI (`Connected`, `Sign in to continue`). `undefined` clears. */
|
|
60
|
+
status(text: string | undefined, level?: StatusLevel): void;
|
|
61
|
+
/** Live state: `set` publishes `<namespace>.<id>`, `get` reads any key (`system.cpu`). */
|
|
62
|
+
readonly state: {
|
|
63
|
+
set(id: string, value: unknown): void;
|
|
64
|
+
get(key: string): unknown;
|
|
65
|
+
};
|
|
66
|
+
/** Emits `<namespace>:<event>` on the event bus (workflows can trigger on it). */
|
|
67
|
+
emit(event: string, payload?: unknown): void;
|
|
68
|
+
/** Global user variables. */
|
|
69
|
+
readonly vars: {
|
|
70
|
+
get(name: string): unknown;
|
|
71
|
+
set(name: string, value: unknown): void;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** How a plugin's status reads: fine, needing attention, or failing. */
|
|
76
|
+
export type StatusLevel = "ok" | "info" | "error";
|
|
77
|
+
|
|
78
|
+
/** Something the user can run from the plugin's page (sign in, reconnect). */
|
|
79
|
+
export interface PluginCommand {
|
|
80
|
+
/** Unique within the plugin. */
|
|
81
|
+
id: string;
|
|
82
|
+
name: string;
|
|
83
|
+
description?: string;
|
|
84
|
+
/** What it returns, if a string, is opened in the user's browser. Throw to fail. */
|
|
85
|
+
run(context: PluginContext): unknown | Promise<unknown>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* One step of setting a plugin up. Studio walks the user through the steps right after the plugin
|
|
90
|
+
* is installed, and again from its "Get started" button.
|
|
91
|
+
*/
|
|
92
|
+
export interface OnboardingStep {
|
|
93
|
+
/** Unique within the plugin's steps. */
|
|
94
|
+
id: string;
|
|
95
|
+
title: string;
|
|
96
|
+
/**
|
|
97
|
+
* What to do, in plain words. Blank lines start paragraphs; `**bold**`, `` `code` `` and
|
|
98
|
+
* `[links](https://…)` work.
|
|
99
|
+
*/
|
|
100
|
+
body?: string;
|
|
101
|
+
/** The plugin's own settings this step asks for, by key (secrets too). */
|
|
102
|
+
settings?: readonly string[];
|
|
103
|
+
/** One of the plugin's commands, run from a button in this step (sign in, find devices). */
|
|
104
|
+
command?: string;
|
|
105
|
+
/** A web page to open, such as where to create an API key. */
|
|
106
|
+
link?: { label: string; url: string };
|
|
107
|
+
/**
|
|
108
|
+
* Whether the step is done: a key is saved, the sign-in went through. Without it, a step that
|
|
109
|
+
* asks for settings is done once they all have a value.
|
|
110
|
+
*/
|
|
111
|
+
done?(context: PluginContext): boolean | Promise<boolean>;
|
|
112
|
+
/** Not needed to use the plugin (Bluetooth lights need no cloud key). */
|
|
113
|
+
optional?: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Keys a plugin offers as a page of their own at the end of onboarding. */
|
|
117
|
+
export interface StarterPage {
|
|
118
|
+
/** The page's name (default: the plugin's). */
|
|
119
|
+
name?: string;
|
|
120
|
+
/** By key index: the action's id and settings. */
|
|
121
|
+
keys: Readonly<Record<number, { action: string; settings?: Record<string, unknown> }>>;
|
|
122
|
+
sensors?: Readonly<Record<number, { action: string; settings?: Record<string, unknown> }>>;
|
|
123
|
+
window?: { action: string; settings?: Record<string, unknown> };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** How a plugin is set up after it is installed. */
|
|
127
|
+
export interface Onboarding {
|
|
128
|
+
/** A sentence or two shown before the steps. */
|
|
129
|
+
intro?: string;
|
|
130
|
+
steps: readonly OnboardingStep[];
|
|
131
|
+
/**
|
|
132
|
+
* The page of keys offered at the end. By default the plugin's first eight actions for keys;
|
|
133
|
+
* `false` offers none.
|
|
134
|
+
*/
|
|
135
|
+
starterPage?: StarterPage | false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Props of an action's component. */
|
|
139
|
+
export interface ActionProps<S extends SettingsSchema = SettingsSchema> {
|
|
140
|
+
/** This placement's settings, resolved against the schema (always complete and typed). */
|
|
141
|
+
settings: InferSettings<S>;
|
|
142
|
+
/** Saves changed settings for this placement (persisted and shown in the UI). */
|
|
143
|
+
setSettings(patch: Partial<InferSettings<S>>): void;
|
|
144
|
+
/** Where the action is drawn. */
|
|
145
|
+
slot: SlotInfo;
|
|
146
|
+
plugin: PluginContext;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Given to an action's `run`. */
|
|
150
|
+
export interface RunContext<S extends SettingsSchema = SettingsSchema> {
|
|
151
|
+
/** Settings with `{{templates}}` already resolved, then checked against the schema. */
|
|
152
|
+
settings: InferSettings<S>;
|
|
153
|
+
plugin: PluginContext;
|
|
154
|
+
/** What started the run (a key, a trigger, a workflow…). */
|
|
155
|
+
source: ExecSource;
|
|
156
|
+
/** Aborted when the execution is cancelled. Long work should stop. */
|
|
157
|
+
signal: AbortSignal;
|
|
158
|
+
/** The triggering event's payload, if any. */
|
|
159
|
+
event: unknown;
|
|
160
|
+
/** Controls of the deck the run started on (every deck, for triggers and workflows). */
|
|
161
|
+
deck: DeckControls;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** What a headless action can do to the decks. */
|
|
165
|
+
export interface DeckControls {
|
|
166
|
+
/** Moves between pages, as a `navigate` workflow step does. Throws for an unknown page. */
|
|
167
|
+
navigate(to: NavigateTarget, page?: string, options?: { wrap?: boolean }): void;
|
|
168
|
+
/** The current brightness (0–100). */
|
|
169
|
+
brightness(): number;
|
|
170
|
+
/** Sets the brightness (0–100). */
|
|
171
|
+
setBrightness(level: number): Promise<void>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* What `run` gives back. `undo` reverts what the run did; it is recorded in the action history
|
|
176
|
+
* and used by toggle buttons and the `undo` step.
|
|
177
|
+
*/
|
|
178
|
+
export interface RunOutcome {
|
|
179
|
+
result?: unknown;
|
|
180
|
+
undo?: () => unknown;
|
|
181
|
+
/** Summary for the history, e.g. `Volume 30%`. */
|
|
182
|
+
label?: string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export type RunResult = RunOutcome | undefined;
|
|
186
|
+
|
|
187
|
+
export interface ActionDef<S extends SettingsSchema = SettingsSchema> {
|
|
188
|
+
/** Unique within the plugin, e.g. `"clock"`. */
|
|
189
|
+
id: string;
|
|
190
|
+
name: string;
|
|
191
|
+
description?: string;
|
|
192
|
+
/** Groups actions in the picker (default: the plugin's name). */
|
|
193
|
+
category?: string;
|
|
194
|
+
/**
|
|
195
|
+
* Icon for the UI: a [lucide](https://lucide.dev/icons) icon name in kebab case (`"timer"`),
|
|
196
|
+
* or an image URL / `data:` URI.
|
|
197
|
+
*/
|
|
198
|
+
icon?: string;
|
|
199
|
+
/** Slot kinds the action supports (default: keys only). */
|
|
200
|
+
slots?: readonly SlotKind[];
|
|
201
|
+
/** Settings shown in the UI, as a form. */
|
|
202
|
+
settings?: S;
|
|
203
|
+
/**
|
|
204
|
+
* Does the work, without drawing anything: it can run from a key, a trigger or a workflow.
|
|
205
|
+
* Throw to fail. An action needs `run`, `component`, or both.
|
|
206
|
+
*/
|
|
207
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: lets `run` simply not return anything.
|
|
208
|
+
run?(context: RunContext<S>): RunResult | void | Promise<RunResult | void>;
|
|
209
|
+
/**
|
|
210
|
+
* Draws the action itself (it then handles its own input). It runs inside the deck's React
|
|
211
|
+
* tree, so the `@sdeck/app` hooks and components work as in any app. Without it, the host
|
|
212
|
+
* draws the button's look and runs `run` on press.
|
|
213
|
+
*/
|
|
214
|
+
component?: ComponentType<ActionProps<S>>;
|
|
215
|
+
/** The look a new button with this action starts with (default: its name on a dark key). */
|
|
216
|
+
look?(settings: InferSettings<S>): Look;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export type RunFn = (context: RunContext) => RunResult | undefined | Promise<RunResult | undefined>;
|
|
220
|
+
|
|
221
|
+
/** An action with its settings type erased, as stored on a plugin. */
|
|
222
|
+
export interface Action {
|
|
223
|
+
readonly id: string;
|
|
224
|
+
readonly name: string;
|
|
225
|
+
readonly description?: string;
|
|
226
|
+
readonly category?: string;
|
|
227
|
+
readonly icon?: string;
|
|
228
|
+
readonly slots: readonly SlotKind[];
|
|
229
|
+
readonly settings: SettingsSchema;
|
|
230
|
+
readonly run?: RunFn;
|
|
231
|
+
readonly component?: ComponentType<ActionProps>;
|
|
232
|
+
readonly look?: (settings: Record<string, unknown>) => Look;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** A live value the plugin publishes as `<namespace>.<id>`. */
|
|
236
|
+
export interface StateDef {
|
|
237
|
+
/** Letters, digits, `-`, `_` and `.` (`cpu`, `core.0`). */
|
|
238
|
+
id: string;
|
|
239
|
+
name: string;
|
|
240
|
+
type: StateValueType;
|
|
241
|
+
unit?: string;
|
|
242
|
+
description?: string;
|
|
243
|
+
/** Read every `everyMs` while something shows or waits on the value. */
|
|
244
|
+
poll?: { everyMs: number; read(context: PluginContext): unknown | Promise<unknown> };
|
|
245
|
+
/** Push updates while watched; return a stop function. */
|
|
246
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: lets `start` simply not return anything.
|
|
247
|
+
start?(set: (value: unknown) => void, context: PluginContext): (() => void) | void;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Given to a trigger's `subscribe`. */
|
|
251
|
+
export interface TriggerContext<S extends SettingsSchema = SettingsSchema> {
|
|
252
|
+
settings: InferSettings<S>;
|
|
253
|
+
plugin: PluginContext;
|
|
254
|
+
/** Starts the trigger's workflow; `event` becomes `event` in its expressions. */
|
|
255
|
+
fire(event?: unknown): void;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export interface TriggerDef<S extends SettingsSchema = SettingsSchema> {
|
|
259
|
+
/** Unique within the plugin; the trigger type is `<plugin id>:<id>`. */
|
|
260
|
+
id: string;
|
|
261
|
+
name: string;
|
|
262
|
+
description?: string;
|
|
263
|
+
settings?: S;
|
|
264
|
+
/** Starts watching; returns a function that stops. */
|
|
265
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: lets `subscribe` simply not return anything.
|
|
266
|
+
subscribe(context: TriggerContext<S>): (() => void) | void;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface Trigger {
|
|
270
|
+
readonly id: string;
|
|
271
|
+
readonly name: string;
|
|
272
|
+
readonly description?: string;
|
|
273
|
+
readonly settings: SettingsSchema;
|
|
274
|
+
readonly subscribe: (context: TriggerContext) => (() => void) | undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Fills fields that name it in `source` (OBS scenes, audio devices…). `settings` holds the
|
|
279
|
+
* fields the asking field `dependsOn` (a server whose channels to list).
|
|
280
|
+
*/
|
|
281
|
+
export type OptionSource = (
|
|
282
|
+
context: PluginContext,
|
|
283
|
+
settings: Readonly<Record<string, unknown>>,
|
|
284
|
+
) => SelectOption[] | Promise<SelectOption[]>;
|
|
285
|
+
|
|
286
|
+
/** Undoes what `activate` did. */
|
|
287
|
+
export type Deactivate = () => void | Promise<void>;
|
|
288
|
+
|
|
289
|
+
export interface PluginDef {
|
|
290
|
+
/** Unique id; for an installed plugin this must be its package name. */
|
|
291
|
+
id: string;
|
|
292
|
+
name: string;
|
|
293
|
+
/** Semver version. */
|
|
294
|
+
version: string;
|
|
295
|
+
description?: string;
|
|
296
|
+
author?: string;
|
|
297
|
+
homepage?: string;
|
|
298
|
+
/** State key and event prefix (default: derived from the id, `@sdeck/plugin-system` → `system`). */
|
|
299
|
+
namespace?: string;
|
|
300
|
+
actions: readonly Action[];
|
|
301
|
+
state?: readonly StateDef[];
|
|
302
|
+
triggers?: readonly Trigger[];
|
|
303
|
+
optionSources?: Readonly<Record<string, OptionSource>>;
|
|
304
|
+
permissions?: readonly Permission[];
|
|
305
|
+
/** The plugin's own settings (a server address, a token), shown on its page. */
|
|
306
|
+
settings?: SettingsSchema;
|
|
307
|
+
/** Buttons on the plugin's page. */
|
|
308
|
+
commands?: readonly PluginCommand[];
|
|
309
|
+
/**
|
|
310
|
+
* How to set the plugin up, step by step, shown after it is installed. Without it, Studio asks
|
|
311
|
+
* for the plugin's settings and offers its commands in one step.
|
|
312
|
+
*/
|
|
313
|
+
onboarding?: Onboarding;
|
|
314
|
+
/** Runs when the plugin is enabled (and when the host starts). May return a cleanup. */
|
|
315
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: lets `activate` simply not return anything.
|
|
316
|
+
activate?(context: PluginContext): void | Deactivate | Promise<void | Deactivate>;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const ID = /^(@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/;
|
|
320
|
+
const NAMESPACE = /^[a-z][\w-]{0,31}$/;
|
|
321
|
+
const LOCAL_ID = /^[\w.-]+$/;
|
|
322
|
+
|
|
323
|
+
/** The namespace a plugin's state and events use. */
|
|
324
|
+
export function pluginNamespace(def: Pick<PluginDef, "id" | "namespace">): string {
|
|
325
|
+
if (def.namespace) return def.namespace;
|
|
326
|
+
const base = def.id
|
|
327
|
+
.replace(/^@[^/]+\//, "")
|
|
328
|
+
.replace(/^sdeck-plugin-/, "")
|
|
329
|
+
.replace(/^plugin-/, "")
|
|
330
|
+
.toLowerCase()
|
|
331
|
+
.replace(/[^\w-]/g, "-");
|
|
332
|
+
return /^[a-z]/.test(base) ? base.slice(0, 32) : `p-${base}`.slice(0, 32);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Defines an action. `settings` is inferred, so `run` and `component` get typed settings:
|
|
337
|
+
*
|
|
338
|
+
* ```ts
|
|
339
|
+
* export const hello = defineAction({
|
|
340
|
+
* id: "hello",
|
|
341
|
+
* name: "Hello",
|
|
342
|
+
* settings: { name: { type: "text", label: "Name", default: "world" } },
|
|
343
|
+
* run: ({ settings, plugin }) => plugin.log(`Hi ${settings.name}`),
|
|
344
|
+
* });
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
export function defineAction<const S extends SettingsSchema = Record<never, never>>(
|
|
348
|
+
def: ActionDef<S>,
|
|
349
|
+
): Action {
|
|
350
|
+
return {
|
|
351
|
+
...def,
|
|
352
|
+
slots: def.slots ?? ["key"],
|
|
353
|
+
settings: def.settings ?? {},
|
|
354
|
+
} as unknown as Action;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Defines a trigger type with typed settings. */
|
|
358
|
+
export function defineTrigger<const S extends SettingsSchema = Record<never, never>>(
|
|
359
|
+
def: TriggerDef<S>,
|
|
360
|
+
): Trigger {
|
|
361
|
+
return { ...def, settings: def.settings ?? {} } as unknown as Trigger;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Defines a plugin, checking it. A plugin module's default export must be one of these. */
|
|
365
|
+
export function definePlugin(def: PluginDef): PluginDef {
|
|
366
|
+
const problems = checkPlugin(def);
|
|
367
|
+
if (problems.length > 0) {
|
|
368
|
+
throw new TypeError(`invalid plugin "${def?.id}": ${problems.join("; ")}`);
|
|
369
|
+
}
|
|
370
|
+
return def;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function checkLocalIds(
|
|
374
|
+
kind: string,
|
|
375
|
+
items: readonly { id?: unknown }[],
|
|
376
|
+
problems: string[],
|
|
377
|
+
): Set<string> {
|
|
378
|
+
const seen = new Set<string>();
|
|
379
|
+
for (const item of items) {
|
|
380
|
+
const name = typeof item?.id === "string" ? item.id : "?";
|
|
381
|
+
if (typeof item?.id !== "string" || !LOCAL_ID.test(item.id)) {
|
|
382
|
+
problems.push(`${kind} "${name}": id must be letters, digits, ".", "-" or "_"`);
|
|
383
|
+
} else if (seen.has(item.id)) {
|
|
384
|
+
problems.push(`${kind} "${name}" is defined twice`);
|
|
385
|
+
}
|
|
386
|
+
seen.add(name);
|
|
387
|
+
}
|
|
388
|
+
return seen;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** Problems with something that should be a plugin (empty when it is fine). */
|
|
392
|
+
export function checkPlugin(value: unknown): string[] {
|
|
393
|
+
if (!value || typeof value !== "object") return ["not an object"];
|
|
394
|
+
const def = value as Partial<PluginDef>;
|
|
395
|
+
const problems: string[] = [];
|
|
396
|
+
if (typeof def.id !== "string" || !ID.test(def.id)) {
|
|
397
|
+
problems.push("id must be a package-style name (lowercase, e.g. `my-plugin` or `@me/plugin`)");
|
|
398
|
+
}
|
|
399
|
+
if (typeof def.name !== "string" || def.name === "") problems.push("name is required");
|
|
400
|
+
if (typeof def.version !== "string" || def.version === "") problems.push("version is required");
|
|
401
|
+
if (def.namespace !== undefined && !NAMESPACE.test(def.namespace)) {
|
|
402
|
+
problems.push("namespace must be lowercase letters, digits, - or _ (starting with a letter)");
|
|
403
|
+
}
|
|
404
|
+
if (def.activate !== undefined && typeof def.activate !== "function") {
|
|
405
|
+
problems.push("activate must be a function");
|
|
406
|
+
}
|
|
407
|
+
if (def.permissions !== undefined) {
|
|
408
|
+
if (!Array.isArray(def.permissions)) problems.push("permissions must be an array");
|
|
409
|
+
else
|
|
410
|
+
for (const p of def.permissions)
|
|
411
|
+
if (!PERMISSIONS.includes(p)) problems.push(`unknown permission "${String(p)}"`);
|
|
412
|
+
}
|
|
413
|
+
if (def.settings !== undefined) {
|
|
414
|
+
for (const p of checkSchema(def.settings, def.optionSources ?? {})) {
|
|
415
|
+
problems.push(`plugin ${p}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (def.commands !== undefined) {
|
|
419
|
+
if (!Array.isArray(def.commands)) problems.push("commands must be an array");
|
|
420
|
+
else {
|
|
421
|
+
checkLocalIds("command", def.commands, problems);
|
|
422
|
+
for (const c of def.commands as Partial<PluginCommand>[]) {
|
|
423
|
+
const name = typeof c?.id === "string" ? c.id : "?";
|
|
424
|
+
if (typeof c?.name !== "string") problems.push(`command "${name}": name is required`);
|
|
425
|
+
if (typeof c?.run !== "function") problems.push(`command "${name}": run is required`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (def.onboarding !== undefined) problems.push(...checkOnboarding(def));
|
|
430
|
+
const sources = def.optionSources ?? {};
|
|
431
|
+
if (typeof sources !== "object") problems.push("optionSources must be an object");
|
|
432
|
+
else
|
|
433
|
+
for (const [name, fn] of Object.entries(sources))
|
|
434
|
+
if (typeof fn !== "function") problems.push(`option source "${name}" must be a function`);
|
|
435
|
+
|
|
436
|
+
if (!Array.isArray(def.actions)) return [...problems, "actions must be an array"];
|
|
437
|
+
checkLocalIds("action", def.actions, problems);
|
|
438
|
+
for (const action of def.actions as Partial<Action>[]) {
|
|
439
|
+
const name = typeof action?.id === "string" ? action.id : "?";
|
|
440
|
+
if (typeof action?.name !== "string") problems.push(`action "${name}": name is required`);
|
|
441
|
+
const hasComponent =
|
|
442
|
+
typeof action?.component === "function" || typeof action?.component === "object";
|
|
443
|
+
const hasRun = typeof action?.run === "function";
|
|
444
|
+
if (!hasComponent && !hasRun) problems.push(`action "${name}": needs run or component`);
|
|
445
|
+
if (action?.look !== undefined && typeof action.look !== "function") {
|
|
446
|
+
problems.push(`action "${name}": look must be a function`);
|
|
447
|
+
}
|
|
448
|
+
const slots = action?.slots ?? ["key"];
|
|
449
|
+
if (
|
|
450
|
+
!Array.isArray(slots) ||
|
|
451
|
+
slots.length === 0 ||
|
|
452
|
+
slots.some((s) => !["key", "sensor", "window"].includes(s))
|
|
453
|
+
) {
|
|
454
|
+
problems.push(`action "${name}": slots must list "key", "sensor" and/or "window"`);
|
|
455
|
+
}
|
|
456
|
+
for (const p of checkSchema(action?.settings ?? {}, sources)) {
|
|
457
|
+
problems.push(`action "${name}": ${p}`);
|
|
458
|
+
}
|
|
459
|
+
// Action settings are stored in the config, which must stay free of secrets.
|
|
460
|
+
for (const [key, field] of Object.entries(action?.settings ?? {})) {
|
|
461
|
+
if (field?.type === "secret") {
|
|
462
|
+
problems.push(`action "${name}": setting "${key}": secrets belong in the plugin settings`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (def.state !== undefined) {
|
|
468
|
+
if (!Array.isArray(def.state)) problems.push("state must be an array");
|
|
469
|
+
else {
|
|
470
|
+
checkLocalIds("state", def.state, problems);
|
|
471
|
+
for (const s of def.state as Partial<StateDef>[]) {
|
|
472
|
+
const name = typeof s?.id === "string" ? s.id : "?";
|
|
473
|
+
if (typeof s?.name !== "string") problems.push(`state "${name}": name is required`);
|
|
474
|
+
if (!["number", "string", "boolean", "json"].includes(s?.type as string)) {
|
|
475
|
+
problems.push(`state "${name}": type must be number, string, boolean or json`);
|
|
476
|
+
}
|
|
477
|
+
if (s?.poll && (typeof s.poll.read !== "function" || !(s.poll.everyMs > 0))) {
|
|
478
|
+
problems.push(`state "${name}": poll needs read() and everyMs > 0`);
|
|
479
|
+
}
|
|
480
|
+
if (s?.start !== undefined && typeof s.start !== "function") {
|
|
481
|
+
problems.push(`state "${name}": start must be a function`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (def.triggers !== undefined) {
|
|
488
|
+
if (!Array.isArray(def.triggers)) problems.push("triggers must be an array");
|
|
489
|
+
else {
|
|
490
|
+
checkLocalIds("trigger", def.triggers, problems);
|
|
491
|
+
for (const t of def.triggers as Partial<Trigger>[]) {
|
|
492
|
+
const name = typeof t?.id === "string" ? t.id : "?";
|
|
493
|
+
if (typeof t?.name !== "string") problems.push(`trigger "${name}": name is required`);
|
|
494
|
+
if (typeof t?.subscribe !== "function") {
|
|
495
|
+
problems.push(`trigger "${name}": subscribe is required`);
|
|
496
|
+
}
|
|
497
|
+
for (const p of checkSchema(t?.settings ?? {}, sources)) {
|
|
498
|
+
problems.push(`trigger "${name}": ${p}`);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return problems;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Problems with a plugin's onboarding: steps that name settings, commands or actions it lacks. */
|
|
507
|
+
function checkOnboarding(def: Partial<PluginDef>): string[] {
|
|
508
|
+
const onboarding = def.onboarding as Partial<Onboarding>;
|
|
509
|
+
if (!Array.isArray(onboarding?.steps)) return ["onboarding: steps must be an array"];
|
|
510
|
+
const problems: string[] = [];
|
|
511
|
+
checkLocalIds("onboarding step", onboarding.steps, problems);
|
|
512
|
+
const settings = def.settings ?? {};
|
|
513
|
+
const commands = new Set((def.commands ?? []).map((c) => c.id));
|
|
514
|
+
for (const step of onboarding.steps as Partial<OnboardingStep>[]) {
|
|
515
|
+
const name = typeof step?.id === "string" ? step.id : "?";
|
|
516
|
+
if (typeof step?.title !== "string")
|
|
517
|
+
problems.push(`onboarding step "${name}": title is required`);
|
|
518
|
+
for (const key of step?.settings ?? []) {
|
|
519
|
+
if (!(key in settings)) problems.push(`onboarding step "${name}": no setting "${key}"`);
|
|
520
|
+
}
|
|
521
|
+
if (step?.command !== undefined && !commands.has(step.command)) {
|
|
522
|
+
problems.push(`onboarding step "${name}": no command "${step.command}"`);
|
|
523
|
+
}
|
|
524
|
+
if (step?.done !== undefined && typeof step.done !== "function") {
|
|
525
|
+
problems.push(`onboarding step "${name}": done must be a function`);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const page = onboarding.starterPage;
|
|
529
|
+
if (page) {
|
|
530
|
+
const actions = new Set((def.actions ?? []).map((a) => a.id));
|
|
531
|
+
const used = [
|
|
532
|
+
...Object.values(page.keys ?? {}),
|
|
533
|
+
...Object.values(page.sensors ?? {}),
|
|
534
|
+
...(page.window ? [page.window] : []),
|
|
535
|
+
];
|
|
536
|
+
for (const { action } of used) {
|
|
537
|
+
if (!actions.has(action)) problems.push(`onboarding starter page: no action "${action}"`);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return problems;
|
|
541
|
+
}
|