@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,38 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { designOf } from "../project/design.ts";
|
|
3
|
+
import type { Project } from "../project/project.ts";
|
|
4
|
+
import type { Result } from "../protocol.ts";
|
|
5
|
+
import { tagAt } from "../source/language.ts";
|
|
6
|
+
import { OVERRIDES_FILE, patchOverrides } from "../source/overrides.ts";
|
|
7
|
+
import { refuse } from "../source/refusal.ts";
|
|
8
|
+
import { declarationsOf } from "../source/style.ts";
|
|
9
|
+
import { editFile, open } from "./file.ts";
|
|
10
|
+
|
|
11
|
+
export async function classIsComputed(project: Project, at: string): Promise<boolean> {
|
|
12
|
+
const { location, source } = await open(project, at);
|
|
13
|
+
const tag = tagAt(source, location);
|
|
14
|
+
|
|
15
|
+
return tag.ok && !tag.classes(project.classAttribute).ok;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function overrideClass(project: Project, at: string, added: string): Promise<Result> {
|
|
19
|
+
const { location } = await open(project, at);
|
|
20
|
+
const design = project.tailwind ? await designOf(project, location.file) : undefined;
|
|
21
|
+
if (!design) return refuse("an override needs Tailwind to turn the class into CSS");
|
|
22
|
+
|
|
23
|
+
const declarations = declarationsOf(design, added);
|
|
24
|
+
if (declarations.length === 0) return refuse(`Tailwind has no CSS for ${added}`);
|
|
25
|
+
if (declarations.some((declaration) => declaration.variant)) {
|
|
26
|
+
return refuse(`${added} holds a variant, which an override does not carry`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const file = path.join(project.root, OVERRIDES_FILE);
|
|
30
|
+
if (!(await Bun.file(file).exists())) await Bun.write(file, "");
|
|
31
|
+
|
|
32
|
+
const result = await editFile(project, OVERRIDES_FILE, (source) => ({
|
|
33
|
+
ok: true,
|
|
34
|
+
code: patchOverrides(source, at, declarations),
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
return result.ok ? { ...result, rung: "override" } : result;
|
|
38
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { patchProp } from "../source/prop.ts";
|
|
2
|
+
import { AT, need, type Command } from "./command.ts";
|
|
3
|
+
import { edit } from "./file.ts";
|
|
4
|
+
|
|
5
|
+
export const propSet: Command = {
|
|
6
|
+
name: "prop.set",
|
|
7
|
+
does: "Sets an attribute of an element, such as href or aria-label, to a string. Refuses an attribute the source computes.",
|
|
8
|
+
params: { at: AT, name: "the attribute name", value: "the new value, a plain string" },
|
|
9
|
+
|
|
10
|
+
run(project, params) {
|
|
11
|
+
const name = need(params, "name");
|
|
12
|
+
const value = need(params, "value");
|
|
13
|
+
|
|
14
|
+
return edit(project, need(params, "at"), (source, location) =>
|
|
15
|
+
patchProp(source, location, name, value),
|
|
16
|
+
);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Project } from "../project/project.ts";
|
|
2
|
+
import type { Result } from "../protocol.ts";
|
|
3
|
+
import { frameList, frameScreenshot, selectionGet } from "./canvas.ts";
|
|
4
|
+
import { frameAdd } from "./frame.ts";
|
|
5
|
+
import { changeCommit, changeList, changePropose, editsList } from "./change.ts";
|
|
6
|
+
import { classRemove, classSet, variantSet } from "./class.ts";
|
|
7
|
+
import type { Command, Params } from "./command.ts";
|
|
8
|
+
import { componentExtract, pageCreate, storyCreate } from "./create.ts";
|
|
9
|
+
import {
|
|
10
|
+
elementDuplicate,
|
|
11
|
+
elementGet,
|
|
12
|
+
elementInsert,
|
|
13
|
+
elementMove,
|
|
14
|
+
elementRemove,
|
|
15
|
+
elementWrap,
|
|
16
|
+
} from "./element.ts";
|
|
17
|
+
import { propSet } from "./prop.ts";
|
|
18
|
+
import { ruleFind, ruleGet, ruleSet } from "./rule.ts";
|
|
19
|
+
import type { Dispatch } from "./stage.ts";
|
|
20
|
+
import { touchesOf, transactionOf } from "./transaction.ts";
|
|
21
|
+
import { argsGet, argsSet, storyList } from "./story.ts";
|
|
22
|
+
import { styleGet, styleSet } from "./style.ts";
|
|
23
|
+
import { textSet } from "./text.ts";
|
|
24
|
+
import { tokenList, tokenSet } from "./token.ts";
|
|
25
|
+
|
|
26
|
+
const dispatch: Dispatch = (project, name, params) => run(project, name, params);
|
|
27
|
+
|
|
28
|
+
// Every command lienzo has. A new one is added here and nowhere else.
|
|
29
|
+
export const commands: Command[] = [
|
|
30
|
+
elementGet,
|
|
31
|
+
elementDuplicate,
|
|
32
|
+
elementMove,
|
|
33
|
+
elementWrap,
|
|
34
|
+
elementInsert,
|
|
35
|
+
elementRemove,
|
|
36
|
+
classSet,
|
|
37
|
+
classRemove,
|
|
38
|
+
variantSet,
|
|
39
|
+
pageCreate,
|
|
40
|
+
storyCreate,
|
|
41
|
+
componentExtract,
|
|
42
|
+
textSet,
|
|
43
|
+
propSet,
|
|
44
|
+
ruleGet,
|
|
45
|
+
ruleSet,
|
|
46
|
+
ruleFind,
|
|
47
|
+
styleGet,
|
|
48
|
+
styleSet,
|
|
49
|
+
tokenList,
|
|
50
|
+
tokenSet,
|
|
51
|
+
selectionGet,
|
|
52
|
+
frameList,
|
|
53
|
+
frameAdd,
|
|
54
|
+
frameScreenshot,
|
|
55
|
+
changeList,
|
|
56
|
+
editsList,
|
|
57
|
+
changeCommit,
|
|
58
|
+
changePropose,
|
|
59
|
+
storyList,
|
|
60
|
+
argsGet,
|
|
61
|
+
argsSet,
|
|
62
|
+
touchesOf(dispatch),
|
|
63
|
+
transactionOf(dispatch),
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
// Runs a command by name. Bad input is a refusal, like an edit with no span.
|
|
67
|
+
export async function run(project: Project, name: string, params: Params): Promise<Result> {
|
|
68
|
+
const command = commands.find((candidate) => candidate.name === name);
|
|
69
|
+
if (!command) return { ok: false, reason: `no command "${name}"` };
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
return await command.run(project, params);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return { ok: false, reason: error instanceof Error ? error.message : String(error) };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { Project } from "../project/project.ts";
|
|
3
|
+
import type { RuleData } from "../protocol.ts";
|
|
4
|
+
import { holdsCss } from "../source/blocks.ts";
|
|
5
|
+
import { moduleClass } from "../source/modules.ts";
|
|
6
|
+
import { refuse, type Refusal } from "../source/refusal.ts";
|
|
7
|
+
import { originOf, patchRule, ruleOf, selectorAt } from "../source/rules.ts";
|
|
8
|
+
import { AT, need, type Command, type Params } from "./command.ts";
|
|
9
|
+
import { editFile, open, read } from "./file.ts";
|
|
10
|
+
|
|
11
|
+
type Addressed = { ok: true; file: string; selector: string; condition: string } | Refusal;
|
|
12
|
+
|
|
13
|
+
// The rule a command names: by an element whose class is one CSS module class,
|
|
14
|
+
// or by its stylesheet and selector.
|
|
15
|
+
async function addressed(project: Project, params: Params): Promise<Addressed> {
|
|
16
|
+
if (params.at !== undefined) {
|
|
17
|
+
const { location, source } = await open(project, params.at);
|
|
18
|
+
|
|
19
|
+
const rule = moduleClass(source, location, project.classAttribute);
|
|
20
|
+
|
|
21
|
+
return rule.ok ? { ...rule, condition: "" } : rule;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (params.file === undefined || params.selector === undefined) {
|
|
25
|
+
return refuse("name the rule with at, or with file and selector");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const condition = params.condition ?? "";
|
|
29
|
+
|
|
30
|
+
return { ok: true, file: params.file, selector: params.selector, condition };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function dataOf(project: Project, file: string, selector: string, condition: string) {
|
|
34
|
+
const { source } = await read(project, file);
|
|
35
|
+
const rule = ruleOf(file, source, selector, condition);
|
|
36
|
+
if (!rule.ok) return rule;
|
|
37
|
+
|
|
38
|
+
const data: RuleData = { file, selector, condition, declarations: rule.declarations };
|
|
39
|
+
|
|
40
|
+
return { ok: true as const, changed: [], data };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ADDRESS = {
|
|
44
|
+
at: `${AT}, whose class is one CSS module class`,
|
|
45
|
+
file: "the stylesheet, or a Svelte or Vue component with style blocks, relative to the project root, when at is not given",
|
|
46
|
+
selector: "the rule's whole selector, with file",
|
|
47
|
+
condition:
|
|
48
|
+
"the at-rules around the rule, outermost first, such as @media (width < 40rem), with file; when not given, a rule outside any",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const ruleGet: Command = {
|
|
52
|
+
name: "rule.get",
|
|
53
|
+
does: "Reads a CSS rule, named by an element's CSS module class or by stylesheet and selector: its stylesheet, selector and declarations.",
|
|
54
|
+
params: {},
|
|
55
|
+
optional: ADDRESS,
|
|
56
|
+
|
|
57
|
+
async run(project, params) {
|
|
58
|
+
const found = await addressed(project, params);
|
|
59
|
+
if (!found.ok) return found;
|
|
60
|
+
|
|
61
|
+
return dataOf(project, found.file, found.selector, found.condition);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const ruleSet: Command = {
|
|
66
|
+
name: "rule.set",
|
|
67
|
+
does: "Sets a property in a CSS rule, named by an element's CSS module class or by stylesheet and selector, adding it when the rule lacks it.",
|
|
68
|
+
params: { property: "the property, for example padding", value: "the new value" },
|
|
69
|
+
optional: ADDRESS,
|
|
70
|
+
|
|
71
|
+
async run(project, params) {
|
|
72
|
+
const property = need(params, "property");
|
|
73
|
+
const value = need(params, "value");
|
|
74
|
+
|
|
75
|
+
const found = await addressed(project, params);
|
|
76
|
+
if (!found.ok) return found;
|
|
77
|
+
|
|
78
|
+
return editFile(project, found.file, (source) =>
|
|
79
|
+
patchRule(found.file, source, found.selector, property, value, found.condition),
|
|
80
|
+
);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// The project's file a source map names, relative to the root: a stylesheet,
|
|
85
|
+
// or a component whose style blocks hold the rule.
|
|
86
|
+
function stylesheetOf(project: Project, file: string): { ok: true; file: string } | Refusal {
|
|
87
|
+
const relative = path.relative(project.root, file).split(path.sep).join("/");
|
|
88
|
+
|
|
89
|
+
if (relative.startsWith("..") || relative.includes("node_modules/")) {
|
|
90
|
+
return refuse(`${file} is not a stylesheet of the project`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const css = holdsCss(relative);
|
|
94
|
+
return css.ok ? { ok: true, file: relative } : css;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const ruleFind: Command = {
|
|
98
|
+
name: "rule.find",
|
|
99
|
+
does: "Finds the source of a rule the browser holds, through the source map the dev server writes into the stylesheet, and reads it as rule.get does.",
|
|
100
|
+
params: {
|
|
101
|
+
css: "the stylesheet's text as the page holds it, with its inline source map",
|
|
102
|
+
index:
|
|
103
|
+
"the rule's index among the stylesheet's top-level rules, as cssRules counts them; for a rule in a group such as @media, the group's index, a dot and the rule's index among the group's rules, as 3.0",
|
|
104
|
+
selector: "the rule's selector as the browser states it",
|
|
105
|
+
},
|
|
106
|
+
optional: {
|
|
107
|
+
from: "the absolute path of the file the stylesheet was served from, which a relative source in its map is relative to",
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
async run(project, params) {
|
|
111
|
+
const index = need(params, "index");
|
|
112
|
+
if (!/^\d+(\.\d+)*$/.test(index)) return refuse(`${index} is not an index`);
|
|
113
|
+
|
|
114
|
+
const origin = originOf(
|
|
115
|
+
need(params, "css"),
|
|
116
|
+
index.split(".").map(Number),
|
|
117
|
+
need(params, "selector"),
|
|
118
|
+
params.from,
|
|
119
|
+
);
|
|
120
|
+
if (!origin.ok) return origin;
|
|
121
|
+
|
|
122
|
+
const sheet = stylesheetOf(project, origin.file);
|
|
123
|
+
if (!sheet.ok) return sheet;
|
|
124
|
+
|
|
125
|
+
const { source } = await read(project, sheet.file);
|
|
126
|
+
const found = selectorAt(sheet.file, source, origin.line, origin.column);
|
|
127
|
+
if (!found.ok) return found;
|
|
128
|
+
|
|
129
|
+
return dataOf(project, sheet.file, found.selector, found.condition);
|
|
130
|
+
},
|
|
131
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Change, Project } from "../project/project.ts";
|
|
4
|
+
import type { Result } from "../protocol.ts";
|
|
5
|
+
import type { Params } from "./command.ts";
|
|
6
|
+
|
|
7
|
+
export type Call = { name: string; params: Params };
|
|
8
|
+
export type Dispatch = (project: Project, name: string, params: Params) => Promise<Result>;
|
|
9
|
+
|
|
10
|
+
export type Staged = { ok: true; changes: Change[] } | { ok: false; reason: string };
|
|
11
|
+
|
|
12
|
+
// Runs calls in order against text held in memory. Each call sees what the one
|
|
13
|
+
// before wrote, and nothing reaches the disk. The changes it returns are the
|
|
14
|
+
// edit log of the calls: each file's text before and after, in order.
|
|
15
|
+
export async function stage(project: Project, calls: Call[], dispatch: Dispatch): Promise<Staged> {
|
|
16
|
+
const changes: Change[] = [];
|
|
17
|
+
const held: Project = {
|
|
18
|
+
...project,
|
|
19
|
+
staged: new Map(project.staged),
|
|
20
|
+
record: (change) => changes.push(change),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
for (const call of calls) {
|
|
24
|
+
const result = await dispatch(held, call.name, call.params);
|
|
25
|
+
if (!result.ok) return { ok: false, reason: `${call.name}: ${result.reason}` };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { ok: true, changes };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const digest = (text: string): string => createHash("sha256").update(text).digest("hex");
|
|
32
|
+
|
|
33
|
+
// Writes staged changes to the disk and into the project's history. A file
|
|
34
|
+
// that changed since the stage began is refused, so nothing is overwritten.
|
|
35
|
+
export async function commitStaged(project: Project, changes: Change[]): Promise<Result> {
|
|
36
|
+
const first = new Map<string, string>();
|
|
37
|
+
const last = new Map<string, string>();
|
|
38
|
+
for (const change of changes) {
|
|
39
|
+
if (!first.has(change.file)) first.set(change.file, change.before);
|
|
40
|
+
last.set(change.file, change.after);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const [file, before] of first) {
|
|
44
|
+
const now = await Bun.file(path.join(project.root, file)).text();
|
|
45
|
+
if (now !== before) return { ok: false, reason: `${file} changed while the edit was staged` };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (const [file, text] of last) await Bun.write(path.join(project.root, file), text);
|
|
49
|
+
for (const change of changes) project.record?.(change);
|
|
50
|
+
|
|
51
|
+
return { ok: true, changed: [...last.keys()] };
|
|
52
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { Result, StoryData } from "../protocol.ts";
|
|
2
|
+
import { patchArg, storyArgs, type Arg } from "../source/story.ts";
|
|
3
|
+
import { need, type Command } from "./command.ts";
|
|
4
|
+
import { editFile, read } from "./file.ts";
|
|
5
|
+
|
|
6
|
+
// The index Storybook serves at /index.json. Version 5 is the one Storybook 7
|
|
7
|
+
// to 10 write; another version may change these fields, so lienzo refuses it.
|
|
8
|
+
type Entry = {
|
|
9
|
+
type: string;
|
|
10
|
+
id: string;
|
|
11
|
+
title: string;
|
|
12
|
+
name: string;
|
|
13
|
+
importPath: string;
|
|
14
|
+
exportName: string;
|
|
15
|
+
};
|
|
16
|
+
type Index = { v: number; entries: Record<string, Entry> };
|
|
17
|
+
|
|
18
|
+
const VERSION = 5;
|
|
19
|
+
|
|
20
|
+
async function indexAt(site: string): Promise<Index | string> {
|
|
21
|
+
const response = await fetch(new URL("index.json", site));
|
|
22
|
+
// A single-page app's server answers an unknown path with its HTML page.
|
|
23
|
+
const json = response.ok && response.headers.get("content-type")?.includes("json");
|
|
24
|
+
const index = json ? ((await response.json()) as Partial<Index>) : undefined;
|
|
25
|
+
|
|
26
|
+
if (!index?.entries)
|
|
27
|
+
return "the dev server serves no Storybook index.json; is the dev script storybook dev?";
|
|
28
|
+
if (index.v !== VERSION)
|
|
29
|
+
return `Storybook index.json version ${index.v} is not ${VERSION}, the one lienzo reads`;
|
|
30
|
+
|
|
31
|
+
return index as Index;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function storyOf(entry: Entry): StoryData {
|
|
35
|
+
return {
|
|
36
|
+
id: entry.id,
|
|
37
|
+
title: entry.title,
|
|
38
|
+
name: entry.name,
|
|
39
|
+
file: entry.importPath.replace(/^\.\//, ""),
|
|
40
|
+
export: entry.exportName,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const storyList: Command = {
|
|
45
|
+
name: "story.list",
|
|
46
|
+
does: "Lists the stories in the Storybook index the running dev server serves, with each story's file and export.",
|
|
47
|
+
params: {},
|
|
48
|
+
async run(project): Promise<Result> {
|
|
49
|
+
if (!project.site)
|
|
50
|
+
return { ok: false, reason: "no dev server serves the project; run lienzo dev" };
|
|
51
|
+
|
|
52
|
+
const index = await indexAt(project.site);
|
|
53
|
+
if (typeof index === "string") return { ok: false, reason: index };
|
|
54
|
+
|
|
55
|
+
const stories = Object.values(index.entries).filter((entry) => entry.type === "story");
|
|
56
|
+
|
|
57
|
+
return { ok: true, changed: [], data: stories.map(storyOf) };
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const STORY = {
|
|
62
|
+
file: "the stories file, relative to the root, as story.list gives it",
|
|
63
|
+
export: "the export that declares the story, as story.list gives it",
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// A JSON string, number or boolean: the only args lienzo writes as literals.
|
|
67
|
+
function argOf(text: string): Arg {
|
|
68
|
+
let value: unknown;
|
|
69
|
+
try {
|
|
70
|
+
value = JSON.parse(text);
|
|
71
|
+
} catch {
|
|
72
|
+
throw new Error(`${text} is not JSON; a string arg needs its quotes`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!["string", "number", "boolean"].includes(typeof value)) {
|
|
76
|
+
throw new Error(`${text} is not a JSON string, number or boolean`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return value as Arg;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const argsGet: Command = {
|
|
83
|
+
name: "args.get",
|
|
84
|
+
does: "Reads the args a story states in its own args object. A computed arg's value is null.",
|
|
85
|
+
params: STORY,
|
|
86
|
+
async run(project, params): Promise<Result> {
|
|
87
|
+
const file = need(params, "file");
|
|
88
|
+
const { source } = await read(project, file);
|
|
89
|
+
const args = storyArgs(source, file, need(params, "export"));
|
|
90
|
+
if (!Array.isArray(args)) return args;
|
|
91
|
+
|
|
92
|
+
return { ok: true, changed: [], data: args };
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const argsSet: Command = {
|
|
97
|
+
name: "args.set",
|
|
98
|
+
does: "Sets one arg of a story to a literal in its args object, adding the arg when the story lacks it. Refuses a computed arg.",
|
|
99
|
+
params: {
|
|
100
|
+
...STORY,
|
|
101
|
+
arg: "the arg's name",
|
|
102
|
+
value: "the new value, as JSON: a string, number or boolean",
|
|
103
|
+
},
|
|
104
|
+
async run(project, params): Promise<Result> {
|
|
105
|
+
const file = need(params, "file");
|
|
106
|
+
const story = need(params, "export");
|
|
107
|
+
const arg = need(params, "arg");
|
|
108
|
+
const value = argOf(need(params, "value"));
|
|
109
|
+
|
|
110
|
+
return editFile(project, file, (source) => patchArg(source, file, story, arg, value));
|
|
111
|
+
},
|
|
112
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { designOf } from "../project/design.ts";
|
|
2
|
+
import type { Project } from "../project/project.ts";
|
|
3
|
+
import type { Result, StyleData } from "../protocol.ts";
|
|
4
|
+
import { tagAt } from "../source/language.ts";
|
|
5
|
+
import { moduleClass } from "../source/modules.ts";
|
|
6
|
+
import { refuse, type Refusal } from "../source/refusal.ts";
|
|
7
|
+
import { patchRule, ruleOf } from "../source/rules.ts";
|
|
8
|
+
import {
|
|
9
|
+
breakpointsOf,
|
|
10
|
+
classFor,
|
|
11
|
+
declarationsOf,
|
|
12
|
+
withoutProperty,
|
|
13
|
+
type DesignSystem,
|
|
14
|
+
} from "../source/style.ts";
|
|
15
|
+
import { withClass } from "../source/tailwind.ts";
|
|
16
|
+
import { editClasses } from "./class.ts";
|
|
17
|
+
import { AT, need, type Command } from "./command.ts";
|
|
18
|
+
import { editFile, open, read } from "./file.ts";
|
|
19
|
+
|
|
20
|
+
// What sets an element's style: the Tailwind utilities of a class list the
|
|
21
|
+
// source states, or the CSS module rule its class attribute names.
|
|
22
|
+
type Styled =
|
|
23
|
+
| { ok: true; source: "tailwind"; design: DesignSystem; list: string }
|
|
24
|
+
| { ok: true; source: "rule"; file: string; selector: string }
|
|
25
|
+
| Refusal;
|
|
26
|
+
|
|
27
|
+
async function styledBy(project: Project, at: string): Promise<Styled> {
|
|
28
|
+
const { location, source } = await open(project, at);
|
|
29
|
+
|
|
30
|
+
const tag = tagAt(source, location);
|
|
31
|
+
if (!tag.ok) return tag;
|
|
32
|
+
|
|
33
|
+
const classes = tag.classes(project.classAttribute);
|
|
34
|
+
const design =
|
|
35
|
+
project.tailwind && classes.ok ? await designOf(project, location.file) : undefined;
|
|
36
|
+
if (design && classes.ok) return { ok: true, source: "tailwind", design, list: classes.value };
|
|
37
|
+
|
|
38
|
+
const rule = moduleClass(source, location, project.classAttribute);
|
|
39
|
+
if (rule.ok) return { ...rule, source: "rule" };
|
|
40
|
+
|
|
41
|
+
const why = !classes.ok
|
|
42
|
+
? classes.reason
|
|
43
|
+
: project.tailwind
|
|
44
|
+
? "no stylesheet of the project imports tailwindcss"
|
|
45
|
+
: "the project does not declare tailwindcss";
|
|
46
|
+
|
|
47
|
+
return refuse(`${why}, and ${rule.reason}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const VARIANT = "a Tailwind variant such as md or md:hover; the style then holds only under it";
|
|
51
|
+
|
|
52
|
+
export const styleGet: Command = {
|
|
53
|
+
name: "style.get",
|
|
54
|
+
does: "Reads the CSS properties an element's Tailwind classes or CSS module rule set, with the variant each holds under.",
|
|
55
|
+
params: { at: AT },
|
|
56
|
+
|
|
57
|
+
async run(project, params) {
|
|
58
|
+
const styled = await styledBy(project, need(params, "at"));
|
|
59
|
+
if (!styled.ok) return styled;
|
|
60
|
+
|
|
61
|
+
if (styled.source === "tailwind") {
|
|
62
|
+
const data: StyleData = {
|
|
63
|
+
source: "tailwind",
|
|
64
|
+
declarations: declarationsOf(styled.design, styled.list),
|
|
65
|
+
breakpoints: breakpointsOf(styled.design),
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return { ok: true, changed: [], data };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const { source } = await read(project, styled.file);
|
|
72
|
+
const rule = ruleOf(styled.file, source, styled.selector);
|
|
73
|
+
if (!rule.ok) return rule;
|
|
74
|
+
|
|
75
|
+
const data: StyleData = {
|
|
76
|
+
source: "rule",
|
|
77
|
+
file: styled.file,
|
|
78
|
+
selector: styled.selector,
|
|
79
|
+
declarations: rule.declarations.map((declaration) => ({ ...declaration, variant: "" })),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return { ok: true, changed: [], data };
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const styleSet: Command = {
|
|
87
|
+
name: "style.set",
|
|
88
|
+
does: "Sets a CSS property of an element. With Tailwind it writes the utility the theme names for the value, replacing the classes it overrides; otherwise it writes the element's CSS module rule. An empty value removes the Tailwind classes that set the property.",
|
|
89
|
+
params: { at: AT, property: "the property, for example padding-top", value: "the new value" },
|
|
90
|
+
optional: { variant: VARIANT },
|
|
91
|
+
|
|
92
|
+
async run(project, params): Promise<Result> {
|
|
93
|
+
const at = need(params, "at");
|
|
94
|
+
const property = need(params, "property").trim();
|
|
95
|
+
const value = need(params, "value");
|
|
96
|
+
const variant = params.variant?.trim() ?? "";
|
|
97
|
+
|
|
98
|
+
const styled = await styledBy(project, at);
|
|
99
|
+
if (!styled.ok) return styled;
|
|
100
|
+
|
|
101
|
+
if (styled.source === "rule") {
|
|
102
|
+
if (variant) {
|
|
103
|
+
return refuse(`a variant needs Tailwind: ${styled.selector} holds for every screen`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return editFile(project, styled.file, (source) =>
|
|
107
|
+
patchRule(styled.file, source, styled.selector, property, value),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const { design } = styled;
|
|
112
|
+
if (!value.trim()) {
|
|
113
|
+
return editClasses(project, at, (list) => withoutProperty(design, list, property, variant));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const added = classFor(design, property, value, variant);
|
|
117
|
+
if (!added.ok) return added;
|
|
118
|
+
|
|
119
|
+
const result = await editClasses(project, at, (list) => withClass(list, added.name));
|
|
120
|
+
|
|
121
|
+
return result.ok ? { ...result, data: { class: added.name } } : result;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { patchText } from "../source/patch.ts";
|
|
2
|
+
import { AT, need, type Command } from "./command.ts";
|
|
3
|
+
import { edit } from "./file.ts";
|
|
4
|
+
|
|
5
|
+
export const textSet: Command = {
|
|
6
|
+
name: "text.set",
|
|
7
|
+
does: "Replaces the text of an element that holds only text.",
|
|
8
|
+
params: { at: AT, text: "the new text" },
|
|
9
|
+
|
|
10
|
+
run(project, params) {
|
|
11
|
+
const text = need(params, "text");
|
|
12
|
+
|
|
13
|
+
return edit(project, need(params, "at"), (source, location) =>
|
|
14
|
+
patchText(source, location, text),
|
|
15
|
+
);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { sourceFiles } from "../project/files.ts";
|
|
2
|
+
import type { Project } from "../project/project.ts";
|
|
3
|
+
import type { TokenData } from "../protocol.ts";
|
|
4
|
+
import { patchToken, tokens } from "../source/tokens.ts";
|
|
5
|
+
import { need, type Command } from "./command.ts";
|
|
6
|
+
import { editFile, read } from "./file.ts";
|
|
7
|
+
|
|
8
|
+
async function declared(project: Project): Promise<TokenData[]> {
|
|
9
|
+
const found: TokenData[] = [];
|
|
10
|
+
|
|
11
|
+
for (const file of await sourceFiles(project.root, ".css")) {
|
|
12
|
+
const { source } = await read(project, file);
|
|
13
|
+
|
|
14
|
+
for (const token of tokens(file, source)) {
|
|
15
|
+
found.push({ name: token.name, value: token.value, file });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return found;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const tokenList: Command = {
|
|
23
|
+
name: "token.list",
|
|
24
|
+
does: "Lists the design tokens: the variables the stylesheets declare in @theme and :root.",
|
|
25
|
+
params: {},
|
|
26
|
+
|
|
27
|
+
async run(project) {
|
|
28
|
+
return { ok: true, changed: [], data: await declared(project) };
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const tokenSet: Command = {
|
|
33
|
+
name: "token.set",
|
|
34
|
+
does: "Sets the value of a design token where a stylesheet declares it.",
|
|
35
|
+
params: { name: "the token, for example --color-brand", value: "the new value" },
|
|
36
|
+
optional: { file: "the stylesheet, when more than one declares the token" },
|
|
37
|
+
|
|
38
|
+
async run(project, params) {
|
|
39
|
+
const name = need(params, "name");
|
|
40
|
+
const value = need(params, "value");
|
|
41
|
+
|
|
42
|
+
const holders = (await declared(project))
|
|
43
|
+
.filter((token) => token.name === name)
|
|
44
|
+
.map((token) => token.file)
|
|
45
|
+
.filter((file) => params.file === undefined || file === params.file);
|
|
46
|
+
const files = [...new Set(holders)];
|
|
47
|
+
|
|
48
|
+
if (files.length === 0) {
|
|
49
|
+
const where = params.file ?? "any stylesheet";
|
|
50
|
+
return { ok: false, reason: `no token ${name} in ${where}` };
|
|
51
|
+
}
|
|
52
|
+
if (files.length > 1) {
|
|
53
|
+
return { ok: false, reason: `${name} is declared in ${files.join(" and ")}; pass file` };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const file = files[0]!;
|
|
57
|
+
|
|
58
|
+
return editFile(project, file, (source) => patchToken(file, source, name, value));
|
|
59
|
+
},
|
|
60
|
+
};
|