@milesroxas/design-hub 0.2.0 → 0.3.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/core/catalog.test.ts +10 -1
- package/core/catalog.ts +11 -0
- package/core/cms.test.ts +103 -0
- package/core/cms.ts +178 -0
- package/core/export.test.ts +127 -0
- package/core/export.ts +120 -0
- package/core/formats.ts +60 -0
- package/core/project.test.ts +71 -0
- package/core/project.ts +147 -0
- package/dist/core/catalog.d.ts +5 -0
- package/dist/core/catalog.js +10 -0
- package/dist/core/cms.d.ts +40 -0
- package/dist/core/cms.js +141 -0
- package/dist/core/export.d.ts +52 -0
- package/dist/core/export.js +84 -0
- package/dist/core/formats.d.ts +90 -0
- package/dist/core/formats.js +39 -0
- package/dist/core/project.d.ts +56 -0
- package/dist/core/project.js +121 -0
- package/dist/scripts/cli.js +4 -0
- package/dist/scripts/commands.js +4 -0
- package/dist/scripts/export.d.ts +42 -0
- package/dist/scripts/export.js +192 -0
- package/dist/scripts/fixtures-pull.d.ts +31 -19
- package/dist/scripts/fixtures-pull.js +102 -62
- package/dist/scripts/lib/cdp.d.ts +11 -0
- package/dist/scripts/lib/cdp.js +110 -0
- package/dist/scripts/new.d.ts +13 -1
- package/dist/scripts/new.js +43 -4
- package/dist/scripts/tracks.d.ts +40 -0
- package/dist/scripts/tracks.js +93 -0
- package/dist/server/paths.d.ts +2 -0
- package/dist/server/paths.js +2 -0
- package/dist/server/project.d.ts +7 -0
- package/dist/server/project.js +29 -0
- package/kit/Artboard.tsx +160 -0
- package/kit/artboard.css +47 -0
- package/kit/dials.tsx +2 -1
- package/package.json +1 -1
- package/shell/Index.tsx +49 -4
- package/shell/main.tsx +6 -2
- package/shell/shell.css +7 -3
- package/skills/miles-brand/SKILL.md +52 -0
- package/skills/miles-design-hub/SKILL.md +60 -25
- package/skills/miles-get-hostile/SKILL.md +7 -5
- package/skills/miles-new-feature/SKILL.md +8 -7
- package/skills/miles-port/SKILL.md +14 -2
- package/skills/miles-port/references/emdash.md +43 -0
- package/skills/miles-port/references/payload.md +31 -0
- package/skills/miles-port/references/wordpress.md +31 -0
- package/skills/miles-prototype/SKILL.md +2 -2
- package/templates/app/README.md +1 -0
- package/templates/brand/README.md +48 -0
- package/templates/brand/index.tsx +16 -0
- package/templates/brand/styles.css +5 -0
- package/templates/site/README.md +42 -0
- package/templates/site/index.tsx +22 -0
- package/templates/site/styles.css +6 -0
- package/templates/space/README.md +1 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export type Output = 'png' | 'pdf';
|
|
2
|
+
export type Format = {
|
|
3
|
+
label: string;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
output: Output;
|
|
7
|
+
/** The CSS page size for PDF; pixel formats print at their pixel size. */
|
|
8
|
+
page?: string;
|
|
9
|
+
/** Bands a platform's own interface covers; type and logos stay out of them. */
|
|
10
|
+
safe?: {
|
|
11
|
+
top: number;
|
|
12
|
+
bottom: number;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export declare const FORMATS: {
|
|
16
|
+
readonly 'post-square': {
|
|
17
|
+
readonly label: 'Post, square';
|
|
18
|
+
readonly width: 1080;
|
|
19
|
+
readonly height: 1080;
|
|
20
|
+
readonly output: 'png';
|
|
21
|
+
};
|
|
22
|
+
readonly 'post-portrait': {
|
|
23
|
+
readonly label: 'Post, portrait';
|
|
24
|
+
readonly width: 1080;
|
|
25
|
+
readonly height: 1350;
|
|
26
|
+
readonly output: 'png';
|
|
27
|
+
};
|
|
28
|
+
readonly story: {
|
|
29
|
+
readonly label: 'Story';
|
|
30
|
+
readonly width: 1080;
|
|
31
|
+
readonly height: 1920;
|
|
32
|
+
readonly output: 'png';
|
|
33
|
+
readonly safe: {
|
|
34
|
+
readonly top: 250;
|
|
35
|
+
readonly bottom: 250;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
readonly 'link-preview': {
|
|
39
|
+
readonly label: 'Link preview';
|
|
40
|
+
readonly width: 1200;
|
|
41
|
+
readonly height: 630;
|
|
42
|
+
readonly output: 'png';
|
|
43
|
+
};
|
|
44
|
+
readonly banner: {
|
|
45
|
+
readonly label: 'Banner';
|
|
46
|
+
readonly width: 1584;
|
|
47
|
+
readonly height: 396;
|
|
48
|
+
readonly output: 'png';
|
|
49
|
+
};
|
|
50
|
+
readonly slide: {
|
|
51
|
+
readonly label: 'Slide';
|
|
52
|
+
readonly width: 1920;
|
|
53
|
+
readonly height: 1080;
|
|
54
|
+
readonly output: 'pdf';
|
|
55
|
+
};
|
|
56
|
+
readonly a4: {
|
|
57
|
+
readonly label: 'A4';
|
|
58
|
+
readonly width: 794;
|
|
59
|
+
readonly height: 1123;
|
|
60
|
+
readonly output: 'pdf';
|
|
61
|
+
readonly page: '210mm 297mm';
|
|
62
|
+
};
|
|
63
|
+
readonly letter: {
|
|
64
|
+
readonly label: 'Letter';
|
|
65
|
+
readonly width: 816;
|
|
66
|
+
readonly height: 1056;
|
|
67
|
+
readonly output: 'pdf';
|
|
68
|
+
readonly page: '8.5in 11in';
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
export type FormatName = keyof typeof FORMATS;
|
|
72
|
+
export declare const FORMAT_NAMES: FormatName[];
|
|
73
|
+
export declare const isFormat: (v: unknown) => v is FormatName;
|
|
74
|
+
/** `1080 × 1350`, as a label under an artboard. */
|
|
75
|
+
export declare const sizeLabel: (name: FormatName) => string;
|
|
76
|
+
/** The CSS `@page` size a format prints at: its paper size, or its pixel size. */
|
|
77
|
+
export declare const pageSize: (name: FormatName) => string;
|
|
78
|
+
/** The screen scale for the Board's zoom dial: `fit`, or a percentage. */
|
|
79
|
+
export declare function zoomFor(zoom: string, name: FormatName, room: {
|
|
80
|
+
width: number;
|
|
81
|
+
height: number;
|
|
82
|
+
}): number;
|
|
83
|
+
/**
|
|
84
|
+
* How large an artboard draws on screen: its real size at most, else what
|
|
85
|
+
* fits the board's width and a comfortable height. Export always draws at 1.
|
|
86
|
+
*/
|
|
87
|
+
export declare function fitZoom(name: FormatName, room: {
|
|
88
|
+
width: number;
|
|
89
|
+
height: number;
|
|
90
|
+
}): number;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The fixed sizes brand collateral is made in. An artboard is designed at its
|
|
2
|
+
// real pixel size, so type and spacing are the values that ship, and
|
|
3
|
+
// `design-hub export` writes it at that size: PNG for anything posted, PDF for
|
|
4
|
+
// anything presented or printed. One list for the kit (the browser) and the
|
|
5
|
+
// export command (Node).
|
|
6
|
+
export const FORMATS = {
|
|
7
|
+
'post-square': { label: 'Post, square', width: 1080, height: 1080, output: 'png' },
|
|
8
|
+
'post-portrait': { label: 'Post, portrait', width: 1080, height: 1350, output: 'png' },
|
|
9
|
+
story: { label: 'Story', width: 1080, height: 1920, output: 'png', safe: { top: 250, bottom: 250 } },
|
|
10
|
+
'link-preview': { label: 'Link preview', width: 1200, height: 630, output: 'png' },
|
|
11
|
+
banner: { label: 'Banner', width: 1584, height: 396, output: 'png' },
|
|
12
|
+
slide: { label: 'Slide', width: 1920, height: 1080, output: 'pdf' },
|
|
13
|
+
a4: { label: 'A4', width: 794, height: 1123, output: 'pdf', page: '210mm 297mm' },
|
|
14
|
+
letter: { label: 'Letter', width: 816, height: 1056, output: 'pdf', page: '8.5in 11in' },
|
|
15
|
+
};
|
|
16
|
+
export const FORMAT_NAMES = Object.keys(FORMATS);
|
|
17
|
+
export const isFormat = (v) => typeof v === 'string' && v in FORMATS;
|
|
18
|
+
/** `1080 × 1350`, as a label under an artboard. */
|
|
19
|
+
export const sizeLabel = (name) => `${FORMATS[name].width} × ${FORMATS[name].height}`;
|
|
20
|
+
/** The CSS `@page` size a format prints at: its paper size, or its pixel size. */
|
|
21
|
+
export const pageSize = (name) => {
|
|
22
|
+
const f = FORMATS[name];
|
|
23
|
+
return f.page ?? `${f.width}px ${f.height}px`;
|
|
24
|
+
};
|
|
25
|
+
/** The screen scale for the Board's zoom dial: `fit`, or a percentage. */
|
|
26
|
+
export function zoomFor(zoom, name, room) {
|
|
27
|
+
if (zoom === 'fit')
|
|
28
|
+
return fitZoom(name, room);
|
|
29
|
+
const percent = Number.parseFloat(zoom);
|
|
30
|
+
return Number.isFinite(percent) && percent > 0 ? percent / 100 : 1;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* How large an artboard draws on screen: its real size at most, else what
|
|
34
|
+
* fits the board's width and a comfortable height. Export always draws at 1.
|
|
35
|
+
*/
|
|
36
|
+
export function fitZoom(name, room) {
|
|
37
|
+
const { width, height } = FORMATS[name];
|
|
38
|
+
return Math.min(1, room.width / width, room.height / height);
|
|
39
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export declare const TRACKS: readonly ['product', 'website', 'brand'];
|
|
2
|
+
export type Track = (typeof TRACKS)[number];
|
|
3
|
+
export declare const PLATFORMS: readonly ['webflow', 'payload', 'wordpress', 'emdash'];
|
|
4
|
+
export type Platform = (typeof PLATFORMS)[number];
|
|
5
|
+
export declare const FRONTENDS: readonly ['astro', 'next'];
|
|
6
|
+
export type Frontend = (typeof FRONTENDS)[number];
|
|
7
|
+
export type Website = {
|
|
8
|
+
platform: Platform;
|
|
9
|
+
/** The code that renders the site. Absent for Webflow, which renders its own pages. */
|
|
10
|
+
frontend?: Frontend;
|
|
11
|
+
/** Where the site is deployed, when the platform decides it. */
|
|
12
|
+
host?: string;
|
|
13
|
+
};
|
|
14
|
+
export type Project = {
|
|
15
|
+
tracks: Track[];
|
|
16
|
+
website?: Website;
|
|
17
|
+
};
|
|
18
|
+
export declare const TRACK_LABEL: Record<Track, string>;
|
|
19
|
+
/** The website settings a platform and an optional front end make, or the problem with them. */
|
|
20
|
+
export declare function websiteFor(platform: string, frontend?: string): {
|
|
21
|
+
website: Website;
|
|
22
|
+
} | {
|
|
23
|
+
problem: string;
|
|
24
|
+
};
|
|
25
|
+
/** A project from flags or a file: the tracks, and the website when the website track is on. */
|
|
26
|
+
export declare function makeProject(tracks: readonly string[], website?: {
|
|
27
|
+
platform?: string;
|
|
28
|
+
frontend?: string;
|
|
29
|
+
}): {
|
|
30
|
+
project: Project;
|
|
31
|
+
} | {
|
|
32
|
+
problems: string[];
|
|
33
|
+
};
|
|
34
|
+
/** project.json's contents, or the problems with them. A missing file is the caller's case, not this one's. */
|
|
35
|
+
export declare function parseProject(raw: unknown): {
|
|
36
|
+
project: Project;
|
|
37
|
+
} | {
|
|
38
|
+
problems: string[];
|
|
39
|
+
};
|
|
40
|
+
/** Whether the website track still needs its platform. Setup asks for it; the port refuses to start without it. */
|
|
41
|
+
export declare const needsPlatform: (p: Project) => boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The track a new prototype belongs to: the one asked for, else the
|
|
44
|
+
* project's only track. A project with no tracks recorded (made before
|
|
45
|
+
* tracks existed) takes any.
|
|
46
|
+
*/
|
|
47
|
+
export declare function trackFor(project: Project | null, asked?: string): {
|
|
48
|
+
track: Track | '';
|
|
49
|
+
} | {
|
|
50
|
+
problem: string;
|
|
51
|
+
};
|
|
52
|
+
/** The samples to remove when a project keeps only these tracks. */
|
|
53
|
+
export declare const samplesOutside: (tracks: readonly Track[]) => {
|
|
54
|
+
slug: string;
|
|
55
|
+
feature: string;
|
|
56
|
+
}[];
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// What kind of engagement a project is: its tracks, and for a website the
|
|
2
|
+
// platform it ships on. A studio works three ways, often at once: a product
|
|
3
|
+
// (designed here, handed to the client's engineers as a spec), a website
|
|
4
|
+
// (built on a CMS and a front end), and brand collateral (fixed-size
|
|
5
|
+
// templates: social posts, slides, print). The answer lives in
|
|
6
|
+
// .design-hub/project.json, so `new`, the hub, the port and every agent read
|
|
7
|
+
// one file instead of guessing from prose. Pure: callers read and write it.
|
|
8
|
+
export const TRACKS = ['product', 'website', 'brand'];
|
|
9
|
+
export const PLATFORMS = ['webflow', 'payload', 'wordpress', 'emdash'];
|
|
10
|
+
export const FRONTENDS = ['astro', 'next'];
|
|
11
|
+
export const TRACK_LABEL = {
|
|
12
|
+
product: 'Product',
|
|
13
|
+
website: 'Website',
|
|
14
|
+
brand: 'Brand',
|
|
15
|
+
};
|
|
16
|
+
const PLATFORM_LABEL = {
|
|
17
|
+
webflow: 'Webflow',
|
|
18
|
+
payload: 'Payload CMS',
|
|
19
|
+
wordpress: 'WordPress',
|
|
20
|
+
emdash: 'EmDash',
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* What each platform fixes. Webflow renders its own pages; EmDash is built
|
|
24
|
+
* on Astro and deployed to Cloudflare; Payload and WordPress are headless
|
|
25
|
+
* here, so the front end is a choice.
|
|
26
|
+
*/
|
|
27
|
+
const PLATFORM_RULES = {
|
|
28
|
+
webflow: { frontend: null, host: 'webflow' },
|
|
29
|
+
emdash: { frontend: 'astro', host: 'cloudflare' },
|
|
30
|
+
payload: { frontend: 'choose' },
|
|
31
|
+
wordpress: { frontend: 'choose' },
|
|
32
|
+
};
|
|
33
|
+
/** The sample prototype and walkthrough feature each track ships with, so setup can drop the ones a project does not use. */
|
|
34
|
+
const SAMPLES = {
|
|
35
|
+
product: { slug: 'sample-list', feature: 'sample' },
|
|
36
|
+
website: { slug: 'sample-page', feature: 'sample-site' },
|
|
37
|
+
brand: { slug: 'sample-social', feature: 'sample-brand' },
|
|
38
|
+
};
|
|
39
|
+
const isTrack = (v) => TRACKS.includes(v);
|
|
40
|
+
const isPlatform = (v) => PLATFORMS.includes(v);
|
|
41
|
+
const isFrontend = (v) => FRONTENDS.includes(v);
|
|
42
|
+
/** Tracks in their canonical order, without repeats. */
|
|
43
|
+
const ordered = (tracks) => TRACKS.filter((t) => tracks.includes(t));
|
|
44
|
+
/** The website settings a platform and an optional front end make, or the problem with them. */
|
|
45
|
+
export function websiteFor(platform, frontend) {
|
|
46
|
+
if (!isPlatform(platform))
|
|
47
|
+
return { problem: `The platform is one of: ${PLATFORMS.join(', ')}.` };
|
|
48
|
+
const rule = PLATFORM_RULES[platform];
|
|
49
|
+
const host = rule.host ? { host: rule.host } : {};
|
|
50
|
+
if (rule.frontend === 'choose') {
|
|
51
|
+
if (!frontend)
|
|
52
|
+
return { problem: `${PLATFORM_LABEL[platform]} needs a front end: ${FRONTENDS.join(' or ')}.` };
|
|
53
|
+
if (!isFrontend(frontend))
|
|
54
|
+
return { problem: `The front end is one of: ${FRONTENDS.join(', ')}.` };
|
|
55
|
+
return { website: { platform, frontend, ...host } };
|
|
56
|
+
}
|
|
57
|
+
if (frontend && frontend !== rule.frontend)
|
|
58
|
+
return {
|
|
59
|
+
problem: rule.frontend
|
|
60
|
+
? `${PLATFORM_LABEL[platform]} always uses ${rule.frontend}.`
|
|
61
|
+
: `${PLATFORM_LABEL[platform]} renders its own pages; it takes no front end.`,
|
|
62
|
+
};
|
|
63
|
+
return { website: { platform, ...(rule.frontend ? { frontend: rule.frontend } : {}), ...host } };
|
|
64
|
+
}
|
|
65
|
+
/** A project from flags or a file: the tracks, and the website when the website track is on. */
|
|
66
|
+
export function makeProject(tracks, website) {
|
|
67
|
+
const problems = [];
|
|
68
|
+
const unknown = tracks.filter((t) => !isTrack(t));
|
|
69
|
+
if (unknown.length)
|
|
70
|
+
problems.push(`Unknown track: ${unknown.join(', ')}. Tracks: ${TRACKS.join(', ')}.`);
|
|
71
|
+
const chosen = ordered(tracks.filter(isTrack));
|
|
72
|
+
if (!chosen.length && !unknown.length)
|
|
73
|
+
problems.push(`Pick at least one track: ${TRACKS.join(', ')}.`);
|
|
74
|
+
const project = { tracks: chosen };
|
|
75
|
+
if (chosen.includes('website') && website?.platform) {
|
|
76
|
+
const made = websiteFor(website.platform, website.frontend);
|
|
77
|
+
if ('problem' in made)
|
|
78
|
+
problems.push(made.problem);
|
|
79
|
+
else
|
|
80
|
+
project.website = made.website;
|
|
81
|
+
}
|
|
82
|
+
else if (!chosen.includes('website') && (website?.platform || website?.frontend)) {
|
|
83
|
+
problems.push('A platform or front end needs the website track.');
|
|
84
|
+
}
|
|
85
|
+
return problems.length ? { problems } : { project };
|
|
86
|
+
}
|
|
87
|
+
/** project.json's contents, or the problems with them. A missing file is the caller's case, not this one's. */
|
|
88
|
+
export function parseProject(raw) {
|
|
89
|
+
if (!raw || typeof raw !== 'object')
|
|
90
|
+
return { problems: ['project.json is not an object.'] };
|
|
91
|
+
const { tracks, website } = raw;
|
|
92
|
+
if (!Array.isArray(tracks))
|
|
93
|
+
return { problems: ['project.json has no tracks list.'] };
|
|
94
|
+
return makeProject(tracks.map(String), {
|
|
95
|
+
platform: website?.platform ? String(website.platform) : undefined,
|
|
96
|
+
frontend: website?.frontend ? String(website.frontend) : undefined,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** Whether the website track still needs its platform. Setup asks for it; the port refuses to start without it. */
|
|
100
|
+
export const needsPlatform = (p) => p.tracks.includes('website') && !p.website;
|
|
101
|
+
/**
|
|
102
|
+
* The track a new prototype belongs to: the one asked for, else the
|
|
103
|
+
* project's only track. A project with no tracks recorded (made before
|
|
104
|
+
* tracks existed) takes any.
|
|
105
|
+
*/
|
|
106
|
+
export function trackFor(project, asked) {
|
|
107
|
+
if (asked && !isTrack(asked))
|
|
108
|
+
return { problem: `--track is one of: ${TRACKS.join(', ')}.` };
|
|
109
|
+
if (!project)
|
|
110
|
+
return { track: asked ?? '' };
|
|
111
|
+
if (asked) {
|
|
112
|
+
return project.tracks.includes(asked)
|
|
113
|
+
? { track: asked }
|
|
114
|
+
: { problem: `This project has no ${asked} track. It has: ${project.tracks.join(', ')}.` };
|
|
115
|
+
}
|
|
116
|
+
if (project.tracks.length === 1)
|
|
117
|
+
return { track: project.tracks[0] };
|
|
118
|
+
return { problem: `This project has several tracks; name one with --track ${project.tracks.join('|')}.` };
|
|
119
|
+
}
|
|
120
|
+
/** The samples to remove when a project keeps only these tracks. */
|
|
121
|
+
export const samplesOutside = (tracks) => TRACKS.filter((t) => !tracks.includes(t)).map((t) => SAMPLES[t]);
|
package/dist/scripts/cli.js
CHANGED
|
@@ -13,6 +13,7 @@ import * as commands from './commands.js';
|
|
|
13
13
|
import * as comments from './comments.js';
|
|
14
14
|
import * as dev from './dev.js';
|
|
15
15
|
import * as elyx from './elyx.js';
|
|
16
|
+
import * as exportCmd from './export.js';
|
|
16
17
|
import * as fixturesPull from './fixtures-pull.js';
|
|
17
18
|
import * as guard from './guard.js';
|
|
18
19
|
import { HOOKS, runHook } from './hook.js';
|
|
@@ -23,6 +24,7 @@ import * as preflight from './preflight.js';
|
|
|
23
24
|
import * as releaseCheck from './release-check.js';
|
|
24
25
|
import * as skillHint from './skill-hint.js';
|
|
25
26
|
import * as skills from './skills.js';
|
|
27
|
+
import * as tracks from './tracks.js';
|
|
26
28
|
import * as visualMatch from './visual-match.js';
|
|
27
29
|
// `guide` is `new --guide`; `open` is `dev` with the browser.
|
|
28
30
|
const COMMANDS = {
|
|
@@ -45,7 +47,9 @@ const COMMANDS = {
|
|
|
45
47
|
'fixtures-pull': fixturesPull,
|
|
46
48
|
'visual-match': visualMatch,
|
|
47
49
|
elyx,
|
|
50
|
+
export: exportCmd,
|
|
48
51
|
skills,
|
|
52
|
+
tracks,
|
|
49
53
|
'skill-hint': skillHint,
|
|
50
54
|
'release-check': releaseCheck,
|
|
51
55
|
commands,
|
package/dist/scripts/commands.js
CHANGED
|
@@ -9,6 +9,7 @@ import * as check from './check.js';
|
|
|
9
9
|
import * as comments from './comments.js';
|
|
10
10
|
import * as dev from './dev.js';
|
|
11
11
|
import * as elyx from './elyx.js';
|
|
12
|
+
import * as exportCmd from './export.js';
|
|
12
13
|
import * as fixturesPull from './fixtures-pull.js';
|
|
13
14
|
import * as guard from './guard.js';
|
|
14
15
|
import { schema } from './lib/cli.js';
|
|
@@ -18,6 +19,7 @@ import * as preflight from './preflight.js';
|
|
|
18
19
|
import * as releaseCheck from './release-check.js';
|
|
19
20
|
import * as skillHint from './skill-hint.js';
|
|
20
21
|
import * as skills from './skills.js';
|
|
22
|
+
import * as tracks from './tracks.js';
|
|
21
23
|
import * as visualMatch from './visual-match.js';
|
|
22
24
|
export const spec = {
|
|
23
25
|
name: 'commands',
|
|
@@ -41,7 +43,9 @@ export const COMMANDS = [
|
|
|
41
43
|
fixturesPull.spec,
|
|
42
44
|
visualMatch.spec,
|
|
43
45
|
elyx.spec,
|
|
46
|
+
exportCmd.spec,
|
|
44
47
|
skills.spec,
|
|
48
|
+
tracks.spec,
|
|
45
49
|
skillHint.spec,
|
|
46
50
|
releaseCheck.spec,
|
|
47
51
|
spec,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type Args } from './lib/cli.ts';
|
|
3
|
+
export declare const spec: {
|
|
4
|
+
readonly name: 'export';
|
|
5
|
+
readonly summary: "Write a prototype's artboards to files: PNG for posts, PDF for slides and print.";
|
|
6
|
+
readonly positionals: [{
|
|
7
|
+
readonly name: 'prototype';
|
|
8
|
+
readonly description: 'The prototype folder, e.g. sample-social';
|
|
9
|
+
readonly required: true;
|
|
10
|
+
}];
|
|
11
|
+
readonly flags: {
|
|
12
|
+
readonly as: {
|
|
13
|
+
readonly type: 'string';
|
|
14
|
+
readonly choices: readonly ["auto", "png", "pdf"];
|
|
15
|
+
readonly default: 'auto';
|
|
16
|
+
readonly description: "auto: each artboard's format decides. png: every artboard a PNG. pdf: every artboard into one PDF.";
|
|
17
|
+
};
|
|
18
|
+
readonly scale: {
|
|
19
|
+
readonly type: 'number';
|
|
20
|
+
readonly default: 1;
|
|
21
|
+
readonly description: 'PNG pixel density: 1 for the exact size platforms ask for, 2 for retina.';
|
|
22
|
+
};
|
|
23
|
+
readonly dials: {
|
|
24
|
+
readonly type: 'string';
|
|
25
|
+
readonly description: 'The dial state to export, as JSON by panel: {"Review":{"fixtures":"longest"}}. Default: the committed defaults.';
|
|
26
|
+
};
|
|
27
|
+
readonly theme: {
|
|
28
|
+
readonly type: 'string';
|
|
29
|
+
readonly choices: readonly ["light", "dark"];
|
|
30
|
+
readonly description: 'The theme, when the artboards follow it.';
|
|
31
|
+
};
|
|
32
|
+
readonly out: {
|
|
33
|
+
readonly type: 'string';
|
|
34
|
+
readonly description: 'Folder for the files. Default: exports/<prototype>/.';
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
readonly output: '{ ok, files: [path], artboards }';
|
|
38
|
+
};
|
|
39
|
+
export declare function main({ positionals: [slug], flags }: Args<typeof spec>): Promise<{
|
|
40
|
+
files: string[];
|
|
41
|
+
artboards: number;
|
|
42
|
+
}>;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
// `design-hub export <slug>`: a brand prototype's artboards as files. It
|
|
4
|
+
// starts the hub on a free port, opens the prototype in headless Chrome with
|
|
5
|
+
// ?export (artboards only, real size, motion off), waits for fonts and
|
|
6
|
+
// images, then writes each artboard as a PNG or the presented ones into one
|
|
7
|
+
// PDF. The format decides by default: posts, stories, previews and banners
|
|
8
|
+
// are PNG; slides and print are PDF.
|
|
9
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { join, relative, resolve } from 'node:path';
|
|
11
|
+
import { createServer } from 'vite';
|
|
12
|
+
import { chromeCandidates, exportUrl, fileNames, pdfFiles, plan, } from '../core/export.js';
|
|
13
|
+
import { PROTOTYPES, ROOT } from '../server/paths.js';
|
|
14
|
+
import { hubConfig } from '../vite/plugin.js';
|
|
15
|
+
import { launch, within } from './lib/cdp.js';
|
|
16
|
+
import { CliError } from './lib/cli.js';
|
|
17
|
+
export const spec = {
|
|
18
|
+
name: 'export',
|
|
19
|
+
summary: "Write a prototype's artboards to files: PNG for posts, PDF for slides and print.",
|
|
20
|
+
positionals: [
|
|
21
|
+
{ name: 'prototype', description: 'The prototype folder, e.g. sample-social', required: true },
|
|
22
|
+
],
|
|
23
|
+
flags: {
|
|
24
|
+
as: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
choices: ['auto', 'png', 'pdf'],
|
|
27
|
+
default: 'auto',
|
|
28
|
+
description: "auto: each artboard's format decides. png: every artboard a PNG. pdf: every artboard into one PDF.",
|
|
29
|
+
},
|
|
30
|
+
scale: {
|
|
31
|
+
type: 'number',
|
|
32
|
+
default: 1,
|
|
33
|
+
description: 'PNG pixel density: 1 for the exact size platforms ask for, 2 for retina.',
|
|
34
|
+
},
|
|
35
|
+
dials: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
description: 'The dial state to export, as JSON by panel: {"Review":{"fixtures":"longest"}}. Default: the committed defaults.',
|
|
38
|
+
},
|
|
39
|
+
theme: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
choices: ['light', 'dark'],
|
|
42
|
+
description: 'The theme, when the artboards follow it.',
|
|
43
|
+
},
|
|
44
|
+
out: { type: 'string', description: 'Folder for the files. Default: exports/<prototype>/.' },
|
|
45
|
+
},
|
|
46
|
+
output: '{ ok, files: [path], artboards }',
|
|
47
|
+
};
|
|
48
|
+
const READY_TIMEOUT_MS = 30_000;
|
|
49
|
+
const POLL_MS = 150;
|
|
50
|
+
const SERVER_CLOSE_MS = 5_000;
|
|
51
|
+
const VIEWPORT = { width: 2400, height: 1600 };
|
|
52
|
+
const READ_BOARDS = `JSON.stringify([...document.querySelectorAll('[data-artboard]')].map((el, index) => {
|
|
53
|
+
el.dataset.index = String(index);
|
|
54
|
+
const r = el.getBoundingClientRect();
|
|
55
|
+
return { index, name: el.dataset.name, format: el.dataset.format, output: el.dataset.output,
|
|
56
|
+
x: r.x + scrollX, y: r.y + scrollY, width: r.width, height: r.height };
|
|
57
|
+
}))`;
|
|
58
|
+
function findChrome() {
|
|
59
|
+
const found = chromeCandidates(process.platform, process.env).find((p) => existsSync(p));
|
|
60
|
+
if (!found)
|
|
61
|
+
throw new CliError('No Chrome, Chromium or Edge found to render the artboards.', {
|
|
62
|
+
fix: 'Install Google Chrome, or set CHROME_PATH to a Chromium-based browser.',
|
|
63
|
+
});
|
|
64
|
+
return found;
|
|
65
|
+
}
|
|
66
|
+
/** The hub on a free port, for this one export. */
|
|
67
|
+
async function startHub() {
|
|
68
|
+
const config = hubConfig({ root: ROOT }, 'build');
|
|
69
|
+
const server = await createServer({
|
|
70
|
+
...config,
|
|
71
|
+
logLevel: 'error',
|
|
72
|
+
// One page, read once: no file watcher, no hot reload, so nothing outlives the export.
|
|
73
|
+
server: { ...config.server, port: 0, strictPort: false, hmr: false, open: false, watch: null, ws: false },
|
|
74
|
+
});
|
|
75
|
+
await server.listen();
|
|
76
|
+
const address = server.httpServer?.address();
|
|
77
|
+
if (!address)
|
|
78
|
+
throw new CliError('The hub did not start for export.');
|
|
79
|
+
return { server, origin: `http://localhost:${address.port}` };
|
|
80
|
+
}
|
|
81
|
+
async function evaluate(browser, expression) {
|
|
82
|
+
const { result } = await browser.send('Runtime.evaluate', {
|
|
83
|
+
expression,
|
|
84
|
+
returnByValue: true,
|
|
85
|
+
awaitPromise: true,
|
|
86
|
+
});
|
|
87
|
+
return result.value;
|
|
88
|
+
}
|
|
89
|
+
async function waitReady(browser) {
|
|
90
|
+
const until = Date.now() + READY_TIMEOUT_MS;
|
|
91
|
+
while (Date.now() < until) {
|
|
92
|
+
if (await evaluate(browser, "document.documentElement.dataset.exportReady === '1'"))
|
|
93
|
+
return;
|
|
94
|
+
await new Promise((r) => setTimeout(r, POLL_MS));
|
|
95
|
+
}
|
|
96
|
+
throw new CliError('The prototype never finished loading for export.', {
|
|
97
|
+
errors: browser.errors.slice(0, 5),
|
|
98
|
+
fix: 'Open the prototype with ?export in the browser and fix what its console says. Export needs a Board from @kit/Artboard.',
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/** One PNG per artboard; the page's device scale (--scale) sets the pixel density. */
|
|
102
|
+
async function writePngs(browser, slug, boards, dir) {
|
|
103
|
+
const names = fileNames(slug, boards, 'png');
|
|
104
|
+
const files = [];
|
|
105
|
+
for (const [i, b] of boards.entries()) {
|
|
106
|
+
const { data } = await browser.send('Page.captureScreenshot', {
|
|
107
|
+
format: 'png',
|
|
108
|
+
clip: { x: b.x, y: b.y, width: b.width, height: b.height, scale: 1 },
|
|
109
|
+
captureBeyondViewport: true,
|
|
110
|
+
});
|
|
111
|
+
const file = join(dir, names[i]);
|
|
112
|
+
writeFileSync(file, Buffer.from(data, 'base64'));
|
|
113
|
+
files.push(file);
|
|
114
|
+
}
|
|
115
|
+
return files;
|
|
116
|
+
}
|
|
117
|
+
/** One PDF per format: every other artboard is hidden while that format prints. */
|
|
118
|
+
async function writePdfs(browser, slug, boards, dir) {
|
|
119
|
+
// Print lays out at each page's own size; the screenshot viewport would shrink it.
|
|
120
|
+
await browser.send('Emulation.clearDeviceMetricsOverride');
|
|
121
|
+
const files = [];
|
|
122
|
+
for (const { format, file } of pdfFiles(slug, boards)) {
|
|
123
|
+
await evaluate(browser, `document.querySelectorAll('[data-artboard]').forEach((el) => {
|
|
124
|
+
el.style.display = el.dataset.format === ${JSON.stringify(format)} && ${JSON.stringify(boards.map((b) => b.index))}.includes(Number(el.dataset.index)) ? '' : 'none';
|
|
125
|
+
})`);
|
|
126
|
+
const { data } = await browser.send('Page.printToPDF', {
|
|
127
|
+
printBackground: true,
|
|
128
|
+
preferCSSPageSize: true,
|
|
129
|
+
marginTop: 0,
|
|
130
|
+
marginBottom: 0,
|
|
131
|
+
marginLeft: 0,
|
|
132
|
+
marginRight: 0,
|
|
133
|
+
});
|
|
134
|
+
const path = join(dir, file);
|
|
135
|
+
writeFileSync(path, Buffer.from(data, 'base64'));
|
|
136
|
+
files.push(path);
|
|
137
|
+
}
|
|
138
|
+
return files;
|
|
139
|
+
}
|
|
140
|
+
function checkFlags(slug, flags) {
|
|
141
|
+
if (!existsSync(join(PROTOTYPES, slug, 'index.tsx')))
|
|
142
|
+
throw new CliError(`No prototype called ${slug}.`);
|
|
143
|
+
if (!(flags.scale >= 1 && flags.scale <= 4))
|
|
144
|
+
throw new CliError('--scale takes 1 to 4.');
|
|
145
|
+
if (flags.dials) {
|
|
146
|
+
try {
|
|
147
|
+
JSON.parse(flags.dials);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
throw new CliError('--dials takes JSON by panel name.', {
|
|
151
|
+
example: '{"Review":{"fixtures":"longest"}}',
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async function render(browser, url, flags, slug, dir) {
|
|
157
|
+
await browser.send('Emulation.setDeviceMetricsOverride', {
|
|
158
|
+
...VIEWPORT,
|
|
159
|
+
deviceScaleFactor: flags.scale,
|
|
160
|
+
mobile: false,
|
|
161
|
+
});
|
|
162
|
+
await browser.send('Page.navigate', { url });
|
|
163
|
+
await waitReady(browser);
|
|
164
|
+
const boards = JSON.parse(await evaluate(browser, READ_BOARDS));
|
|
165
|
+
if (!boards.length)
|
|
166
|
+
throw new CliError(`${slug} has no artboards to export.`, {
|
|
167
|
+
fix: 'Export reads <Artboard> elements inside a <Board> from @kit/Artboard.',
|
|
168
|
+
});
|
|
169
|
+
const { png, pdf } = plan(boards, flags.as);
|
|
170
|
+
mkdirSync(dir, { recursive: true });
|
|
171
|
+
const files = await writePngs(browser, slug, png, dir);
|
|
172
|
+
if (pdf.length)
|
|
173
|
+
files.push(...(await writePdfs(browser, slug, pdf, dir)));
|
|
174
|
+
return { files, artboards: boards.length };
|
|
175
|
+
}
|
|
176
|
+
export async function main({ positionals: [slug], flags }) {
|
|
177
|
+
checkFlags(slug, flags);
|
|
178
|
+
const chrome = findChrome();
|
|
179
|
+
const dir = resolve(ROOT, flags.out ?? join('exports', slug));
|
|
180
|
+
const { server, origin } = await startHub();
|
|
181
|
+
let browser;
|
|
182
|
+
try {
|
|
183
|
+
browser = await launch(chrome);
|
|
184
|
+
const url = exportUrl(origin, slug, { dials: flags.dials, theme: flags.theme });
|
|
185
|
+
const { files, artboards } = await render(browser, url, flags, slug, dir);
|
|
186
|
+
return { files: files.map((f) => relative(ROOT, f)), artboards };
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
await browser?.close();
|
|
190
|
+
await within(server.close(), SERVER_CLOSE_MS);
|
|
191
|
+
}
|
|
192
|
+
}
|