@astryxdesign/cli 0.7.13 → 0.7.14-canary.108f354
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 +18 -18
- package/api/component/__fixtures__/resolutions/modules.txt +1 -1
- package/api/doctor/doctor.mjs +3 -7
- package/api/experimental.d.mts +2 -0
- package/api/experimental.mjs +12 -0
- package/api/icon/_adapter.d.mts +10 -0
- package/api/icon/_adapter.mjs +31 -2
- package/api/icon/_authoring.d.mts +2 -1
- package/api/icon/_authoring.mjs +91 -6
- package/api/icon/_options.mjs +1 -0
- package/api/icon/_records.d.mts +18 -0
- package/api/icon/_records.mjs +26 -0
- package/api/icon/add/add.mjs +7 -1
- package/api/icon/definitions/definitions.d.mts +12 -0
- package/api/icon/definitions/definitions.mjs +83 -0
- package/api/icon/icon.doc.mjs +5 -4
- package/api/icon/icon.mjs +5 -16
- package/api/icon/icon.type.d.mts +29 -2
- package/api/icon/icon.type.mjs +17 -3
- package/api/icon/iconAdd.doc.mjs +1 -1
- package/api/icon/iconMap.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +8 -2
- package/api/icon/map/map.mjs +21 -2
- package/api/icon/new-set/new-set.mjs +14 -2
- package/api/icon/search/search.mjs +2 -15
- package/api/icon/use/use.mjs +4 -3
- package/api/integration/validate-integration.mjs +6 -1
- package/api/integration/validate-integration.type.d.mts +12 -0
- package/api/integration/validate-integration.type.mjs +5 -0
- package/api/theme-edit/_adapter.d.mts +116 -0
- package/api/theme-edit/_adapter.mjs +1683 -0
- package/api/theme-edit/_catalog.d.mts +59 -0
- package/api/theme-edit/_catalog.mjs +564 -0
- package/api/theme-edit/_check.d.mts +49 -0
- package/api/theme-edit/_check.mjs +845 -0
- package/api/theme-edit/_color.d.mts +29 -0
- package/api/theme-edit/_color.mjs +300 -0
- package/api/theme-edit/_compiler.d.mts +61 -0
- package/api/theme-edit/_compiler.mjs +522 -0
- package/api/theme-edit/_css-properties.d.mts +10 -0
- package/api/theme-edit/_css-properties.mjs +556 -0
- package/api/theme-edit/_parts.d.mts +111 -0
- package/api/theme-edit/_parts.mjs +409 -0
- package/api/theme-edit/_png.d.mts +40 -0
- package/api/theme-edit/_png.mjs +187 -0
- package/api/theme-edit/_preview.d.mts +121 -0
- package/api/theme-edit/_preview.mjs +720 -0
- package/api/theme-edit/_project.d.mts +83 -0
- package/api/theme-edit/_project.mjs +289 -0
- package/api/theme-edit/_settings.d.mts +222 -0
- package/api/theme-edit/_settings.mjs +861 -0
- package/api/theme-edit/_tables.d.mts +132 -0
- package/api/theme-edit/_tables.mjs +700 -0
- package/api/theme-edit/theme-edit.d.mts +93 -0
- package/api/theme-edit/theme-edit.mjs +145 -0
- package/api/theme-edit/theme-edit.type.d.mts +413 -0
- package/api/theme-edit/theme-edit.type.mjs +233 -0
- package/api/theme-edit/themeEditHow.doc.d.mts +6 -0
- package/api/theme-edit/themeEditHow.doc.mjs +88 -0
- package/api/theme-edit/themeEditNew.doc.d.mts +6 -0
- package/api/theme-edit/themeEditNew.doc.mjs +89 -0
- package/api/theme-edit/themeEditPreviewAdd.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +100 -0
- package/api/theme-edit/themeEditPreviewApp.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +84 -0
- package/api/theme-edit/themeEditPreviewList.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewList.doc.mjs +53 -0
- package/api/theme-edit/themeEditPreviewRemove.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +66 -0
- package/api/theme-edit/themeEditSet.doc.d.mts +6 -0
- package/api/theme-edit/themeEditSet.doc.mjs +165 -0
- package/api/theme-edit/themeEditShow.doc.d.mts +6 -0
- package/api/theme-edit/themeEditShow.doc.mjs +86 -0
- package/api/theme-edit/themeEditUndo.doc.d.mts +6 -0
- package/api/theme-edit/themeEditUndo.doc.mjs +124 -0
- package/assets/docs/color.doc.mjs +5 -5
- package/assets/docs/tree/icon-sets.doc.mjs +6 -2
- package/assets/docs/tree/tokens-border.doc.mjs +1 -1
- package/assets/docs/tree/tokens-color.doc.mjs +2 -2
- package/assets/docs/tree/tokens-data.doc.mjs +2 -2
- package/assets/docs/tree/tokens-duration.doc.mjs +1 -1
- package/assets/docs/tree/tokens-ease.doc.mjs +1 -1
- package/assets/docs/tree/tokens-focus.doc.mjs +1 -1
- package/assets/docs/tree/tokens-font-weight.doc.mjs +1 -1
- package/assets/docs/tree/tokens-radius.doc.mjs +1 -1
- package/assets/docs/tree/tokens-shadow.doc.mjs +1 -1
- package/assets/docs/tree/tokens-size.doc.mjs +1 -1
- package/assets/docs/tree/tokens-spacing.doc.mjs +1 -1
- package/assets/docs/tree/tokens-syntax.doc.mjs +1 -1
- package/assets/docs/tree/tokens-text-size.doc.mjs +1 -1
- package/assets/docs/tree/tokens-type-scale.doc.mjs +1 -1
- package/assets/docs/tree/tokens-typography.doc.mjs +1 -1
- package/assets/icons/definitions/bank.doc.mjs +1 -1
- package/assets/icons/definitions/save.doc.mjs +1 -1
- package/assets/preview-app/README.md +6 -0
- package/assets/preview-app/css.d.ts +5 -0
- package/assets/preview-app/gitignore +1 -0
- package/assets/preview-app/next.config.mjs +6 -0
- package/assets/preview-app/package.json +23 -0
- package/assets/preview-app/src/app/globals.css +5 -0
- package/assets/preview-app/src/app/layout.tsx +21 -0
- package/assets/preview-app/src/app/page.tsx +94 -0
- package/assets/preview-app/src/app/providers.tsx +16 -0
- package/assets/preview-app/tsconfig.json +27 -0
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider01ShippedLocale.doc.mjs +2 -1
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider02Overrides.doc.mjs +2 -1
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider03RtlDirection.doc.mjs +2 -1
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider04Calendar.doc.mjs +2 -1
- package/assets/templates/blocks/components/Text/useTruncationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Text/useTruncationHookUsage.tsx +112 -0
- package/authoring/doctypes/_schema.d.mts +1 -0
- package/authoring/doctypes/_schema.mjs +1 -0
- package/authoring/doctypes/command/command.doc.mjs +7 -0
- package/authoring/doctypes/command/type.ts +3 -0
- package/authoring/doctypes/icon-set/parse.d.mts +7 -0
- package/authoring/doctypes/icon-set/parse.mjs +18 -8
- package/clients/cli/commands/doctor.mjs +33 -3
- package/clients/cli/commands/experimental/icon.mjs +67 -14
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/theme.mjs +684 -0
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +7 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +3 -2
- package/clients/cli/commands/experimental-theme-add.doc.mjs +78 -0
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +97 -0
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +84 -0
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +66 -0
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +121 -0
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +66 -0
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +104 -0
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +49 -0
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +71 -0
- package/clients/cli/commands/experimental-theme-list.doc.mjs +49 -0
- package/clients/cli/commands/experimental-theme-palette-generate.doc.mjs +73 -0
- package/clients/cli/commands/experimental-theme-palette.doc.mjs +37 -0
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +86 -0
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +66 -0
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +48 -0
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +59 -0
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +40 -0
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +61 -0
- package/clients/cli/commands/experimental-theme-template.doc.mjs +61 -0
- package/clients/cli/commands/experimental-theme-use.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme.doc.mjs +57 -0
- package/clients/cli/commands/experimental.doc.mjs +10 -3
- package/clients/cli/commands/theme.mjs +101 -34
- package/clients/cli/index.mjs +18 -0
- package/clients/cli/lib/define-command.mjs +3 -0
- package/foundation/discovery/icon-set-checks.d.mts +16 -1
- package/foundation/discovery/icon-set-checks.mjs +68 -7
- package/foundation/discovery/icon-set-discovery.d.mts +21 -6
- package/foundation/discovery/icon-set-discovery.mjs +17 -8
- package/foundation/discovery/template-icon-meanings.d.mts +39 -0
- package/foundation/discovery/template-icon-meanings.mjs +95 -0
- package/foundation/discovery/theming-targets.d.mts +6 -0
- package/foundation/discovery/theming-targets.mjs +33 -0
- package/foundation/response/error-codes.d.mts +12 -1
- package/foundation/response/error-codes.doc.mjs +66 -0
- package/foundation/response/error-codes.mjs +55 -0
- package/foundation/response/response-types.doc.mjs +55 -0
- package/foundation/text/template-meanings-line.d.mts +16 -0
- package/foundation/text/template-meanings-line.mjs +37 -0
- package/foundation/xle/browser.d.mts +2 -2
- package/package.json +6 -6
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Previews for `astryx experimental theme editor` and `theme preview`: the sample app page and
|
|
5
|
+
* the theme-building page (every component grouped, overlays open, and real
|
|
6
|
+
* template pages), rendered with the theme to self-contained HTML by the
|
|
7
|
+
* render machinery; and the sources of the in-app preview route and its
|
|
8
|
+
* pages. PNG pictures
|
|
9
|
+
* are made only on request, with a Playwright or Chrome installed on the
|
|
10
|
+
* machine.
|
|
11
|
+
*
|
|
12
|
+
* @position api/theme-edit — previews and pictures
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as fs from 'node:fs';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import {spawnSync} from 'node:child_process';
|
|
18
|
+
import {fileURLToPath, pathToFileURL} from 'node:url';
|
|
19
|
+
import {
|
|
20
|
+
EXTRA,
|
|
21
|
+
OPEN_OVERLAYS,
|
|
22
|
+
SAMPLE_PAGE,
|
|
23
|
+
SHEET_ORDER,
|
|
24
|
+
elementNames,
|
|
25
|
+
pageSource,
|
|
26
|
+
} from './_catalog.mjs';
|
|
27
|
+
import {renderDir, renderGenerated} from './_parts.mjs';
|
|
28
|
+
import {fontPackages, modesOf, themeFiles, shown} from './_settings.mjs';
|
|
29
|
+
import {importPathOf, installLine} from './_project.mjs';
|
|
30
|
+
|
|
31
|
+
const TEMPLATES_DIR = path.resolve(
|
|
32
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
33
|
+
'../../assets/templates/pages',
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
/** The groups of the theme-building page, in order. */
|
|
37
|
+
export const SHOWCASE_GROUPS = [
|
|
38
|
+
'actions',
|
|
39
|
+
'inputs',
|
|
40
|
+
'selection',
|
|
41
|
+
'navigation',
|
|
42
|
+
'feedback',
|
|
43
|
+
'data',
|
|
44
|
+
'containers',
|
|
45
|
+
'overlays',
|
|
46
|
+
'pages',
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
/** What each group holds, for help and `--group`. */
|
|
50
|
+
export const SHOWCASE_GROUP_HELP = {
|
|
51
|
+
actions: 'buttons and links',
|
|
52
|
+
inputs: 'text, number, date and time fields, forms',
|
|
53
|
+
selection: 'selectors, checkboxes, radios, switches, segmented controls',
|
|
54
|
+
navigation: 'top and side navigation, tabs, breadcrumbs, pagination',
|
|
55
|
+
feedback: 'banners, badges, progress, status dots, toasts',
|
|
56
|
+
data: 'text, headings, tables, lists, code, content',
|
|
57
|
+
containers: 'cards and layout',
|
|
58
|
+
overlays: 'dialogs, popovers, menus and tooltips, shown open',
|
|
59
|
+
pages:
|
|
60
|
+
'the sample app page and real template pages (dashboard, form, settings)',
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Real template pages the theme-building page shows in full. */
|
|
64
|
+
const PAGE_TEMPLATES = ['dashboard', 'contact-form', 'settings'];
|
|
65
|
+
|
|
66
|
+
const SELECTION =
|
|
67
|
+
/Select|Checkbox|Radio|Switch|Segmented|Toggle|Slider|Typeahead|Tokenizer/;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The group a component belongs to on the theme-building page.
|
|
71
|
+
* @param {string} name
|
|
72
|
+
* @param {string} category
|
|
73
|
+
* @returns {string | null}
|
|
74
|
+
*/
|
|
75
|
+
function groupOf(name, category) {
|
|
76
|
+
if (SELECTION.test(name)) return 'selection';
|
|
77
|
+
const c = category.toLowerCase();
|
|
78
|
+
if (c === 'action') return 'actions';
|
|
79
|
+
if (c === 'form controls') return 'inputs';
|
|
80
|
+
if (c === 'navigation') return 'navigation';
|
|
81
|
+
if (c === 'feedback & status') return 'feedback';
|
|
82
|
+
if (c === 'content' || c === 'table & list' || c === 'text' || c === 'media')
|
|
83
|
+
return 'data';
|
|
84
|
+
if (c === 'container' || c === 'layout') return 'containers';
|
|
85
|
+
if (c === 'overlay') return 'overlays';
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Items of the theme-building page: each component once per variant, the
|
|
91
|
+
* hand-made extras, overlays open, the sample page, and template pages
|
|
92
|
+
* whose packages the project has.
|
|
93
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
94
|
+
* @param {string | undefined} group only this group
|
|
95
|
+
* @returns {{items: import('./_catalog.mjs').CatalogItem[], imports: Record<string, string>, skipped: string[]}}
|
|
96
|
+
*/
|
|
97
|
+
export function showcaseItems(project, group) {
|
|
98
|
+
/** @type {import('./_catalog.mjs').CatalogItem[]} */
|
|
99
|
+
const items = [];
|
|
100
|
+
/** @param {string} name */
|
|
101
|
+
const groupFor = name =>
|
|
102
|
+
groupOf(name, project.components[name]?.category ?? '');
|
|
103
|
+
const extraNames = new Set([...EXTRA, ...OPEN_OVERLAYS].map(([n]) => n));
|
|
104
|
+
for (const [name, list] of [...EXTRA, ...OPEN_OVERLAYS]) {
|
|
105
|
+
const g = groupFor(name);
|
|
106
|
+
if (g)
|
|
107
|
+
for (const [label, tree] of list)
|
|
108
|
+
items.push({group: g, name, label, tree});
|
|
109
|
+
}
|
|
110
|
+
for (const [name, info] of Object.entries(project.components)) {
|
|
111
|
+
const defaults = /** @type {any} */ (info.defaults);
|
|
112
|
+
const g = groupFor(name);
|
|
113
|
+
if (!g || !defaults || typeof defaults !== 'object' || extraNames.has(name))
|
|
114
|
+
continue;
|
|
115
|
+
const values = info.targets[0]?.values ?? {};
|
|
116
|
+
/** @type {Array<Record<string, string>>} */
|
|
117
|
+
const variants = [{}];
|
|
118
|
+
for (const p of ['variant', 'status', 'color'])
|
|
119
|
+
for (const v of values[p] ?? []) variants.push({[p]: v});
|
|
120
|
+
for (const v of variants.slice(0, 12))
|
|
121
|
+
items.push({
|
|
122
|
+
group: g,
|
|
123
|
+
name,
|
|
124
|
+
label: Object.values(v).join(' '),
|
|
125
|
+
tree: {__element: name, props: {...defaults, ...v}},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
items.push({
|
|
129
|
+
group: 'pages',
|
|
130
|
+
name: 'Sample app',
|
|
131
|
+
label: '',
|
|
132
|
+
tree: SAMPLE_PAGE,
|
|
133
|
+
});
|
|
134
|
+
/** @type {string[]} */
|
|
135
|
+
const skipped = [];
|
|
136
|
+
/** @type {Record<string, string>} */
|
|
137
|
+
const templateImports = {};
|
|
138
|
+
for (const t of PAGE_TEMPLATES) {
|
|
139
|
+
const file = path.join(TEMPLATES_DIR, t, 'page.tsx');
|
|
140
|
+
if (!fs.existsSync(file)) continue;
|
|
141
|
+
const src = fs.readFileSync(file, 'utf8');
|
|
142
|
+
const missing = [
|
|
143
|
+
...src.matchAll(/^\s*(?:import|\})[^'"\n]*from\s+['"]([^'"]+)['"]/gm),
|
|
144
|
+
]
|
|
145
|
+
.map(m => m[1])
|
|
146
|
+
.filter(spec => !spec.startsWith('.') && !resolves(project, spec));
|
|
147
|
+
if (missing.length) {
|
|
148
|
+
const pkgs = [
|
|
149
|
+
...new Set(
|
|
150
|
+
missing.map(s =>
|
|
151
|
+
s.startsWith('@')
|
|
152
|
+
? s.split('/').slice(0, 2).join('/')
|
|
153
|
+
: s.split('/')[0],
|
|
154
|
+
),
|
|
155
|
+
),
|
|
156
|
+
];
|
|
157
|
+
skipped.push(
|
|
158
|
+
`the ${t} page needs ${pkgs.join(', ')} (${installLine(project.cwd, pkgs)})`,
|
|
159
|
+
);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const local = `Template_${t.replace(/-/g, '_')}`;
|
|
163
|
+
templateImports[local] = `./templates/${t}.tsx`;
|
|
164
|
+
items.push({
|
|
165
|
+
group: 'pages',
|
|
166
|
+
name: `${t} template`,
|
|
167
|
+
label: '',
|
|
168
|
+
tree: {__element: local, props: {}},
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
const order = (/** @type {import('./_catalog.mjs').CatalogItem} */ it) => {
|
|
172
|
+
const i = SHEET_ORDER.indexOf(it.name);
|
|
173
|
+
return SHOWCASE_GROUPS.indexOf(it.group ?? '') * 1000 + (i < 0 ? 500 : i);
|
|
174
|
+
};
|
|
175
|
+
const chosen = items
|
|
176
|
+
.filter(it => !group || it.group === group)
|
|
177
|
+
.sort((a, b) => order(a) - order(b) || a.name.localeCompare(b.name));
|
|
178
|
+
/** @type {Record<string, string>} */
|
|
179
|
+
const imports = {};
|
|
180
|
+
for (const n of elementNames(chosen.map(i => i.tree))) {
|
|
181
|
+
if (templateImports[n]) {
|
|
182
|
+
imports[n] = templateImports[n];
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const spec = importPathOf(project, n);
|
|
186
|
+
if (spec) imports[n] = spec;
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
items: chosen,
|
|
190
|
+
imports,
|
|
191
|
+
skipped: !group || group === 'pages' ? skipped : [],
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @param {import('./_project.mjs').ThemeProject} project @param {string} spec */
|
|
196
|
+
function resolves(project, spec) {
|
|
197
|
+
try {
|
|
198
|
+
project.req.resolve(spec);
|
|
199
|
+
return true;
|
|
200
|
+
} catch {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Template page modules the theme-building page imports, copied beside it.
|
|
207
|
+
* @param {Record<string, string>} imports
|
|
208
|
+
* @returns {Array<{name: string, from: string}>}
|
|
209
|
+
*/
|
|
210
|
+
function templateFiles(imports) {
|
|
211
|
+
return Object.values(imports).flatMap(spec => {
|
|
212
|
+
const m = /^\.\/templates\/([a-z0-9-]+)\.tsx$/.exec(spec);
|
|
213
|
+
return m
|
|
214
|
+
? [
|
|
215
|
+
{
|
|
216
|
+
name: `templates/${m[1]}.tsx`,
|
|
217
|
+
from: path.join(TEMPLATES_DIR, m[1], 'page.tsx'),
|
|
218
|
+
},
|
|
219
|
+
]
|
|
220
|
+
: [];
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* How a generated page imports the theme: its built module, its stylesheet,
|
|
226
|
+
* and the stylesheets of open fonts the project has installed.
|
|
227
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
228
|
+
* @param {string} slug
|
|
229
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
230
|
+
* @param {string} fromDir the folder of the page
|
|
231
|
+
*/
|
|
232
|
+
export function themeImports(ctx, slug, data, fromDir) {
|
|
233
|
+
const f = themeFiles(ctx.project.cwd, slug);
|
|
234
|
+
/** @param {string} file */
|
|
235
|
+
const rel = file => {
|
|
236
|
+
const r = path.relative(fromDir, file).split(path.sep).join('/');
|
|
237
|
+
return r.startsWith('.') ? r : `./${r}`;
|
|
238
|
+
};
|
|
239
|
+
const modes = modesOf(data);
|
|
240
|
+
const fonts = fontPackages(ctx, data)
|
|
241
|
+
.filter(x => x.installed)
|
|
242
|
+
.map(x => `${x.pkg}/index.css`);
|
|
243
|
+
return {
|
|
244
|
+
module: rel(f.js),
|
|
245
|
+
css: [rel(f.css), ...fonts],
|
|
246
|
+
mode: /** @type {'system' | 'light' | 'dark'} */ (
|
|
247
|
+
modes.length > 1 ? 'system' : modes[0]
|
|
248
|
+
),
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Render the sample app page with the theme to themes/<slug>/preview.html.
|
|
254
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
255
|
+
* @param {string} slug
|
|
256
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
257
|
+
* @returns {Promise<string>} the file written, as shown
|
|
258
|
+
*/
|
|
259
|
+
export async function renderPreview(ctx, slug, data) {
|
|
260
|
+
const {project} = ctx;
|
|
261
|
+
const imports = /** @type {Record<string, string>} */ ({});
|
|
262
|
+
for (const n of elementNames(SAMPLE_PAGE)) {
|
|
263
|
+
const spec = importPathOf(project, n);
|
|
264
|
+
if (spec) imports[n] = spec;
|
|
265
|
+
}
|
|
266
|
+
const source = pageSource({
|
|
267
|
+
items: [{name: 'page', label: '', tree: SAMPLE_PAGE}],
|
|
268
|
+
imports,
|
|
269
|
+
theme: themeImports(ctx, slug, data, renderDir(project)),
|
|
270
|
+
layout: 'page',
|
|
271
|
+
title: `${slug}: sample page`,
|
|
272
|
+
});
|
|
273
|
+
const {html} = await renderGenerated(project, `preview-${slug}.mjs`, source);
|
|
274
|
+
const out = themeFiles(project.cwd, slug).preview;
|
|
275
|
+
fs.writeFileSync(out, html);
|
|
276
|
+
return shown(project.cwd, out);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Render the theme-building page with the theme to one HTML file.
|
|
281
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
282
|
+
* @param {string} slug
|
|
283
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
284
|
+
* @param {{group?: string, out?: string}} [options]
|
|
285
|
+
* @returns {Promise<{file: string, components: number, skipped: string[]}>}
|
|
286
|
+
*/
|
|
287
|
+
export async function renderSheet(ctx, slug, data, options = {}) {
|
|
288
|
+
const {project} = ctx;
|
|
289
|
+
const {items, imports, skipped} = showcaseItems(project, options.group);
|
|
290
|
+
const source = pageSource({
|
|
291
|
+
items,
|
|
292
|
+
imports,
|
|
293
|
+
theme: themeImports(ctx, slug, data, renderDir(project)),
|
|
294
|
+
layout: 'sheet',
|
|
295
|
+
title: `${slug}: every component${options.group ? ` (${options.group})` : ''}`,
|
|
296
|
+
});
|
|
297
|
+
const {html} = await renderGenerated(
|
|
298
|
+
project,
|
|
299
|
+
`sheet-${slug}.mjs`,
|
|
300
|
+
source,
|
|
301
|
+
templateFiles(imports),
|
|
302
|
+
);
|
|
303
|
+
const out = options.out ?? themeFiles(project.cwd, slug).sheet;
|
|
304
|
+
fs.mkdirSync(path.dirname(out), {recursive: true});
|
|
305
|
+
fs.writeFileSync(out, html);
|
|
306
|
+
return {
|
|
307
|
+
file: shown(project.cwd, out),
|
|
308
|
+
components: new Set(items.map(i => i.name)).size,
|
|
309
|
+
skipped,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** @param {unknown} v @returns {v is import('./_catalog.mjs').Tree} */
|
|
314
|
+
const isElement = v =>
|
|
315
|
+
Boolean(
|
|
316
|
+
v &&
|
|
317
|
+
typeof v === 'object' &&
|
|
318
|
+
!Array.isArray(v) &&
|
|
319
|
+
typeof (/** @type {any} */ (v).__element) === 'string',
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The Core name an element stands for, or null when Core has no module for
|
|
324
|
+
* it. Some playground samples still use the old `XDS` prefix (`XDSVStack`).
|
|
325
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
326
|
+
* @param {string} name
|
|
327
|
+
* @returns {string | null}
|
|
328
|
+
*/
|
|
329
|
+
function coreName(project, name) {
|
|
330
|
+
if (importPathOf(project, name)) return name;
|
|
331
|
+
const bare = name.replace(/^XDS(?=[A-Z])/, '');
|
|
332
|
+
return bare !== name && importPathOf(project, bare) ? bare : null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* A copy of an element tree with Core names only. An element Core has no
|
|
337
|
+
* module for is left out.
|
|
338
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
339
|
+
* @param {unknown} v
|
|
340
|
+
* @returns {unknown}
|
|
341
|
+
*/
|
|
342
|
+
function withCoreNames(project, v) {
|
|
343
|
+
if (Array.isArray(v))
|
|
344
|
+
return v.map(x => withCoreNames(project, x)).filter(x => x !== undefined);
|
|
345
|
+
if (isElement(v)) {
|
|
346
|
+
const name = coreName(project, v.__element);
|
|
347
|
+
if (!name) return undefined;
|
|
348
|
+
const props = Object.fromEntries(
|
|
349
|
+
Object.entries(v.props ?? {})
|
|
350
|
+
.map(([k, x]) => [k, withCoreNames(project, x)])
|
|
351
|
+
.filter(([, x]) => x !== undefined),
|
|
352
|
+
);
|
|
353
|
+
return {
|
|
354
|
+
__element: name,
|
|
355
|
+
props,
|
|
356
|
+
...(v.children === undefined
|
|
357
|
+
? {}
|
|
358
|
+
: {children: withCoreNames(project, v.children)}),
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
if (v && typeof v === 'object')
|
|
362
|
+
return Object.fromEntries(
|
|
363
|
+
Object.entries(v).map(([k, x]) => [k, withCoreNames(project, x)]),
|
|
364
|
+
);
|
|
365
|
+
return v;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** @param {unknown} v @returns {boolean} */
|
|
369
|
+
const holdsElement = v =>
|
|
370
|
+
isElement(v) || (Array.isArray(v) && v.some(holdsElement));
|
|
371
|
+
|
|
372
|
+
/** @param {unknown} v @param {number} depth @returns {string} */
|
|
373
|
+
function jsxValue(v, depth) {
|
|
374
|
+
if (isElement(v)) return jsx(v, depth);
|
|
375
|
+
if (Array.isArray(v) && v.some(holdsElement))
|
|
376
|
+
return `[${v
|
|
377
|
+
.map((x, i) =>
|
|
378
|
+
isElement(x)
|
|
379
|
+
? jsx({...x, props: {key: String(i), ...x.props}}, depth)
|
|
380
|
+
: jsxValue(x, depth),
|
|
381
|
+
)
|
|
382
|
+
.join(', ')}]`;
|
|
383
|
+
return JSON.stringify(v);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* JSX source for an element tree whose names are Core names. Each element
|
|
388
|
+
* renders through `C`, the page's table of Core components.
|
|
389
|
+
* @param {import('./_catalog.mjs').Tree} tree
|
|
390
|
+
* @param {number} depth
|
|
391
|
+
* @returns {string}
|
|
392
|
+
*/
|
|
393
|
+
function jsx(tree, depth) {
|
|
394
|
+
const pad = ' '.repeat(depth);
|
|
395
|
+
const {children: childProp, ...rest} = tree.props ?? {};
|
|
396
|
+
const props = Object.entries(rest).map(([k, v]) =>
|
|
397
|
+
typeof v === 'string' && !/[{}"<>&\n]/.test(v)
|
|
398
|
+
? `${k}="${v}"`
|
|
399
|
+
: v === true
|
|
400
|
+
? k
|
|
401
|
+
: `${k}={${jsxValue(v, depth + 1)}}`,
|
|
402
|
+
);
|
|
403
|
+
const kids = /** @type {unknown[]} */ (
|
|
404
|
+
[].concat(/** @type {any} */ (tree.children ?? childProp ?? []))
|
|
405
|
+
);
|
|
406
|
+
const tag = `C.${tree.__element}`;
|
|
407
|
+
const open = `<${tag}${props.length ? ` ${props.join(' ')}` : ''}`;
|
|
408
|
+
if (!kids.length) return `${open} />`;
|
|
409
|
+
const inner = kids
|
|
410
|
+
.map(k =>
|
|
411
|
+
isElement(k)
|
|
412
|
+
? `${pad} ${jsx(k, depth + 1)}`
|
|
413
|
+
: `${pad} {${jsxValue(k, depth + 1)}}`,
|
|
414
|
+
)
|
|
415
|
+
.join('\n');
|
|
416
|
+
return `${open}>\n${inner}\n${pad}</${tag}>`;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* The imports of a page and its table of components, `C`. Playground samples
|
|
421
|
+
* are not full prop sets (a controlled part has no handler), so `C` takes
|
|
422
|
+
* any props for each part; children must still be React nodes, and every
|
|
423
|
+
* name must be imported from Core.
|
|
424
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
425
|
+
* @param {unknown} trees element trees with Core names
|
|
426
|
+
* @returns {string[]}
|
|
427
|
+
*/
|
|
428
|
+
function componentTable(project, trees) {
|
|
429
|
+
/** @type {Map<string, string[]>} */
|
|
430
|
+
const byModule = new Map();
|
|
431
|
+
for (const name of [...elementNames(trees)].sort()) {
|
|
432
|
+
const spec = /** @type {string} */ (importPathOf(project, name));
|
|
433
|
+
byModule.set(spec, [...(byModule.get(spec) ?? []), name]);
|
|
434
|
+
}
|
|
435
|
+
const names = [...byModule.values()].flat();
|
|
436
|
+
return [
|
|
437
|
+
...[...byModule].map(
|
|
438
|
+
([spec, list]) =>
|
|
439
|
+
`import {${list.join(', ')}} from ${JSON.stringify(spec)};`,
|
|
440
|
+
),
|
|
441
|
+
'',
|
|
442
|
+
'type Part = React.ComponentType<{children?: React.ReactNode} & Record<string, unknown>>;',
|
|
443
|
+
`const C = {${names.map(n => `${n}: ${n} as unknown as Part`).join(', ')}};`,
|
|
444
|
+
];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const SAFE = [
|
|
448
|
+
'/** Shows nothing for a component that throws, so the rest of the page renders. */',
|
|
449
|
+
'class Safe extends React.Component<{children: React.ReactNode}, {failed: boolean}> {',
|
|
450
|
+
' state = {failed: false};',
|
|
451
|
+
' static getDerivedStateFromError() {',
|
|
452
|
+
' return {failed: true};',
|
|
453
|
+
' }',
|
|
454
|
+
' render() {',
|
|
455
|
+
' return this.state.failed ? null : this.props.children;',
|
|
456
|
+
' }',
|
|
457
|
+
'}',
|
|
458
|
+
];
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* The source of the preview's components page: every component grouped,
|
|
462
|
+
* each variant side by side, overlays open. Template pages are pages of
|
|
463
|
+
* their own (`preview add --template`), so they are not on it.
|
|
464
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
465
|
+
* @returns {{source: string, components: number}}
|
|
466
|
+
*/
|
|
467
|
+
export function componentsPageSource(project) {
|
|
468
|
+
const items = showcaseItems(project, undefined)
|
|
469
|
+
.items.filter(it => it.group !== 'pages')
|
|
470
|
+
.map(it => ({...it, tree: withCoreNames(project, it.tree)}))
|
|
471
|
+
.filter(it => it.tree !== undefined);
|
|
472
|
+
/** @type {Map<string, Map<string, import('./_catalog.mjs').CatalogItem[]>>} */
|
|
473
|
+
const groups = new Map();
|
|
474
|
+
for (const it of items) {
|
|
475
|
+
const g = groups.get(it.group ?? '') ?? new Map();
|
|
476
|
+
const list = /** @type {import('./_catalog.mjs').CatalogItem[]} */ (
|
|
477
|
+
g.get(it.name) ?? []
|
|
478
|
+
);
|
|
479
|
+
if (!list.some(x => x.label === it.label)) list.push(it);
|
|
480
|
+
g.set(it.name, list);
|
|
481
|
+
groups.set(it.group ?? '', g);
|
|
482
|
+
}
|
|
483
|
+
const sections = [...groups]
|
|
484
|
+
.map(([g, comps]) => {
|
|
485
|
+
const cells = [...comps]
|
|
486
|
+
.map(([name, list]) => {
|
|
487
|
+
const body = list
|
|
488
|
+
.map(
|
|
489
|
+
it =>
|
|
490
|
+
` <div className="tp-item">${it.label ? `\n <div className="tp-label">${it.label}</div>` : ''}\n <Safe>\n ${jsx(/** @type {any} */ (it.tree), 7)}\n </Safe>\n </div>`,
|
|
491
|
+
)
|
|
492
|
+
.join('\n');
|
|
493
|
+
return ` <div className="tp-cell">\n <div className="tp-name">${name}</div>\n <div className="tp-row">\n${body}\n </div>\n </div>`;
|
|
494
|
+
})
|
|
495
|
+
.join('\n');
|
|
496
|
+
return ` <section>\n <h2 className="tp-group">${g}</h2>\n <div className="tp-grid">\n${cells}\n </div>\n </section>`;
|
|
497
|
+
})
|
|
498
|
+
.join('\n');
|
|
499
|
+
const css = `.tp-group{font:600 14px ui-monospace,monospace;margin:16px 14px 0;opacity:.75}.tp-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:14px;padding:14px}.tp-cell{border:1px dashed #8885;padding:10px;overflow:hidden}.tp-name{font:600 12px ui-monospace,monospace;opacity:.6;margin-bottom:8px}.tp-row{display:flex;flex-wrap:wrap;gap:10px;align-items:flex-start}.tp-label{font:10px ui-monospace,monospace;opacity:.5}`;
|
|
500
|
+
const source = [
|
|
501
|
+
'// Written by `astryx experimental theme preview add`: every component grouped, each variant side by side, overlays open.',
|
|
502
|
+
"'use client';",
|
|
503
|
+
'',
|
|
504
|
+
"import * as React from 'react';",
|
|
505
|
+
...componentTable(
|
|
506
|
+
project,
|
|
507
|
+
items.map(it => it.tree),
|
|
508
|
+
),
|
|
509
|
+
'',
|
|
510
|
+
...SAFE,
|
|
511
|
+
'',
|
|
512
|
+
'export default function ComponentsPage() {',
|
|
513
|
+
' return (',
|
|
514
|
+
' <div>',
|
|
515
|
+
` <style>{${JSON.stringify(css)}}</style>`,
|
|
516
|
+
sections,
|
|
517
|
+
' </div>',
|
|
518
|
+
' );',
|
|
519
|
+
'}',
|
|
520
|
+
'',
|
|
521
|
+
].join('\n');
|
|
522
|
+
return {source, components: new Set(items.map(i => i.name)).size};
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* The source of the preview's overview page: the sample app page.
|
|
527
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
528
|
+
* @returns {string}
|
|
529
|
+
*/
|
|
530
|
+
export function overviewPageSource(project) {
|
|
531
|
+
const page = withCoreNames(project, SAMPLE_PAGE);
|
|
532
|
+
return [
|
|
533
|
+
'// Written by `astryx experimental theme preview add`: a sample app page.',
|
|
534
|
+
"'use client';",
|
|
535
|
+
'',
|
|
536
|
+
"import * as React from 'react';",
|
|
537
|
+
...componentTable(project, page),
|
|
538
|
+
'',
|
|
539
|
+
'export default function OverviewPage() {',
|
|
540
|
+
` return ${jsx(/** @type {any} */ (page), 1)};`,
|
|
541
|
+
'}',
|
|
542
|
+
'',
|
|
543
|
+
].join('\n');
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* The source of the preview route: an app shell with a side nav item per
|
|
548
|
+
* preview page, a top bar with the theme name and, with a theme, a
|
|
549
|
+
* light/dark/system switch, and the selected page in the content area. It is a normal page of
|
|
550
|
+
* the app, so it keeps the app's layout, global CSS and providers.
|
|
551
|
+
* @param {{pages: Array<{id: string, label: string, file: string}>, theme: {name: string, module: string, css: string[]} | null}} spec
|
|
552
|
+
* `file` relative to the route folder, without extension; no theme: the app's own theme
|
|
553
|
+
* @returns {string}
|
|
554
|
+
*/
|
|
555
|
+
export function previewRouteSource({pages, theme}) {
|
|
556
|
+
const name = theme?.name ?? 'app theme';
|
|
557
|
+
const mode = theme
|
|
558
|
+
? [
|
|
559
|
+
' <SegmentedControl label="Color mode" size="sm" value={mode} onChange={v => setMode(v as Mode)}>',
|
|
560
|
+
' <SegmentedControlItem value="light" label="Light" />',
|
|
561
|
+
' <SegmentedControlItem value="dark" label="Dark" />',
|
|
562
|
+
' <SegmentedControlItem value="system" label="System" />',
|
|
563
|
+
' </SegmentedControl>',
|
|
564
|
+
]
|
|
565
|
+
: [];
|
|
566
|
+
const shell = [
|
|
567
|
+
' <AppShell',
|
|
568
|
+
' height="fill"',
|
|
569
|
+
' topNav={',
|
|
570
|
+
` <TopNav label="Theme preview" heading={<strong>{${JSON.stringify(name)}}</strong>} endContent={`,
|
|
571
|
+
...(mode.length ? mode : [' null']),
|
|
572
|
+
' } />',
|
|
573
|
+
' }',
|
|
574
|
+
' sideNav={',
|
|
575
|
+
' <SideNav>',
|
|
576
|
+
' {PAGES.map(p => (',
|
|
577
|
+
' <SideNavItem key={p.id} label={p.label} isSelected={p.id === current.id} onClick={() => setPage(p.id)} />',
|
|
578
|
+
' ))}',
|
|
579
|
+
' </SideNav>',
|
|
580
|
+
' }>',
|
|
581
|
+
' <current.Page />',
|
|
582
|
+
' </AppShell>',
|
|
583
|
+
];
|
|
584
|
+
return [
|
|
585
|
+
'// Written by `astryx experimental theme preview add`. `preview add`, `preview remove` and',
|
|
586
|
+
'// theme-preview.json keep it: edit the list with those commands, not by hand.',
|
|
587
|
+
"'use client';",
|
|
588
|
+
'',
|
|
589
|
+
"import * as React from 'react';",
|
|
590
|
+
"import {AppShell} from '@astryxdesign/core/AppShell';",
|
|
591
|
+
"import {SideNav, SideNavItem} from '@astryxdesign/core/SideNav';",
|
|
592
|
+
"import {TopNav} from '@astryxdesign/core/TopNav';",
|
|
593
|
+
...(theme
|
|
594
|
+
? [
|
|
595
|
+
"import {SegmentedControl, SegmentedControlItem} from '@astryxdesign/core/SegmentedControl';",
|
|
596
|
+
"import {Theme} from '@astryxdesign/core/theme';",
|
|
597
|
+
`import {${camelName(theme.name)}Theme as theme} from ${JSON.stringify(theme.module.replace(/\.js$/, ''))};`,
|
|
598
|
+
...theme.css.map(c => `import ${JSON.stringify(c)};`),
|
|
599
|
+
]
|
|
600
|
+
: []),
|
|
601
|
+
...pages.map(
|
|
602
|
+
(p, i) => `import Page${i} from ${JSON.stringify(`./${p.file}`)};`,
|
|
603
|
+
),
|
|
604
|
+
'',
|
|
605
|
+
...(theme ? ["type Mode = 'light' | 'dark' | 'system';", ''] : []),
|
|
606
|
+
'const PAGES = [',
|
|
607
|
+
...pages.map(
|
|
608
|
+
(p, i) =>
|
|
609
|
+
` {id: ${JSON.stringify(p.id)}, label: ${JSON.stringify(p.label)}, Page: Page${i}},`,
|
|
610
|
+
),
|
|
611
|
+
'];',
|
|
612
|
+
'',
|
|
613
|
+
'export default function ThemePreviewPage() {',
|
|
614
|
+
' const [page, setPage] = React.useState(PAGES[0]?.id);',
|
|
615
|
+
...(theme
|
|
616
|
+
? [" const [mode, setMode] = React.useState<Mode>('system');"]
|
|
617
|
+
: []),
|
|
618
|
+
' const current = PAGES.find(p => p.id === page) ?? PAGES[0];',
|
|
619
|
+
' return (',
|
|
620
|
+
...(theme
|
|
621
|
+
? [' <Theme theme={theme} mode={mode}>', ...shell, ' </Theme>']
|
|
622
|
+
: [' <>', ...shell, ' </>']),
|
|
623
|
+
' );',
|
|
624
|
+
'}',
|
|
625
|
+
'',
|
|
626
|
+
].join('\n');
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** @param {string} slug */
|
|
630
|
+
const camelName = slug => slug.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* A PNG of an HTML file, with a Playwright the project has installed, else
|
|
634
|
+
* a Chrome or Chromium found on the machine. Null with a reason when neither
|
|
635
|
+
* is there.
|
|
636
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
637
|
+
* @param {string} html absolute
|
|
638
|
+
* @param {string} png absolute
|
|
639
|
+
* @param {'light' | 'dark'} mode
|
|
640
|
+
* @returns {Promise<{ok: true, file: string} | {ok: false, reason: string}>}
|
|
641
|
+
*/
|
|
642
|
+
export async function screenshot(project, html, png, mode) {
|
|
643
|
+
for (const pkg of ['playwright', 'playwright-core']) {
|
|
644
|
+
/** @type {string} */
|
|
645
|
+
let entry;
|
|
646
|
+
try {
|
|
647
|
+
entry = project.req.resolve(pkg);
|
|
648
|
+
} catch {
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
const pw = await import(pathToFileURL(entry).href);
|
|
653
|
+
const chromium = pw.chromium ?? pw.default?.chromium;
|
|
654
|
+
const browser = await chromium.launch(
|
|
655
|
+
chromeBinary() ? {executablePath: chromeBinary()} : {},
|
|
656
|
+
);
|
|
657
|
+
try {
|
|
658
|
+
const page = await browser.newPage({
|
|
659
|
+
viewport: {width: 1280, height: 900},
|
|
660
|
+
colorScheme: mode,
|
|
661
|
+
});
|
|
662
|
+
await page.goto(pathToFileURL(html).href, {waitUntil: 'load'});
|
|
663
|
+
await page.screenshot({path: png, fullPage: true});
|
|
664
|
+
} finally {
|
|
665
|
+
await browser.close();
|
|
666
|
+
}
|
|
667
|
+
return {ok: true, file: shown(project.cwd, png)};
|
|
668
|
+
} catch {
|
|
669
|
+
// This Playwright has no browser to launch: try Chrome directly.
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
const chrome = chromeBinary();
|
|
673
|
+
if (chrome) {
|
|
674
|
+
const r = spawnSync(
|
|
675
|
+
chrome,
|
|
676
|
+
[
|
|
677
|
+
'--headless=new',
|
|
678
|
+
'--disable-gpu',
|
|
679
|
+
'--no-sandbox',
|
|
680
|
+
'--hide-scrollbars',
|
|
681
|
+
`--screenshot=${png}`,
|
|
682
|
+
'--window-size=1280,2400',
|
|
683
|
+
...(mode === 'dark' ? ['--force-dark-mode'] : []),
|
|
684
|
+
pathToFileURL(html).href,
|
|
685
|
+
],
|
|
686
|
+
{encoding: 'utf8', timeout: 60_000},
|
|
687
|
+
);
|
|
688
|
+
if (r.status === 0 && fs.existsSync(png))
|
|
689
|
+
return {ok: true, file: shown(project.cwd, png)};
|
|
690
|
+
}
|
|
691
|
+
return {
|
|
692
|
+
ok: false,
|
|
693
|
+
reason: `No browser was found to take the picture. Install Playwright with its browser (${installLine(project.cwd, ['playwright'])}, then npx playwright install chromium), or install Google Chrome or Chromium. The HTML preview needs neither.`,
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/** A Chrome or Chromium on this machine, at a usual place or on PATH. */
|
|
698
|
+
function chromeBinary() {
|
|
699
|
+
const names = [
|
|
700
|
+
'google-chrome',
|
|
701
|
+
'google-chrome-stable',
|
|
702
|
+
'chromium',
|
|
703
|
+
'chromium-browser',
|
|
704
|
+
'chrome',
|
|
705
|
+
];
|
|
706
|
+
const fixed = [
|
|
707
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
708
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
709
|
+
'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
|
|
710
|
+
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
|
|
711
|
+
];
|
|
712
|
+
for (const f of fixed) if (fs.existsSync(f)) return f;
|
|
713
|
+
for (const dir of (process.env.PATH ?? '').split(path.delimiter)) {
|
|
714
|
+
for (const n of names) {
|
|
715
|
+
const f = path.join(dir, n);
|
|
716
|
+
if (dir && fs.existsSync(f)) return f;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return null;
|
|
720
|
+
}
|