@sashaangelm/designkit 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/LICENSE +21 -0
- package/README.md +22 -2
- package/THIRD_PARTY_NOTICES.md +33 -0
- package/dist/browser.d.ts +17 -0
- package/dist/browser.js +108 -0
- package/dist/catalog.d.ts +22 -0
- package/dist/catalog.js +33 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +222 -0
- package/dist/definitions.d.ts +37 -0
- package/dist/definitions.js +39 -0
- package/dist/exporter.d.ts +24 -0
- package/dist/exporter.js +342 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +3 -0
- package/dist/nested.d.ts +2 -0
- package/dist/nested.js +60 -0
- package/dist/output.d.ts +3 -0
- package/dist/output.js +52 -0
- package/dist/primitives.d.ts +34 -0
- package/dist/primitives.js +60 -0
- package/dist/project.d.ts +27 -0
- package/dist/project.js +60 -0
- package/dist/render-types.d.ts +51 -0
- package/dist/render-types.js +1 -0
- package/dist/render.d.ts +1 -0
- package/dist/render.js +129 -0
- package/dist/scaffold.d.ts +8 -0
- package/dist/scaffold.js +71 -0
- package/dist/selection.d.ts +16 -0
- package/dist/selection.js +39 -0
- package/dist/server.d.ts +26 -0
- package/dist/server.js +123 -0
- package/dist/studio-server.d.ts +22 -0
- package/dist/studio-server.js +170 -0
- package/dist/studio.css +470 -0
- package/dist/studio.d.ts +1 -0
- package/dist/studio.js +396 -0
- package/package.json +56 -3
- package/template/AGENTS.md +6 -0
- package/template/README.md +21 -0
- package/template/assets/README.md +5 -0
- package/template/assets/designkit-starter-MIT.txt +21 -0
- package/template/assets/orbit.svg +33 -0
- package/template/assets/space-grotesk-OFL.txt +93 -0
- package/template/assets/space-grotesk-latin-700-normal.woff2 +0 -0
- package/template/designkit.config.ts +7 -0
- package/template/designkit.root.tsx +30 -0
- package/template/designs/assets.d.ts +6 -0
- package/template/designs/social.design.tsx +46 -0
- package/template/gitignore.txt +3 -0
- package/template/package.json +27 -0
- package/template/styles/social.css +53 -0
- package/template/tsconfig.json +19 -0
package/dist/exporter.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import sharp from "sharp";
|
|
3
|
+
import { createContext, launchBrowser, readState } from "./browser.js";
|
|
4
|
+
import { outputPath, writeOutput } from "./output.js";
|
|
5
|
+
import { resolvePreset, validateSelection } from "./selection.js";
|
|
6
|
+
import { startServer } from "./server.js";
|
|
7
|
+
const diagnostic = (code, error, target) => ({
|
|
8
|
+
code,
|
|
9
|
+
message: error instanceof Error ? error.message : String(error),
|
|
10
|
+
selector: target?.selector,
|
|
11
|
+
source: target?.page.source,
|
|
12
|
+
});
|
|
13
|
+
async function withRuntime(project, run, existing, config) {
|
|
14
|
+
const session = existing ?? (await startServer(project, config));
|
|
15
|
+
try {
|
|
16
|
+
const browser = await launchBrowser();
|
|
17
|
+
try {
|
|
18
|
+
return await run({ ...session, browser });
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
await browser.close();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
if (!existing)
|
|
26
|
+
await session.server.close();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function mapConcurrent(values, concurrency, work) {
|
|
30
|
+
const results = [];
|
|
31
|
+
let next = 0;
|
|
32
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, values.length) }, async () => {
|
|
33
|
+
while (next < values.length) {
|
|
34
|
+
const index = next++;
|
|
35
|
+
results[index] = await work(values[index]);
|
|
36
|
+
}
|
|
37
|
+
}));
|
|
38
|
+
return results;
|
|
39
|
+
}
|
|
40
|
+
export function renderUrl(origin, page, frame, capture = false) {
|
|
41
|
+
const url = new URL(`/render/${page.projectId}/${page.id}/${frame.id}`, origin);
|
|
42
|
+
if (capture)
|
|
43
|
+
url.searchParams.set("static", "1");
|
|
44
|
+
return url.href;
|
|
45
|
+
}
|
|
46
|
+
async function registry(runtime) {
|
|
47
|
+
const context = await createContext(runtime.browser, runtime.origin, {
|
|
48
|
+
width: 800,
|
|
49
|
+
height: 600,
|
|
50
|
+
});
|
|
51
|
+
try {
|
|
52
|
+
return (await readState(context, runtime.origin + "/registry")).state
|
|
53
|
+
.registry;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
await context.close();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function checkArea(root, scale) {
|
|
60
|
+
const width = Math.round(root.width * scale);
|
|
61
|
+
const height = Math.round(root.height * scale);
|
|
62
|
+
if (width < 1 ||
|
|
63
|
+
height < 1 ||
|
|
64
|
+
width * height > 64_000_000 ||
|
|
65
|
+
width > 32_767 ||
|
|
66
|
+
height > 32_767 ||
|
|
67
|
+
root.width > 32_767 ||
|
|
68
|
+
root.height > 32_767) {
|
|
69
|
+
throw new Error("Artboard capture must fit within 64 megapixels and 1–32767 pixels per edge.");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function withFrame(runtime, target, scale, run) {
|
|
73
|
+
checkArea(target.root, scale);
|
|
74
|
+
const context = await createContext(runtime.browser, runtime.origin, target.root, scale);
|
|
75
|
+
try {
|
|
76
|
+
const frame = await readState(context, renderUrl(runtime.origin, target.page, target.root, true));
|
|
77
|
+
if (frame.errors.length)
|
|
78
|
+
throw new Error(frame.errors.join("\n"));
|
|
79
|
+
const result = await run(frame);
|
|
80
|
+
if (frame.errors.length)
|
|
81
|
+
throw new Error(frame.errors.join("\n"));
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
await context.close();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function artboards(pages) {
|
|
89
|
+
return pages.flatMap((page) => page.frames.map((root) => ({
|
|
90
|
+
page,
|
|
91
|
+
root,
|
|
92
|
+
selector: `${page.projectId}/${page.id}/${root.id}`,
|
|
93
|
+
})));
|
|
94
|
+
}
|
|
95
|
+
async function inspect(runtime, frames) {
|
|
96
|
+
return mapConcurrent(frames, runtime.config.concurrency, async (frame) => {
|
|
97
|
+
try {
|
|
98
|
+
frame.root.nested = await withFrame(runtime, frame, 1, async ({ state }) => state.nested ?? []);
|
|
99
|
+
return { frame };
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
return { frame, error: diagnostic("RENDER_FAILED", error, frame) };
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function inspectProject(project, check = false) {
|
|
107
|
+
return withRuntime(project, async (runtime) => {
|
|
108
|
+
const report = await registry(runtime);
|
|
109
|
+
for (const { frame, error } of await inspect(runtime, artboards(report.projects.flatMap(({ pages }) => pages)))) {
|
|
110
|
+
if (error)
|
|
111
|
+
report.errors.push(error);
|
|
112
|
+
if (!check)
|
|
113
|
+
continue;
|
|
114
|
+
for (const nested of frame.root.nested ?? []) {
|
|
115
|
+
if (nested.exportError) {
|
|
116
|
+
report.errors.push(diagnostic("NESTED_BOUNDS", nested.exportError, {
|
|
117
|
+
...frame,
|
|
118
|
+
selector: frame.selector + "/" + nested.id,
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return report;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function selectedArtboards(projects, options, errors) {
|
|
127
|
+
const pages = projects.flatMap(({ pages }) => pages);
|
|
128
|
+
const frames = artboards(pages);
|
|
129
|
+
if (options.frame?.length) {
|
|
130
|
+
const selected = new Set();
|
|
131
|
+
for (const selector of options.frame) {
|
|
132
|
+
const root = selector.split("/").slice(0, 3).join("/");
|
|
133
|
+
if (frames.some((frame) => frame.selector === root))
|
|
134
|
+
selected.add(root);
|
|
135
|
+
else
|
|
136
|
+
errors.push({
|
|
137
|
+
code: "UNKNOWN_FRAME",
|
|
138
|
+
selector,
|
|
139
|
+
message: `Unknown frame: ${selector}`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return frames.filter(({ selector }) => selected.has(selector));
|
|
143
|
+
}
|
|
144
|
+
if (options.project) {
|
|
145
|
+
if (!projects.some(({ id }) => id === options.project)) {
|
|
146
|
+
errors.push({
|
|
147
|
+
code: "UNKNOWN_PROJECT",
|
|
148
|
+
message: `Unknown project: ${options.project}`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return frames.filter(({ page }) => page.projectId === options.project);
|
|
152
|
+
}
|
|
153
|
+
if (!options.page)
|
|
154
|
+
return frames;
|
|
155
|
+
const pageSelector = (page) => `${page.projectId}/${page.id}`;
|
|
156
|
+
if (!pages.some((page) => pageSelector(page) === options.page)) {
|
|
157
|
+
errors.push({
|
|
158
|
+
code: "UNKNOWN_PAGE",
|
|
159
|
+
message: `Unknown page: ${options.page}`,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return frames.filter(({ page }) => pageSelector(page) === options.page);
|
|
163
|
+
}
|
|
164
|
+
function selectTargets(frame, options, errors) {
|
|
165
|
+
const target = (nested) => ({
|
|
166
|
+
...frame,
|
|
167
|
+
nested,
|
|
168
|
+
selector: `${frame.selector}/${nested.id}`,
|
|
169
|
+
});
|
|
170
|
+
if (!options.frame?.length) {
|
|
171
|
+
return [
|
|
172
|
+
frame,
|
|
173
|
+
...(frame.root.nested ?? [])
|
|
174
|
+
.filter((nested) => nested.exports?.length)
|
|
175
|
+
.map(target),
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
return options.frame
|
|
179
|
+
.filter((selector) => selector === frame.selector ||
|
|
180
|
+
selector.startsWith(frame.selector + "/"))
|
|
181
|
+
.flatMap((selector) => {
|
|
182
|
+
if (selector === frame.selector)
|
|
183
|
+
return [frame];
|
|
184
|
+
const id = selector.split("/")[3];
|
|
185
|
+
const nested = frame.root.nested?.find((entry) => entry.id === id);
|
|
186
|
+
if (nested)
|
|
187
|
+
return [target(nested)];
|
|
188
|
+
errors.push({
|
|
189
|
+
code: "UNKNOWN_FRAME",
|
|
190
|
+
selector,
|
|
191
|
+
source: frame.page.source,
|
|
192
|
+
message: `Unknown frame: ${selector}`,
|
|
193
|
+
});
|
|
194
|
+
return [];
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function planJobs(targets, options, config, errors) {
|
|
198
|
+
const override = resolvePreset(options, config.export);
|
|
199
|
+
const jobs = [];
|
|
200
|
+
const paths = new Map();
|
|
201
|
+
for (const target of targets) {
|
|
202
|
+
const { root, nested } = target;
|
|
203
|
+
const configured = (nested ?? root).exports;
|
|
204
|
+
for (const preset of override
|
|
205
|
+
? [override]
|
|
206
|
+
: configured?.length
|
|
207
|
+
? configured
|
|
208
|
+
: [config.export]) {
|
|
209
|
+
if (nested?.exportError) {
|
|
210
|
+
errors.push(diagnostic("NESTED_BOUNDS", nested.exportError, target));
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const suffix = preset.scale === 1 ? "" : `@${preset.scale}x`;
|
|
214
|
+
const path = join(config.output, ...target.selector.split("/")) +
|
|
215
|
+
suffix +
|
|
216
|
+
"." +
|
|
217
|
+
preset.format;
|
|
218
|
+
const key = path.toLowerCase();
|
|
219
|
+
paths.set(key, (paths.get(key) ?? 0) + 1);
|
|
220
|
+
jobs.push({ ...target, path, preset });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const collisions = new Set();
|
|
224
|
+
return jobs.filter((job) => {
|
|
225
|
+
const key = job.path.toLowerCase();
|
|
226
|
+
if (paths.get(key) === 1)
|
|
227
|
+
return true;
|
|
228
|
+
if (!collisions.has(key))
|
|
229
|
+
errors.push(diagnostic("OUTPUT_COLLISION", `Multiple exports resolve to ${job.path}`, job));
|
|
230
|
+
collisions.add(key);
|
|
231
|
+
return false;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
async function capture(runtime, job) {
|
|
235
|
+
const { scale, format, quality = 95, transparent = format !== "jpeg", } = job.preset;
|
|
236
|
+
return withFrame(runtime, job, scale, async ({ page, state }) => {
|
|
237
|
+
const bounds = job.nested && state.nested?.find(({ id }) => id === job.nested?.id);
|
|
238
|
+
if (job.nested && !bounds)
|
|
239
|
+
throw new Error("Nested frame disappeared at capture scale.");
|
|
240
|
+
if (bounds?.exportError)
|
|
241
|
+
throw new Error(bounds.exportError);
|
|
242
|
+
const png = await page.screenshot({
|
|
243
|
+
omitBackground: transparent,
|
|
244
|
+
animations: "disabled",
|
|
245
|
+
timeout: 15_000,
|
|
246
|
+
});
|
|
247
|
+
const pipeline = sharp(png);
|
|
248
|
+
const metadata = await pipeline.metadata();
|
|
249
|
+
const width = metadata.width;
|
|
250
|
+
const height = metadata.height;
|
|
251
|
+
const left = bounds ? Math.max(0, Math.floor(bounds.x * scale)) : 0;
|
|
252
|
+
const top = bounds ? Math.max(0, Math.floor(bounds.y * scale)) : 0;
|
|
253
|
+
const right = bounds
|
|
254
|
+
? Math.min(width, Math.ceil((bounds.x + bounds.width) * scale))
|
|
255
|
+
: width;
|
|
256
|
+
const bottom = bounds
|
|
257
|
+
? Math.min(height, Math.ceil((bounds.y + bounds.height) * scale))
|
|
258
|
+
: height;
|
|
259
|
+
if (bounds)
|
|
260
|
+
pipeline.extract({
|
|
261
|
+
left,
|
|
262
|
+
top,
|
|
263
|
+
width: right - left,
|
|
264
|
+
height: bottom - top,
|
|
265
|
+
});
|
|
266
|
+
if (!transparent)
|
|
267
|
+
pipeline.flatten({ background: "#ffffff" });
|
|
268
|
+
if (format === "webp")
|
|
269
|
+
pipeline.webp({ quality });
|
|
270
|
+
if (format === "jpeg")
|
|
271
|
+
pipeline.jpeg({ quality });
|
|
272
|
+
const buffer = format === "png" && !bounds && transparent
|
|
273
|
+
? png
|
|
274
|
+
: await pipeline.toBuffer();
|
|
275
|
+
return { buffer, width: right - left, height: bottom - top };
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
export async function exportProject(project, options, existing, config) {
|
|
279
|
+
validateSelection(options);
|
|
280
|
+
return withRuntime(project, async (runtime) => {
|
|
281
|
+
const report = { files: [], errors: [] };
|
|
282
|
+
const discovered = await registry(runtime);
|
|
283
|
+
report.errors.push(...discovered.errors);
|
|
284
|
+
const frames = selectedArtboards(discovered.projects, options, report.errors);
|
|
285
|
+
const inspected = await inspect(runtime, frames);
|
|
286
|
+
const targets = inspected.flatMap(({ frame, error }) => {
|
|
287
|
+
if (!error)
|
|
288
|
+
return selectTargets(frame, options, report.errors);
|
|
289
|
+
report.errors.push(error);
|
|
290
|
+
return [];
|
|
291
|
+
});
|
|
292
|
+
if (!frames.length && !report.errors.length) {
|
|
293
|
+
report.errors.push({
|
|
294
|
+
code: "NO_FRAMES",
|
|
295
|
+
message: "No artboards found. Add a <Frame> inside a <Page> in the DesignKit entry.",
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
const jobs = planJobs(targets, options, runtime.config, report.errors);
|
|
299
|
+
const prepared = [];
|
|
300
|
+
for (const job of jobs) {
|
|
301
|
+
try {
|
|
302
|
+
checkArea(job.root, job.preset.scale);
|
|
303
|
+
prepared.push({
|
|
304
|
+
job,
|
|
305
|
+
path: await outputPath(runtime.config.root, job.path, options.overwrite),
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
report.errors.push(diagnostic("OUTPUT_PREFLIGHT", error, job));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
for (const result of await mapConcurrent(prepared, runtime.config.concurrency, async ({ job, path }) => {
|
|
313
|
+
try {
|
|
314
|
+
const { buffer, width, height } = await capture(runtime, job);
|
|
315
|
+
const checked = await outputPath(runtime.config.root, job.path, options.overwrite);
|
|
316
|
+
if (checked !== path)
|
|
317
|
+
throw new Error("Output path changed during capture.");
|
|
318
|
+
await writeOutput(path, buffer, options.overwrite ?? false);
|
|
319
|
+
const { selector, preset } = job;
|
|
320
|
+
return {
|
|
321
|
+
file: {
|
|
322
|
+
selector,
|
|
323
|
+
path: job.path.replaceAll("\\", "/"),
|
|
324
|
+
width,
|
|
325
|
+
height,
|
|
326
|
+
format: preset.format,
|
|
327
|
+
scale: preset.scale,
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
return { error: diagnostic("CAPTURE_FAILED", error, job) };
|
|
333
|
+
}
|
|
334
|
+
})) {
|
|
335
|
+
if (result.file)
|
|
336
|
+
report.files.push(result.file);
|
|
337
|
+
else
|
|
338
|
+
report.errors.push(result.error);
|
|
339
|
+
}
|
|
340
|
+
return report;
|
|
341
|
+
}, existing, config);
|
|
342
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { exportPresetSchema, idSchema } from "./definitions.js";
|
|
2
|
+
export type { ExportPreset, ExportPresetInput } from "./definitions.js";
|
|
3
|
+
export { Root, Project, Page } from "./catalog.js";
|
|
4
|
+
export type { RootProps, ProjectProps, PageProps } from "./catalog.js";
|
|
5
|
+
export { Ellipse, Frame, Group, Image, Rectangle, Stack, Text, } from "./primitives.js";
|
|
6
|
+
export type { FrameProps, ImageProps, ShapeProps, StackProps, } from "./primitives.js";
|
|
7
|
+
export type { ProjectConfig } from "./project.js";
|
package/dist/index.js
ADDED
package/dist/nested.d.ts
ADDED
package/dist/nested.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { exportPresetSchema, idSchema } from "./definitions.js";
|
|
2
|
+
function checkBounds(element, root, bounds) {
|
|
3
|
+
let clipped = false;
|
|
4
|
+
for (let ancestor = element; ancestor; ancestor = ancestor.parentElement) {
|
|
5
|
+
const style = getComputedStyle(ancestor);
|
|
6
|
+
if (style.transform !== "none" ||
|
|
7
|
+
style.rotate !== "none" ||
|
|
8
|
+
style.scale !== "none" ||
|
|
9
|
+
style.translate !== "none" ||
|
|
10
|
+
style.perspective !== "none") {
|
|
11
|
+
return "Transformed nested frame bounds are unsupported.";
|
|
12
|
+
}
|
|
13
|
+
if (ancestor !== element) {
|
|
14
|
+
const box = ancestor.getBoundingClientRect();
|
|
15
|
+
const left = box.left + ancestor.clientLeft;
|
|
16
|
+
const top = box.top + ancestor.clientTop;
|
|
17
|
+
const outsideX = bounds.left < left - 0.01 ||
|
|
18
|
+
bounds.right > left + ancestor.clientWidth + 0.01;
|
|
19
|
+
const outsideY = bounds.top < top - 0.01 ||
|
|
20
|
+
bounds.bottom > top + ancestor.clientHeight + 0.01;
|
|
21
|
+
clipped ||=
|
|
22
|
+
(outsideX && (ancestor === root || style.overflowX !== "visible")) ||
|
|
23
|
+
(outsideY && (ancestor === root || style.overflowY !== "visible"));
|
|
24
|
+
if (style.clipPath !== "none" || style.maskImage !== "none") {
|
|
25
|
+
return "Nested frame may be clipped by an ancestor mask or clip-path.";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (ancestor === root)
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
if (clipped)
|
|
32
|
+
return "Nested frame extends outside its artboard or a clipping ancestor.";
|
|
33
|
+
if (bounds.width <= 0 || bounds.height <= 0)
|
|
34
|
+
return "Nested frame has empty bounds.";
|
|
35
|
+
}
|
|
36
|
+
export function inspectNested(root) {
|
|
37
|
+
const ids = new Set();
|
|
38
|
+
return Array.from(root.querySelectorAll("[data-designkit-frame]"), (element) => {
|
|
39
|
+
const id = element.getAttribute("data-designkit-frame");
|
|
40
|
+
if (!idSchema.safeParse(id).success)
|
|
41
|
+
throw new Error(`Invalid nested frame ID "${id}"`);
|
|
42
|
+
if (ids.has(id))
|
|
43
|
+
throw new Error(`Duplicate nested frame ID "${id}"`);
|
|
44
|
+
ids.add(id);
|
|
45
|
+
const presets = element.getAttribute("data-designkit-exports");
|
|
46
|
+
const bounds = element.getBoundingClientRect();
|
|
47
|
+
return {
|
|
48
|
+
id,
|
|
49
|
+
name: element.getAttribute("data-designkit-name") ?? id,
|
|
50
|
+
x: bounds.x,
|
|
51
|
+
y: bounds.y,
|
|
52
|
+
width: bounds.width,
|
|
53
|
+
height: bounds.height,
|
|
54
|
+
exports: presets
|
|
55
|
+
? exportPresetSchema.array().parse(JSON.parse(presets))
|
|
56
|
+
: undefined,
|
|
57
|
+
exportError: checkBounds(element, root, bounds),
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
}
|
package/dist/output.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare function isInside(root: string, target: string): boolean;
|
|
2
|
+
export declare function outputPath(project: string, name: string, overwrite?: boolean): Promise<string>;
|
|
3
|
+
export declare function writeOutput(target: string, bytes: Buffer, overwrite: boolean): Promise<void>;
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { link, lstat, mkdir, realpath, rename, rm, writeFile, } from "node:fs/promises";
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
export function isInside(root, target) {
|
|
5
|
+
const rel = relative(root, target);
|
|
6
|
+
return rel !== ".." && !rel.startsWith(".." + sep) && !isAbsolute(rel);
|
|
7
|
+
}
|
|
8
|
+
export async function outputPath(project, name, overwrite = false) {
|
|
9
|
+
const root = await realpath(project);
|
|
10
|
+
const full = resolve(root, name);
|
|
11
|
+
if (!isInside(root, full) || full === root)
|
|
12
|
+
throw new Error(`Output must be inside the project: ${name}`);
|
|
13
|
+
const parts = relative(root, full).split(sep);
|
|
14
|
+
for (const part of parts) {
|
|
15
|
+
if (/[. ]$|[<>:"|?*\x00-\x1f]|^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(part)) {
|
|
16
|
+
throw new Error(`Output name is incompatible with Windows: ${part}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
let parent = root;
|
|
20
|
+
for (const part of parts.slice(0, -1)) {
|
|
21
|
+
parent = join(parent, part);
|
|
22
|
+
await mkdir(parent).catch((error) => {
|
|
23
|
+
if (error.code !== "EEXIST")
|
|
24
|
+
throw error;
|
|
25
|
+
});
|
|
26
|
+
if (!(await lstat(parent)).isDirectory() ||
|
|
27
|
+
!isInside(root, await realpath(parent))) {
|
|
28
|
+
throw new Error(`Unsafe output directory: ${parent}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const existing = await lstat(full).catch((error) => {
|
|
32
|
+
if (error.code !== "ENOENT")
|
|
33
|
+
throw error;
|
|
34
|
+
});
|
|
35
|
+
if (existing && (!overwrite || !existing.isFile())) {
|
|
36
|
+
throw new Error(`Output already exists: ${name}${existing.isFile() ? " (use --overwrite to replace it)" : ""}`);
|
|
37
|
+
}
|
|
38
|
+
return full;
|
|
39
|
+
}
|
|
40
|
+
export async function writeOutput(target, bytes, overwrite) {
|
|
41
|
+
const temporary = join(dirname(target), `.designkit-${randomUUID()}.tmp`);
|
|
42
|
+
try {
|
|
43
|
+
await writeFile(temporary, bytes, { flag: "wx" });
|
|
44
|
+
if (overwrite)
|
|
45
|
+
await rename(temporary, target);
|
|
46
|
+
else
|
|
47
|
+
await link(temporary, target);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
await rm(temporary, { force: true });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef, CSSProperties } from "react";
|
|
2
|
+
import type { ExportPresetInput } from "./definitions.js";
|
|
3
|
+
export interface FrameProps extends ComponentPropsWithoutRef<"div"> {
|
|
4
|
+
name?: string;
|
|
5
|
+
width?: CSSProperties["width"];
|
|
6
|
+
height?: CSSProperties["height"];
|
|
7
|
+
background?: CSSProperties["background"];
|
|
8
|
+
x?: number;
|
|
9
|
+
y?: number;
|
|
10
|
+
exports?: readonly ExportPresetInput[];
|
|
11
|
+
}
|
|
12
|
+
export declare function Frame({ id, name, width, height, background, exports: presets, x, y, children, style, ...props }: FrameProps): import("react/jsx-runtime").JSX.Element | null;
|
|
13
|
+
export interface StackProps extends ComponentPropsWithoutRef<"div"> {
|
|
14
|
+
direction?: "vertical" | "horizontal";
|
|
15
|
+
gap?: CSSProperties["gap"];
|
|
16
|
+
padding?: CSSProperties["padding"];
|
|
17
|
+
align?: CSSProperties["alignItems"];
|
|
18
|
+
justify?: CSSProperties["justifyContent"];
|
|
19
|
+
}
|
|
20
|
+
export declare function Stack({ direction, gap, padding, align, justify, style, ...props }: StackProps): import("react/jsx-runtime").JSX.Element;
|
|
21
|
+
export declare function Text(props: ComponentPropsWithoutRef<"span">): import("react/jsx-runtime").JSX.Element;
|
|
22
|
+
export declare function Group(props: ComponentPropsWithoutRef<"div">): import("react/jsx-runtime").JSX.Element;
|
|
23
|
+
export interface ImageProps extends ComponentPropsWithoutRef<"img"> {
|
|
24
|
+
src: string;
|
|
25
|
+
alt: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function Image(props: ImageProps): import("react/jsx-runtime").JSX.Element;
|
|
28
|
+
export interface ShapeProps extends ComponentPropsWithoutRef<"div"> {
|
|
29
|
+
width?: CSSProperties["width"];
|
|
30
|
+
height?: CSSProperties["height"];
|
|
31
|
+
background?: CSSProperties["background"];
|
|
32
|
+
}
|
|
33
|
+
export declare function Rectangle({ width, height, background, style, ...props }: ShapeProps): import("react/jsx-runtime").JSX.Element;
|
|
34
|
+
export declare function Ellipse({ style, ...props }: ShapeProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useContext } from "react";
|
|
3
|
+
import { CatalogContext } from "./catalog.js";
|
|
4
|
+
import { exportPresetSchema, frameSchema } from "./definitions.js";
|
|
5
|
+
export function Frame({ id, name, width, height, background, exports: presets, x, y, children, style, ...props }) {
|
|
6
|
+
const runtime = useContext(CatalogContext);
|
|
7
|
+
const artboard = runtime?.scope === "page";
|
|
8
|
+
if (runtime && !artboard && runtime.scope !== "artwork") {
|
|
9
|
+
throw new Error("Place an artboard <Frame> inside <Page>.");
|
|
10
|
+
}
|
|
11
|
+
const metadata = artboard
|
|
12
|
+
? frameSchema.parse({ id, name, width, height, x, y, exports: presets })
|
|
13
|
+
: undefined;
|
|
14
|
+
if (metadata && !runtime?.selection) {
|
|
15
|
+
return _jsx("div", { "data-designkit-artboard": JSON.stringify(metadata) });
|
|
16
|
+
}
|
|
17
|
+
if (metadata && runtime?.selection?.frame !== metadata.id)
|
|
18
|
+
return null;
|
|
19
|
+
if (presets?.length && !id) {
|
|
20
|
+
throw new Error("Frame export presets require a frame ID");
|
|
21
|
+
}
|
|
22
|
+
return (_jsx("div", { ...props, "data-designkit-root": artboard || undefined, "data-designkit-frame": id, "data-designkit-name": name, "data-designkit-exports": presets &&
|
|
23
|
+
JSON.stringify(metadata?.exports ??
|
|
24
|
+
presets.map((preset) => exportPresetSchema.parse(preset))), style: {
|
|
25
|
+
position: "relative",
|
|
26
|
+
boxSizing: "border-box",
|
|
27
|
+
overflow: "hidden",
|
|
28
|
+
width,
|
|
29
|
+
height,
|
|
30
|
+
background,
|
|
31
|
+
...style,
|
|
32
|
+
...(artboard ? { width, height, boxSizing: "border-box" } : {}),
|
|
33
|
+
}, children: artboard ? (_jsx(CatalogContext.Provider, { value: { scope: "artwork" }, children: children })) : (children) }));
|
|
34
|
+
}
|
|
35
|
+
export function Stack({ direction = "vertical", gap, padding, align, justify, style, ...props }) {
|
|
36
|
+
return (_jsx("div", { ...props, style: {
|
|
37
|
+
display: "flex",
|
|
38
|
+
flexDirection: direction === "vertical" ? "column" : "row",
|
|
39
|
+
gap,
|
|
40
|
+
padding,
|
|
41
|
+
alignItems: align,
|
|
42
|
+
justifyContent: justify,
|
|
43
|
+
...style,
|
|
44
|
+
} }));
|
|
45
|
+
}
|
|
46
|
+
export function Text(props) {
|
|
47
|
+
return _jsx("span", { ...props });
|
|
48
|
+
}
|
|
49
|
+
export function Group(props) {
|
|
50
|
+
return _jsx("div", { ...props });
|
|
51
|
+
}
|
|
52
|
+
export function Image(props) {
|
|
53
|
+
return _jsx("img", { ...props });
|
|
54
|
+
}
|
|
55
|
+
export function Rectangle({ width, height, background, style, ...props }) {
|
|
56
|
+
return (_jsx("div", { ...props, style: { boxSizing: "border-box", width, height, background, ...style } }));
|
|
57
|
+
}
|
|
58
|
+
export function Ellipse({ style, ...props }) {
|
|
59
|
+
return _jsx(Rectangle, { ...props, style: { borderRadius: "50%", ...style } });
|
|
60
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
declare const projectSchema: z.ZodObject<{
|
|
3
|
+
entry: z.ZodDefault<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
|
|
4
|
+
output: z.ZodDefault<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
|
|
5
|
+
globalCss: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
|
|
6
|
+
studio: z.ZodDefault<z.ZodObject<{
|
|
7
|
+
port: z.ZodOptional<z.ZodNumber>;
|
|
8
|
+
}, z.core.$strict>>;
|
|
9
|
+
export: z.ZodDefault<z.ZodObject<{
|
|
10
|
+
format: z.ZodEnum<{
|
|
11
|
+
png: "png";
|
|
12
|
+
jpeg: "jpeg";
|
|
13
|
+
webp: "webp";
|
|
14
|
+
}>;
|
|
15
|
+
scale: z.ZodDefault<z.ZodNumber>;
|
|
16
|
+
quality: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
transparent: z.ZodOptional<z.ZodBoolean>;
|
|
18
|
+
}, z.core.$strict>>;
|
|
19
|
+
concurrency: z.ZodDefault<z.ZodNumber>;
|
|
20
|
+
}, z.core.$strict>;
|
|
21
|
+
export type ProjectConfig = z.input<typeof projectSchema>;
|
|
22
|
+
export type LoadedProject = z.output<typeof projectSchema> & {
|
|
23
|
+
root: string;
|
|
24
|
+
};
|
|
25
|
+
export declare function projectFile(root: string, path: string): Promise<string>;
|
|
26
|
+
export declare function loadProject(project: string): Promise<LoadedProject>;
|
|
27
|
+
export {};
|
package/dist/project.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { lstat, realpath, stat } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { loadConfigFromFile } from "vite";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { exportPresetSchema } from "./definitions.js";
|
|
6
|
+
import { isInside } from "./output.js";
|
|
7
|
+
const relativePath = z
|
|
8
|
+
.string()
|
|
9
|
+
.min(1)
|
|
10
|
+
.transform((value) => value.replaceAll("\\", "/"))
|
|
11
|
+
.refine((value) => !value.startsWith("/") &&
|
|
12
|
+
!/[:\x00-\x1f]/.test(value) &&
|
|
13
|
+
!value.split("/").includes("..") &&
|
|
14
|
+
value.split("/").some((segment) => segment && segment !== "."), "Use a path relative to the project root");
|
|
15
|
+
const filePath = relativePath.refine((value) => !/[<>"|?*]/.test(value), "Expected a file or directory path without glob characters");
|
|
16
|
+
const projectSchema = z.strictObject({
|
|
17
|
+
entry: filePath.default("designkit.root.tsx"),
|
|
18
|
+
output: filePath.default("output"),
|
|
19
|
+
globalCss: filePath.optional(),
|
|
20
|
+
studio: z
|
|
21
|
+
.strictObject({ port: z.number().int().min(1).max(65535).optional() })
|
|
22
|
+
.default({}),
|
|
23
|
+
export: exportPresetSchema.default({ format: "png", scale: 1 }),
|
|
24
|
+
concurrency: z.number().int().min(1).max(8).default(2),
|
|
25
|
+
});
|
|
26
|
+
export async function projectFile(root, path) {
|
|
27
|
+
const absolute = await realpath(resolve(root, path)).catch((error) => {
|
|
28
|
+
if (error.code === "ENOENT")
|
|
29
|
+
throw new Error(`File not found: ${path}`);
|
|
30
|
+
throw error;
|
|
31
|
+
});
|
|
32
|
+
if (!isInside(root, absolute) || !(await stat(absolute)).isFile()) {
|
|
33
|
+
throw new Error(`Expected a file inside the project: ${path}`);
|
|
34
|
+
}
|
|
35
|
+
return absolute;
|
|
36
|
+
}
|
|
37
|
+
export async function loadProject(project) {
|
|
38
|
+
const root = await realpath(project);
|
|
39
|
+
if (!(await stat(root)).isDirectory()) {
|
|
40
|
+
throw new Error(`Expected a project directory: ${project}`);
|
|
41
|
+
}
|
|
42
|
+
const filename = "designkit.config.ts";
|
|
43
|
+
const configPath = join(root, filename);
|
|
44
|
+
const existing = await lstat(configPath).catch((error) => {
|
|
45
|
+
if (error.code !== "ENOENT")
|
|
46
|
+
throw error;
|
|
47
|
+
});
|
|
48
|
+
try {
|
|
49
|
+
const loaded = existing
|
|
50
|
+
? await loadConfigFromFile({ command: "serve", mode: "development" }, await projectFile(root, filename), root, "silent", undefined, "bundle")
|
|
51
|
+
: undefined;
|
|
52
|
+
const config = projectSchema.parse(loaded?.config ?? {});
|
|
53
|
+
const globalCss = config.globalCss && (await projectFile(root, config.globalCss));
|
|
54
|
+
return { ...config, root, globalCss };
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
const details = error instanceof Error ? error.message : String(error);
|
|
58
|
+
throw new Error(`${filename}: ${details}`, { cause: error });
|
|
59
|
+
}
|
|
60
|
+
}
|