@astryxdesign/cli 0.7.13 → 0.7.14-canary.d08ce65
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 +10 -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 +96 -0
- package/api/theme-edit/_adapter.mjs +1039 -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 +842 -0
- package/api/theme-edit/_color.d.mts +35 -0
- package/api/theme-edit/_color.mjs +125 -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 +86 -0
- package/api/theme-edit/_preview.mjs +564 -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 +848 -0
- package/api/theme-edit/_tables.d.mts +130 -0
- package/api/theme-edit/_tables.mjs +697 -0
- package/api/theme-edit/theme-edit.d.mts +89 -0
- package/api/theme-edit/theme-edit.mjs +119 -0
- package/api/theme-edit/theme-edit.type.d.mts +270 -0
- package/api/theme-edit/theme-edit.type.mjs +155 -0
- package/api/theme-edit/themeEditExplain.doc.d.mts +6 -0
- package/api/theme-edit/themeEditExplain.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/themeEditPreview.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreview.doc.mjs +96 -0
- package/api/theme-edit/themeEditReset.doc.d.mts +6 -0
- package/api/theme-edit/themeEditReset.doc.mjs +161 -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 +80 -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/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 +542 -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-eject.doc.mjs +71 -0
- package/clients/cli/commands/experimental-theme-explain.doc.mjs +84 -0
- package/clients/cli/commands/experimental-theme-list.doc.mjs +49 -0
- package/clients/cli/commands/experimental-theme-new.doc.mjs +66 -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.doc.mjs +76 -0
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme-reset.doc.mjs +103 -0
- package/clients/cli/commands/experimental-theme-set.doc.mjs +121 -0
- package/clients/cli/commands/experimental-theme-show.doc.mjs +66 -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-undo.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme-use.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme.doc.mjs +66 -0
- package/clients/cli/commands/experimental.doc.mjs +10 -3
- package/clients/cli/commands/theme.mjs +101 -34
- package/clients/cli/index.mjs +16 -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 +43 -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,1039 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The theme-edit subject's adapter: the work behind `astryx experimental
|
|
5
|
+
* theme`, and its only access to the environment (files, the installed Core,
|
|
6
|
+
* the renderer). theme-edit.mjs is the public API over it.
|
|
7
|
+
*
|
|
8
|
+
* themeEditNew('brand', {like: 'neutral'}) themeEdit.new
|
|
9
|
+
* themeEditSet('brand', 'corners', 'pill') themeEdit.set
|
|
10
|
+
* themeEditReset('brand', 'corners') themeEdit.reset
|
|
11
|
+
* themeEditShow('brand') themeEdit.show
|
|
12
|
+
* themeEditUndo('brand') themeEdit.undo
|
|
13
|
+
* themeEditPreview('brand', {write: 'src/app/theme'}) themeEdit.preview
|
|
14
|
+
* themeEditExplain('banner') themeEdit.explain
|
|
15
|
+
* themeEditExplain('white text in the banner') themeEdit.explain (a question)
|
|
16
|
+
* themeEditExplain('target.png', {at: ['4,8']}) themeEdit.explain (a picture)
|
|
17
|
+
* themeEditExplain('accent', {list: 'tokens'}) themeEdit.explain (a list)
|
|
18
|
+
*
|
|
19
|
+
* Every change is checked first against what the installed Core's parts
|
|
20
|
+
* really read; a change that cannot take effect is refused and nothing is
|
|
21
|
+
* written. An accepted change rebuilds the theme with the theme build and
|
|
22
|
+
* writes a preview.
|
|
23
|
+
*
|
|
24
|
+
* @input a theme name and a change, a question, or a picture
|
|
25
|
+
* @output themeEdit.* envelopes
|
|
26
|
+
* @position api/theme-edit — adapter for api/theme-edit/theme-edit.mjs
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import * as fs from 'node:fs';
|
|
30
|
+
import * as path from 'node:path';
|
|
31
|
+
import {AstryxError} from '../error.mjs';
|
|
32
|
+
import {isOutsideProject} from '../../foundation/env/project-scope.mjs';
|
|
33
|
+
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
34
|
+
import {
|
|
35
|
+
assertWithin,
|
|
36
|
+
PathSafetyError,
|
|
37
|
+
} from '../../foundation/fs/path-safety.mjs';
|
|
38
|
+
import {CSS_PROPERTIES} from './_css-properties.mjs';
|
|
39
|
+
import {
|
|
40
|
+
AUTO_SPREAD,
|
|
41
|
+
CLOSEST_OPEN_FONT,
|
|
42
|
+
COMMON_PROPERTIES,
|
|
43
|
+
DIALS,
|
|
44
|
+
FAMILIES,
|
|
45
|
+
GROUPS,
|
|
46
|
+
KEY_TOKENS,
|
|
47
|
+
LIKE,
|
|
48
|
+
OPEN_FONTS,
|
|
49
|
+
P,
|
|
50
|
+
SYSTEM_FONTS,
|
|
51
|
+
familyOf,
|
|
52
|
+
} from './_tables.mjs';
|
|
53
|
+
import {accentSet} from './_color.mjs';
|
|
54
|
+
import {colorsAt, decodePng, mainColors} from './_png.mjs';
|
|
55
|
+
import {makeCompiler} from './_compiler.mjs';
|
|
56
|
+
import {openProject, installLine} from './_project.mjs';
|
|
57
|
+
import {partData} from './_parts.mjs';
|
|
58
|
+
import {
|
|
59
|
+
commitTheme,
|
|
60
|
+
compileTheme,
|
|
61
|
+
contrastProblems,
|
|
62
|
+
describe,
|
|
63
|
+
fontOf,
|
|
64
|
+
fontPackages,
|
|
65
|
+
keyOf,
|
|
66
|
+
loadTheme,
|
|
67
|
+
modesOf,
|
|
68
|
+
nearest,
|
|
69
|
+
partsOf,
|
|
70
|
+
refuse,
|
|
71
|
+
resolvedTokens,
|
|
72
|
+
shown,
|
|
73
|
+
slugOk,
|
|
74
|
+
themeFiles,
|
|
75
|
+
buildTheme,
|
|
76
|
+
} from './_settings.mjs';
|
|
77
|
+
import {ASPECTS, answerHow, checkSetting, wholeThemeTokens} from './_check.mjs';
|
|
78
|
+
import {MODAL} from './_compiler.mjs';
|
|
79
|
+
import {
|
|
80
|
+
SHOWCASE_GROUPS,
|
|
81
|
+
renderPreview,
|
|
82
|
+
renderSheet,
|
|
83
|
+
screenshot,
|
|
84
|
+
writeShowcase,
|
|
85
|
+
} from './_preview.mjs';
|
|
86
|
+
import {baseResolved} from './_project.mjs';
|
|
87
|
+
|
|
88
|
+
const E = ERROR_CODES;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @typedef {import('./theme-edit.type.mjs').ThemeEditChangeOptions} ChangeOptions
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Refuse outside a project: a theme is written into an app, and every check
|
|
96
|
+
* reads the app's own Core.
|
|
97
|
+
* @param {string} cwd
|
|
98
|
+
*/
|
|
99
|
+
function requireProject(cwd) {
|
|
100
|
+
if (!isOutsideProject(cwd)) return;
|
|
101
|
+
throw new AstryxError(
|
|
102
|
+
'No package.json found. astryx experimental theme works inside an app project, which it writes the theme into and whose installed Core it checks every change against. Nothing was written.',
|
|
103
|
+
[],
|
|
104
|
+
E.ERR_PROJECT_REQUIRED,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @param {string} cwd */
|
|
109
|
+
async function context(cwd) {
|
|
110
|
+
requireProject(cwd);
|
|
111
|
+
const project = await openProject(cwd);
|
|
112
|
+
const parts = await partData(project);
|
|
113
|
+
return {project, C: makeCompiler(project, parts), parts};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Pictures of HTML files, on request.
|
|
118
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
119
|
+
* @param {Array<{html: string, png: string}>} shots
|
|
120
|
+
* @param {Array<'light' | 'dark'>} modes
|
|
121
|
+
* @returns {Promise<{files: string[], problem: string | null}>}
|
|
122
|
+
*/
|
|
123
|
+
async function pictures(project, shots, modes) {
|
|
124
|
+
/** @type {string[]} */
|
|
125
|
+
const files = [];
|
|
126
|
+
for (const {html, png} of shots) {
|
|
127
|
+
for (const m of modes) {
|
|
128
|
+
const out =
|
|
129
|
+
modes.length > 1 && m === 'dark'
|
|
130
|
+
? png.replace(/\.png$/, '-dark.png')
|
|
131
|
+
: png;
|
|
132
|
+
const r = await screenshot(project, html, out, m);
|
|
133
|
+
if (!r.ok) return {files, problem: r.reason};
|
|
134
|
+
files.push(r.file);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return {files, problem: null};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Font notes for the settings: the import line for an open font, the
|
|
142
|
+
* install line when its package is missing, and a warning for a family no
|
|
143
|
+
* package loads, naming the closest open font.
|
|
144
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
145
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
146
|
+
* @returns {string[]}
|
|
147
|
+
*/
|
|
148
|
+
function fontNotes(ctx, data) {
|
|
149
|
+
/** @type {string[]} */
|
|
150
|
+
const notes = [];
|
|
151
|
+
for (const {pkg, installed} of fontPackages(ctx, data)) {
|
|
152
|
+
notes.push(
|
|
153
|
+
installed
|
|
154
|
+
? `the app loads this font with one import: import '${pkg}';`
|
|
155
|
+
: `${pkg} is not installed, so previews fall back to the next font: ${installLine(ctx.project.cwd, [pkg])}; then the app imports it once: import '${pkg}';`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
for (const s of data.settings) {
|
|
159
|
+
if (
|
|
160
|
+
s.thing !== 'font' ||
|
|
161
|
+
SYSTEM_FONTS.includes(s.value) ||
|
|
162
|
+
OPEN_FONTS[s.value] ||
|
|
163
|
+
fontOf(s.value).pkg ||
|
|
164
|
+
s.value === 'rounded' ||
|
|
165
|
+
s.value === 'comic'
|
|
166
|
+
)
|
|
167
|
+
continue;
|
|
168
|
+
const family = fontOf(s.value).family.replace(/["']/g, '').toLowerCase();
|
|
169
|
+
const near = CLOSEST_OPEN_FONT[family];
|
|
170
|
+
notes.push(
|
|
171
|
+
`"${fontOf(s.value).family.replace(/["']/g, '')}" is not a system font and no package loads it: the app must load it, or it falls back.${near ? ` The closest open font is ${near}: ${P} set ${data.name} font ${near}${s.on ? ` --on ${s.on}` : ''}` : ''}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return notes;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The accent note: accent text in each mode, made readable in the direction
|
|
179
|
+
* that mode needs, while buttons keep the color exactly.
|
|
180
|
+
* @param {string} value
|
|
181
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
182
|
+
* @param {'light' | 'dark' | undefined} only
|
|
183
|
+
*/
|
|
184
|
+
function accentNote(value, data, only) {
|
|
185
|
+
const a = accentSet(value);
|
|
186
|
+
const ta = /** @type {string[]} */ (
|
|
187
|
+
[].concat(/** @type {any} */ (a['--color-text-accent']))
|
|
188
|
+
);
|
|
189
|
+
if (ta.length < 2) return null;
|
|
190
|
+
const modes = only ? [only] : modesOf(data);
|
|
191
|
+
/** @type {string[]} */
|
|
192
|
+
const parts = [];
|
|
193
|
+
if (modes.includes('light') && ta[0].toLowerCase() !== value.toLowerCase())
|
|
194
|
+
parts.push(
|
|
195
|
+
`in light, accent-colored text is ${ta[0]} (darker, so it reads at 4.5:1 on light surfaces)`,
|
|
196
|
+
);
|
|
197
|
+
if (modes.includes('dark') && ta[1].toLowerCase() !== value.toLowerCase())
|
|
198
|
+
parts.push(
|
|
199
|
+
`in dark, accent-colored text and icons are ${ta[1]} (lighter, so they read at 4.5:1 on dark surfaces)`,
|
|
200
|
+
);
|
|
201
|
+
return parts.length
|
|
202
|
+
? `${parts.join('; ')}; buttons and fills keep ${value} exactly`
|
|
203
|
+
: null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* What a change did to part styles (component settings), one line each.
|
|
208
|
+
* @param {any} before
|
|
209
|
+
* @param {any} after
|
|
210
|
+
* @returns {string[]}
|
|
211
|
+
*/
|
|
212
|
+
function styledChanges(before, after) {
|
|
213
|
+
/** @type {string[]} */
|
|
214
|
+
const out = [];
|
|
215
|
+
const a = before?.components ?? {};
|
|
216
|
+
const b = after?.components ?? {};
|
|
217
|
+
for (const target of new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
218
|
+
for (const key of new Set([
|
|
219
|
+
...Object.keys(a[target] ?? {}),
|
|
220
|
+
...Object.keys(b[target] ?? {}),
|
|
221
|
+
])) {
|
|
222
|
+
const x = a[target]?.[key] ?? {};
|
|
223
|
+
const y = b[target]?.[key] ?? {};
|
|
224
|
+
for (const prop of new Set([...Object.keys(x), ...Object.keys(y)])) {
|
|
225
|
+
if (JSON.stringify(x[prop]) === JSON.stringify(y[prop])) continue;
|
|
226
|
+
const name = prop.startsWith('--')
|
|
227
|
+
? prop
|
|
228
|
+
: prop.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
|
|
229
|
+
out.push(
|
|
230
|
+
`${target}${key === 'base' ? '' : ` ${key}`}: ${name} ${y[prop] === undefined ? '(removed)' : typeof y[prop] === 'string' ? y[prop] : JSON.stringify(y[prop])}`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const ta = JSON.stringify(before?.typography ?? {});
|
|
236
|
+
const tb = JSON.stringify(after?.typography ?? {});
|
|
237
|
+
if (ta !== tb) {
|
|
238
|
+
for (const role of ['body', 'heading', 'code']) {
|
|
239
|
+
const x = after?.typography?.[role];
|
|
240
|
+
if (
|
|
241
|
+
JSON.stringify(before?.typography?.[role] ?? null) !==
|
|
242
|
+
JSON.stringify(x ?? null)
|
|
243
|
+
)
|
|
244
|
+
out.push(
|
|
245
|
+
`typography ${role}: ${
|
|
246
|
+
x
|
|
247
|
+
? Object.entries(x)
|
|
248
|
+
.map(([k, v]) => `${k} ${v}`)
|
|
249
|
+
.join(', ')
|
|
250
|
+
: '(removed)'
|
|
251
|
+
}`,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
if (
|
|
255
|
+
JSON.stringify(before?.typography?.scale ?? null) !==
|
|
256
|
+
JSON.stringify(after?.typography?.scale ?? null)
|
|
257
|
+
)
|
|
258
|
+
out.push(
|
|
259
|
+
`typography scale: base ${after?.typography?.scale?.base ?? 'default'}`,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Previews after a change: the sample page always, pictures on request.
|
|
267
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
268
|
+
* @param {string} slug
|
|
269
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
270
|
+
* @param {boolean} png
|
|
271
|
+
*/
|
|
272
|
+
async function previewAfter(ctx, slug, data, png) {
|
|
273
|
+
const preview = await renderPreview(ctx, slug, data);
|
|
274
|
+
const f = themeFiles(ctx.project.cwd, slug);
|
|
275
|
+
const shots = png
|
|
276
|
+
? await pictures(
|
|
277
|
+
ctx.project,
|
|
278
|
+
[{html: f.preview, png: f.previewPng}],
|
|
279
|
+
modesOf(data),
|
|
280
|
+
)
|
|
281
|
+
: {files: [], problem: null};
|
|
282
|
+
return {preview, pictures: shots.files, pictureProblem: shots.problem};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Start a theme in themes/<name>/, optionally on a built-in theme.
|
|
287
|
+
* @experimental
|
|
288
|
+
* @param {string} name
|
|
289
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditNewOptions} [options]
|
|
290
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditNewResponse>}
|
|
291
|
+
*/
|
|
292
|
+
export async function themeEditNew(name, options = {}) {
|
|
293
|
+
const {cwd = process.cwd(), like, png = false} = options;
|
|
294
|
+
requireProject(cwd);
|
|
295
|
+
if (!slugOk(name))
|
|
296
|
+
refuse(
|
|
297
|
+
E.ERR_INVALID_ARGUMENT,
|
|
298
|
+
`"${name ?? ''}" is not a theme name (lowercase letters, digits, dashes, starting with a letter).`,
|
|
299
|
+
{run: [`${P} new my-theme`]},
|
|
300
|
+
);
|
|
301
|
+
const f = themeFiles(cwd, name);
|
|
302
|
+
if (fs.existsSync(f.settings))
|
|
303
|
+
refuse(E.ERR_FILE_EXISTS, `Theme "${name}" already exists.`, {
|
|
304
|
+
run: [`${P} show ${name}`, `${P} set ${name} base <theme>`],
|
|
305
|
+
});
|
|
306
|
+
if (fs.existsSync(f.dir) && fs.readdirSync(f.dir).length)
|
|
307
|
+
refuse(
|
|
308
|
+
E.ERR_FILE_EXISTS,
|
|
309
|
+
`themes/${name}/ already holds files this command did not make; choose another name.`,
|
|
310
|
+
{run: [`${P} new ${name}-2`]},
|
|
311
|
+
);
|
|
312
|
+
if (like && !LIKE.includes(like))
|
|
313
|
+
refuse(
|
|
314
|
+
E.ERR_THEME_BAD_VALUE,
|
|
315
|
+
`--like takes ${LIKE.join(', ')}, not "${like}".`,
|
|
316
|
+
{didYouMean: nearest(like, LIKE)},
|
|
317
|
+
);
|
|
318
|
+
const ctx = await context(cwd);
|
|
319
|
+
/** @type {import('./_settings.mjs').ThemeData} */
|
|
320
|
+
const data = {name, ...(like ? {like} : {}), mode: 'both', settings: []};
|
|
321
|
+
const r = await commitTheme(ctx, name, data, {history: false});
|
|
322
|
+
const p = await previewAfter(ctx, name, data, png);
|
|
323
|
+
return {
|
|
324
|
+
type: 'themeEdit.new',
|
|
325
|
+
data: {
|
|
326
|
+
theme: name,
|
|
327
|
+
like: like ?? null,
|
|
328
|
+
files: {
|
|
329
|
+
settings: shown(cwd, f.settings),
|
|
330
|
+
source: shown(cwd, f.ts),
|
|
331
|
+
css: shown(cwd, f.css),
|
|
332
|
+
module: shown(cwd, f.js),
|
|
333
|
+
},
|
|
334
|
+
problems: r.problems,
|
|
335
|
+
...p,
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Change one thing in a theme: checked, built, previewed.
|
|
342
|
+
* @experimental
|
|
343
|
+
* @param {string} theme
|
|
344
|
+
* @param {string} thing a dial, a token, or a CSS property
|
|
345
|
+
* @param {string} value
|
|
346
|
+
* @param {ChangeOptions} [options]
|
|
347
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditChangeResponse>}
|
|
348
|
+
*/
|
|
349
|
+
export async function themeEditSet(theme, thing, value, options = {}) {
|
|
350
|
+
return change('set', theme, thing, value, options);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Return one setting to the base theme's value: the setting is removed,
|
|
355
|
+
* and the theme is rebuilt and previewed.
|
|
356
|
+
* @experimental
|
|
357
|
+
* @param {string} theme
|
|
358
|
+
* @param {string} thing
|
|
359
|
+
* @param {ChangeOptions & {value?: string}} [options] `value` names which half of
|
|
360
|
+
* the `headings` dial to reset (`weight` or `tracking`)
|
|
361
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditChangeResponse>}
|
|
362
|
+
*/
|
|
363
|
+
export async function themeEditReset(theme, thing, options = {}) {
|
|
364
|
+
return change('reset', theme, thing, options.value, options);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @param {'set' | 'reset'} sub
|
|
369
|
+
* @param {string} slug
|
|
370
|
+
* @param {string} thing
|
|
371
|
+
* @param {string | undefined} value
|
|
372
|
+
* @param {ChangeOptions} options
|
|
373
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditChangeResponse>}
|
|
374
|
+
*/
|
|
375
|
+
async function change(sub, slug, thing, value, options) {
|
|
376
|
+
const {cwd = process.cwd(), png = false} = options;
|
|
377
|
+
requireProject(cwd);
|
|
378
|
+
const data = loadTheme(cwd, slug);
|
|
379
|
+
const ctx = await context(cwd);
|
|
380
|
+
const flags = {
|
|
381
|
+
on: options.on,
|
|
382
|
+
when: options.when,
|
|
383
|
+
state: options.state,
|
|
384
|
+
mode: options.mode,
|
|
385
|
+
only: options.only,
|
|
386
|
+
};
|
|
387
|
+
const s = checkSetting(ctx, slug, data, thing, value, flags, sub === 'reset');
|
|
388
|
+
const base = await baseResolved(ctx.project, data.like);
|
|
389
|
+
const compiledBefore = compileTheme(ctx, data, base);
|
|
390
|
+
if (sub === 'set' && (s.thing === 'base' || s.thing === 'mode')) {
|
|
391
|
+
const before = s.thing === 'base' ? (data.like ?? 'none') : data.mode;
|
|
392
|
+
if (s.thing === 'base') {
|
|
393
|
+
if (s.value === 'none') delete data.like;
|
|
394
|
+
else data.like = s.value;
|
|
395
|
+
} else
|
|
396
|
+
data.mode = /** @type {'both' | 'light-only' | 'dark-only'} */ (s.value);
|
|
397
|
+
const r = await commitTheme(ctx, slug, data, {history: true});
|
|
398
|
+
const p = await previewAfter(ctx, slug, data, png);
|
|
399
|
+
return {
|
|
400
|
+
type: 'themeEdit.set',
|
|
401
|
+
data: {
|
|
402
|
+
theme: slug,
|
|
403
|
+
action: 'set',
|
|
404
|
+
setting: s,
|
|
405
|
+
replaced: null,
|
|
406
|
+
was: before,
|
|
407
|
+
parts: [],
|
|
408
|
+
family: null,
|
|
409
|
+
changedTokens: r.changed,
|
|
410
|
+
styled: [],
|
|
411
|
+
kept: [],
|
|
412
|
+
lifted: r.lifted,
|
|
413
|
+
notes: [`your ${data.settings.length} settings are kept`],
|
|
414
|
+
problems: r.problems,
|
|
415
|
+
warnings: r.warnings,
|
|
416
|
+
...p,
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
const k = keyOf(s);
|
|
421
|
+
const prevIdx = data.settings.findIndex(x => keyOf(x) === k);
|
|
422
|
+
const prev = prevIdx >= 0 ? data.settings[prevIdx] : null;
|
|
423
|
+
if (sub === 'reset') {
|
|
424
|
+
if (!prev) {
|
|
425
|
+
const near = data.settings.filter(x => x.thing === s.thing).map(describe);
|
|
426
|
+
refuse(
|
|
427
|
+
E.ERR_THEME_NOT_SET,
|
|
428
|
+
`The theme does not set ${describe({...s, value: '?'}).replace(' = ?', '')}.`,
|
|
429
|
+
{
|
|
430
|
+
also: near.length
|
|
431
|
+
? [`settings for ${s.thing}: ${near.join('; ')}`]
|
|
432
|
+
: [],
|
|
433
|
+
run: [`${P} show ${slug}`],
|
|
434
|
+
},
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
data.settings.splice(prevIdx, 1);
|
|
438
|
+
} else if (prev) data.settings[prevIdx] = s;
|
|
439
|
+
else data.settings.push(s);
|
|
440
|
+
const r = await commitTheme(ctx, slug, data, {history: true});
|
|
441
|
+
const styled = styledChanges(compiledBefore, compileTheme(ctx, data, base));
|
|
442
|
+
const parts = partsOf(ctx, s);
|
|
443
|
+
const kept =
|
|
444
|
+
sub === 'set' && s.source === 'dial' && !s.on
|
|
445
|
+
? data.settings.filter(x => x.thing === s.thing && x.on).map(describe)
|
|
446
|
+
: [];
|
|
447
|
+
/** @type {string[]} */
|
|
448
|
+
const notes = [];
|
|
449
|
+
const fam = s.on
|
|
450
|
+
? FAMILIES[s.on]
|
|
451
|
+
? s.on
|
|
452
|
+
: parts.length > 1
|
|
453
|
+
? familyOf(s.on)
|
|
454
|
+
: null
|
|
455
|
+
: null;
|
|
456
|
+
if (s.note) notes.push(s.note);
|
|
457
|
+
if (s.thing === 'accent' && sub === 'set' && !s.on) {
|
|
458
|
+
const n = accentNote(s.value, data, s.mode);
|
|
459
|
+
if (n) notes.push(n);
|
|
460
|
+
}
|
|
461
|
+
if (s.thing === 'font' && sub === 'set')
|
|
462
|
+
notes.push(...fontNotes(ctx, {...data, settings: [s]}));
|
|
463
|
+
const p = await previewAfter(ctx, slug, data, png);
|
|
464
|
+
return {
|
|
465
|
+
type: sub === 'set' ? 'themeEdit.set' : 'themeEdit.reset',
|
|
466
|
+
data: {
|
|
467
|
+
theme: slug,
|
|
468
|
+
action: sub,
|
|
469
|
+
setting:
|
|
470
|
+
sub === 'set'
|
|
471
|
+
? s
|
|
472
|
+
: /** @type {import('./_settings.mjs').Setting} */ (prev),
|
|
473
|
+
replaced: sub === 'set' ? prev : null,
|
|
474
|
+
was: null,
|
|
475
|
+
parts,
|
|
476
|
+
family: fam
|
|
477
|
+
? {
|
|
478
|
+
name: fam,
|
|
479
|
+
members: parts,
|
|
480
|
+
onlyHint: !FAMILIES[s.on ?? ''] && AUTO_SPREAD.includes(fam),
|
|
481
|
+
}
|
|
482
|
+
: null,
|
|
483
|
+
changedTokens: r.changed,
|
|
484
|
+
styled,
|
|
485
|
+
kept,
|
|
486
|
+
lifted: r.lifted,
|
|
487
|
+
notes,
|
|
488
|
+
problems: r.problems,
|
|
489
|
+
warnings: r.warnings,
|
|
490
|
+
...p,
|
|
491
|
+
},
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Every setting of a theme, key resolved values, problems it caused, the
|
|
497
|
+
* sample page and the theme-building page with every component.
|
|
498
|
+
* @experimental
|
|
499
|
+
* @param {string} theme
|
|
500
|
+
* @param {{cwd?: string, png?: boolean, group?: string}} [options]
|
|
501
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditShowResponse>}
|
|
502
|
+
*/
|
|
503
|
+
export async function themeEditShow(theme, options = {}) {
|
|
504
|
+
const {cwd = process.cwd(), png = false, group} = options;
|
|
505
|
+
requireProject(cwd);
|
|
506
|
+
checkGroup(group);
|
|
507
|
+
const data = loadTheme(cwd, theme);
|
|
508
|
+
const ctx = await context(cwd);
|
|
509
|
+
const f = themeFiles(cwd, theme);
|
|
510
|
+
if (!fs.existsSync(f.js)) await buildTheme(ctx, theme, data);
|
|
511
|
+
const res = await resolvedTokens(ctx, theme);
|
|
512
|
+
const problems = await contrastProblems(ctx, data, res);
|
|
513
|
+
const preview = await renderPreview(ctx, theme, data);
|
|
514
|
+
const sheet = await renderSheet(ctx, theme, data, {group});
|
|
515
|
+
const modes = modesOf(data);
|
|
516
|
+
const shots = png
|
|
517
|
+
? await pictures(
|
|
518
|
+
ctx.project,
|
|
519
|
+
[
|
|
520
|
+
{html: f.preview, png: f.previewPng},
|
|
521
|
+
{html: f.sheet, png: f.sheetPng},
|
|
522
|
+
],
|
|
523
|
+
modes,
|
|
524
|
+
)
|
|
525
|
+
: {files: [], problem: null};
|
|
526
|
+
return {
|
|
527
|
+
type: 'themeEdit.show',
|
|
528
|
+
data: {
|
|
529
|
+
theme,
|
|
530
|
+
like: data.like ?? null,
|
|
531
|
+
mode: data.mode,
|
|
532
|
+
settings: data.settings.map(s => ({...s, parts: partsOf(ctx, s)})),
|
|
533
|
+
resolved: Object.fromEntries(
|
|
534
|
+
KEY_TOKENS.map(k => [
|
|
535
|
+
k,
|
|
536
|
+
Object.fromEntries(modes.map(m => [m, res[m][k] ?? null])),
|
|
537
|
+
]),
|
|
538
|
+
),
|
|
539
|
+
problems,
|
|
540
|
+
notes: fontNotes(ctx, data),
|
|
541
|
+
preview,
|
|
542
|
+
sheet: sheet.file,
|
|
543
|
+
sheetComponents: sheet.components,
|
|
544
|
+
skipped: sheet.skipped,
|
|
545
|
+
pictures: shots.files,
|
|
546
|
+
pictureProblem: shots.problem,
|
|
547
|
+
},
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Put the theme back as it was before its last change.
|
|
553
|
+
* @experimental
|
|
554
|
+
* @param {string} theme
|
|
555
|
+
* @param {{cwd?: string, png?: boolean}} [options]
|
|
556
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditUndoResponse>}
|
|
557
|
+
*/
|
|
558
|
+
export async function themeEditUndo(theme, options = {}) {
|
|
559
|
+
const {cwd = process.cwd(), png = false} = options;
|
|
560
|
+
requireProject(cwd);
|
|
561
|
+
const current = loadTheme(cwd, theme);
|
|
562
|
+
const f = themeFiles(cwd, theme);
|
|
563
|
+
const history = fs.existsSync(f.history)
|
|
564
|
+
? JSON.parse(fs.readFileSync(f.history, 'utf8'))
|
|
565
|
+
: [];
|
|
566
|
+
if (!history.length)
|
|
567
|
+
refuse(
|
|
568
|
+
E.ERR_THEME_NOTHING_TO_UNDO,
|
|
569
|
+
`Nothing to undo: ${theme} has no earlier change.`,
|
|
570
|
+
{run: [`${P} show ${theme}`]},
|
|
571
|
+
);
|
|
572
|
+
const ctx = await context(cwd);
|
|
573
|
+
const prevText = history.pop();
|
|
574
|
+
const prev = /** @type {import('./_settings.mjs').ThemeData} */ (
|
|
575
|
+
JSON.parse(prevText)
|
|
576
|
+
);
|
|
577
|
+
await commitTheme(ctx, theme, prev, {history: false});
|
|
578
|
+
fs.writeFileSync(f.history, JSON.stringify(history));
|
|
579
|
+
/** @type {string[]} */
|
|
580
|
+
const undone = [];
|
|
581
|
+
const now = new Map(prev.settings.map(s => [keyOf(s), s]));
|
|
582
|
+
const was = new Map(current.settings.map(s => [keyOf(s), s]));
|
|
583
|
+
for (const [k, s] of was) {
|
|
584
|
+
const back = now.get(k);
|
|
585
|
+
if (!back) undone.push(`removed ${describe(s)}`);
|
|
586
|
+
else if (back.value !== s.value)
|
|
587
|
+
undone.push(`${describe(s)} is back to ${back.value}`);
|
|
588
|
+
}
|
|
589
|
+
for (const [k, s] of now)
|
|
590
|
+
if (!was.has(k)) undone.push(`put back ${describe(s)}`);
|
|
591
|
+
if ((current.like ?? 'none') !== (prev.like ?? 'none'))
|
|
592
|
+
undone.push(
|
|
593
|
+
`base ${current.like ?? 'none'} is back to ${prev.like ?? 'none'}`,
|
|
594
|
+
);
|
|
595
|
+
if (current.mode !== prev.mode)
|
|
596
|
+
undone.push(`mode ${current.mode} is back to ${prev.mode}`);
|
|
597
|
+
const p = await previewAfter(ctx, theme, prev, png);
|
|
598
|
+
return {
|
|
599
|
+
type: 'themeEdit.undo',
|
|
600
|
+
data: {
|
|
601
|
+
theme,
|
|
602
|
+
undone,
|
|
603
|
+
settings: prev.settings.length,
|
|
604
|
+
stepsLeft: history.length,
|
|
605
|
+
...p,
|
|
606
|
+
},
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** @param {string | undefined} group */
|
|
611
|
+
function checkGroup(group) {
|
|
612
|
+
if (group != null && !SHOWCASE_GROUPS.includes(group))
|
|
613
|
+
refuse(
|
|
614
|
+
E.ERR_INVALID_ARGUMENT,
|
|
615
|
+
`--group takes ${SHOWCASE_GROUPS.join(', ')}, not "${group}".`,
|
|
616
|
+
{didYouMean: nearest(group, SHOWCASE_GROUPS)},
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* The theme-building page: every component grouped, each variant side by
|
|
622
|
+
* side, overlays open, and full template pages. Rendered to one HTML file,
|
|
623
|
+
* or with `write` written into the app as a page module.
|
|
624
|
+
* @experimental
|
|
625
|
+
* @param {string} theme
|
|
626
|
+
* @param {{cwd?: string, write?: string, group?: string, png?: boolean}} [options]
|
|
627
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewResponse>}
|
|
628
|
+
*/
|
|
629
|
+
export async function themeEditPreview(theme, options = {}) {
|
|
630
|
+
const {cwd = process.cwd(), write, group, png = false} = options;
|
|
631
|
+
requireProject(cwd);
|
|
632
|
+
checkGroup(group);
|
|
633
|
+
if (write != null && png)
|
|
634
|
+
refuse(
|
|
635
|
+
E.ERR_INVALID_ARGUMENT,
|
|
636
|
+
'--png pictures the rendered HTML page; with --write the page runs in your app instead, so drop one of them.',
|
|
637
|
+
);
|
|
638
|
+
const data = loadTheme(cwd, theme);
|
|
639
|
+
const ctx = await context(cwd);
|
|
640
|
+
const f = themeFiles(cwd, theme);
|
|
641
|
+
if (!fs.existsSync(f.js)) await buildTheme(ctx, theme, data);
|
|
642
|
+
if (write != null) {
|
|
643
|
+
/** @type {string} */
|
|
644
|
+
let dir;
|
|
645
|
+
try {
|
|
646
|
+
dir = assertWithin(path.resolve(cwd, write), cwd, {
|
|
647
|
+
allowAbsolute: true,
|
|
648
|
+
label: '--write',
|
|
649
|
+
});
|
|
650
|
+
} catch (e) {
|
|
651
|
+
if (e instanceof PathSafetyError)
|
|
652
|
+
refuse(
|
|
653
|
+
E.ERR_PATH_TRAVERSAL,
|
|
654
|
+
`--write ${write} is outside the current folder; write inside the folder you run the command from.`,
|
|
655
|
+
);
|
|
656
|
+
throw e;
|
|
657
|
+
}
|
|
658
|
+
const r = writeShowcase(ctx, theme, data, dir, group);
|
|
659
|
+
return {
|
|
660
|
+
type: 'themeEdit.preview',
|
|
661
|
+
data: {
|
|
662
|
+
theme,
|
|
663
|
+
group: group ?? null,
|
|
664
|
+
mode: 'page',
|
|
665
|
+
files: r.files,
|
|
666
|
+
html: null,
|
|
667
|
+
components: r.components,
|
|
668
|
+
skipped: r.skipped,
|
|
669
|
+
pictures: [],
|
|
670
|
+
pictureProblem: null,
|
|
671
|
+
},
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
const out = group ? path.join(f.dir, `sheet-${group}.html`) : f.sheet;
|
|
675
|
+
const sheet = await renderSheet(ctx, theme, data, {group, out});
|
|
676
|
+
const shots = png
|
|
677
|
+
? await pictures(
|
|
678
|
+
ctx.project,
|
|
679
|
+
[{html: out, png: out.replace(/\.html$/, '.png')}],
|
|
680
|
+
modesOf(data),
|
|
681
|
+
)
|
|
682
|
+
: {files: [], problem: null};
|
|
683
|
+
return {
|
|
684
|
+
type: 'themeEdit.preview',
|
|
685
|
+
data: {
|
|
686
|
+
theme,
|
|
687
|
+
group: group ?? null,
|
|
688
|
+
mode: 'html',
|
|
689
|
+
files: [sheet.file],
|
|
690
|
+
html: sheet.file,
|
|
691
|
+
components: sheet.components,
|
|
692
|
+
skipped: sheet.skipped,
|
|
693
|
+
pictures: shots.files,
|
|
694
|
+
pictureProblem: shots.problem,
|
|
695
|
+
},
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/** What `explain --list` takes. */
|
|
700
|
+
const EXPLAIN_LISTS = ['dials', 'tokens', 'parts', 'families', 'properties'];
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Explain anything a theme change can name, or answer a question about one:
|
|
704
|
+
* a part (what it reads for each aspect, its variants, states and the parts
|
|
705
|
+
* inside it), a family (its members), a setting (a dial's rungs, a token's
|
|
706
|
+
* type and readers, a CSS property), an aspect (colors, text, corners...: the
|
|
707
|
+
* whole-theme tokens and dial), a plain-words question (exact commands), a
|
|
708
|
+
* PNG picture (its main colors, or the color at points), or with `list` a
|
|
709
|
+
* list of dials, tokens, parts, families or CSS properties.
|
|
710
|
+
* @experimental
|
|
711
|
+
* @param {string} subject a part, family, setting, aspect, question, or PNG file; with `list`, a filter
|
|
712
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditExplainOptions} [options]
|
|
713
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditExplainResponse>}
|
|
714
|
+
*/
|
|
715
|
+
export async function themeEditExplain(subject, options = {}) {
|
|
716
|
+
const {cwd = process.cwd(), list, at = [], count} = options;
|
|
717
|
+
requireProject(cwd);
|
|
718
|
+
const t = (subject ?? '').trim();
|
|
719
|
+
if (list != null) {
|
|
720
|
+
if (at.length)
|
|
721
|
+
refuse(
|
|
722
|
+
E.ERR_INVALID_ARGUMENT,
|
|
723
|
+
'--at reads a picture; --list lists names, so drop one of them.',
|
|
724
|
+
);
|
|
725
|
+
return {
|
|
726
|
+
type: 'themeEdit.explain',
|
|
727
|
+
data: {kind: 'list', subject: t, ...(await listItems(list, t, cwd))},
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
if (/\.png$/i.test(t))
|
|
731
|
+
return {
|
|
732
|
+
type: 'themeEdit.explain',
|
|
733
|
+
data: {kind: 'picture', subject: t, ...pictureColors(t, cwd, at, count)},
|
|
734
|
+
};
|
|
735
|
+
if (at.length)
|
|
736
|
+
refuse(
|
|
737
|
+
E.ERR_INVALID_ARGUMENT,
|
|
738
|
+
'--at reads the color at points of a PNG picture; name the picture first.',
|
|
739
|
+
{run: [`${P} explain picture.png --at "40,20"`]},
|
|
740
|
+
);
|
|
741
|
+
if (count != null)
|
|
742
|
+
refuse(E.ERR_INVALID_ARGUMENT, 'A count applies only to a picture.', {
|
|
743
|
+
run: [`${P} explain picture.png ${count}`],
|
|
744
|
+
});
|
|
745
|
+
if (!t) {
|
|
746
|
+
refuse(
|
|
747
|
+
E.ERR_MISSING_ARGUMENT,
|
|
748
|
+
'Name a part, a family, a setting, an aspect, a question in quotes, or a PNG picture.',
|
|
749
|
+
{
|
|
750
|
+
run: [
|
|
751
|
+
`${P} explain card`,
|
|
752
|
+
`${P} explain "white text in the banner"`,
|
|
753
|
+
`${P} explain --list dials`,
|
|
754
|
+
],
|
|
755
|
+
},
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
const ctx = await context(cwd);
|
|
759
|
+
const {C} = ctx;
|
|
760
|
+
if (FAMILIES[t]) {
|
|
761
|
+
return {
|
|
762
|
+
type: 'themeEdit.explain',
|
|
763
|
+
data: {
|
|
764
|
+
kind: 'family',
|
|
765
|
+
subject: t,
|
|
766
|
+
family: t,
|
|
767
|
+
members: FAMILIES[t],
|
|
768
|
+
spreads: AUTO_SPREAD.includes(t),
|
|
769
|
+
lines: [
|
|
770
|
+
`${t} (family): ${FAMILIES[t].join(', ')}`,
|
|
771
|
+
AUTO_SPREAD.includes(t)
|
|
772
|
+
? ` styling one of them styles all (--only to opt out); ${P} explain <one of them> for details`
|
|
773
|
+
: ` --on ${t} styles all of them; --on <one of them> styles just that one`,
|
|
774
|
+
],
|
|
775
|
+
},
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
if (ctx.project.target.has(t)) {
|
|
779
|
+
const fam = familyOf(t);
|
|
780
|
+
const lines = [...C.explain(t, P)];
|
|
781
|
+
if (fam)
|
|
782
|
+
lines.push(
|
|
783
|
+
AUTO_SPREAD.includes(fam)
|
|
784
|
+
? ` family: ${fam} (styling ${t} styles all of: ${FAMILIES[fam].join(', ')}; --only to opt out)`
|
|
785
|
+
: ` family: ${fam} (--on ${fam} styles all of: ${FAMILIES[fam].join(', ')})`,
|
|
786
|
+
);
|
|
787
|
+
return {
|
|
788
|
+
type: 'themeEdit.explain',
|
|
789
|
+
data: {
|
|
790
|
+
kind: 'part',
|
|
791
|
+
subject: t,
|
|
792
|
+
family: fam,
|
|
793
|
+
variants: C.variantValues(t),
|
|
794
|
+
states: ctx.project.target.get(t)?.states ?? [],
|
|
795
|
+
reads: Object.fromEntries(
|
|
796
|
+
Object.keys(GROUPS).map(g => [g, C.partReads(t, g)]),
|
|
797
|
+
),
|
|
798
|
+
contains: C.MAP[t]?.contains ?? [],
|
|
799
|
+
lines,
|
|
800
|
+
},
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
if (ASPECTS.includes(t)) {
|
|
804
|
+
const aspects = t === 'colors' ? ['text', 'background', 'border'] : [t];
|
|
805
|
+
const results = aspects.flatMap(a => wholeThemeTokens(ctx, a));
|
|
806
|
+
const dial = /** @type {Record<string, string>} */ ({
|
|
807
|
+
corners: 'corners',
|
|
808
|
+
spacing: 'spacing',
|
|
809
|
+
font: 'font',
|
|
810
|
+
size: 'text-size',
|
|
811
|
+
})[t];
|
|
812
|
+
if (t === 'colors' || t === 'text' || t === 'background')
|
|
813
|
+
results.unshift({
|
|
814
|
+
text: DIALS.accent.help,
|
|
815
|
+
commands: DIALS.accent.examples.map(e => `${P} ${e}`),
|
|
816
|
+
});
|
|
817
|
+
if (dial)
|
|
818
|
+
results.unshift({
|
|
819
|
+
text: DIALS[dial].help,
|
|
820
|
+
commands: DIALS[dial].examples.map(e => `${P} ${e}`),
|
|
821
|
+
});
|
|
822
|
+
results.push({
|
|
823
|
+
text: `for one part: ${P} explain <part> lists what it reads for ${t}`,
|
|
824
|
+
commands: [`${P} explain button`],
|
|
825
|
+
});
|
|
826
|
+
return {
|
|
827
|
+
type: 'themeEdit.explain',
|
|
828
|
+
data: {kind: 'aspect', subject: t, results, lines: []},
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
if (DIALS[t]) {
|
|
832
|
+
const d = DIALS[t];
|
|
833
|
+
return {
|
|
834
|
+
type: 'themeEdit.explain',
|
|
835
|
+
data: {
|
|
836
|
+
kind: 'setting',
|
|
837
|
+
subject: t,
|
|
838
|
+
source: 'dial',
|
|
839
|
+
values: d.values,
|
|
840
|
+
lines: [
|
|
841
|
+
`${t} (dial): ${d.values}`,
|
|
842
|
+
` ${d.help}`,
|
|
843
|
+
...d.examples.map(e => ` e.g. ${P} ${e}`),
|
|
844
|
+
],
|
|
845
|
+
},
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
if (t.startsWith('--') && t in ctx.project.core.tokenDefaults) {
|
|
849
|
+
const readers = C.anyReader(t);
|
|
850
|
+
const type = C.typeOf(t);
|
|
851
|
+
return {
|
|
852
|
+
type: 'themeEdit.explain',
|
|
853
|
+
data: {
|
|
854
|
+
kind: 'setting',
|
|
855
|
+
subject: t,
|
|
856
|
+
source: 'token',
|
|
857
|
+
tokenType: type,
|
|
858
|
+
default: String(ctx.project.core.tokenDefaults[t]),
|
|
859
|
+
readBy: readers,
|
|
860
|
+
lines: [
|
|
861
|
+
`${t} (token): a ${type}, default ${ctx.project.core.tokenDefaults[t]}${MODAL.has(type) ? '; may differ in light and dark (--mode)' : '; the same in light and dark'}`,
|
|
862
|
+
` read by ${readers.length} part${readers.length === 1 ? '' : 's'}${readers.length ? `: ${readers.slice(0, 12).join(', ')}${readers.length > 12 ? ', ...' : ''}` : ' (setting it changes nothing)'}`,
|
|
863
|
+
` e.g. ${P} set <theme> ${t} <value>${readers[0] ? ` or only inside one part: --on ${readers[0]}` : ''}`,
|
|
864
|
+
],
|
|
865
|
+
},
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
if (CSS_PROPERTIES.has(t)) {
|
|
869
|
+
const setters = Object.keys(C.MAP).filter(p => t in C.MAP[p].own);
|
|
870
|
+
return {
|
|
871
|
+
type: 'themeEdit.explain',
|
|
872
|
+
data: {
|
|
873
|
+
kind: 'setting',
|
|
874
|
+
subject: t,
|
|
875
|
+
source: 'property',
|
|
876
|
+
setBy: setters,
|
|
877
|
+
lines: [
|
|
878
|
+
`${t} (CSS property): set it on a part or a family with --on; any CSS value works.`,
|
|
879
|
+
` parts that set it now: ${setters.length ? `${setters.slice(0, 12).join(', ')}${setters.length > 12 ? ', ...' : ''}` : 'none (it still applies when you set it)'}`,
|
|
880
|
+
` e.g. ${P} set <theme> ${t} <value> --on ${setters[0] ?? '<part>'}`,
|
|
881
|
+
],
|
|
882
|
+
},
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
const h = answerHow(ctx, t);
|
|
886
|
+
return {
|
|
887
|
+
type: 'themeEdit.explain',
|
|
888
|
+
data: {
|
|
889
|
+
kind: 'question',
|
|
890
|
+
subject: t,
|
|
891
|
+
understood: {parts: h.parts, aspects: h.aspects},
|
|
892
|
+
results: h.results,
|
|
893
|
+
lines: [],
|
|
894
|
+
},
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/**
|
|
899
|
+
* The items of `explain --list`.
|
|
900
|
+
* @param {string} kind
|
|
901
|
+
* @param {string} filter
|
|
902
|
+
* @param {string} cwd
|
|
903
|
+
* @returns {Promise<{list: string, items: Array<Record<string, unknown>>, common?: boolean}>}
|
|
904
|
+
*/
|
|
905
|
+
async function listItems(kind, filter, cwd) {
|
|
906
|
+
if (!EXPLAIN_LISTS.includes(kind))
|
|
907
|
+
refuse(
|
|
908
|
+
E.ERR_INVALID_ARGUMENT,
|
|
909
|
+
`--list takes ${EXPLAIN_LISTS.join(', ')}${kind ? `, not "${kind}"` : ''}.`,
|
|
910
|
+
{
|
|
911
|
+
didYouMean: kind ? nearest(kind, EXPLAIN_LISTS) : [],
|
|
912
|
+
run: [`${P} explain --list dials`],
|
|
913
|
+
},
|
|
914
|
+
);
|
|
915
|
+
const f = (/** @type {string} */ x) =>
|
|
916
|
+
x.toLowerCase().includes(filter.toLowerCase());
|
|
917
|
+
if (kind === 'dials')
|
|
918
|
+
return {
|
|
919
|
+
list: kind,
|
|
920
|
+
items: Object.entries(DIALS)
|
|
921
|
+
.filter(([k]) => f(k))
|
|
922
|
+
.map(([name, d]) => ({
|
|
923
|
+
name,
|
|
924
|
+
values: d.values,
|
|
925
|
+
help: d.help,
|
|
926
|
+
examples: d.examples.map(e => `${P} ${e}`),
|
|
927
|
+
})),
|
|
928
|
+
};
|
|
929
|
+
if (kind === 'families')
|
|
930
|
+
return {
|
|
931
|
+
list: kind,
|
|
932
|
+
items: Object.entries(FAMILIES)
|
|
933
|
+
.filter(([k, v]) => f(k) || v.some(f))
|
|
934
|
+
.map(([name, members]) => ({
|
|
935
|
+
name,
|
|
936
|
+
members,
|
|
937
|
+
spreads: AUTO_SPREAD.includes(name),
|
|
938
|
+
})),
|
|
939
|
+
};
|
|
940
|
+
if (kind === 'properties') {
|
|
941
|
+
const props = filter
|
|
942
|
+
? [...CSS_PROPERTIES].filter(x => f(x)).slice(0, 60)
|
|
943
|
+
: COMMON_PROPERTIES;
|
|
944
|
+
return {list: kind, items: props.map(name => ({name})), common: !filter};
|
|
945
|
+
}
|
|
946
|
+
const ctx = await context(cwd);
|
|
947
|
+
const {C} = ctx;
|
|
948
|
+
if (kind === 'tokens') {
|
|
949
|
+
const toks = Object.keys(ctx.project.core.tokenDefaults).filter(k => f(k));
|
|
950
|
+
return {
|
|
951
|
+
list: kind,
|
|
952
|
+
items: toks.map(k => ({
|
|
953
|
+
name: k,
|
|
954
|
+
type: C.typeOf(k),
|
|
955
|
+
default: String(ctx.project.core.tokenDefaults[k]),
|
|
956
|
+
readBy: C.anyReader(k).length,
|
|
957
|
+
})),
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
const targets = ctx.project.targets.filter(x => f(x.key) || f(x.component));
|
|
961
|
+
return {
|
|
962
|
+
list: kind,
|
|
963
|
+
items: targets.map(x => ({
|
|
964
|
+
name: x.key,
|
|
965
|
+
component: x.component,
|
|
966
|
+
family: familyOf(x.key),
|
|
967
|
+
variants: C.variantValues(x.key),
|
|
968
|
+
states: x.states,
|
|
969
|
+
recorded: Boolean(C.MAP[x.key]),
|
|
970
|
+
})),
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* Read `--at` points: "x,y;x,y", "x,y x,y", repeated, or a flat "a,b,c,d".
|
|
976
|
+
* @param {string[]} at
|
|
977
|
+
*/
|
|
978
|
+
export function parsePoints(at) {
|
|
979
|
+
const nums = at
|
|
980
|
+
.join(' ')
|
|
981
|
+
.split(/[^\d]+/)
|
|
982
|
+
.filter(Boolean)
|
|
983
|
+
.map(Number);
|
|
984
|
+
if (nums.length % 2)
|
|
985
|
+
refuse(
|
|
986
|
+
E.ERR_INVALID_ARGUMENT,
|
|
987
|
+
`--at takes x,y pairs; "${at.join(' ')}" has an odd count of numbers.`,
|
|
988
|
+
{run: [`${P} colors picture.png --at "40,20;300,180"`]},
|
|
989
|
+
);
|
|
990
|
+
/** @type {Array<{x: number, y: number}>} */
|
|
991
|
+
const points = [];
|
|
992
|
+
for (let i = 0; i < nums.length; i += 2)
|
|
993
|
+
points.push({x: nums[i], y: nums[i + 1]});
|
|
994
|
+
return points;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* The main colors of a PNG picture, or the exact color at points.
|
|
999
|
+
* @param {string} file
|
|
1000
|
+
* @param {string} cwd
|
|
1001
|
+
* @param {string[]} at
|
|
1002
|
+
* @param {number | undefined} count
|
|
1003
|
+
*/
|
|
1004
|
+
function pictureColors(file, cwd, at, count = 14) {
|
|
1005
|
+
const abs = path.resolve(cwd, file);
|
|
1006
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isFile())
|
|
1007
|
+
throw new AstryxError(`${file} does not exist.`, [], E.ERR_FILE_NOT_FOUND);
|
|
1008
|
+
if (!Number.isInteger(count) || count <= 0)
|
|
1009
|
+
refuse(
|
|
1010
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1011
|
+
`The count must be a positive whole number, not "${count}".`,
|
|
1012
|
+
);
|
|
1013
|
+
/** @type {import('./_png.mjs').Picture} */
|
|
1014
|
+
let pic;
|
|
1015
|
+
try {
|
|
1016
|
+
pic = decodePng(fs.readFileSync(abs));
|
|
1017
|
+
} catch (e) {
|
|
1018
|
+
throw new AstryxError(
|
|
1019
|
+
`Could not read ${file} as a PNG picture: ${e instanceof Error ? e.message : String(e)}.`,
|
|
1020
|
+
[],
|
|
1021
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1022
|
+
);
|
|
1023
|
+
}
|
|
1024
|
+
if (at.length)
|
|
1025
|
+
return {
|
|
1026
|
+
file,
|
|
1027
|
+
width: pic.width,
|
|
1028
|
+
height: pic.height,
|
|
1029
|
+
colors: null,
|
|
1030
|
+
points: colorsAt(pic, parsePoints(at)),
|
|
1031
|
+
};
|
|
1032
|
+
return {
|
|
1033
|
+
file,
|
|
1034
|
+
width: pic.width,
|
|
1035
|
+
height: pic.height,
|
|
1036
|
+
colors: mainColors(pic, count),
|
|
1037
|
+
points: null,
|
|
1038
|
+
};
|
|
1039
|
+
}
|