deictic 0.0.1 → 0.2.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/LICENSE +21 -0
- package/README.md +167 -0
- package/dist/annotations/client.d.ts +4 -0
- package/dist/annotations/client.d.ts.map +1 -0
- package/dist/annotations/client.js +497 -0
- package/dist/annotations/client.js.map +1 -0
- package/dist/annotations/index.d.ts +110 -0
- package/dist/annotations/index.d.ts.map +1 -0
- package/dist/annotations/index.js +22 -0
- package/dist/annotations/index.js.map +1 -0
- package/dist/astro.d.ts +29 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +24 -0
- package/dist/astro.js.map +1 -0
- package/dist/bun.d.ts +12 -0
- package/dist/bun.d.ts.map +1 -0
- package/dist/bun.js +67 -0
- package/dist/bun.js.map +1 -0
- package/dist/client/index.d.ts +95 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +1333 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +15 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +17 -0
- package/dist/loader.js.map +1 -0
- package/dist/next.d.ts +17 -0
- package/dist/next.d.ts.map +1 -0
- package/dist/next.js +150 -0
- package/dist/next.js.map +1 -0
- package/dist/recommended/index.d.ts +25 -0
- package/dist/recommended/index.d.ts.map +1 -0
- package/dist/recommended/index.js +19 -0
- package/dist/recommended/index.js.map +1 -0
- package/dist/recordings/client.d.ts +4 -0
- package/dist/recordings/client.d.ts.map +1 -0
- package/dist/recordings/client.js +376 -0
- package/dist/recordings/client.js.map +1 -0
- package/dist/recordings/index.d.ts +2 -0
- package/dist/recordings/index.js +3 -0
- package/dist/screenshots/client.d.ts +23 -0
- package/dist/screenshots/client.d.ts.map +1 -0
- package/dist/screenshots/client.js +76 -0
- package/dist/screenshots/client.js.map +1 -0
- package/dist/screenshots/dom.d.ts +4 -0
- package/dist/screenshots/dom.d.ts.map +1 -0
- package/dist/screenshots/dom.js +92 -0
- package/dist/screenshots/dom.js.map +1 -0
- package/dist/screenshots/index.d.ts +2 -0
- package/dist/screenshots/index.js +98 -0
- package/dist/screenshots/index.js.map +1 -0
- package/dist/screenshots/rrweb.d.ts +9 -0
- package/dist/screenshots/rrweb.d.ts.map +1 -0
- package/dist/screenshots/rrweb.js +60 -0
- package/dist/screenshots/rrweb.js.map +1 -0
- package/dist/shared/endpoints.js +354 -0
- package/dist/shared/endpoints.js.map +1 -0
- package/dist/shared/index.d.ts +226 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index2.d.ts +98 -0
- package/dist/shared/index2.d.ts.map +1 -0
- package/dist/shared/index3.d.ts +86 -0
- package/dist/shared/index3.d.ts.map +1 -0
- package/dist/shared/index4.d.ts +184 -0
- package/dist/shared/index4.d.ts.map +1 -0
- package/dist/shared/index5.d.ts +70 -0
- package/dist/shared/index5.d.ts.map +1 -0
- package/dist/shared/integration.d.ts +20 -0
- package/dist/shared/integration.d.ts.map +1 -0
- package/dist/shared/integration.js +279 -0
- package/dist/shared/integration.js.map +1 -0
- package/dist/shared/kept.js +23 -0
- package/dist/shared/kept.js.map +1 -0
- package/dist/shared/marks.js +14 -0
- package/dist/shared/marks.js.map +1 -0
- package/dist/shared/playwright.js +105 -0
- package/dist/shared/playwright.js.map +1 -0
- package/dist/shared/recordings.js +246 -0
- package/dist/shared/recordings.js.map +1 -0
- package/dist/shared/shortcut.js +63 -0
- package/dist/shared/shortcut.js.map +1 -0
- package/dist/shared/tasks.js +521 -0
- package/dist/shared/tasks.js.map +1 -0
- package/dist/shared/waveform.js +93 -0
- package/dist/shared/waveform.js.map +1 -0
- package/dist/tailwind/client.d.ts +4 -0
- package/dist/tailwind/client.d.ts.map +1 -0
- package/dist/tailwind/client.js +613 -0
- package/dist/tailwind/client.js.map +1 -0
- package/dist/tailwind/index.d.ts +2 -0
- package/dist/tailwind/index.js +557 -0
- package/dist/tailwind/index.js.map +1 -0
- package/dist/tasks/client.d.ts +6 -0
- package/dist/tasks/client.d.ts.map +1 -0
- package/dist/tasks/client.js +642 -0
- package/dist/tasks/client.js.map +1 -0
- package/dist/tasks/index.d.ts +2 -0
- package/dist/tasks/index.js +1 -0
- package/dist/vite.d.ts +16 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +61 -0
- package/dist/vite.js.map +1 -0
- package/dist/voice/client.d.ts +4 -0
- package/dist/voice/client.d.ts.map +1 -0
- package/dist/voice/client.js +66 -0
- package/dist/voice/client.js.map +1 -0
- package/dist/voice/index.d.ts +47 -0
- package/dist/voice/index.d.ts.map +1 -0
- package/dist/voice/index.js +74 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/webpack.d.ts +50 -0
- package/dist/webpack.d.ts.map +1 -0
- package/dist/webpack.js +78 -0
- package/dist/webpack.js.map +1 -0
- package/package.json +307 -7
- package/skills/deictic/SKILL.md +47 -0
- package/skills/deictic/references/install.md +96 -0
- package/skills/deictic/references/recordings.md +83 -0
- package/skills/deictic/references/tasks.md +79 -0
- package/skills/deictic/references/watch.md +43 -0
- package/skills/deictic/scripts/frame.mjs +62 -0
- package/skills/deictic/scripts/lib.mjs +211 -0
- package/skills/deictic/scripts/recording.mjs +186 -0
- package/skills/deictic/scripts/task.mjs +84 -0
- package/skills/deictic/scripts/transcribe.mjs +93 -0
- package/skills/deictic/scripts/voice.mjs +50 -0
- package/skills/deictic/scripts/watch.mjs +51 -0
- package/src/astro.ts +34 -0
- package/src/bun.ts +79 -0
- package/src/client/index.ts +846 -0
- package/src/client/lightbox.ts +256 -0
- package/src/client/style.ts +243 -0
- package/src/client/tooltip.ts +78 -0
- package/src/client/waveform.ts +98 -0
- package/src/index.ts +247 -0
- package/src/loader.ts +28 -0
- package/src/next.ts +133 -0
- package/src/plugins/annotations/client.ts +301 -0
- package/src/plugins/annotations/draw.ts +169 -0
- package/src/plugins/annotations/index.ts +105 -0
- package/src/plugins/annotations/svg.ts +61 -0
- package/src/plugins/recommended/index.ts +35 -0
- package/src/plugins/recordings/client.ts +316 -0
- package/src/plugins/recordings/index.ts +175 -0
- package/src/plugins/recordings/marks.ts +11 -0
- package/src/plugins/recordings/ours.ts +70 -0
- package/src/plugins/recordings/replay.ts +165 -0
- package/src/plugins/screenshots/client.ts +64 -0
- package/src/plugins/screenshots/dom.ts +79 -0
- package/src/plugins/screenshots/index.ts +194 -0
- package/src/plugins/screenshots/playwright.ts +119 -0
- package/src/plugins/screenshots/rrweb.ts +45 -0
- package/src/plugins/tailwind/catalog.ts +396 -0
- package/src/plugins/tailwind/client.ts +669 -0
- package/src/plugins/tailwind/index.ts +267 -0
- package/src/plugins/tasks/client.ts +622 -0
- package/src/plugins/tasks/index.ts +557 -0
- package/src/plugins/tasks/local.ts +170 -0
- package/src/plugins/voice/client.ts +58 -0
- package/src/plugins/voice/index.ts +114 -0
- package/src/shortcut.ts +83 -0
- package/src/tools/astro.ts +81 -0
- package/src/tools/editor.ts +22 -0
- package/src/tools/endpoints.ts +163 -0
- package/src/tools/history.ts +203 -0
- package/src/tools/integration.ts +108 -0
- package/src/tools/kept.ts +21 -0
- package/src/tools/locate.ts +94 -0
- package/src/tools/plugins.ts +41 -0
- package/src/tools/source.ts +72 -0
- package/src/vite.ts +81 -0
- package/src/webpack.ts +107 -0
- package/index.js +0 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dev server's half of deictic, under `/__deictic/`: `read` and `apply` an op through a
|
|
3
|
+
* plugin, `undo`, `redo` and `history`, and `open`, which opens a place in the editor. Written over
|
|
4
|
+
* `Request` and `Response`, so each integration mounts them its own way (Vite as middleware), and
|
|
5
|
+
* served from the page's own origin, so no token travels to the page. `undo`, `redo` and `history` are a
|
|
6
|
+
* plugin's on an element where the request names them (as the popup does: its tab, its element), and
|
|
7
|
+
* the whole history's where it doesn't.
|
|
8
|
+
*
|
|
9
|
+
* Everything but `open` and `history` writes or may, so only the page itself may send it: a POST of
|
|
10
|
+
* JSON from the same origin, which a form or a script on another site can't send without asking first.
|
|
11
|
+
*
|
|
12
|
+
* Each plugin's own `routes` are under `plugins/<name>/`. A `GET` there reaches the plugin as it
|
|
13
|
+
* comes; anything else only from the page's own origin, in whatever type it sends (an image, say).
|
|
14
|
+
* Nothing is answered before every plugin's `setup` has run.
|
|
15
|
+
*/
|
|
16
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
17
|
+
|
|
18
|
+
import { type Loc, Refusal, type ServerPlugin, type Target } from '#src/index.ts';
|
|
19
|
+
|
|
20
|
+
import { History, type Scope } from './history.ts';
|
|
21
|
+
import { setUp } from './plugins.ts';
|
|
22
|
+
import { applyEdits, resolveFile, sourceAt } from './source.ts';
|
|
23
|
+
|
|
24
|
+
export interface EndpointOptions {
|
|
25
|
+
root: string;
|
|
26
|
+
plugins: ServerPlugin[];
|
|
27
|
+
/** The path the endpoints sit under, ending in `/`: `/` by default. */
|
|
28
|
+
base?: string;
|
|
29
|
+
/** Opens a place in the editor: a redirect to the dev server's own launcher. */
|
|
30
|
+
open: (path: string, loc: Loc) => Response | Promise<Response>;
|
|
31
|
+
history?: History;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Body {
|
|
35
|
+
plugin: string;
|
|
36
|
+
target: Target;
|
|
37
|
+
op?: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const scopeOf = (plugin: string, { file, line, column }: Loc): Scope => ({ plugin, target: `${file}:${line}:${column}` });
|
|
41
|
+
/** The scope an undo or a redo names, or none for the whole history. */
|
|
42
|
+
const scopeIn = ({ plugin, target }: Partial<Body>) =>
|
|
43
|
+
plugin === undefined || target === undefined ? undefined : scopeOf(plugin, target.loc);
|
|
44
|
+
|
|
45
|
+
/** Answers deictic's requests, and nothing for any other path. */
|
|
46
|
+
export function endpoints({ root, plugins, base = '/', open, history = new History(root, { plugins }) }: EndpointOptions) {
|
|
47
|
+
const byName = new Map(plugins.map((p) => [p.name, p]));
|
|
48
|
+
const prefix = `${base}__deictic/`;
|
|
49
|
+
// Started now, awaited by every request: a plugin's API is there before anything asks for it.
|
|
50
|
+
const ready = setUp(root, plugins);
|
|
51
|
+
|
|
52
|
+
const pluginOf = (name: string) => {
|
|
53
|
+
const plugin = byName.get(name);
|
|
54
|
+
if (plugin === undefined) throw new Refusal(`no plugin "${name}" on the server: is it in the bundler plugin's \`plugins\`?`);
|
|
55
|
+
return plugin;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const routes: Record<string, (body: Body) => Promise<unknown>> = {
|
|
59
|
+
read: async ({ plugin, target }) => ({ model: await readOf(pluginOf(plugin), target) }),
|
|
60
|
+
apply: async ({ plugin: name, target, op }) => {
|
|
61
|
+
const plugin = pluginOf(name);
|
|
62
|
+
if (plugin.apply === undefined) throw new Refusal(`${name} changes nothing from its panel: it has no \`apply\``);
|
|
63
|
+
const source = sourceAt(root, target);
|
|
64
|
+
const before = await readOf(plugin, target);
|
|
65
|
+
const { file, line } = target.loc;
|
|
66
|
+
const label = `${name} ${file}:${line}: ${plugin.describe?.(op, before) ?? 'edit'}`;
|
|
67
|
+
const result = await plugin.apply(source, target, op);
|
|
68
|
+
const scope = scopeOf(name, target.loc);
|
|
69
|
+
const change = Array.isArray(result)
|
|
70
|
+
? history.commit(label, applyEdits(source, result), scope)
|
|
71
|
+
: history.record(label, name, result.done, scope);
|
|
72
|
+
return { model: await readOf(plugin, target), change };
|
|
73
|
+
},
|
|
74
|
+
undo: async (body) => ({ change: await history.undo(scopeIn(body)) }),
|
|
75
|
+
redo: async (body) => ({ change: await history.redo(scopeIn(body)) }),
|
|
76
|
+
};
|
|
77
|
+
// A plugin without `read` has no model, and no need for its element to be where the page says.
|
|
78
|
+
const readOf = (plugin: ServerPlugin, target: Target) => (plugin.read === undefined ? null : plugin.read(sourceAt(root, target), target));
|
|
79
|
+
// One write at a time: a plugin's change may wait on the network, and an undo sent meanwhile must
|
|
80
|
+
// take back that change, not the one before it.
|
|
81
|
+
let queue: Promise<unknown> = Promise.resolve();
|
|
82
|
+
const serially = <T>(run: () => Promise<T>): Promise<T> => {
|
|
83
|
+
const next = queue.then(run, run);
|
|
84
|
+
queue = next.catch(() => {});
|
|
85
|
+
return next;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return async (req: Request): Promise<Response | undefined> => {
|
|
89
|
+
const url = new URL(req.url);
|
|
90
|
+
if (!url.pathname.startsWith(prefix)) return undefined;
|
|
91
|
+
const route = url.pathname.slice(prefix.length);
|
|
92
|
+
const origin = req.headers.get('origin');
|
|
93
|
+
const sameOrigin = origin !== null && new URL(origin).host === url.host;
|
|
94
|
+
try {
|
|
95
|
+
const context = await ready;
|
|
96
|
+
if (route.startsWith('plugins/')) {
|
|
97
|
+
const [name = '', first = ''] = route.slice('plugins/'.length).split('/');
|
|
98
|
+
const handle = byName.get(name)?.routes?.[first];
|
|
99
|
+
if (handle === undefined) return new Response(`no endpoint ${url.pathname}`, { status: 404 });
|
|
100
|
+
if (req.method !== 'GET' && req.method !== 'HEAD' && !sameOrigin)
|
|
101
|
+
return Response.json({ problem: `only the page itself may ${req.method} to ${name}` }, { status: 403 });
|
|
102
|
+
return await handle(req, context);
|
|
103
|
+
}
|
|
104
|
+
const loc = {
|
|
105
|
+
file: url.searchParams.get('file') ?? '',
|
|
106
|
+
line: Number(url.searchParams.get('line')),
|
|
107
|
+
column: Number(url.searchParams.get('column')),
|
|
108
|
+
};
|
|
109
|
+
if (route === 'history') {
|
|
110
|
+
const plugin = url.searchParams.get('plugin');
|
|
111
|
+
return Response.json(history.list(plugin === null ? undefined : scopeOf(plugin, loc)));
|
|
112
|
+
}
|
|
113
|
+
if (route === 'open') return await open(resolveFile(root, loc.file), loc);
|
|
114
|
+
const handler = routes[route];
|
|
115
|
+
if (handler === undefined) return new Response(`no endpoint ${url.pathname}`, { status: 404 });
|
|
116
|
+
if (req.method !== 'POST' || !sameOrigin || req.headers.get('content-type')?.startsWith('application/json') !== true)
|
|
117
|
+
return Response.json({ problem: 'edits come from the page, as JSON' }, { status: 403 });
|
|
118
|
+
const body = (await req.json()) as Body;
|
|
119
|
+
return Response.json(await (route === 'read' ? handler(body) : serially(() => handler(body))));
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (e instanceof Refusal) return Response.json({ problem: e.message }, { status: 422 });
|
|
122
|
+
return Response.json({ problem: e instanceof Error ? (e.stack ?? e.message) : String(e) }, { status: 500 });
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A redirect within the page's origin, to a launcher the dev server answers itself. */
|
|
128
|
+
export const redirect = (path: string) => new Response(null, { status: 307, headers: { Location: path } });
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The endpoints as Node middleware, for Vite's server. The request's host is the one the page was
|
|
132
|
+
* served from: a proxy's `x-forwarded-host` before its own.
|
|
133
|
+
*/
|
|
134
|
+
export function nodeMiddleware(
|
|
135
|
+
handle: (req: Request) => Promise<Response | undefined>,
|
|
136
|
+
): (req: IncomingMessage, res: ServerResponse, next: () => void) => void {
|
|
137
|
+
return (req, res, next) => {
|
|
138
|
+
// Before reading the body, which a request for anything else leaves to whoever answers it.
|
|
139
|
+
if (req.url?.includes('/__deictic/') !== true) return next();
|
|
140
|
+
const forwarded = req.headers['x-forwarded-host'];
|
|
141
|
+
const host = (Array.isArray(forwarded) ? forwarded[0] : forwarded) ?? req.headers.host ?? 'localhost';
|
|
142
|
+
const headers = new Headers();
|
|
143
|
+
for (const [name, value] of Object.entries(req.headers))
|
|
144
|
+
for (const v of Array.isArray(value) ? value : value === undefined ? [] : [value]) headers.append(name, v);
|
|
145
|
+
|
|
146
|
+
const chunks: Buffer[] = [];
|
|
147
|
+
req.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
148
|
+
req.on('end', () => {
|
|
149
|
+
const body = req.method === 'GET' || req.method === 'HEAD' ? undefined : Buffer.concat(chunks);
|
|
150
|
+
handle(new Request(`http://${host}${req.url}`, { method: req.method, headers, body }))
|
|
151
|
+
.then(async (response) => {
|
|
152
|
+
if (response === undefined) return next();
|
|
153
|
+
res.statusCode = response.status;
|
|
154
|
+
response.headers.forEach((value, name) => res.setHeader(name, value));
|
|
155
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
156
|
+
})
|
|
157
|
+
.catch((e: unknown) => {
|
|
158
|
+
res.statusCode = 500;
|
|
159
|
+
res.end(e instanceof Error ? e.message : String(e));
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every change deictic made, to undo and redo. A change to files holds each file's whole text
|
|
3
|
+
* before and after: source files are small, and the texts make undo exact without a diff to go stale.
|
|
4
|
+
* A change a plugin made itself (`Done`) holds the plugin's name and its record, and undoing it is the
|
|
5
|
+
* plugin's `undo`.
|
|
6
|
+
*
|
|
7
|
+
* Each change has a scope, the plugin and the element it was made on, and the popup undoes and redoes
|
|
8
|
+
* within its own: the latest change of this tab on this element, not the latest of all. So a change
|
|
9
|
+
* is taken back out of order, with later ones to the same file still in it. Then only the lines the
|
|
10
|
+
* change wrote go back to what they were, where they're still in the file as it wrote them, once.
|
|
11
|
+
*
|
|
12
|
+
* Anything else is refused, not merged: a file whose lines were edited since has the editor's own undo
|
|
13
|
+
* for it; and a redo likewise. A plugin's undo refuses on its own terms. Kept under `.deictic`, so it
|
|
14
|
+
* outlives a restart of the dev server and stays out of git.
|
|
15
|
+
*/
|
|
16
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
17
|
+
import { dirname } from 'node:path';
|
|
18
|
+
|
|
19
|
+
import { Refusal, type ServerPlugin } from '#src/index.ts';
|
|
20
|
+
|
|
21
|
+
import { keptIn } from './kept.ts';
|
|
22
|
+
import { resolveFile } from './source.ts';
|
|
23
|
+
|
|
24
|
+
export interface FileChange {
|
|
25
|
+
file: string;
|
|
26
|
+
/** The text before, or nothing for a file the change made. */
|
|
27
|
+
before: string | null;
|
|
28
|
+
after: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Whose a change is: the plugin whose tab made it, and the element it was open on, as `file:line:column`. */
|
|
32
|
+
export interface Scope {
|
|
33
|
+
plugin: string;
|
|
34
|
+
target: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Base {
|
|
38
|
+
id: number;
|
|
39
|
+
at: string;
|
|
40
|
+
label: string;
|
|
41
|
+
/** None on a change made without one, which only an undo of the whole history reaches. */
|
|
42
|
+
scope?: Scope;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type Change = Base & ({ files: FileChange[] } | { plugin: string; record: unknown });
|
|
46
|
+
|
|
47
|
+
/** A change as the page lists it. `files` is empty for one a plugin made itself. */
|
|
48
|
+
export interface Summary {
|
|
49
|
+
id: number;
|
|
50
|
+
at: string;
|
|
51
|
+
label: string;
|
|
52
|
+
files: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface HistoryOptions {
|
|
56
|
+
/** Whose `undo` and `redo` take back the changes they made themselves. */
|
|
57
|
+
plugins?: ServerPlugin[];
|
|
58
|
+
store?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const LIMIT = 200;
|
|
62
|
+
|
|
63
|
+
/** Whether a change is in a scope: any is in none. */
|
|
64
|
+
const within = (scope: Scope | undefined) => (c: Change) =>
|
|
65
|
+
scope === undefined || (c.scope?.plugin === scope.plugin && c.scope.target === scope.target);
|
|
66
|
+
|
|
67
|
+
const summary = (c: Change): Summary => ({ id: c.id, at: c.at, label: c.label, files: 'files' in c ? c.files.map((f) => f.file) : [] });
|
|
68
|
+
|
|
69
|
+
export class History {
|
|
70
|
+
#done: Change[] = [];
|
|
71
|
+
#undone: Change[] = [];
|
|
72
|
+
readonly #root: string;
|
|
73
|
+
readonly #store: string;
|
|
74
|
+
readonly #plugins: Map<string, ServerPlugin>;
|
|
75
|
+
|
|
76
|
+
constructor(root: string, { plugins = [], store = keptIn(root, 'history.json') }: HistoryOptions = {}) {
|
|
77
|
+
this.#root = root;
|
|
78
|
+
this.#store = store;
|
|
79
|
+
this.#plugins = new Map(plugins.map((p) => [p.name, p]));
|
|
80
|
+
if (!existsSync(store)) return;
|
|
81
|
+
try {
|
|
82
|
+
({ done: this.#done, undone: this.#undone } = JSON.parse(readFileSync(store, 'utf8')) as { done: Change[]; undone: Change[] });
|
|
83
|
+
} catch {
|
|
84
|
+
// A history that doesn't read is one we start over, not one to stop the dev server for.
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Writes each file's new text and records it as one change. A redo after it, in its scope, is gone, as in any editor. */
|
|
89
|
+
commit(label: string, texts: Map<string, string>, scope?: Scope): Summary {
|
|
90
|
+
const files: FileChange[] = [];
|
|
91
|
+
for (const [file, after] of texts) {
|
|
92
|
+
const path = resolveFile(this.#root, file);
|
|
93
|
+
const before = existsSync(path) ? readFileSync(path, 'utf8') : null;
|
|
94
|
+
if (before !== after) files.push({ file, before, after });
|
|
95
|
+
}
|
|
96
|
+
if (files.length === 0) throw new Refusal('nothing to change: the source already says that');
|
|
97
|
+
for (const f of files) write(resolveFile(this.#root, f.file), f.after);
|
|
98
|
+
return this.#push({ label, files }, scope);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Records a change `plugin` made itself, with what its `undo` needs. */
|
|
102
|
+
record(label: string, plugin: string, record: unknown, scope?: Scope): Summary {
|
|
103
|
+
return this.#push({ label, plugin, record }, scope);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Takes back the latest change, of a scope where one is given. */
|
|
107
|
+
async undo(scope?: Scope): Promise<Summary> {
|
|
108
|
+
const change = this.#done.findLast(within(scope));
|
|
109
|
+
if (change === undefined) throw new Refusal(scope === undefined ? 'nothing to undo' : 'nothing to undo here');
|
|
110
|
+
if ('files' in change) this.#put(change, 'after', 'before');
|
|
111
|
+
else change.record = (await this.#pluginOf(change, 'undo').undo!(change.record)) ?? change.record;
|
|
112
|
+
this.#done = this.#done.filter((c) => c !== change);
|
|
113
|
+
this.#undone.push(change);
|
|
114
|
+
this.#save();
|
|
115
|
+
return summary(change);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Makes the change last undone again, of a scope where one is given. */
|
|
119
|
+
async redo(scope?: Scope): Promise<Summary> {
|
|
120
|
+
const change = this.#undone.findLast(within(scope));
|
|
121
|
+
if (change === undefined) throw new Refusal(scope === undefined ? 'nothing to redo' : 'nothing to redo here');
|
|
122
|
+
if ('files' in change) this.#put(change, 'before', 'after');
|
|
123
|
+
else change.record = await this.#pluginOf(change, 'redo').redo!(change.record);
|
|
124
|
+
this.#undone = this.#undone.filter((c) => c !== change);
|
|
125
|
+
this.#done.push(change);
|
|
126
|
+
this.#save();
|
|
127
|
+
return summary(change);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
list(scope?: Scope): { done: Summary[]; undone: Summary[] } {
|
|
131
|
+
return { done: this.#done.filter(within(scope)).map(summary), undone: this.#undone.filter(within(scope)).map(summary) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
#push(change: Omit<Base, 'id' | 'at'> & ({ files: FileChange[] } | { plugin: string; record: unknown }), scope?: Scope): Summary {
|
|
135
|
+
const id = Math.max(0, ...[...this.#done, ...this.#undone].map((c) => c.id)) + 1;
|
|
136
|
+
const made = { id, at: new Date().toISOString(), ...change, ...(scope === undefined ? {} : { scope }) };
|
|
137
|
+
this.#done = [...this.#done, made].slice(-LIMIT);
|
|
138
|
+
// What was undone in this scope can't be redone over the new change; another scope's still can.
|
|
139
|
+
this.#undone = this.#undone.filter((c) => !within(scope)(c));
|
|
140
|
+
this.#save();
|
|
141
|
+
return summary(made);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#pluginOf(change: { label: string; plugin: string }, step: 'undo' | 'redo'): ServerPlugin {
|
|
145
|
+
const plugin = this.#plugins.get(change.plugin);
|
|
146
|
+
if (plugin?.[step] === undefined) {
|
|
147
|
+
throw new Refusal(
|
|
148
|
+
`can't ${step} "${change.label}": the ${change.plugin} plugin that made it isn't loaded, so add it back to \`plugins\``,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return plugin;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Puts each file's `to` text back, once every one of them still reads as its `from`, or still has the
|
|
156
|
+
* lines that differ as `from` has them: then those lines alone change, and the rest stays as it is now.
|
|
157
|
+
*/
|
|
158
|
+
#put(change: Base & { files: FileChange[] }, from: 'before' | 'after', to: 'before' | 'after'): void {
|
|
159
|
+
const paths = change.files.map((f) => resolveFile(this.#root, f.file));
|
|
160
|
+
const texts = change.files.map((f, i) => {
|
|
161
|
+
const now = existsSync(paths[i]!) ? readFileSync(paths[i]!, 'utf8') : null;
|
|
162
|
+
if (now === f[from]) return f[to];
|
|
163
|
+
const text = now === null || f[from] === null || f[to] === null ? undefined : swapped(now, f[from], f[to]);
|
|
164
|
+
if (text === undefined) {
|
|
165
|
+
throw new Refusal(
|
|
166
|
+
`${f.file} changed since "${change.label}" ${from === 'after' ? 'was made' : 'was undone'}: use the editor's undo there`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return text;
|
|
170
|
+
});
|
|
171
|
+
texts.forEach((text, i) => {
|
|
172
|
+
if (text === null) rmSync(paths[i]!);
|
|
173
|
+
else write(paths[i]!, text);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#save(): void {
|
|
178
|
+
write(this.#store, JSON.stringify({ done: this.#done, undone: this.#undone }));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* `now` with the lines that differ between `from` and `to` put as `to` has them, where `now` has them as
|
|
184
|
+
* `from` does, in one place only. Nothing where it doesn't (they were edited, or there are two of them), or
|
|
185
|
+
* where `from` has no such lines to find (the change only removed some).
|
|
186
|
+
*/
|
|
187
|
+
function swapped(now: string, from: string, to: string): string | undefined {
|
|
188
|
+
const [a, b, lines] = [from.split('\n'), to.split('\n'), now.split('\n')];
|
|
189
|
+
let head = 0;
|
|
190
|
+
while (head < a.length && head < b.length && a[head] === b[head]) head++;
|
|
191
|
+
let tail = 0;
|
|
192
|
+
while (tail < a.length - head && tail < b.length - head && a.at(-1 - tail) === b.at(-1 - tail)) tail++;
|
|
193
|
+
const theirs = a.slice(head, a.length - tail);
|
|
194
|
+
if (theirs.length === 0) return undefined;
|
|
195
|
+
const found = lines.flatMap((_, i) => (theirs.every((line, j) => lines[i + j] === line) ? [i] : []));
|
|
196
|
+
if (found.length !== 1) return undefined;
|
|
197
|
+
return [...lines.slice(0, found[0]), ...b.slice(head, b.length - tail), ...lines.slice(found[0]! + theirs.length)].join('\n');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function write(path: string, text: string): void {
|
|
201
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
202
|
+
writeFileSync(path, text);
|
|
203
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every bundler's integration shares: the options, the client module that starts the overlay, and
|
|
3
|
+
* the transform that marks a JSX module. Each integration adds what its bundler does its own way: how a
|
|
4
|
+
* module is transformed (a hook, a loader, `onLoad`), how the page loads the client (a virtual module
|
|
5
|
+
* under Vite, a file in `.deictic/` elsewhere, as webpack, Turbopack and Bun have none), where the
|
|
6
|
+
* endpoints are served, and what opens the editor.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { dirname, isAbsolute, relative } from 'node:path';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
|
|
12
|
+
import type { ServerPlugin } from '#src/index.ts';
|
|
13
|
+
import { checkShortcuts, OWN, ownShortcut, type Shortcut } from '#src/shortcut.ts';
|
|
14
|
+
|
|
15
|
+
import { ASTRO, locateAstro } from './astro.ts';
|
|
16
|
+
import { keptIn } from './kept.ts';
|
|
17
|
+
import { JSX, locate, type Located } from './locate.ts';
|
|
18
|
+
import { checkPlugins } from './plugins.ts';
|
|
19
|
+
|
|
20
|
+
export interface DeicticOptions {
|
|
21
|
+
/** The plugins to load, server halves: `tailwind()`, `tasks()`, your own. None by default. */
|
|
22
|
+
plugins?: ServerPlugin[];
|
|
23
|
+
/**
|
|
24
|
+
* What opens the popup on any plugin, at the tab used last: Alt and the left button by default, or
|
|
25
|
+
* `false` for none, leaving only plugins' own shortcuts.
|
|
26
|
+
*/
|
|
27
|
+
shortcut?: Shortcut | false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The options with their defaults, checked now, so a wrong set stops the dev server as it starts, not the page. */
|
|
31
|
+
export function prepare(options: DeicticOptions): { plugins: ServerPlugin[]; shortcut: Shortcut | false | undefined } {
|
|
32
|
+
const plugins = options.plugins ?? [];
|
|
33
|
+
checkPlugins(plugins, options.shortcut);
|
|
34
|
+
return { plugins, shortcut: options.shortcut };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The client module: the overlay, started with deictic's own shortcut and each plugin that has a half
|
|
39
|
+
* in the page, with its shortcut, checked; and with `server`, a module of the page's that answers its requests
|
|
40
|
+
* where no dev server does.
|
|
41
|
+
*/
|
|
42
|
+
export const clientModule = (plugins: ServerPlugin[], base: string, own?: Shortcut | false, server?: string) => {
|
|
43
|
+
const [shortcut, ...shortcuts] = checkShortcuts([{ name: OWN, shortcut: ownShortcut(own) }, ...plugins]);
|
|
44
|
+
const paged = plugins.flatMap((p, i) =>
|
|
45
|
+
p.client === undefined ? [] : [{ client: p.client, rest: { shortcut: shortcuts[i], options: p.clientOptions } }],
|
|
46
|
+
);
|
|
47
|
+
return [
|
|
48
|
+
`import { start } from 'deictic/client';`,
|
|
49
|
+
...paged.map((p, i) => `import p${i} from ${JSON.stringify(p.client)};`),
|
|
50
|
+
...(server === undefined ? [] : [`import server from ${JSON.stringify(server)};`]),
|
|
51
|
+
// Its shortcut and options as JSON, where it has them: `JSON.stringify` leaves out what's undefined.
|
|
52
|
+
`start({ base: ${JSON.stringify(base)}, shortcut: ${JSON.stringify(shortcut) ?? 'undefined'}, plugins: [${paged.map((p, i) => `{ plugin: p${i}, ...${JSON.stringify(p.rest)} }`).join(', ')}]${server === undefined ? '' : ', server'} });`,
|
|
53
|
+
].join('\n');
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Writes the client module to `.deictic/client.js` for a bundler without virtual modules, and answers
|
|
58
|
+
* its path. Its imports are bare (`deictic/client`, each plugin's), found from the project as its own
|
|
59
|
+
* are. Written only when it changed, so a watching bundler doesn't rebuild for nothing.
|
|
60
|
+
*/
|
|
61
|
+
export function writeClient(root: string, plugins: ServerPlugin[], base: string, own?: Shortcut | false): string {
|
|
62
|
+
const code = `// Written by deictic as the dev server starts: it starts the overlay in the page.\n${clientModule(plugins, base, own)}\n`;
|
|
63
|
+
// Its own package: an ES module that runs for its effect. An app whose package.json says
|
|
64
|
+
// `"sideEffects": false` would otherwise have webpack drop the import, which is only for its effect.
|
|
65
|
+
const manifest = `${JSON.stringify({ private: true, type: 'module', sideEffects: true }, null, 2)}\n`;
|
|
66
|
+
writeIfChanged(keptIn(root, 'package.json'), manifest);
|
|
67
|
+
return writeIfChanged(keptIn(root, 'client.js'), code);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function writeIfChanged(path: string, text: string): string {
|
|
71
|
+
if (!existsSync(path) || readFileSync(path, 'utf8') !== text) writeFileSync(path, text);
|
|
72
|
+
return path;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A module beside `url`, by its name: `.ts` from the source, `.js` from `dist`, as `url` is. */
|
|
76
|
+
export const beside = (url: string, name: string) =>
|
|
77
|
+
fileURLToPath(new URL(`./${name}${url.endsWith('.ts') ? '.ts' : '.js'}`, url)).replaceAll('\\', '/');
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The import of the written client module in the module at `from`, by a relative path with forward
|
|
81
|
+
* slashes: Turbopack can't import a Windows path, and every bundler takes a relative one.
|
|
82
|
+
*/
|
|
83
|
+
export function importOf(path: string, from: string): string {
|
|
84
|
+
const to = relative(dirname(from), path).replaceAll('\\', '/');
|
|
85
|
+
return `\nimport ${JSON.stringify(to.startsWith('.') ? to : `./${to}`)};\n`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A module marked, by its absolute path: its host elements, and `append` after it (the client's import,
|
|
90
|
+
* in the browser). Nothing for a file that isn't JSX, is a dependency's, or is outside the project.
|
|
91
|
+
*/
|
|
92
|
+
export function mark(code: string, path: string, root: string, append = ''): Located | undefined {
|
|
93
|
+
const file = JSX.test(path) ? fileIn(path, root) : undefined;
|
|
94
|
+
return file === undefined ? undefined : locate(code, file, append);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** An `.astro` component marked, by its absolute path, as `mark` does a module. It's no module of the browser's, so nothing is appended. */
|
|
98
|
+
export function markAstro(code: string, path: string, root: string): Located | undefined {
|
|
99
|
+
const file = ASTRO.test(path) ? fileIn(path, root) : undefined;
|
|
100
|
+
return file === undefined ? undefined : locateAstro(code, file, root);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A project file's path from the root, as a mark has it; nothing for a dependency's or one outside the project. */
|
|
104
|
+
function fileIn(path: string, root: string): string | undefined {
|
|
105
|
+
if (/[\\/]node_modules[\\/]/.test(path) || !isAbsolute(path)) return undefined;
|
|
106
|
+
const file = relative(root, path).replaceAll('\\', '/');
|
|
107
|
+
return file.startsWith('..') || isAbsolute(file) ? undefined : file;
|
|
108
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where deictic keeps what it makes for a project: `.deictic/` at its root, beside `.board/` and the like,
|
|
3
|
+
* rather than in `node_modules`, which an install may clear. The history is there, the shots,
|
|
4
|
+
* recordings and voice clips plugins keep, and the tasks plugin's own board. It ignores itself in git: the history holds whole files, and
|
|
5
|
+
* the rest is the page as it looked, neither of which belongs in a commit.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
|
|
10
|
+
const IGNORE = "# deictic's own: the history, and the shots, recordings and voice clips it keeps.\n*\n";
|
|
11
|
+
|
|
12
|
+
/** A path under the project's `.deictic/`, made with its `.gitignore` the first time. */
|
|
13
|
+
export function keptIn(root: string, ...parts: string[]): string {
|
|
14
|
+
const dir = join(root, '.deictic');
|
|
15
|
+
const ignore = join(dir, '.gitignore');
|
|
16
|
+
if (!existsSync(ignore)) {
|
|
17
|
+
mkdirSync(dir, { recursive: true });
|
|
18
|
+
writeFileSync(ignore, IGNORE);
|
|
19
|
+
}
|
|
20
|
+
return join(dir, ...parts);
|
|
21
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks each host element in a JSX module with where its opening tag starts:
|
|
3
|
+
* `<div data-deictic="src/App.tsx:12:5">`. The page reads it back to know what was clicked.
|
|
4
|
+
*
|
|
5
|
+
* At build time and not from React's fibers at runtime: it works for any framework that compiles JSX,
|
|
6
|
+
* for server components (which have no fiber in the browser), and it names the element's own tag
|
|
7
|
+
* rather than the component that rendered it. Only host elements (`div`, `button`) are marked, since a
|
|
8
|
+
* component may not pass the attribute on; a component's call site is reached from its elements.
|
|
9
|
+
*
|
|
10
|
+
* The transform must run on the source as written (`enforce: 'pre'` under Vite), so lines and columns
|
|
11
|
+
* are the file's. It runs alike on the server and in the browser, so a hydrated page matches its HTML.
|
|
12
|
+
*/
|
|
13
|
+
import MagicString from 'magic-string';
|
|
14
|
+
import { type JSXOpeningElement, parseSync, type Program, Visitor } from 'oxc-parser';
|
|
15
|
+
|
|
16
|
+
export const ATTRIBUTE = 'data-deictic';
|
|
17
|
+
/** Modules that may hold JSX: `.jsx` and `.tsx`, and plain `.js`, as many React projects write theirs. */
|
|
18
|
+
export const JSX = /\.[cm]?(?:[jt]sx|js)$/;
|
|
19
|
+
|
|
20
|
+
/** A module parsed, with JSX read in a `.js` file too, which the parser reads there only when told. */
|
|
21
|
+
export const parse = (file: string, code: string) => parseSync(file, code, /\.[cm]?js$/.test(file) ? { lang: 'jsx' } : undefined);
|
|
22
|
+
|
|
23
|
+
export interface Located {
|
|
24
|
+
code: string;
|
|
25
|
+
map: ReturnType<MagicString['generateMap']>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Whether an opening tag is a host element's, `<div>` or `<svg>`, not a component's or a namespaced one. */
|
|
29
|
+
export const isHost = (el: JSXOpeningElement) => el.name.type === 'JSXIdentifier' && /^[a-z]/.test(el.name.name);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `code` with every host element marked, `file` being its path from the project's root, and `append`
|
|
33
|
+
* after it (the client's import, in the browser); nothing when there is nothing to mark, or the module
|
|
34
|
+
* doesn't parse, which the framework's own transform reports better.
|
|
35
|
+
*/
|
|
36
|
+
export function locate(code: string, file: string, append = ''): Located | undefined {
|
|
37
|
+
// No tag, no parse: most `.js` modules have none, and every one of the project's comes through here.
|
|
38
|
+
if (!/<[a-z]/.test(code)) return undefined;
|
|
39
|
+
const { program, errors } = parse(file, code);
|
|
40
|
+
if (errors.length > 0) return undefined;
|
|
41
|
+
return marked(code, file, openingsIn(program), append);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Every opening tag in a module, in its order. */
|
|
45
|
+
export function openingsIn(program: Program): JSXOpeningElement[] {
|
|
46
|
+
const elements: JSXOpeningElement[] = [];
|
|
47
|
+
new Visitor({ JSXOpeningElement: (el) => void elements.push(el) }).visit(program);
|
|
48
|
+
return elements;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* `code` with a mark in each of `elements` that is a host element's and has none yet, and `append` after
|
|
53
|
+
* it; nothing with none to mark.
|
|
54
|
+
*/
|
|
55
|
+
export function marked(code: string, file: string, elements: JSXOpeningElement[], append = ''): Located | undefined {
|
|
56
|
+
const lines = lineStarts(code);
|
|
57
|
+
const s = new MagicString(code);
|
|
58
|
+
for (const el of elements) {
|
|
59
|
+
if (!isHost(el) || el.attributes.some((a) => a.type === 'JSXAttribute' && nameOf(a.name) === ATTRIBUTE)) continue;
|
|
60
|
+
const { line, column } = position(lines, el.start);
|
|
61
|
+
s.appendLeft(el.name.end, ` ${ATTRIBUTE}="${file}:${line}:${column}"`);
|
|
62
|
+
}
|
|
63
|
+
if (!s.hasChanged()) return undefined;
|
|
64
|
+
if (append !== '') s.append(append);
|
|
65
|
+
// A map's source is read from the module's own folder, so it's the file's name, not its path from the root.
|
|
66
|
+
return { code: s.toString(), map: s.generateMap({ source: file.slice(file.lastIndexOf('/') + 1), hires: 'boundary' }) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const nameOf = (name: { type: string; name?: unknown }) => (name.type === 'JSXIdentifier' ? name.name : undefined);
|
|
70
|
+
|
|
71
|
+
/** Where each line starts, by offset. */
|
|
72
|
+
export function lineStarts(code: string): number[] {
|
|
73
|
+
const starts = [0];
|
|
74
|
+
for (let i = code.indexOf('\n'); i !== -1; i = code.indexOf('\n', i + 1)) starts.push(i + 1);
|
|
75
|
+
return starts;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** An offset's line and column, from 1. */
|
|
79
|
+
export function position(lines: number[], offset: number): { line: number; column: number } {
|
|
80
|
+
let lo = 0;
|
|
81
|
+
let hi = lines.length - 1;
|
|
82
|
+
while (lo < hi) {
|
|
83
|
+
const mid = (lo + hi + 1) >> 1;
|
|
84
|
+
if (lines[mid]! <= offset) lo = mid;
|
|
85
|
+
else hi = mid - 1;
|
|
86
|
+
}
|
|
87
|
+
return { line: lo + 1, column: offset - lines[lo]! + 1 };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A line and column's offset, or nothing past the file's end. */
|
|
91
|
+
export function offsetOf(lines: number[], line: number, column: number): number | undefined {
|
|
92
|
+
const start = lines[line - 1];
|
|
93
|
+
return start === undefined ? undefined : start + column - 1;
|
|
94
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugins an integration was given: checked as it starts, so a wrong set fails the dev server with
|
|
3
|
+
* what to write instead, and set up once before the endpoints answer anything.
|
|
4
|
+
*/
|
|
5
|
+
import type { ServerContext, ServerPlugin } from '#src/index.ts';
|
|
6
|
+
import { checkShortcuts, OWN, ownShortcut, type Shortcut } from '#src/shortcut.ts';
|
|
7
|
+
|
|
8
|
+
const NAME = /^[a-z][a-z0-9-]*$/;
|
|
9
|
+
|
|
10
|
+
/** Throws what to fix in a set of plugins: names, what they need, and their shortcuts, beside deictic's own. */
|
|
11
|
+
export function checkPlugins(plugins: readonly ServerPlugin[], own?: Shortcut | false): void {
|
|
12
|
+
const names = new Set<string>();
|
|
13
|
+
for (const { name, client, shortcut } of plugins) {
|
|
14
|
+
if (!NAME.test(name)) throw new Error(`deictic: a plugin is named "${name}": name it in lowercase letters, digits and -`);
|
|
15
|
+
if (names.has(name)) throw new Error(`deictic: two plugins are named ${name}: load each once`);
|
|
16
|
+
if (shortcut !== undefined && client === undefined)
|
|
17
|
+
throw new Error(`deictic: ${name} has a shortcut but no \`client\` to draw its panel: give it one, or leave out its shortcut`);
|
|
18
|
+
names.add(name);
|
|
19
|
+
}
|
|
20
|
+
for (const { name, needs = [] } of plugins) {
|
|
21
|
+
const missing = needs.find((other) => !names.has(other));
|
|
22
|
+
if (missing !== undefined) throw new Error(`deictic: ${name} needs the ${missing} plugin: add it to \`plugins\``);
|
|
23
|
+
}
|
|
24
|
+
checkShortcuts([{ name: OWN, shortcut: ownShortcut(own) }, ...plugins]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Runs every plugin's `setup`, all at once, and answers the context they share: `use` reads an API
|
|
29
|
+
* when it's asked for, so setups may `use` each other's only after they've all run, from a route or an
|
|
30
|
+
* op, never in `setup` itself.
|
|
31
|
+
*/
|
|
32
|
+
export async function setUp(root: string, plugins: readonly ServerPlugin[]): Promise<ServerContext> {
|
|
33
|
+
const apis = new Map<string, unknown>();
|
|
34
|
+
const context: ServerContext = { root, use: (name) => apis.get(name) as never };
|
|
35
|
+
await Promise.all(
|
|
36
|
+
plugins.map(async (p) => {
|
|
37
|
+
if (p.setup !== undefined) apis.set(p.name, await p.setup(context));
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
return context;
|
|
41
|
+
}
|