@risinghq/lienzo 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/README.md +82 -2
- package/dist/shell/assets/index-Cna8wlzF.js +4 -0
- package/dist/shell/assets/index-CoJSASvW.css +2 -0
- package/dist/shell/index.html +13 -0
- package/package.json +76 -4
- package/src/bench/bench.ts +132 -0
- package/src/bridge/page.ts +443 -0
- package/src/bridge/protocol.ts +124 -0
- package/src/bridge/record.ts +75 -0
- package/src/bridge/script.ts +24 -0
- package/src/bridge/state.ts +143 -0
- package/src/capture/capture.ts +125 -0
- package/src/capture/compare.ts +46 -0
- package/src/capture/diff.ts +16 -0
- package/src/capture/manifest.ts +60 -0
- package/src/capture/refs.ts +20 -0
- package/src/capture/replay.ts +25 -0
- package/src/capture/review.ts +78 -0
- package/src/capture/serve.ts +32 -0
- package/src/capture/snap.ts +62 -0
- package/src/capture/store.ts +28 -0
- package/src/cli.ts +287 -0
- package/src/cloud/checkout.ts +33 -0
- package/src/cloud/credentials.ts +25 -0
- package/src/cloud/link.ts +60 -0
- package/src/cloud/login.ts +85 -0
- package/src/commands/canvas.ts +46 -0
- package/src/commands/change.ts +218 -0
- package/src/commands/class.ts +66 -0
- package/src/commands/command.ts +27 -0
- package/src/commands/create.ts +95 -0
- package/src/commands/element.ts +118 -0
- package/src/commands/file.ts +96 -0
- package/src/commands/frame.ts +58 -0
- package/src/commands/override.ts +38 -0
- package/src/commands/prop.ts +18 -0
- package/src/commands/registry.ts +76 -0
- package/src/commands/rule.ts +131 -0
- package/src/commands/stage.ts +52 -0
- package/src/commands/story.ts +112 -0
- package/src/commands/style.ts +123 -0
- package/src/commands/text.ts +17 -0
- package/src/commands/token.ts +60 -0
- package/src/commands/transaction.ts +66 -0
- package/src/job/docker.ts +109 -0
- package/src/job/job.ts +125 -0
- package/src/job/server.ts +62 -0
- package/src/mirror/apply.ts +121 -0
- package/src/mirror/entry.ts +7 -0
- package/src/mirror/host.ts +68 -0
- package/src/mirror/observe.ts +271 -0
- package/src/mirror/script.ts +18 -0
- package/src/mirror/transport.ts +7 -0
- package/src/mirror/wire.ts +25 -0
- package/src/project/config.ts +98 -0
- package/src/project/design.ts +59 -0
- package/src/project/detect.ts +184 -0
- package/src/project/files.ts +30 -0
- package/src/project/git.ts +68 -0
- package/src/project/github.ts +42 -0
- package/src/project/history.ts +89 -0
- package/src/project/project.ts +106 -0
- package/src/project/spawn.ts +23 -0
- package/src/project/step.ts +15 -0
- package/src/protocol.ts +125 -0
- package/src/sandbox/forward.ts +73 -0
- package/src/sandbox/install.ts +44 -0
- package/src/sandbox/sandbox.ts +57 -0
- package/src/serve/build.ts +49 -0
- package/src/serve/canvases.ts +65 -0
- package/src/serve/dev.ts +296 -0
- package/src/serve/env.ts +16 -0
- package/src/serve/mcp.ts +65 -0
- package/src/serve/middleware.ts +172 -0
- package/src/serve/next/config.ts +68 -0
- package/src/serve/next/hook.ts +74 -0
- package/src/serve/next/loader.ts +37 -0
- package/src/serve/nuxt.ts +14 -0
- package/src/serve/plugin.ts +95 -0
- package/src/serve/preload.ts +167 -0
- package/src/serve/resend.ts +29 -0
- package/src/serve/running.ts +115 -0
- package/src/serve/screenshot.ts +26 -0
- package/src/serve/sources.ts +46 -0
- package/src/serve/uncached.ts +25 -0
- package/src/serve/vite-build.ts +15 -0
- package/src/serve/vite.ts +19 -0
- package/src/session/session.ts +96 -0
- package/src/share/ask.ts +24 -0
- package/src/share/client.ts +140 -0
- package/src/share/consent.ts +39 -0
- package/src/share/frame.ts +26 -0
- package/src/source/astro/astro.ts +10 -0
- package/src/source/astro/elements.ts +60 -0
- package/src/source/blocks.ts +42 -0
- package/src/source/css.ts +48 -0
- package/src/source/estree.ts +55 -0
- package/src/source/extract.ts +68 -0
- package/src/source/insert.ts +189 -0
- package/src/source/jsx/elements.ts +136 -0
- package/src/source/jsx/jsx.ts +5 -0
- package/src/source/language.ts +176 -0
- package/src/source/location.ts +34 -0
- package/src/source/modules.ts +41 -0
- package/src/source/overrides.ts +35 -0
- package/src/source/page.ts +69 -0
- package/src/source/patch.ts +79 -0
- package/src/source/prop.ts +34 -0
- package/src/source/refusal.ts +7 -0
- package/src/source/rules.ts +298 -0
- package/src/source/stamp.ts +28 -0
- package/src/source/story-file.ts +40 -0
- package/src/source/story.ts +162 -0
- package/src/source/structure.ts +253 -0
- package/src/source/style.ts +143 -0
- package/src/source/svelte/elements.ts +154 -0
- package/src/source/svelte/svelte.ts +11 -0
- package/src/source/tag.ts +99 -0
- package/src/source/tailwind.ts +46 -0
- package/src/source/tokens.ts +56 -0
- package/src/source/vue/elements.ts +236 -0
- package/src/source/vue/vue.ts +11 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The git calls lienzo makes, run in the project's root. A project in a
|
|
2
|
+
// monorepo is a folder of the repository, so paths stay relative to the root.
|
|
3
|
+
import { spawnIn, type Run } from "./spawn.ts";
|
|
4
|
+
|
|
5
|
+
type Made = { ok: true; commit: string } | { ok: false; reason: string };
|
|
6
|
+
type Done = { ok: true } | { ok: false; reason: string };
|
|
7
|
+
|
|
8
|
+
function git(root: string, args: string[]): Promise<Run> {
|
|
9
|
+
return spawnIn(root, ["git", ...args]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function done(run: Run, failed: string): Done {
|
|
13
|
+
return run.ok ? { ok: true } : { ok: false, reason: run.err || failed };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Whether the root is in a repository that has a commit to compare with.
|
|
17
|
+
export async function hasHead(root: string): Promise<boolean> {
|
|
18
|
+
return (await git(root, ["rev-parse", "--verify", "--quiet", "HEAD"])).ok;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// The file's text in the last commit, or undefined when it has none there.
|
|
22
|
+
export async function headText(root: string, file: string): Promise<string | undefined> {
|
|
23
|
+
const shown = await git(root, ["show", `HEAD:./${file}`]);
|
|
24
|
+
|
|
25
|
+
return shown.ok ? shown.out : undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The file's changes since the last commit, as `git diff` writes them.
|
|
29
|
+
export async function diffOf(root: string, file: string): Promise<string> {
|
|
30
|
+
return (await git(root, ["diff", "--no-color", "--no-ext-diff", "HEAD", "--", file])).out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Commits the files as they stand, and only them, whatever else is staged.
|
|
34
|
+
// Returns the new commit's hash, or the reason git gave.
|
|
35
|
+
export async function commitFiles(root: string, files: string[], message: string): Promise<Made> {
|
|
36
|
+
const made = await git(root, ["commit", "--only", "--quiet", "-m", message, "--", ...files]);
|
|
37
|
+
if (!made.ok) return { ok: false, reason: made.err || "git commit failed" };
|
|
38
|
+
|
|
39
|
+
const head = await git(root, ["rev-parse", "--short", "HEAD"]);
|
|
40
|
+
|
|
41
|
+
return { ok: true, commit: head.out.trim() };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// The checked-out branch, or undefined when HEAD is detached.
|
|
45
|
+
export async function currentBranch(root: string): Promise<string | undefined> {
|
|
46
|
+
const named = await git(root, ["symbolic-ref", "--quiet", "--short", "HEAD"]);
|
|
47
|
+
|
|
48
|
+
return named.ok ? named.out.trim() : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function hasBranch(root: string, name: string): Promise<boolean> {
|
|
52
|
+
return (await git(root, ["rev-parse", "--verify", "--quiet", `refs/heads/${name}`])).ok;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function hasRemote(root: string, name: string): Promise<boolean> {
|
|
56
|
+
return (await git(root, ["remote", "get-url", name])).ok;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Starts a branch at HEAD and checks it out. The working tree stays as it is.
|
|
60
|
+
export async function startBranch(root: string, name: string): Promise<Done> {
|
|
61
|
+
return done(await git(root, ["switch", "--quiet", "-c", name]), "git switch failed");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function pushBranch(root: string, remote: string, name: string): Promise<Done> {
|
|
65
|
+
const pushed = await git(root, ["push", "--quiet", "--set-upstream", remote, name]);
|
|
66
|
+
|
|
67
|
+
return done(pushed, "git push failed");
|
|
68
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// The GitHub calls lienzo makes, through the GitHub CLI in the project's root.
|
|
2
|
+
// gh acts as the person logged in to it, so a pull request is theirs.
|
|
3
|
+
import { spawnIn, type Run } from "./spawn.ts";
|
|
4
|
+
|
|
5
|
+
type Found = { ok: true; value: string } | { ok: false; reason: string };
|
|
6
|
+
|
|
7
|
+
export type Pull = { base: string; head: string; title: string; body: string };
|
|
8
|
+
|
|
9
|
+
async function gh(root: string, args: string[], failed: string): Promise<Found> {
|
|
10
|
+
const run: Run = await spawnIn(root, ["gh", ...args]);
|
|
11
|
+
if (!run.ok) return { ok: false, reason: run.err || failed };
|
|
12
|
+
|
|
13
|
+
return { ok: true, value: run.out.trim() };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The repository's default branch on GitHub. lienzo asks before it commits or
|
|
17
|
+
// pushes, so gh missing or logged out stops it with nothing done.
|
|
18
|
+
export function defaultBranch(root: string): Promise<Found> {
|
|
19
|
+
const view = ["repo", "view", "--json", "defaultBranchRef", "--jq", ".defaultBranchRef.name"];
|
|
20
|
+
|
|
21
|
+
return gh(root, view, "gh repo view failed");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// The URL of the open pull request from the branch, or "" when there is none.
|
|
25
|
+
export function openPull(root: string, branch: string): Promise<Found> {
|
|
26
|
+
const list = ["pr", "list", "--head", branch, "--state", "open", "--json", "url"];
|
|
27
|
+
|
|
28
|
+
return gh(root, [...list, "--jq", ".[0].url // empty"], "gh pr list failed");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Opens a pull request and returns its URL, the last line gh prints.
|
|
32
|
+
export async function createPull(root: string, pull: Pull): Promise<Found> {
|
|
33
|
+
const create = ["pr", "create", "--base", pull.base, "--head", pull.head];
|
|
34
|
+
const made = await gh(
|
|
35
|
+
root,
|
|
36
|
+
[...create, "--title", pull.title, "--body", pull.body],
|
|
37
|
+
"gh pr create failed",
|
|
38
|
+
);
|
|
39
|
+
if (!made.ok) return made;
|
|
40
|
+
|
|
41
|
+
return { ok: true, value: made.value.split("\n").at(-1)! };
|
|
42
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { Placed, Result } from "../protocol.ts";
|
|
3
|
+
import type { Actor, Change, Project, Session, Written } from "./project.ts";
|
|
4
|
+
|
|
5
|
+
type Made = { change: Change; by: Actor };
|
|
6
|
+
|
|
7
|
+
// Undo and redo for one dev server. It refuses to step over a file that
|
|
8
|
+
// changed since, because someone else's edit would be lost. It also keeps
|
|
9
|
+
// what it wrote to each file, which is the session a commit takes. `written`
|
|
10
|
+
// hears the absolute path of each file once it is on disk.
|
|
11
|
+
export function history(project: Project, written: (file: string) => void = () => {}) {
|
|
12
|
+
const done: Made[] = [];
|
|
13
|
+
const undone: Made[] = [];
|
|
14
|
+
const writes = new Map<string, Written>();
|
|
15
|
+
|
|
16
|
+
async function write(
|
|
17
|
+
change: Change,
|
|
18
|
+
expected: string,
|
|
19
|
+
wanted: string,
|
|
20
|
+
at: string | null | undefined,
|
|
21
|
+
): Promise<Result> {
|
|
22
|
+
const file = Bun.file(path.join(project.root, change.file));
|
|
23
|
+
|
|
24
|
+
if ((await file.text()) !== expected) {
|
|
25
|
+
done.length = 0;
|
|
26
|
+
undone.length = 0;
|
|
27
|
+
return { ok: false, reason: `${change.file} changed outside lienzo; history cleared` };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await Bun.write(file, wanted);
|
|
31
|
+
wrote(change.file, expected, wanted);
|
|
32
|
+
|
|
33
|
+
const placed: Placed | undefined = at === undefined ? undefined : { at };
|
|
34
|
+
|
|
35
|
+
return { ok: true, changed: [change.file], data: placed };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function wrote(file: string, before: string, after: string) {
|
|
39
|
+
const known = writes.get(file);
|
|
40
|
+
|
|
41
|
+
writes.set(file, { file, base: known?.base ?? before, wrote: after });
|
|
42
|
+
written(path.join(project.root, file));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const session: Session = {
|
|
46
|
+
written: () => [...writes.values()],
|
|
47
|
+
|
|
48
|
+
edits: () => done.map(({ change, by }) => ({ file: change.file, by })),
|
|
49
|
+
|
|
50
|
+
committed(files) {
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
const known = writes.get(file);
|
|
53
|
+
if (known) writes.set(file, { ...known, base: known.wrote });
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
session,
|
|
60
|
+
|
|
61
|
+
record(change: Change, by: Actor = "human") {
|
|
62
|
+
done.push({ change, by });
|
|
63
|
+
undone.length = 0;
|
|
64
|
+
wrote(change.file, change.before, change.after);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
async undo(): Promise<Result> {
|
|
68
|
+
const made = done.at(-1);
|
|
69
|
+
if (!made) return { ok: false, reason: "nothing to undo" };
|
|
70
|
+
|
|
71
|
+
const { change } = made;
|
|
72
|
+
const result = await write(change, change.after, change.before, change.at?.before);
|
|
73
|
+
if (result.ok) undone.push(done.pop()!);
|
|
74
|
+
|
|
75
|
+
return result;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
async redo(): Promise<Result> {
|
|
79
|
+
const made = undone.at(-1);
|
|
80
|
+
if (!made) return { ok: false, reason: "nothing to redo" };
|
|
81
|
+
|
|
82
|
+
const { change } = made;
|
|
83
|
+
const result = await write(change, change.before, change.after, change.at?.after);
|
|
84
|
+
if (result.ok) done.push(undone.pop()!);
|
|
85
|
+
|
|
86
|
+
return result;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { Question, Result } from "../protocol.ts";
|
|
3
|
+
import type { ClassAttribute } from "../source/language.ts";
|
|
4
|
+
|
|
5
|
+
// An edit a command made, kept so it can be undone. A structure edit keeps the
|
|
6
|
+
// element selected before it and the one selected after it, so undo and redo
|
|
7
|
+
// select them again.
|
|
8
|
+
export type Change = {
|
|
9
|
+
file: string;
|
|
10
|
+
before: string;
|
|
11
|
+
after: string;
|
|
12
|
+
at?: { before: string; after: string | null };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Who made an edit: a person at the canvas, or an agent through MCP.
|
|
16
|
+
export type Actor = "human" | "agent";
|
|
17
|
+
|
|
18
|
+
// One edit still in effect, oldest first.
|
|
19
|
+
export type Edit = { file: string; by: Actor };
|
|
20
|
+
|
|
21
|
+
// A file a dev server's commands wrote: its text before lienzo's first write
|
|
22
|
+
// since the last commit, and the text lienzo wrote last.
|
|
23
|
+
export type Written = { file: string; base: string; wrote: string };
|
|
24
|
+
|
|
25
|
+
// What one dev server wrote, which only it knows.
|
|
26
|
+
export type Session = {
|
|
27
|
+
written(): Written[];
|
|
28
|
+
|
|
29
|
+
// The edits not undone, and who made each.
|
|
30
|
+
edits(): Edit[];
|
|
31
|
+
|
|
32
|
+
// The files a commit took, whose text is now the base.
|
|
33
|
+
committed(files: string[]): void;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type Project = {
|
|
37
|
+
root: string;
|
|
38
|
+
name: string;
|
|
39
|
+
classAttribute: ClassAttribute;
|
|
40
|
+
|
|
41
|
+
// Whether class names are Tailwind utilities, which override each other.
|
|
42
|
+
tailwind: boolean;
|
|
43
|
+
|
|
44
|
+
// Called with each file a command writes.
|
|
45
|
+
record?: (change: Change) => void;
|
|
46
|
+
|
|
47
|
+
// Text a command wrote that is not on disk yet. A read sees it, a write
|
|
48
|
+
// replaces it. Present only inside `stage`.
|
|
49
|
+
staged?: Map<string, string>;
|
|
50
|
+
|
|
51
|
+
// Called with the absolute path of each file a command reads, so the canvas
|
|
52
|
+
// hears when it changes. Absent outside a dev server.
|
|
53
|
+
follow?: (file: string) => void;
|
|
54
|
+
|
|
55
|
+
// Asks the open canvas what only it knows. Absent outside a dev server.
|
|
56
|
+
ask?: (name: Question["name"]) => Promise<Result>;
|
|
57
|
+
|
|
58
|
+
// Absent outside a dev server.
|
|
59
|
+
session?: Session;
|
|
60
|
+
|
|
61
|
+
// The origin that served the request, ending in "/". Absent outside a dev
|
|
62
|
+
// server.
|
|
63
|
+
site?: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type Manifest = {
|
|
67
|
+
name?: unknown;
|
|
68
|
+
dependencies?: Record<string, string>;
|
|
69
|
+
devDependencies?: Record<string, string>;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
async function manifestOf(root: string): Promise<Manifest> {
|
|
73
|
+
const file = Bun.file(path.join(root, "package.json"));
|
|
74
|
+
|
|
75
|
+
return (await file.exists()) ? ((await file.json()) as Manifest) : {};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function needsOf(manifest: Manifest): Record<string, string> {
|
|
79
|
+
return { ...manifest.dependencies, ...manifest.devDependencies };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A workspace declares shared tools in a manifest above the project.
|
|
83
|
+
export async function declaredAbove(root: string, dependency: string): Promise<boolean> {
|
|
84
|
+
if (dependency in needsOf(await manifestOf(root))) return true;
|
|
85
|
+
|
|
86
|
+
const parent = path.dirname(root);
|
|
87
|
+
|
|
88
|
+
return parent === root ? false : declaredAbove(parent, dependency);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function loadProject(root: string, attribute?: ClassAttribute): Promise<Project> {
|
|
92
|
+
const absolute = path.resolve(root);
|
|
93
|
+
const manifest = await manifestOf(absolute);
|
|
94
|
+
const needs = needsOf(manifest);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
root: absolute,
|
|
98
|
+
name: typeof manifest.name === "string" ? manifest.name : path.basename(absolute),
|
|
99
|
+
// The attribute of JSX, since the other languages fix their own: Solid and
|
|
100
|
+
// Vue write `class`, React writes `className`.
|
|
101
|
+
classAttribute:
|
|
102
|
+
attribute ??
|
|
103
|
+
("solid-js" in needs || "vue" in needs || "nuxt" in needs ? "class" : "className"),
|
|
104
|
+
tailwind: await declaredAbove(absolute, "tailwindcss"),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Run = { ok: boolean; out: string; err: string };
|
|
2
|
+
|
|
3
|
+
// Runs a program in a folder and collects what it printed. A program that is
|
|
4
|
+
// not installed is a failed run whose `err` says so, not a throw. Bun looks
|
|
5
|
+
// the program up on the PATH it started with unless `env` is passed, so the
|
|
6
|
+
// environment is passed to follow later changes to it.
|
|
7
|
+
export async function spawnIn(cwd: string, argv: string[]): Promise<Run> {
|
|
8
|
+
let child: Bun.Subprocess<"ignore", "pipe", "pipe">;
|
|
9
|
+
try {
|
|
10
|
+
const io = { stdin: "ignore", stdout: "pipe", stderr: "pipe" } as const;
|
|
11
|
+
child = Bun.spawn(argv, { cwd, env: process.env, ...io });
|
|
12
|
+
} catch {
|
|
13
|
+
return { ok: false, out: "", err: `${argv[0]} is not installed` };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const [out, err, code] = await Promise.all([
|
|
17
|
+
new Response(child.stdout).text(),
|
|
18
|
+
new Response(child.stderr).text(),
|
|
19
|
+
child.exited,
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
return { ok: code === 0, out, err: err.trim() };
|
|
23
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
// One interaction, addressed by the stamp of the element it acts on
|
|
4
|
+
// (`file:line:column`, as `data-lz` holds it), so a flow survives restyling.
|
|
5
|
+
// A component rendered in a list stamps many elements alike, and `nth` picks
|
|
6
|
+
// one by its index among them in document order.
|
|
7
|
+
const target = { at: z.string().min(1), nth: z.number().int().min(0).optional() };
|
|
8
|
+
|
|
9
|
+
export const step = z.discriminatedUnion("do", [
|
|
10
|
+
z.strictObject({ do: z.enum(["click", "hover"]), ...target }),
|
|
11
|
+
z.strictObject({ do: z.literal("fill"), ...target, value: z.string() }),
|
|
12
|
+
z.strictObject({ do: z.literal("press"), ...target, key: z.string().min(1) }),
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
export type Step = z.infer<typeof step>;
|
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// What the canvas and the dev server say to each other. The canvas imports
|
|
2
|
+
// only this file from the server's side.
|
|
3
|
+
|
|
4
|
+
export const BASE = "/__lienzo/";
|
|
5
|
+
export const API = `${BASE}api/`;
|
|
6
|
+
export const ATTRIBUTE = "data-lz";
|
|
7
|
+
|
|
8
|
+
// The MCP endpoint of a dev server, for an agent that cannot run a process
|
|
9
|
+
// beside it.
|
|
10
|
+
export const MCP = `${BASE}mcp`;
|
|
11
|
+
|
|
12
|
+
// The request header that says who sends a command: "agent", or a person when
|
|
13
|
+
// absent. The edge sets it on requests it relays, from how the caller signed in.
|
|
14
|
+
export const ACTOR = "x-lienzo-actor";
|
|
15
|
+
|
|
16
|
+
// The script a framed page runs to answer the canvas.
|
|
17
|
+
export const BRIDGE = `${BASE}bridge.js`;
|
|
18
|
+
|
|
19
|
+
// The stylesheet a framed page loads: the project's `lienzo.overrides.css`.
|
|
20
|
+
export const OVERRIDES = `${BASE}overrides.css`;
|
|
21
|
+
|
|
22
|
+
// How a class edit reached the source: in the class list the source states,
|
|
23
|
+
// in a string argument of a class helper such as `cn(...)`, or as a rule in
|
|
24
|
+
// the project's overrides file.
|
|
25
|
+
export type Rung = "literal" | "helper" | "override";
|
|
26
|
+
|
|
27
|
+
// `changed` lists the files a command wrote, relative to the project root.
|
|
28
|
+
export type Result =
|
|
29
|
+
| { ok: true; changed: string[]; data?: unknown; rung?: Rung }
|
|
30
|
+
| { ok: false; reason: string };
|
|
31
|
+
|
|
32
|
+
// What an edit that moves the selection returns, and its undo and redo: the
|
|
33
|
+
// location of the element to select, or null to clear the selection.
|
|
34
|
+
export type Placed = { at: string | null };
|
|
35
|
+
|
|
36
|
+
// What `element.get` returns. null marks a class list or text the source
|
|
37
|
+
// computes, which no command can set.
|
|
38
|
+
export type ElementData = { tag: string; classes: string | null; text: string | null };
|
|
39
|
+
|
|
40
|
+
// One row of `token.list`. `file` is the stylesheet that declares the token.
|
|
41
|
+
export type TokenData = { name: string; value: string; file: string };
|
|
42
|
+
|
|
43
|
+
// What `rule.get` returns: the rule a CSS module class names. `file` is the
|
|
44
|
+
// stylesheet; `condition` is the at-rules around the rule, outermost first, as
|
|
45
|
+
// `@media (width < 40rem)`, or "" for none; the declarations keep their source
|
|
46
|
+
// order.
|
|
47
|
+
export type RuleData = {
|
|
48
|
+
file: string;
|
|
49
|
+
selector: string;
|
|
50
|
+
condition: string;
|
|
51
|
+
declarations: { property: string; value: string }[];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// One property an element's style sets. `variant` is the Tailwind variant it
|
|
55
|
+
// holds under, such as `md` or `md:hover`, or "" for every screen; `class` is
|
|
56
|
+
// the utility that sets it.
|
|
57
|
+
export type StyleDeclaration = {
|
|
58
|
+
property: string;
|
|
59
|
+
value: string;
|
|
60
|
+
variant: string;
|
|
61
|
+
class?: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// A theme breakpoint: the variant's name and the frame width, in px, where it
|
|
65
|
+
// starts to hold.
|
|
66
|
+
export type Breakpoint = { name: string; width: number };
|
|
67
|
+
|
|
68
|
+
// What `style.get` returns. Tailwind utilities set the style of an element
|
|
69
|
+
// with a class list the source states; a CSS module rule sets it otherwise.
|
|
70
|
+
export type StyleData =
|
|
71
|
+
| { source: "tailwind"; declarations: StyleDeclaration[]; breakpoints: Breakpoint[] }
|
|
72
|
+
| { source: "rule"; file: string; selector: string; declarations: StyleDeclaration[] };
|
|
73
|
+
|
|
74
|
+
// What the dev server asks the open canvas, on its event stream. The canvas
|
|
75
|
+
// posts its answer, a Result, with the id.
|
|
76
|
+
export type Question = { id: string; name: "selection" | "frames" };
|
|
77
|
+
|
|
78
|
+
// The event the dev server sends a canvas when a file a command read changes.
|
|
79
|
+
export const FILES_CHANGED = "files";
|
|
80
|
+
|
|
81
|
+
// What `selection.get` returns: the selected node, by its frame and its
|
|
82
|
+
// element's location, and how many nodes that element draws in the frame.
|
|
83
|
+
export type SelectionData = { frame: string; at: string; index: number; count: number };
|
|
84
|
+
|
|
85
|
+
// One row of `frame.list`: a page of the site, drawn at a device size, in px.
|
|
86
|
+
// `url` is the page on the origin the canvas serves it from.
|
|
87
|
+
export type FrameData = { id: string; path: string; url: string; width: number; height: number };
|
|
88
|
+
|
|
89
|
+
// One row of `story.list`: a story in the project's Storybook index. `file`
|
|
90
|
+
// holds its stories file, relative to the project root, and `export` the
|
|
91
|
+
// named export that declares it.
|
|
92
|
+
export type StoryData = { id: string; title: string; name: string; file: string; export: string };
|
|
93
|
+
|
|
94
|
+
// One row of `args.get`: an arg a story states in its own `args`. `value` is
|
|
95
|
+
// null when the source computes it, so `args.set` refuses it.
|
|
96
|
+
export type ArgData = { name: string; value: string | number | boolean | null };
|
|
97
|
+
|
|
98
|
+
// One row of `change.list`: a file lienzo wrote that differs from the last
|
|
99
|
+
// commit, and its diff. `foreign` says why the file holds changes lienzo did
|
|
100
|
+
// not make, which `change.commit` leaves to a person.
|
|
101
|
+
export type ChangeData = { file: string; diff: string; foreign: string | null };
|
|
102
|
+
|
|
103
|
+
// What `change.commit` returns: the new commit and the files it took.
|
|
104
|
+
export type CommitData = { commit: string; files: string[] };
|
|
105
|
+
|
|
106
|
+
// What `change.propose` returns: the branch it pushed, the commit it made, or
|
|
107
|
+
// null when lienzo had nothing new, and the pull request, which `opened` says
|
|
108
|
+
// it opened rather than found open.
|
|
109
|
+
export type ProposeData = {
|
|
110
|
+
branch: string;
|
|
111
|
+
commit: string | null;
|
|
112
|
+
files: string[];
|
|
113
|
+
pull: string;
|
|
114
|
+
opened: boolean;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// `client` is the script that reloads a page when its source changes, which
|
|
118
|
+
// the canvas adds to a page that lacks it, as Vite's client under the dev
|
|
119
|
+
// server's base. It is null for a framework whose pages always carry theirs.
|
|
120
|
+
export type ProjectInfo = {
|
|
121
|
+
name: string;
|
|
122
|
+
classAttribute: "className" | "class";
|
|
123
|
+
tailwind: boolean;
|
|
124
|
+
client: string | null;
|
|
125
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { lookup } from "node:dns/promises";
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
|
|
4
|
+
export type Forwarded = { port: number; close(): Promise<void> };
|
|
5
|
+
|
|
6
|
+
// Forwards each connection to the port on every interface to the target, as
|
|
7
|
+
// bytes, so HTTP and the HMR socket pass alike whatever the framework. The
|
|
8
|
+
// dev server keeps listening only where it chose to. Closing ends the open
|
|
9
|
+
// connections too, since a browser keeps them alive.
|
|
10
|
+
export async function forward(port: number, target: URL): Promise<Forwarded> {
|
|
11
|
+
const open = new Set<net.Socket>();
|
|
12
|
+
const server = net.createServer((client) => {
|
|
13
|
+
open.add(client);
|
|
14
|
+
client.pause();
|
|
15
|
+
const end = (upstream?: net.Socket) => {
|
|
16
|
+
client.destroy();
|
|
17
|
+
upstream?.destroy();
|
|
18
|
+
open.delete(client);
|
|
19
|
+
};
|
|
20
|
+
client.on("error", () => end()).on("close", () => end());
|
|
21
|
+
dial(target).then(
|
|
22
|
+
(upstream) => {
|
|
23
|
+
if (client.destroyed) return void upstream.destroy();
|
|
24
|
+
upstream.on("error", () => end(upstream)).on("close", () => end(upstream));
|
|
25
|
+
client.on("close", () => upstream.destroy());
|
|
26
|
+
client.pipe(upstream).pipe(client);
|
|
27
|
+
client.resume();
|
|
28
|
+
},
|
|
29
|
+
() => end(),
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await new Promise<void>((resolve, reject) => {
|
|
34
|
+
server.once("error", reject).listen(port, "0.0.0.0", resolve);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
port: (server.address() as net.AddressInfo).port,
|
|
39
|
+
close: () =>
|
|
40
|
+
new Promise((resolve) => {
|
|
41
|
+
server.close(() => resolve());
|
|
42
|
+
for (const client of open) client.destroy();
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Tries each address the target's host resolves to, in order. A dev server
|
|
48
|
+
// may listen on `::1` alone while `localhost` resolves to 127.0.0.1 first, and
|
|
49
|
+
// Bun's net.connect dials only the first address.
|
|
50
|
+
export async function dial(target: URL): Promise<net.Socket> {
|
|
51
|
+
const host = target.hostname.replace(/^\[(.*)\]$/, "$1");
|
|
52
|
+
const addresses = await lookup(host, { all: true });
|
|
53
|
+
let failure: unknown;
|
|
54
|
+
for (const { address } of addresses) {
|
|
55
|
+
try {
|
|
56
|
+
return await connect(Number(target.port), address);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
failure = error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
throw failure;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function connect(port: number, address: string): Promise<net.Socket> {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const socket = net.connect(port, address);
|
|
67
|
+
socket.once("connect", () => {
|
|
68
|
+
socket.off("error", reject);
|
|
69
|
+
resolve(socket);
|
|
70
|
+
});
|
|
71
|
+
socket.once("error", reject);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { CONFIG } from "../project/config.ts";
|
|
4
|
+
|
|
5
|
+
// What installs a project's dependencies, and the folder it runs in.
|
|
6
|
+
export type Install = { command: string; cwd: string };
|
|
7
|
+
|
|
8
|
+
// Each lockfile lienzo knows, with the command that installs exactly what it
|
|
9
|
+
// locks, or none when the right command depends on the tool's version.
|
|
10
|
+
const LOCKS: [string, string | undefined][] = [
|
|
11
|
+
["bun.lock", "bun install --frozen-lockfile"],
|
|
12
|
+
["bun.lockb", "bun install --frozen-lockfile"],
|
|
13
|
+
["package-lock.json", "npm ci"],
|
|
14
|
+
["pnpm-lock.yaml", undefined],
|
|
15
|
+
["yarn.lock", undefined],
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const exists = (file: string) => Bun.file(file).exists();
|
|
19
|
+
|
|
20
|
+
// The nearest folder at or above the root that holds a lockfile, up to the
|
|
21
|
+
// repository's top, where a workspace keeps its one lockfile.
|
|
22
|
+
async function lockOf(root: string): Promise<{ dir: string; lock: string } | undefined> {
|
|
23
|
+
for (let dir = root; ; dir = path.dirname(dir)) {
|
|
24
|
+
for (const [lock] of LOCKS) if (await exists(path.join(dir, lock))) return { dir, lock };
|
|
25
|
+
|
|
26
|
+
const top = existsSync(path.join(dir, ".git")) || path.dirname(dir) === dir;
|
|
27
|
+
if (top) return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The install for the project at the root: the configured command, or the
|
|
32
|
+
// one its lockfile picks, in the lockfile's folder.
|
|
33
|
+
export async function installOf(root: string, configured?: string): Promise<Install> {
|
|
34
|
+
const found = await lockOf(root);
|
|
35
|
+
if (!found) {
|
|
36
|
+
if (configured) return { command: configured, cwd: root };
|
|
37
|
+
throw new Error(`no lockfile at or above the project; set install in ${CONFIG}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const command = configured ?? LOCKS.find(([lock]) => lock === found.lock)![1];
|
|
41
|
+
if (!command) throw new Error(`${found.lock} has no default install; set install in ${CONFIG}`);
|
|
42
|
+
|
|
43
|
+
return { command, cwd: found.dir };
|
|
44
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { loadConfig } from "../project/config.ts";
|
|
2
|
+
import type { Project } from "../project/project.ts";
|
|
3
|
+
import { dev, type Running } from "../serve/dev.ts";
|
|
4
|
+
import { forward, type Forwarded } from "./forward.ts";
|
|
5
|
+
import { installOf } from "./install.ts";
|
|
6
|
+
|
|
7
|
+
export type Sandboxed = { running: Running; forwarded: Forwarded };
|
|
8
|
+
|
|
9
|
+
// The env names the project needs that the sandbox was not given.
|
|
10
|
+
export function missingEnv(names: string[], env = process.env): string[] {
|
|
11
|
+
return names.filter((name) => env[name] === undefined);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// The lines of a failed install's stderr a reason carries: a hosted session's
|
|
15
|
+
// stderr is a container log nobody reads, and the reason is what the person sees.
|
|
16
|
+
const REASON_LINES = 15;
|
|
17
|
+
|
|
18
|
+
async function install(root: string, configured?: string): Promise<void> {
|
|
19
|
+
const { command, cwd } = await installOf(root, configured);
|
|
20
|
+
const child = Bun.spawn(["sh", "-c", command], {
|
|
21
|
+
cwd,
|
|
22
|
+
env: process.env,
|
|
23
|
+
stdio: ["ignore", "inherit", "pipe"],
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
let stderr = "";
|
|
27
|
+
const decoder = new TextDecoder();
|
|
28
|
+
for await (const chunk of child.stderr) {
|
|
29
|
+
process.stderr.write(chunk);
|
|
30
|
+
stderr += decoder.decode(chunk, { stream: true });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const code = await child.exited;
|
|
34
|
+
if (code === 0) return;
|
|
35
|
+
|
|
36
|
+
const tail = stderr.trim().split("\n").slice(-REASON_LINES).join("\n");
|
|
37
|
+
throw new Error(`${command} exited with code ${code}${tail ? `:\n${tail}` : ""}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// What a hosted session runs in its container, in a clone of the project's
|
|
41
|
+
// repository: it installs the project, starts `dev`, and opens the dev server
|
|
42
|
+
// on the port. It refuses before it installs anything when an env name the
|
|
43
|
+
// project's config lists is not set.
|
|
44
|
+
export async function sandbox(project: Project, port: number): Promise<Sandboxed> {
|
|
45
|
+
const config = await loadConfig(project.root);
|
|
46
|
+
const missing = missingEnv(Object.keys(config.env));
|
|
47
|
+
if (missing.length > 0) throw new Error(`the sandbox lacks env ${missing.join(", ")}`);
|
|
48
|
+
|
|
49
|
+
await install(project.root, config.install);
|
|
50
|
+
const running = await dev(project);
|
|
51
|
+
try {
|
|
52
|
+
return { running, forwarded: await forward(port, new URL(running.site)) };
|
|
53
|
+
} catch (error) {
|
|
54
|
+
await running.close();
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|