@milesroxas/design-hub 0.2.0 → 0.4.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 +1 -1
- 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/bootstrap.d.ts +1 -1
- package/dist/scripts/bootstrap.js +38 -6
- package/dist/scripts/cli.js +8 -0
- package/dist/scripts/commands.js +8 -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/lib/deploy.d.ts +76 -0
- package/dist/scripts/lib/deploy.js +94 -0
- package/dist/scripts/lib/vercel.d.ts +5 -0
- package/dist/scripts/lib/vercel.js +24 -0
- package/dist/scripts/new.d.ts +13 -1
- package/dist/scripts/new.js +43 -4
- package/dist/scripts/share.d.ts +63 -0
- package/dist/scripts/share.js +181 -0
- package/dist/scripts/signin.d.ts +31 -0
- package/dist/scripts/signin.js +119 -0
- 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-deploy/SKILL.md +26 -19
- package/skills/miles-design-hub/SKILL.md +77 -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,71 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { test } from 'node:test';
|
|
3
|
+
import { makeProject, needsPlatform, parseProject, samplesOutside, trackFor, websiteFor } from './project.ts';
|
|
4
|
+
|
|
5
|
+
test('a platform fixes or asks for its front end', () => {
|
|
6
|
+
assert.deepEqual(websiteFor('webflow'), { website: { platform: 'webflow', host: 'webflow' } });
|
|
7
|
+
assert.deepEqual(websiteFor('emdash'), {
|
|
8
|
+
website: { platform: 'emdash', frontend: 'astro', host: 'cloudflare' },
|
|
9
|
+
});
|
|
10
|
+
assert.deepEqual(websiteFor('payload', 'next'), { website: { platform: 'payload', frontend: 'next' } });
|
|
11
|
+
assert.match((websiteFor('wordpress') as { problem: string }).problem, /needs a front end/);
|
|
12
|
+
assert.match((websiteFor('payload', 'vue') as { problem: string }).problem, /front end is one of/);
|
|
13
|
+
assert.match((websiteFor('emdash', 'next') as { problem: string }).problem, /always uses astro/);
|
|
14
|
+
assert.match((websiteFor('webflow', 'astro') as { problem: string }).problem, /renders its own pages/);
|
|
15
|
+
assert.match((websiteFor('squarespace') as { problem: string }).problem, /platform is one of/);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('tracks come back in order, once each, with the website only on the website track', () => {
|
|
19
|
+
assert.deepEqual(makeProject(['brand', 'product', 'brand']), { project: { tracks: ['product', 'brand'] } });
|
|
20
|
+
assert.deepEqual(makeProject(['website'], { platform: 'emdash' }), {
|
|
21
|
+
project: { tracks: ['website'], website: { platform: 'emdash', frontend: 'astro', host: 'cloudflare' } },
|
|
22
|
+
});
|
|
23
|
+
assert.deepEqual(makeProject(['website']), { project: { tracks: ['website'] } });
|
|
24
|
+
assert.match(
|
|
25
|
+
(makeProject(['product'], { platform: 'webflow' }) as { problems: string[] }).problems[0],
|
|
26
|
+
/needs the website track/,
|
|
27
|
+
);
|
|
28
|
+
assert.match((makeProject([]) as { problems: string[] }).problems[0], /at least one track/);
|
|
29
|
+
assert.match((makeProject(['app']) as { problems: string[] }).problems[0], /Unknown track: app/);
|
|
30
|
+
assert.match(
|
|
31
|
+
(makeProject(['website'], { platform: 'payload' }) as { problems: string[] }).problems[0],
|
|
32
|
+
/needs a front end/,
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('project.json is read through the same rules', () => {
|
|
37
|
+
assert.deepEqual(
|
|
38
|
+
parseProject({ tracks: ['website'], website: { platform: 'payload', frontend: 'astro' } }),
|
|
39
|
+
{
|
|
40
|
+
project: { tracks: ['website'], website: { platform: 'payload', frontend: 'astro' } },
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
assert.match((parseProject(null) as { problems: string[] }).problems[0], /not an object/);
|
|
44
|
+
assert.match((parseProject({}) as { problems: string[] }).problems[0], /no tracks list/);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('the website track without a platform still needs one', () => {
|
|
48
|
+
assert.equal(needsPlatform({ tracks: ['website'] }), true);
|
|
49
|
+
assert.equal(needsPlatform({ tracks: ['website'], website: { platform: 'webflow' } }), false);
|
|
50
|
+
assert.equal(needsPlatform({ tracks: ['product'] }), false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a new prototype takes the project's only track, or the one named", () => {
|
|
54
|
+
const one = { tracks: ['website' as const] };
|
|
55
|
+
const many = { tracks: ['product' as const, 'brand' as const] };
|
|
56
|
+
assert.deepEqual(trackFor(one), { track: 'website' });
|
|
57
|
+
assert.deepEqual(trackFor(many, 'brand'), { track: 'brand' });
|
|
58
|
+
assert.match((trackFor(many) as { problem: string }).problem, /several tracks/);
|
|
59
|
+
assert.match((trackFor(one, 'brand') as { problem: string }).problem, /no brand track/);
|
|
60
|
+
assert.match((trackFor(one, 'app') as { problem: string }).problem, /--track is one of/);
|
|
61
|
+
assert.deepEqual(trackFor(null), { track: '' });
|
|
62
|
+
assert.deepEqual(trackFor(null, 'product'), { track: 'product' });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('the samples outside the kept tracks', () => {
|
|
66
|
+
assert.deepEqual(
|
|
67
|
+
samplesOutside(['website']).map((s) => s.slug),
|
|
68
|
+
['sample-list', 'sample-social'],
|
|
69
|
+
);
|
|
70
|
+
assert.deepEqual(samplesOutside(['product', 'website', 'brand']), []);
|
|
71
|
+
});
|
package/core/project.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
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
|
+
|
|
9
|
+
export const TRACKS = ['product', 'website', 'brand'] as const;
|
|
10
|
+
export type Track = (typeof TRACKS)[number];
|
|
11
|
+
|
|
12
|
+
export const PLATFORMS = ['webflow', 'payload', 'wordpress', 'emdash'] as const;
|
|
13
|
+
export type Platform = (typeof PLATFORMS)[number];
|
|
14
|
+
|
|
15
|
+
export const FRONTENDS = ['astro', 'next'] as const;
|
|
16
|
+
export type Frontend = (typeof FRONTENDS)[number];
|
|
17
|
+
|
|
18
|
+
export type Website = {
|
|
19
|
+
platform: Platform;
|
|
20
|
+
/** The code that renders the site. Absent for Webflow, which renders its own pages. */
|
|
21
|
+
frontend?: Frontend;
|
|
22
|
+
/** Where the site is deployed, when the platform decides it. */
|
|
23
|
+
host?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type Project = { tracks: Track[]; website?: Website };
|
|
27
|
+
|
|
28
|
+
export const TRACK_LABEL: Record<Track, string> = {
|
|
29
|
+
product: 'Product',
|
|
30
|
+
website: 'Website',
|
|
31
|
+
brand: 'Brand',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const PLATFORM_LABEL: Record<Platform, string> = {
|
|
35
|
+
webflow: 'Webflow',
|
|
36
|
+
payload: 'Payload CMS',
|
|
37
|
+
wordpress: 'WordPress',
|
|
38
|
+
emdash: 'EmDash',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What each platform fixes. Webflow renders its own pages; EmDash is built
|
|
43
|
+
* on Astro and deployed to Cloudflare; Payload and WordPress are headless
|
|
44
|
+
* here, so the front end is a choice.
|
|
45
|
+
*/
|
|
46
|
+
const PLATFORM_RULES: Record<Platform, { frontend: Frontend | null | 'choose'; host?: string }> = {
|
|
47
|
+
webflow: { frontend: null, host: 'webflow' },
|
|
48
|
+
emdash: { frontend: 'astro', host: 'cloudflare' },
|
|
49
|
+
payload: { frontend: 'choose' },
|
|
50
|
+
wordpress: { frontend: 'choose' },
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** The sample prototype and walkthrough feature each track ships with, so setup can drop the ones a project does not use. */
|
|
54
|
+
const SAMPLES: Record<Track, { slug: string; feature: string }> = {
|
|
55
|
+
product: { slug: 'sample-list', feature: 'sample' },
|
|
56
|
+
website: { slug: 'sample-page', feature: 'sample-site' },
|
|
57
|
+
brand: { slug: 'sample-social', feature: 'sample-brand' },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const isTrack = (v: unknown): v is Track => TRACKS.includes(v as Track);
|
|
61
|
+
const isPlatform = (v: unknown): v is Platform => PLATFORMS.includes(v as Platform);
|
|
62
|
+
const isFrontend = (v: unknown): v is Frontend => FRONTENDS.includes(v as Frontend);
|
|
63
|
+
|
|
64
|
+
/** Tracks in their canonical order, without repeats. */
|
|
65
|
+
const ordered = (tracks: Track[]): Track[] => TRACKS.filter((t) => tracks.includes(t));
|
|
66
|
+
|
|
67
|
+
/** The website settings a platform and an optional front end make, or the problem with them. */
|
|
68
|
+
export function websiteFor(platform: string, frontend?: string): { website: Website } | { problem: string } {
|
|
69
|
+
if (!isPlatform(platform)) return { problem: `The platform is one of: ${PLATFORMS.join(', ')}.` };
|
|
70
|
+
const rule = PLATFORM_RULES[platform];
|
|
71
|
+
const host = rule.host ? { host: rule.host } : {};
|
|
72
|
+
if (rule.frontend === 'choose') {
|
|
73
|
+
if (!frontend)
|
|
74
|
+
return { problem: `${PLATFORM_LABEL[platform]} needs a front end: ${FRONTENDS.join(' or ')}.` };
|
|
75
|
+
if (!isFrontend(frontend)) return { problem: `The front end is one of: ${FRONTENDS.join(', ')}.` };
|
|
76
|
+
return { website: { platform, frontend, ...host } };
|
|
77
|
+
}
|
|
78
|
+
if (frontend && frontend !== rule.frontend)
|
|
79
|
+
return {
|
|
80
|
+
problem: rule.frontend
|
|
81
|
+
? `${PLATFORM_LABEL[platform]} always uses ${rule.frontend}.`
|
|
82
|
+
: `${PLATFORM_LABEL[platform]} renders its own pages; it takes no front end.`,
|
|
83
|
+
};
|
|
84
|
+
return { website: { platform, ...(rule.frontend ? { frontend: rule.frontend } : {}), ...host } };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A project from flags or a file: the tracks, and the website when the website track is on. */
|
|
88
|
+
export function makeProject(
|
|
89
|
+
tracks: readonly string[],
|
|
90
|
+
website?: { platform?: string; frontend?: string },
|
|
91
|
+
): { project: Project } | { problems: string[] } {
|
|
92
|
+
const problems: string[] = [];
|
|
93
|
+
const unknown = tracks.filter((t) => !isTrack(t));
|
|
94
|
+
if (unknown.length) problems.push(`Unknown track: ${unknown.join(', ')}. Tracks: ${TRACKS.join(', ')}.`);
|
|
95
|
+
const chosen = ordered(tracks.filter(isTrack));
|
|
96
|
+
if (!chosen.length && !unknown.length) problems.push(`Pick at least one track: ${TRACKS.join(', ')}.`);
|
|
97
|
+
const project: Project = { tracks: chosen };
|
|
98
|
+
if (chosen.includes('website') && website?.platform) {
|
|
99
|
+
const made = websiteFor(website.platform, website.frontend);
|
|
100
|
+
if ('problem' in made) problems.push(made.problem);
|
|
101
|
+
else project.website = made.website;
|
|
102
|
+
} else if (!chosen.includes('website') && (website?.platform || website?.frontend)) {
|
|
103
|
+
problems.push('A platform or front end needs the website track.');
|
|
104
|
+
}
|
|
105
|
+
return problems.length ? { problems } : { project };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** project.json's contents, or the problems with them. A missing file is the caller's case, not this one's. */
|
|
109
|
+
export function parseProject(raw: unknown): { project: Project } | { problems: string[] } {
|
|
110
|
+
if (!raw || typeof raw !== 'object') return { problems: ['project.json is not an object.'] };
|
|
111
|
+
const { tracks, website } = raw as {
|
|
112
|
+
tracks?: unknown;
|
|
113
|
+
website?: { platform?: unknown; frontend?: unknown };
|
|
114
|
+
};
|
|
115
|
+
if (!Array.isArray(tracks)) return { problems: ['project.json has no tracks list.'] };
|
|
116
|
+
return makeProject(tracks.map(String), {
|
|
117
|
+
platform: website?.platform ? String(website.platform) : undefined,
|
|
118
|
+
frontend: website?.frontend ? String(website.frontend) : undefined,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Whether the website track still needs its platform. Setup asks for it; the port refuses to start without it. */
|
|
123
|
+
export const needsPlatform = (p: Project) => p.tracks.includes('website') && !p.website;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The track a new prototype belongs to: the one asked for, else the
|
|
127
|
+
* project's only track. A project with no tracks recorded (made before
|
|
128
|
+
* tracks existed) takes any.
|
|
129
|
+
*/
|
|
130
|
+
export function trackFor(
|
|
131
|
+
project: Project | null,
|
|
132
|
+
asked?: string,
|
|
133
|
+
): { track: Track | '' } | { problem: string } {
|
|
134
|
+
if (asked && !isTrack(asked)) return { problem: `--track is one of: ${TRACKS.join(', ')}.` };
|
|
135
|
+
if (!project) return { track: (asked as Track | undefined) ?? '' };
|
|
136
|
+
if (asked) {
|
|
137
|
+
return project.tracks.includes(asked as Track)
|
|
138
|
+
? { track: asked as Track }
|
|
139
|
+
: { problem: `This project has no ${asked} track. It has: ${project.tracks.join(', ')}.` };
|
|
140
|
+
}
|
|
141
|
+
if (project.tracks.length === 1) return { track: project.tracks[0] };
|
|
142
|
+
return { problem: `This project has several tracks; name one with --track ${project.tracks.join('|')}.` };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The samples to remove when a project keeps only these tracks. */
|
|
146
|
+
export const samplesOutside = (tracks: readonly Track[]) =>
|
|
147
|
+
TRACKS.filter((t) => !tracks.includes(t)).map((t) => SAMPLES[t]);
|
package/dist/core/catalog.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Track } from './project.ts';
|
|
1
2
|
import { type State } from './states.ts';
|
|
2
3
|
export type Kind = 'sprint' | 'alternate' | 'exploration';
|
|
3
4
|
export type Meta = {
|
|
@@ -5,6 +6,8 @@ export type Meta = {
|
|
|
5
6
|
title: string;
|
|
6
7
|
summary: string;
|
|
7
8
|
context: string;
|
|
9
|
+
/** The track it belongs to; empty in a project made before tracks. */
|
|
10
|
+
track: Track | '';
|
|
8
11
|
status: string;
|
|
9
12
|
kind: Kind;
|
|
10
13
|
feature: string;
|
|
@@ -15,3 +18,5 @@ export type Meta = {
|
|
|
15
18
|
export declare function readMeta(slug: string, readme: string): Meta;
|
|
16
19
|
export declare const isLive: (m: Pick<Meta, 'status'>) => boolean;
|
|
17
20
|
export declare const byIndexOrder: (a: Meta, b: Meta) => number;
|
|
21
|
+
/** Where a prototype stands, in words: the Space, the shell its track draws, or a board of artboards. */
|
|
22
|
+
export declare function placeLabel(m: Pick<Meta, 'context' | 'track'>): string;
|
package/dist/core/catalog.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// (shell/registry.ts) and the Node scripts (scripts/lib/catalog.ts), so
|
|
4
4
|
// they never disagree about what a prototype is.
|
|
5
5
|
import { frontmatter } from './frontmatter.js';
|
|
6
|
+
import { TRACKS } from './project.js';
|
|
6
7
|
import { parseStates } from './states.js';
|
|
7
8
|
export function readMeta(slug, readme) {
|
|
8
9
|
const { data } = frontmatter(readme);
|
|
@@ -11,6 +12,7 @@ export function readMeta(slug, readme) {
|
|
|
11
12
|
title: data.title || slug,
|
|
12
13
|
summary: data.summary || '',
|
|
13
14
|
context: data.context || 'space',
|
|
15
|
+
track: TRACKS.includes(data.track) ? data.track : '',
|
|
14
16
|
status: data.status || 'exploring',
|
|
15
17
|
kind: data.kind === 'sprint' || data.kind === 'alternate' ? data.kind : 'exploration',
|
|
16
18
|
feature: data.feature || '',
|
|
@@ -22,3 +24,11 @@ export const isLive = (m) => m.status !== 'archived';
|
|
|
22
24
|
// Open questions sit above answered ones; within a status, newest first.
|
|
23
25
|
const rank = (status) => (status === 'exploring' ? 0 : 1);
|
|
24
26
|
export const byIndexOrder = (a, b) => rank(a.status) - rank(b.status) || b.created.localeCompare(a.created) || a.title.localeCompare(b.title);
|
|
27
|
+
/** Where a prototype stands, in words: the Space, the shell its track draws, or a board of artboards. */
|
|
28
|
+
export function placeLabel(m) {
|
|
29
|
+
if (m.context === 'board')
|
|
30
|
+
return 'Artboards';
|
|
31
|
+
if (m.context !== 'app')
|
|
32
|
+
return 'Space';
|
|
33
|
+
return m.track === 'website' ? 'Site shell' : 'App shell';
|
|
34
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare const SOURCES: readonly ['webflow', 'payload', 'wordpress', 'file'];
|
|
2
|
+
export type Source = (typeof SOURCES)[number];
|
|
3
|
+
export type Item = Record<string, unknown>;
|
|
4
|
+
export type Request = {
|
|
5
|
+
url: string;
|
|
6
|
+
headers: Record<string, string>;
|
|
7
|
+
};
|
|
8
|
+
export type Page = {
|
|
9
|
+
items: Item[];
|
|
10
|
+
more: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type Env = Record<string, string | undefined>;
|
|
13
|
+
/** The env a source needs, as `.env` lines, and why. */
|
|
14
|
+
export declare const NEEDS: Record<Exclude<Source, 'file'>, {
|
|
15
|
+
keys: string[];
|
|
16
|
+
fix: string;
|
|
17
|
+
}>;
|
|
18
|
+
/** The env keys a source still lacks. */
|
|
19
|
+
export declare const missingEnv: (source: Exclude<Source, 'file'>, env: Env) => string[];
|
|
20
|
+
/** The request for one page (`page` counts from 1) of a collection. */
|
|
21
|
+
export declare function pageRequest(source: Exclude<Source, 'file'>, env: Env, collection: string, page: number, staged: boolean): Request;
|
|
22
|
+
/**
|
|
23
|
+
* One page's items and whether another follows. Webflow's archived items
|
|
24
|
+
* are dropped, since they are not on the site. WordPress answers with a bare
|
|
25
|
+
* array and puts the page count in X-WP-TotalPages.
|
|
26
|
+
*/
|
|
27
|
+
export declare function readPage(source: Exclude<Source, 'file'>, body: unknown, page: number, totalPages?: string | null): Page;
|
|
28
|
+
/** Where a file keeps its items: a bare array, or the first array under a common key. */
|
|
29
|
+
export declare function itemsInFile(json: unknown): Item[] | null;
|
|
30
|
+
/** Where an item's fields start: Webflow nests them under fieldData; every other source maps from the item itself. */
|
|
31
|
+
export declare const fieldRoot: (source: Source, item: Item) => Item;
|
|
32
|
+
/** WordPress's `rendered` fields are HTML; a fixture holds the text a reader sees. */
|
|
33
|
+
export declare const plainText: (html: string) => string;
|
|
34
|
+
/** The value at a dotted path (`title.rendered`, `_embedded.wp:featuredmedia.0.source_url`); undefined when it is not there. */
|
|
35
|
+
export declare function valueAt(root: unknown, path: string[]): unknown;
|
|
36
|
+
/** Every leaf path in an item, with a short sample, so the user can pick what to map. */
|
|
37
|
+
export declare function fieldPaths(item: unknown, prefix?: string, depth?: number): {
|
|
38
|
+
path: string;
|
|
39
|
+
sample: string;
|
|
40
|
+
}[];
|
package/dist/core/cms.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Where real fixtures come from: a CMS's own API, read page by page, or a
|
|
2
|
+
// JSON file another tool wrote. Pure: this module builds each request and
|
|
3
|
+
// reads each response; the caller fetches and passes the environment in, so
|
|
4
|
+
// every source is tested without a network.
|
|
5
|
+
//
|
|
6
|
+
// Webflow: Data API v2, https://developers.webflow.com/data/reference/cms/collection-items
|
|
7
|
+
// Payload: REST, GET <url>/api/<collection>, `Authorization: <auth collection> API-Key <key>`
|
|
8
|
+
// WordPress: REST, GET <url>/wp-json/wp/v2/<rest base>, application passwords for drafts
|
|
9
|
+
// File: any JSON with an array of items (EmDash: `npx emdash content list <collection> --json`)
|
|
10
|
+
export const SOURCES = ['webflow', 'payload', 'wordpress', 'file'];
|
|
11
|
+
/** The env a source needs, as `.env` lines, and why. */
|
|
12
|
+
export const NEEDS = {
|
|
13
|
+
webflow: {
|
|
14
|
+
keys: ['WEBFLOW_TOKEN'],
|
|
15
|
+
fix: 'Create a site token with cms:read in Webflow (Site settings, Apps & integrations) and add WEBFLOW_TOKEN=<token> to .env.',
|
|
16
|
+
},
|
|
17
|
+
payload: {
|
|
18
|
+
keys: ['PAYLOAD_URL', 'PAYLOAD_API_KEY'],
|
|
19
|
+
fix: 'Add PAYLOAD_URL=<site origin> and PAYLOAD_API_KEY=<key> to .env. The key comes from a user in an auth collection with useAPIKey on; set PAYLOAD_AUTH_COLLECTION when that collection is not users.',
|
|
20
|
+
},
|
|
21
|
+
wordpress: {
|
|
22
|
+
keys: ['WORDPRESS_URL'],
|
|
23
|
+
fix: 'Add WORDPRESS_URL=<site origin> to .env. For drafts (--staged), add WORDPRESS_USER and WORDPRESS_APP_PASSWORD (Users, Profile, Application Passwords).',
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const PAGE_SIZE = 100;
|
|
27
|
+
/** The env keys a source still lacks. */
|
|
28
|
+
export const missingEnv = (source, env) => NEEDS[source].keys.filter((k) => !env[k]);
|
|
29
|
+
const origin = (url) => url.replace(/\/+$/, '');
|
|
30
|
+
/** The request for one page (`page` counts from 1) of a collection. */
|
|
31
|
+
export function pageRequest(source, env, collection, page, staged) {
|
|
32
|
+
const c = encodeURIComponent(collection);
|
|
33
|
+
if (source === 'webflow') {
|
|
34
|
+
const listing = staged ? 'items' : 'items/live';
|
|
35
|
+
return {
|
|
36
|
+
url: `https://api.webflow.com/v2/collections/${c}/${listing}?offset=${(page - 1) * PAGE_SIZE}&limit=${PAGE_SIZE}`,
|
|
37
|
+
headers: { authorization: `Bearer ${env.WEBFLOW_TOKEN}`, accept: 'application/json' },
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (source === 'payload') {
|
|
41
|
+
const auth = env.PAYLOAD_AUTH_COLLECTION || 'users';
|
|
42
|
+
return {
|
|
43
|
+
url: `${origin(env.PAYLOAD_URL ?? '')}/api/${c}?limit=${PAGE_SIZE}&page=${page}&depth=1${staged ? '&draft=true' : ''}`,
|
|
44
|
+
headers: { authorization: `${auth} API-Key ${env.PAYLOAD_API_KEY}`, accept: 'application/json' },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const headers = { accept: 'application/json' };
|
|
48
|
+
if (env.WORDPRESS_USER && env.WORDPRESS_APP_PASSWORD)
|
|
49
|
+
headers.authorization = `Basic ${btoa(`${env.WORDPRESS_USER}:${env.WORDPRESS_APP_PASSWORD}`)}`;
|
|
50
|
+
return {
|
|
51
|
+
url: `${origin(env.WORDPRESS_URL ?? '')}/wp-json/wp/v2/${c}?per_page=${PAGE_SIZE}&page=${page}&_embed=1${staged ? '&status=any' : ''}`,
|
|
52
|
+
headers,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* One page's items and whether another follows. Webflow's archived items
|
|
57
|
+
* are dropped, since they are not on the site. WordPress answers with a bare
|
|
58
|
+
* array and puts the page count in X-WP-TotalPages.
|
|
59
|
+
*/
|
|
60
|
+
export function readPage(source, body, page, totalPages) {
|
|
61
|
+
if (source === 'webflow') {
|
|
62
|
+
const { items, pagination } = body;
|
|
63
|
+
return {
|
|
64
|
+
items: items.filter((i) => !i.isArchived),
|
|
65
|
+
more: items.length > 0 && pagination.offset + PAGE_SIZE < pagination.total,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (source === 'payload') {
|
|
69
|
+
const { docs, hasNextPage } = body;
|
|
70
|
+
return { items: docs, more: hasNextPage };
|
|
71
|
+
}
|
|
72
|
+
const items = body;
|
|
73
|
+
return { items, more: items.length > 0 && page < Number(totalPages ?? 1) };
|
|
74
|
+
}
|
|
75
|
+
/** Where a file keeps its items: a bare array, or the first array under a common key. */
|
|
76
|
+
export function itemsInFile(json) {
|
|
77
|
+
if (Array.isArray(json))
|
|
78
|
+
return json;
|
|
79
|
+
if (!json || typeof json !== 'object')
|
|
80
|
+
return null;
|
|
81
|
+
for (const key of ['items', 'entries', 'docs', 'data', 'results']) {
|
|
82
|
+
const value = json[key];
|
|
83
|
+
if (Array.isArray(value))
|
|
84
|
+
return value;
|
|
85
|
+
if (value && typeof value === 'object') {
|
|
86
|
+
const nested = itemsInFile(value);
|
|
87
|
+
if (nested)
|
|
88
|
+
return nested;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
/** Where an item's fields start: Webflow nests them under fieldData; every other source maps from the item itself. */
|
|
94
|
+
export const fieldRoot = (source, item) => source === 'webflow' ? (item.fieldData ?? {}) : item;
|
|
95
|
+
/** The named entities CMS output carries in practice; anything else is left as written. */
|
|
96
|
+
const NAMED = {
|
|
97
|
+
quot: '"',
|
|
98
|
+
apos: "'",
|
|
99
|
+
lt: '<',
|
|
100
|
+
gt: '>',
|
|
101
|
+
nbsp: ' ',
|
|
102
|
+
hellip: '…',
|
|
103
|
+
mdash: '—',
|
|
104
|
+
ndash: '–',
|
|
105
|
+
lsquo: '‘',
|
|
106
|
+
rsquo: '’',
|
|
107
|
+
ldquo: '“',
|
|
108
|
+
rdquo: '”',
|
|
109
|
+
eacute: 'é',
|
|
110
|
+
copy: '©',
|
|
111
|
+
reg: '®',
|
|
112
|
+
trade: '™',
|
|
113
|
+
amp: '&',
|
|
114
|
+
};
|
|
115
|
+
const decode = (s) => s
|
|
116
|
+
.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n)))
|
|
117
|
+
.replace(/&#x([\da-f]+);/gi, (_, n) => String.fromCodePoint(Number.parseInt(n, 16)))
|
|
118
|
+
.replace(/&([a-z]+);/gi, (match, name) => NAMED[name.toLowerCase()] ?? match);
|
|
119
|
+
/** WordPress's `rendered` fields are HTML; a fixture holds the text a reader sees. */
|
|
120
|
+
export const plainText = (html) => decode(html.replace(/<[^>]*>/g, ''))
|
|
121
|
+
.replace(/\s+/g, ' ')
|
|
122
|
+
.trim();
|
|
123
|
+
/** The value at a dotted path (`title.rendered`, `_embedded.wp:featuredmedia.0.source_url`); undefined when it is not there. */
|
|
124
|
+
export function valueAt(root, path) {
|
|
125
|
+
let value = root;
|
|
126
|
+
for (const part of path)
|
|
127
|
+
value = value && typeof value === 'object' ? value[part] : undefined;
|
|
128
|
+
if (typeof value === 'string' && path.at(-1) === 'rendered')
|
|
129
|
+
return plainText(value);
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
const MAX_DEPTH = 3;
|
|
133
|
+
/** Every leaf path in an item, with a short sample, so the user can pick what to map. */
|
|
134
|
+
export function fieldPaths(item, prefix = '', depth = 0) {
|
|
135
|
+
if (!item || typeof item !== 'object' || depth > MAX_DEPTH)
|
|
136
|
+
return prefix ? [{ path: prefix, sample: JSON.stringify(item ?? null).slice(0, 60) }] : [];
|
|
137
|
+
const entries = Array.isArray(item)
|
|
138
|
+
? item.slice(0, 1).map((v, i) => [String(i), v])
|
|
139
|
+
: Object.entries(item);
|
|
140
|
+
return entries.flatMap(([key, value]) => fieldPaths(value, prefix ? `${prefix}.${key}` : key, depth + 1));
|
|
141
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type Output } from './formats.ts';
|
|
2
|
+
/** An artboard as the page reports it: its name, format, and box in page pixels. */
|
|
3
|
+
export type Board = {
|
|
4
|
+
/** Its place on the page, so the exporter can show or hide exactly this one. */
|
|
5
|
+
index: number;
|
|
6
|
+
name: string;
|
|
7
|
+
format: string;
|
|
8
|
+
output: Output;
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
};
|
|
14
|
+
export type As = 'auto' | Output;
|
|
15
|
+
/** Where Chrome, Chromium or Edge usually lives; $CHROME_PATH first. */
|
|
16
|
+
export declare function chromeCandidates(platform: string, env: Record<string, string | undefined>): string[];
|
|
17
|
+
/** The browser's DevTools address, from the line headless Chrome prints on start. */
|
|
18
|
+
export declare const devtoolsUrl: (stderr: string) => string | null;
|
|
19
|
+
/** What one DevTools message means to the exporter: a reply to a command, a page exception, or nothing. */
|
|
20
|
+
export type DevtoolsMessage = {
|
|
21
|
+
kind: 'reply';
|
|
22
|
+
id: number;
|
|
23
|
+
result: unknown;
|
|
24
|
+
error?: string;
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'exception';
|
|
27
|
+
text: string;
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'other';
|
|
30
|
+
};
|
|
31
|
+
export declare function readDevtools(data: string): DevtoolsMessage;
|
|
32
|
+
/** Which artboards become PNG files and which go into the one PDF. */
|
|
33
|
+
export declare function plan(boards: Board[], as: As): {
|
|
34
|
+
png: Board[];
|
|
35
|
+
pdf: Board[];
|
|
36
|
+
};
|
|
37
|
+
/** `<slug>-<name>.png`, unique within one export: a repeated name gets -2, -3. */
|
|
38
|
+
export declare function fileNames(slug: string, boards: Board[], ext: string): string[];
|
|
39
|
+
/**
|
|
40
|
+
* The PDFs an export writes: one per format, in the order the formats first
|
|
41
|
+
* appear. Chrome lays out one PDF at a single width, so a deck and an A4 page
|
|
42
|
+
* in the same file would shrink each other.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pdfFiles(slug: string, boards: Board[]): {
|
|
45
|
+
format: string;
|
|
46
|
+
file: string;
|
|
47
|
+
}[];
|
|
48
|
+
/** The page the browser opens: the prototype in export mode, with the dial state and theme asked for. */
|
|
49
|
+
export declare function exportUrl(origin: string, slug: string, opts: {
|
|
50
|
+
dials?: string;
|
|
51
|
+
theme?: string;
|
|
52
|
+
}): string;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The decisions `design-hub export` makes, kept pure: which browser to drive,
|
|
2
|
+
// what each artboard becomes, and what its file is called. The command
|
|
3
|
+
// (scripts/export.ts) does the driving.
|
|
4
|
+
import { isFormat } from './formats.js';
|
|
5
|
+
/** Where Chrome, Chromium or Edge usually lives; $CHROME_PATH first. */
|
|
6
|
+
export function chromeCandidates(platform, env) {
|
|
7
|
+
const own = env.CHROME_PATH ? [env.CHROME_PATH] : [];
|
|
8
|
+
if (platform === 'darwin')
|
|
9
|
+
return [
|
|
10
|
+
...own,
|
|
11
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
12
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
13
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
14
|
+
];
|
|
15
|
+
if (platform === 'win32') {
|
|
16
|
+
const roots = [env.PROGRAMFILES, env['PROGRAMFILES(X86)'], env.LOCALAPPDATA].filter(Boolean);
|
|
17
|
+
return [
|
|
18
|
+
...own,
|
|
19
|
+
...roots.flatMap((r) => [
|
|
20
|
+
`${r}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
21
|
+
`${r}\\Microsoft\\Edge\\Application\\msedge.exe`,
|
|
22
|
+
]),
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
return [
|
|
26
|
+
...own,
|
|
27
|
+
'/usr/bin/google-chrome',
|
|
28
|
+
'/usr/bin/google-chrome-stable',
|
|
29
|
+
'/usr/bin/chromium',
|
|
30
|
+
'/usr/bin/chromium-browser',
|
|
31
|
+
'/snap/bin/chromium',
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
/** The browser's DevTools address, from the line headless Chrome prints on start. */
|
|
35
|
+
export const devtoolsUrl = (stderr) => stderr.match(/DevTools listening on (ws:\/\/\S+)/)?.[1] ?? null;
|
|
36
|
+
export function readDevtools(data) {
|
|
37
|
+
const msg = JSON.parse(data);
|
|
38
|
+
if (msg.id !== undefined)
|
|
39
|
+
return { kind: 'reply', id: msg.id, result: msg.result, error: msg.error?.message };
|
|
40
|
+
if (msg.method !== 'Runtime.exceptionThrown')
|
|
41
|
+
return { kind: 'other' };
|
|
42
|
+
const d = msg.params?.exceptionDetails;
|
|
43
|
+
return { kind: 'exception', text: d?.exception?.description ?? d?.text ?? 'An exception' };
|
|
44
|
+
}
|
|
45
|
+
/** Which artboards become PNG files and which go into the one PDF. */
|
|
46
|
+
export function plan(boards, as) {
|
|
47
|
+
const output = (b) => (as === 'auto' ? b.output : as);
|
|
48
|
+
return { png: boards.filter((b) => output(b) === 'png'), pdf: boards.filter((b) => output(b) === 'pdf') };
|
|
49
|
+
}
|
|
50
|
+
const slugify = (s) => s
|
|
51
|
+
.toLowerCase()
|
|
52
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
53
|
+
.replace(/^-|-$/g, '');
|
|
54
|
+
/** `<slug>-<name>.png`, unique within one export: a repeated name gets -2, -3. */
|
|
55
|
+
export function fileNames(slug, boards, ext) {
|
|
56
|
+
const seen = new Map();
|
|
57
|
+
return boards.map((b, i) => {
|
|
58
|
+
const base = slugify(b.name) || (isFormat(b.format) ? `${b.format}-${i + 1}` : `artboard-${i + 1}`);
|
|
59
|
+
const count = (seen.get(base) ?? 0) + 1;
|
|
60
|
+
seen.set(base, count);
|
|
61
|
+
return `${slug}-${count > 1 ? `${base}-${count}` : base}.${ext}`;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The PDFs an export writes: one per format, in the order the formats first
|
|
66
|
+
* appear. Chrome lays out one PDF at a single width, so a deck and an A4 page
|
|
67
|
+
* in the same file would shrink each other.
|
|
68
|
+
*/
|
|
69
|
+
export function pdfFiles(slug, boards) {
|
|
70
|
+
const formats = [...new Set(boards.map((b) => b.format))];
|
|
71
|
+
return formats.map((format) => ({
|
|
72
|
+
format,
|
|
73
|
+
file: formats.length > 1 ? `${slug}-${format}.pdf` : `${slug}.pdf`,
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
/** The page the browser opens: the prototype in export mode, with the dial state and theme asked for. */
|
|
77
|
+
export function exportUrl(origin, slug, opts) {
|
|
78
|
+
const params = new URLSearchParams({ export: '1' });
|
|
79
|
+
if (opts.dials)
|
|
80
|
+
params.set('dials', opts.dials);
|
|
81
|
+
if (opts.theme)
|
|
82
|
+
params.set('theme', opts.theme);
|
|
83
|
+
return `${origin}/${slug}/?${params}`;
|
|
84
|
+
}
|