@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,848 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Where an `astryx experimental theme` theme lives and how it is made:
|
|
5
|
+
* the CLI-owned settings file (themes/<slug>/<slug>.settings.json), the
|
|
6
|
+
* generated theme source (<slug>.theme.ts), the build through the same code
|
|
7
|
+
* path as `astryx theme build`, the lift pass, resolved tokens, contrast
|
|
8
|
+
* problems, and undo history.
|
|
9
|
+
*
|
|
10
|
+
* @position api/theme-edit — storage and compile for the theme commands
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as fs from 'node:fs';
|
|
14
|
+
import * as path from 'node:path';
|
|
15
|
+
import {pathToFileURL} from 'node:url';
|
|
16
|
+
import {AstryxError} from '../error.mjs';
|
|
17
|
+
import {logger} from '../logger.mjs';
|
|
18
|
+
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
19
|
+
import {themeBuildForReport} from '../theme/build/build.mjs';
|
|
20
|
+
import {
|
|
21
|
+
AUTO_SPREAD,
|
|
22
|
+
CORNERS,
|
|
23
|
+
FAMILIES,
|
|
24
|
+
FONTS,
|
|
25
|
+
HEADING_TRACKING,
|
|
26
|
+
HEADING_WEIGHTS,
|
|
27
|
+
OPEN_FONTS,
|
|
28
|
+
P,
|
|
29
|
+
SPACING,
|
|
30
|
+
SURFACES,
|
|
31
|
+
SURFACE_PARTS,
|
|
32
|
+
TEXT_SIZE,
|
|
33
|
+
TYPE_STYLES,
|
|
34
|
+
familyOf,
|
|
35
|
+
} from './_tables.mjs';
|
|
36
|
+
import {accentSet, parseColor, ratio} from './_color.mjs';
|
|
37
|
+
import {MODAL} from './_compiler.mjs';
|
|
38
|
+
import {baseResolved, baseThemeFile} from './_project.mjs';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* One setting the CLI owns.
|
|
42
|
+
* @typedef {object} Setting
|
|
43
|
+
* @property {string} thing - A dial, a token (`--color-accent`), or a CSS property.
|
|
44
|
+
* @property {string} value
|
|
45
|
+
* @property {'dial' | 'token' | 'property'} source
|
|
46
|
+
* @property {string} [on] - A part, a family, or a typography role (font).
|
|
47
|
+
* @property {string} [when] - `<prop>:<value>`.
|
|
48
|
+
* @property {string} [state]
|
|
49
|
+
* @property {'light' | 'dark'} [mode]
|
|
50
|
+
* @property {boolean} [only]
|
|
51
|
+
* @property {string} [axis] - For the `headings` dial: `weight` or `tracking`.
|
|
52
|
+
* @property {string} [note]
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The settings file.
|
|
57
|
+
* @typedef {object} ThemeData
|
|
58
|
+
* @property {string} name
|
|
59
|
+
* @property {string} [like] - The built-in theme it builds on.
|
|
60
|
+
* @property {'both' | 'light-only' | 'dark-only'} mode
|
|
61
|
+
* @property {Setting[]} settings
|
|
62
|
+
* @property {string[]} [lifted] - Variant keys the last build repeated a part setting on.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/** @typedef {{project: import('./_project.mjs').ThemeProject, C: import('./_compiler.mjs').Compiler}} Ctx */
|
|
66
|
+
|
|
67
|
+
/** @param {string} s */
|
|
68
|
+
export const slugOk = s => /^[a-z][a-z0-9-]*$/.test(s ?? '');
|
|
69
|
+
/** @param {string} s */
|
|
70
|
+
export const kebab = s =>
|
|
71
|
+
s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
72
|
+
/** @param {string} s */
|
|
73
|
+
export const camel = s => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
74
|
+
/** Quote a value for a suggested command. @param {unknown} v */
|
|
75
|
+
export const q = v =>
|
|
76
|
+
/[\s"#(]/.test(String(v)) ? `"${String(v).replace(/"/g, '\\"')}"` : String(v);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The files of one theme, absolute.
|
|
80
|
+
* @param {string} cwd
|
|
81
|
+
* @param {string} slug
|
|
82
|
+
*/
|
|
83
|
+
export function themeFiles(cwd, slug) {
|
|
84
|
+
const dir = path.join(cwd, 'themes', slug);
|
|
85
|
+
return {
|
|
86
|
+
root: path.join(cwd, 'themes'),
|
|
87
|
+
dir,
|
|
88
|
+
settings: path.join(dir, `${slug}.settings.json`),
|
|
89
|
+
ts: path.join(dir, `${slug}.theme.ts`),
|
|
90
|
+
settingsTs: path.join(dir, `${slug}.settings.ts`),
|
|
91
|
+
js: path.join(dir, `${slug}.js`),
|
|
92
|
+
css: path.join(dir, `${slug}.css`),
|
|
93
|
+
history: path.join(dir, `${slug}.history.json`),
|
|
94
|
+
preview: path.join(dir, 'preview.html'),
|
|
95
|
+
sheet: path.join(dir, 'sheet.html'),
|
|
96
|
+
previewPng: path.join(dir, 'preview.png'),
|
|
97
|
+
sheetPng: path.join(dir, 'sheet.png'),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A path as the person sees it: relative to cwd. @param {string} cwd @param {string} f */
|
|
102
|
+
export const shown = (cwd, f) =>
|
|
103
|
+
path.relative(cwd, f).split(path.sep).join('/');
|
|
104
|
+
|
|
105
|
+
/** @param {string} a @param {string} b */
|
|
106
|
+
function lev(a, b) {
|
|
107
|
+
const d = Array.from({length: a.length + 1}, (_, i) => [
|
|
108
|
+
i,
|
|
109
|
+
...Array(b.length).fill(0),
|
|
110
|
+
]);
|
|
111
|
+
for (let j = 1; j <= b.length; j++) d[0][j] = j;
|
|
112
|
+
for (let i = 1; i <= a.length; i++) {
|
|
113
|
+
for (let j = 1; j <= b.length; j++)
|
|
114
|
+
d[i][j] = Math.min(
|
|
115
|
+
d[i - 1][j] + 1,
|
|
116
|
+
d[i][j - 1] + 1,
|
|
117
|
+
d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return d[a.length][b.length];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Up to `n` names close to `x`.
|
|
125
|
+
* @param {string} x
|
|
126
|
+
* @param {Iterable<string>} list
|
|
127
|
+
* @param {number} [n]
|
|
128
|
+
*/
|
|
129
|
+
export function nearest(x, list, n = 3) {
|
|
130
|
+
return [...list]
|
|
131
|
+
.filter(c => c.length > 2)
|
|
132
|
+
.map(
|
|
133
|
+
c =>
|
|
134
|
+
/** @type {[string, number]} */ ([
|
|
135
|
+
c,
|
|
136
|
+
c.includes(x) || x.includes(c) ? 1 : lev(x, c),
|
|
137
|
+
]),
|
|
138
|
+
)
|
|
139
|
+
.filter(([, s]) => s <= Math.max(2, Math.floor(x.length / 3)))
|
|
140
|
+
.sort((a, b) => a[1] - b[1])
|
|
141
|
+
.slice(0, n)
|
|
142
|
+
.map(([c]) => c);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A refusal: nothing was changed, and the suggestions lead with the command
|
|
147
|
+
* that does what the person wanted.
|
|
148
|
+
* @param {string} code
|
|
149
|
+
* @param {string} message
|
|
150
|
+
* @param {{run?: string[], didYouMean?: string[], fix?: string, also?: string[]}} [extra]
|
|
151
|
+
* @returns {never}
|
|
152
|
+
*/
|
|
153
|
+
export function refuse(code, message, extra = {}) {
|
|
154
|
+
/** @type {Array<{name: string, reason: string}>} */
|
|
155
|
+
const suggestions = [];
|
|
156
|
+
for (const r of extra.run ?? []) {
|
|
157
|
+
const [cmd, comment] = r.split(/\s{2,}#\s*/);
|
|
158
|
+
suggestions.push({
|
|
159
|
+
name: cmd.trim(),
|
|
160
|
+
reason: comment
|
|
161
|
+
? `do this instead: ${comment.trim()}`
|
|
162
|
+
: 'do this instead',
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
for (const d of extra.didYouMean ?? [])
|
|
166
|
+
suggestions.push({name: d, reason: 'did you mean'});
|
|
167
|
+
const tail = [
|
|
168
|
+
extra.fix ? `Fix: ${extra.fix}.` : '',
|
|
169
|
+
...(extra.also ?? []).map(a => `${a.replace(/\.$/, '')}.`),
|
|
170
|
+
'Nothing was changed.',
|
|
171
|
+
]
|
|
172
|
+
.filter(Boolean)
|
|
173
|
+
.join(' ');
|
|
174
|
+
throw new AstryxError(`${message} ${tail}`, suggestions, code);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The settings of a theme, or a refusal naming the command that makes it.
|
|
179
|
+
* @param {string} cwd
|
|
180
|
+
* @param {string} slug
|
|
181
|
+
* @returns {ThemeData}
|
|
182
|
+
*/
|
|
183
|
+
export function loadTheme(cwd, slug) {
|
|
184
|
+
if (!slugOk(slug))
|
|
185
|
+
refuse(
|
|
186
|
+
ERROR_CODES.ERR_INVALID_ARGUMENT,
|
|
187
|
+
`"${slug ?? ''}" is not a theme name (lowercase letters, digits, dashes).`,
|
|
188
|
+
{run: [`${P} new my-theme`]},
|
|
189
|
+
);
|
|
190
|
+
const f = themeFiles(cwd, slug);
|
|
191
|
+
if (!fs.existsSync(f.settings)) {
|
|
192
|
+
const have = fs.existsSync(f.root)
|
|
193
|
+
? fs
|
|
194
|
+
.readdirSync(f.root)
|
|
195
|
+
.filter(d => fs.existsSync(themeFiles(cwd, d).settings))
|
|
196
|
+
: [];
|
|
197
|
+
refuse(
|
|
198
|
+
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
199
|
+
`There is no theme "${slug}" made by this command (no themes/${slug}/${slug}.settings.json).`,
|
|
200
|
+
{didYouMean: nearest(slug, have), run: [`${P} new ${slug}`]},
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
return JSON.parse(fs.readFileSync(f.settings, 'utf8'));
|
|
205
|
+
} catch (e) {
|
|
206
|
+
throw new AstryxError(
|
|
207
|
+
`themes/${slug}/${slug}.settings.json is not valid JSON (${e instanceof Error ? e.message : String(e)}). Fix it by hand, or run ${P} undo ${slug}.`,
|
|
208
|
+
[],
|
|
209
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The identity of a setting: a new one with the same identity replaces it. @param {Setting} s */
|
|
215
|
+
export const keyOf = s =>
|
|
216
|
+
[
|
|
217
|
+
s.thing,
|
|
218
|
+
s.axis ?? '',
|
|
219
|
+
s.on ?? '',
|
|
220
|
+
s.when ?? '',
|
|
221
|
+
s.state ?? '',
|
|
222
|
+
s.mode ?? '',
|
|
223
|
+
s.only ? 'only' : '',
|
|
224
|
+
].join('|');
|
|
225
|
+
|
|
226
|
+
/** One line for a setting. @param {Setting} s */
|
|
227
|
+
export function describe(s) {
|
|
228
|
+
const where = s.on
|
|
229
|
+
? ` on ${s.on}${s.only ? ' only' : ''}${s.when ? ` when ${s.when}` : ''}${s.state ? ` :${s.state}` : ''}`
|
|
230
|
+
: ' (whole theme)';
|
|
231
|
+
return `${s.thing} = ${s.value}${where}${s.mode ? ` [${s.mode} only]` : ''}`;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The parts a setting styles: the family when `--on` names one, the whole
|
|
236
|
+
* family of an always-alike member unless `--only`, else the one part.
|
|
237
|
+
* @param {Ctx} ctx
|
|
238
|
+
* @param {Pick<Setting, 'on' | 'only' | 'thing'>} s
|
|
239
|
+
* @returns {string[]}
|
|
240
|
+
*/
|
|
241
|
+
export function partsOf(ctx, s) {
|
|
242
|
+
if (!s.on) return [];
|
|
243
|
+
if (s.thing === 'font') return [];
|
|
244
|
+
const has = (/** @type {string} */ t) => ctx.project.target.has(t);
|
|
245
|
+
if (FAMILIES[s.on]) return FAMILIES[s.on].filter(has);
|
|
246
|
+
const fam = familyOf(s.on);
|
|
247
|
+
if (fam && AUTO_SPREAD.includes(fam) && !s.only)
|
|
248
|
+
return FAMILIES[fam].filter(has);
|
|
249
|
+
return [s.on];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** @param {ThemeData} d @returns {Array<'light' | 'dark'>} */
|
|
253
|
+
export const modesOf = d =>
|
|
254
|
+
d.mode === 'light-only'
|
|
255
|
+
? ['light']
|
|
256
|
+
: d.mode === 'dark-only'
|
|
257
|
+
? ['dark']
|
|
258
|
+
: ['light', 'dark'];
|
|
259
|
+
|
|
260
|
+
/** @param {string} v */
|
|
261
|
+
export const cornerPx = v =>
|
|
262
|
+
CORNERS.find(([n]) => n === v)?.[1] ?? parseFloat(v);
|
|
263
|
+
/** @param {string} v */
|
|
264
|
+
export const textPx = v => TEXT_SIZE.find(([n]) => n === v)?.[1] ?? Number(v);
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* The family and fallbacks a font value means: a preset, an open font, a
|
|
268
|
+
* whole stack with commas, or one family name.
|
|
269
|
+
* @param {string} value
|
|
270
|
+
* @returns {{family: string, fallbacks: string, pkg: string | null}}
|
|
271
|
+
*/
|
|
272
|
+
export function fontOf(value) {
|
|
273
|
+
// Core quotes the family itself, so it is stored without quotes.
|
|
274
|
+
const unquote = (/** @type {string} */ x) => x.replace(/^["']|["']$/g, '');
|
|
275
|
+
if (FONTS[value])
|
|
276
|
+
return {
|
|
277
|
+
family: FONTS[value][0],
|
|
278
|
+
fallbacks: FONTS[value][1],
|
|
279
|
+
pkg: value === 'rounded' ? OPEN_FONTS.nunito.pkg : null,
|
|
280
|
+
};
|
|
281
|
+
if (OPEN_FONTS[value]) return {...OPEN_FONTS[value]};
|
|
282
|
+
const v = value.trim();
|
|
283
|
+
/** The package of an open font named by its family. @param {string} family */
|
|
284
|
+
const pkgOf = family =>
|
|
285
|
+
Object.values(OPEN_FONTS).find(
|
|
286
|
+
f =>
|
|
287
|
+
f.family.replace(/"/g, '').toLowerCase() ===
|
|
288
|
+
family.replace(/["']/g, '').toLowerCase(),
|
|
289
|
+
)?.pkg ?? null;
|
|
290
|
+
if (v.includes(',')) {
|
|
291
|
+
const parts = v
|
|
292
|
+
.split(',')
|
|
293
|
+
.map(x => x.trim())
|
|
294
|
+
.filter(Boolean);
|
|
295
|
+
return {
|
|
296
|
+
family: unquote(parts[0]),
|
|
297
|
+
fallbacks: parts.slice(1).join(', ') || 'system-ui, sans-serif',
|
|
298
|
+
pkg: pkgOf(parts[0]),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
family: unquote(v),
|
|
303
|
+
fallbacks: 'system-ui, sans-serif',
|
|
304
|
+
pkg: pkgOf(v),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* What a part reads for a property when the theme does not set it, so a
|
|
310
|
+
* one-mode component setting keeps the other mode as it was.
|
|
311
|
+
* @param {Ctx} ctx
|
|
312
|
+
* @param {string} target
|
|
313
|
+
* @param {string} prop camelCase or kebab-case
|
|
314
|
+
* @param {string | undefined} when
|
|
315
|
+
*/
|
|
316
|
+
function partDefault(ctx, target, prop, when) {
|
|
317
|
+
const k = kebab(prop);
|
|
318
|
+
const g = ctx.C.groupOfProp(k === 'background' ? 'background-color' : k);
|
|
319
|
+
const want = (/** @type {import('./_compiler.mjs').Read} */ x) =>
|
|
320
|
+
x.props.some(
|
|
321
|
+
p => p === k || (k === 'background' && p === 'background-color'),
|
|
322
|
+
);
|
|
323
|
+
const reads = g ? ctx.C.partReads(target, g).filter(want) : [];
|
|
324
|
+
const wv = when ? when.replace(':', '=') : null;
|
|
325
|
+
const r =
|
|
326
|
+
(wv && reads.find(x => x.when.some(w => w === wv))) ||
|
|
327
|
+
reads.find(x => x.when.includes('')) ||
|
|
328
|
+
reads.find(x => x.when.some(w => /=default\b/.test(w) && !/:/.test(w))) ||
|
|
329
|
+
reads.find(x => x.when.every(w => !/:|selected/.test(w)));
|
|
330
|
+
return r ? `var(${r.token})` : null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The settings, as a `defineTheme` object.
|
|
335
|
+
* @param {Ctx} ctx
|
|
336
|
+
* @param {ThemeData} data
|
|
337
|
+
* @param {{light: Record<string, string>, dark: Record<string, string>}} base
|
|
338
|
+
* @returns {any}
|
|
339
|
+
*/
|
|
340
|
+
export function compileTheme(ctx, data, base) {
|
|
341
|
+
const TOKENS = ctx.project.core.tokenDefaults;
|
|
342
|
+
/** @type {any} */
|
|
343
|
+
const theme = {name: data.name, tokens: {}, components: {}};
|
|
344
|
+
const modeOnly =
|
|
345
|
+
data.mode === 'light-only'
|
|
346
|
+
? 'light'
|
|
347
|
+
: data.mode === 'dark-only'
|
|
348
|
+
? 'dark'
|
|
349
|
+
: null;
|
|
350
|
+
/** @type {Record<string, {light?: string, dark?: string}>} */
|
|
351
|
+
const tok = {};
|
|
352
|
+
/** @param {string} name @param {string | [string, string]} v @param {string} [mode] */
|
|
353
|
+
const putTok = (name, v, mode) => {
|
|
354
|
+
const t = (tok[name] ??= {});
|
|
355
|
+
if (Array.isArray(v)) {
|
|
356
|
+
if (!mode || mode === 'light') t.light = v[0];
|
|
357
|
+
if (!mode || mode === 'dark') t.dark = v[1];
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (!MODAL.has(ctx.C.typeOf(name)) || modeOnly || !mode) {
|
|
361
|
+
t.light = v;
|
|
362
|
+
t.dark = v;
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
t[/** @type {'light' | 'dark'} */ (mode)] = v;
|
|
366
|
+
};
|
|
367
|
+
/** @param {Setting} s @param {string} target @returns {Record<string, any>} */
|
|
368
|
+
const block = (s, target) => {
|
|
369
|
+
const t = (theme.components[target] ??= {});
|
|
370
|
+
let b = (t[s.when ?? 'base'] ??= {});
|
|
371
|
+
if (s.state) {
|
|
372
|
+
const tg = ctx.project.target.get(target);
|
|
373
|
+
if (tg?.states.includes(s.state) && s.when == null) b = t[s.state] ??= {};
|
|
374
|
+
else if (tg?.states.includes(s.state))
|
|
375
|
+
b = t[`${s.when}+${s.state}`] ??= {};
|
|
376
|
+
else b = b[`:${s.state}`] ??= {};
|
|
377
|
+
}
|
|
378
|
+
return b;
|
|
379
|
+
};
|
|
380
|
+
/** @param {Setting} s @param {string} target @param {string} prop @param {string} v */
|
|
381
|
+
const putComp = (s, target, prop, v) => {
|
|
382
|
+
const b = block(s, target);
|
|
383
|
+
if (s.mode && !modeOnly) {
|
|
384
|
+
const prev = b[prop];
|
|
385
|
+
const other = prev ?? partDefault(ctx, target, prop, s.when) ?? v;
|
|
386
|
+
const split = (/** @type {string} */ x, /** @type {1 | 2} */ side) =>
|
|
387
|
+
x.replace(/^light-dark\((.*),\s*(.*)\)$/, `$${side}`);
|
|
388
|
+
b[prop] =
|
|
389
|
+
s.mode === 'light'
|
|
390
|
+
? `light-dark(${v}, ${prev ? split(prev, 2) : other})`
|
|
391
|
+
: `light-dark(${prev ? split(prev, 1) : other}, ${v})`;
|
|
392
|
+
} else b[prop] = v;
|
|
393
|
+
};
|
|
394
|
+
const headingWeightTokens = Object.keys(TOKENS).filter(k =>
|
|
395
|
+
/^--text-(heading|display)-\d-weight$/.test(k),
|
|
396
|
+
);
|
|
397
|
+
for (const s of data.settings) {
|
|
398
|
+
const parts = partsOf(ctx, s);
|
|
399
|
+
if (s.source === 'dial') {
|
|
400
|
+
if (s.thing === 'corners') {
|
|
401
|
+
const px = cornerPx(s.value);
|
|
402
|
+
const val = (/** @type {number} */ x) =>
|
|
403
|
+
px >= 9999 ? '9999px' : `${x}px`;
|
|
404
|
+
if (parts.length)
|
|
405
|
+
for (const t of parts)
|
|
406
|
+
putComp({...s, mode: undefined}, t, 'borderRadius', val(px));
|
|
407
|
+
else {
|
|
408
|
+
putTok('--radius-element', val(px));
|
|
409
|
+
putTok('--radius-inner', val(Math.round(px * 0.6)));
|
|
410
|
+
putTok(
|
|
411
|
+
'--radius-container',
|
|
412
|
+
px >= 9999 ? '24px' : `${Math.round(px * 1.25)}px`,
|
|
413
|
+
);
|
|
414
|
+
putTok(
|
|
415
|
+
'--radius-page',
|
|
416
|
+
px >= 9999 ? '32px' : `${Math.round(px * 2.5)}px`,
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
} else if (s.thing === 'accent') {
|
|
420
|
+
for (const [k, v] of Object.entries(accentSet(s.value))) {
|
|
421
|
+
if (parts.length)
|
|
422
|
+
for (const t of parts)
|
|
423
|
+
putComp(s, t, k, Array.isArray(v) ? v[0] : v);
|
|
424
|
+
else
|
|
425
|
+
putTok(
|
|
426
|
+
k,
|
|
427
|
+
s.mode
|
|
428
|
+
? Array.isArray(v)
|
|
429
|
+
? s.mode === 'light'
|
|
430
|
+
? v[0]
|
|
431
|
+
: v[1]
|
|
432
|
+
: v
|
|
433
|
+
: v,
|
|
434
|
+
s.mode,
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
} else if (s.thing === 'font') {
|
|
438
|
+
const f = fontOf(s.value);
|
|
439
|
+
theme.typography ??= {};
|
|
440
|
+
for (const r of s.on ? [s.on] : ['body', 'heading'])
|
|
441
|
+
theme.typography[r] = {
|
|
442
|
+
...(theme.typography[r] ?? {}),
|
|
443
|
+
family: f.family,
|
|
444
|
+
fallbacks: f.fallbacks,
|
|
445
|
+
};
|
|
446
|
+
} else if (s.thing === 'surfaces') {
|
|
447
|
+
for (const t of SURFACE_PARTS.filter(x => ctx.project.target.has(x)))
|
|
448
|
+
Object.assign(block({...s, mode: undefined}, t), SURFACES[s.value]);
|
|
449
|
+
} else if (s.thing === 'buttons') {
|
|
450
|
+
const primary = block(
|
|
451
|
+
{...s, mode: undefined, when: 'variant:primary'},
|
|
452
|
+
'button',
|
|
453
|
+
);
|
|
454
|
+
if (s.value === 'outline')
|
|
455
|
+
Object.assign(primary, {
|
|
456
|
+
backgroundColor: 'transparent',
|
|
457
|
+
color: 'var(--color-text-accent)',
|
|
458
|
+
borderStyle: 'solid',
|
|
459
|
+
borderWidth: '1px',
|
|
460
|
+
borderColor: 'var(--color-accent)',
|
|
461
|
+
});
|
|
462
|
+
if (s.value === 'soft')
|
|
463
|
+
Object.assign(primary, {
|
|
464
|
+
backgroundColor: 'var(--color-accent-muted)',
|
|
465
|
+
color: 'var(--color-text-accent)',
|
|
466
|
+
});
|
|
467
|
+
if (s.value === 'caps')
|
|
468
|
+
Object.assign(block({...s, mode: undefined}, 'button'), {
|
|
469
|
+
textTransform: 'uppercase',
|
|
470
|
+
letterSpacing: '0.06em',
|
|
471
|
+
fontWeight: '700',
|
|
472
|
+
flexShrink: '0',
|
|
473
|
+
});
|
|
474
|
+
} else if (s.thing === 'type') {
|
|
475
|
+
const T = TYPE_STYLES[s.value];
|
|
476
|
+
theme.typography ??= {};
|
|
477
|
+
theme.typography.scale = {
|
|
478
|
+
base: theme.typography.scale?.base ?? 14,
|
|
479
|
+
ratio: T.ratio,
|
|
480
|
+
};
|
|
481
|
+
if (T.weight) for (const k of headingWeightTokens) putTok(k, T.weight);
|
|
482
|
+
if (T.tracking)
|
|
483
|
+
block({...s, mode: undefined}, 'heading').letterSpacing = T.tracking;
|
|
484
|
+
} else if (s.thing === 'headings') {
|
|
485
|
+
if (s.axis === 'tracking')
|
|
486
|
+
block({...s, mode: undefined}, 'heading').letterSpacing =
|
|
487
|
+
HEADING_TRACKING[s.value];
|
|
488
|
+
else
|
|
489
|
+
for (const k of headingWeightTokens)
|
|
490
|
+
putTok(k, HEADING_WEIGHTS[s.value]);
|
|
491
|
+
} else if (s.thing === 'text-size') {
|
|
492
|
+
theme.typography ??= {};
|
|
493
|
+
theme.typography.scale = {
|
|
494
|
+
base: textPx(s.value),
|
|
495
|
+
ratio: theme.typography.scale?.ratio ?? 1.2,
|
|
496
|
+
};
|
|
497
|
+
} else if (s.thing === 'spacing') {
|
|
498
|
+
const f = SPACING.find(([n]) => n === s.value)?.[1] ?? 1;
|
|
499
|
+
for (const [k, v] of Object.entries(
|
|
500
|
+
/** @type {Record<string, string>} */ (TOKENS),
|
|
501
|
+
)) {
|
|
502
|
+
let nv = null;
|
|
503
|
+
if (/^--spacing-/.test(k) && /rem$/.test(v))
|
|
504
|
+
nv = `${Number((parseFloat(v) * f).toFixed(3))}rem`;
|
|
505
|
+
if (/^--size-element-/.test(k) && /px$/.test(v))
|
|
506
|
+
nv = `${Math.round(parseFloat(v) * f)}px`;
|
|
507
|
+
if (nv === null) continue;
|
|
508
|
+
if (parts.length) for (const t of parts) block(s, t)[k] = nv;
|
|
509
|
+
else putTok(k, nv);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
} else if (s.source === 'token') {
|
|
513
|
+
if (parts.length) for (const t of parts) putComp(s, t, s.thing, s.value);
|
|
514
|
+
else putTok(s.thing, s.value, s.mode);
|
|
515
|
+
} else if (s.source === 'property') {
|
|
516
|
+
for (const t of parts) putComp(s, t, camel(s.thing), s.value);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
for (const [k, {light, dark}] of Object.entries(tok)) {
|
|
520
|
+
const l = light ?? base.light[k] ?? TOKENS[k];
|
|
521
|
+
const d = dark ?? base.dark[k] ?? TOKENS[k];
|
|
522
|
+
theme.tokens[k] = l === d ? l : [l, d];
|
|
523
|
+
}
|
|
524
|
+
if (!Object.keys(theme.tokens).length) delete theme.tokens;
|
|
525
|
+
if (!Object.keys(theme.components).length) delete theme.components;
|
|
526
|
+
return theme;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* A plain part setting can lose to a more specific rule for the same part
|
|
531
|
+
* and property, such as a base theme's `.astryx-heading[data-level="1"]
|
|
532
|
+
* {font-weight}`. Find those rules in the built stylesheet.
|
|
533
|
+
* @param {string} cssText
|
|
534
|
+
* @param {any} theme
|
|
535
|
+
* @returns {Array<{target: string, key: string, prop: string, value: string}>}
|
|
536
|
+
*/
|
|
537
|
+
export function findOverrides(cssText, theme) {
|
|
538
|
+
/** @type {Array<{target: string, key: string, prop: string, value: string}>} */
|
|
539
|
+
const extra = [];
|
|
540
|
+
for (const [target, keys] of Object.entries(
|
|
541
|
+
/** @type {Record<string, any>} */ (theme.components ?? {}),
|
|
542
|
+
)) {
|
|
543
|
+
const props = Object.keys(keys.base ?? {}).filter(
|
|
544
|
+
p => !p.startsWith('--') && !p.startsWith(':'),
|
|
545
|
+
);
|
|
546
|
+
if (!props.length) continue;
|
|
547
|
+
const re = new RegExp(
|
|
548
|
+
`\\.astryx-${target.replace(/-/g, '\\-')}\\[data-([a-z-]+)="([^"]+)"\\]\\s*\\{([^}]*)\\}`,
|
|
549
|
+
'g',
|
|
550
|
+
);
|
|
551
|
+
for (const m of cssText.matchAll(re)) {
|
|
552
|
+
for (const prop of props) {
|
|
553
|
+
// A prop named for the property itself (weight for font-weight,
|
|
554
|
+
// color for color) is the author's own choice: it keeps winning.
|
|
555
|
+
const css = kebab(prop);
|
|
556
|
+
if (css === m[1] || css.endsWith(`-${m[1]}`)) continue;
|
|
557
|
+
if (new RegExp(`(^|[;\\s])${kebab(prop)}\\s*:`).test(m[3]))
|
|
558
|
+
extra.push({
|
|
559
|
+
target,
|
|
560
|
+
key: `${m[1]}:${m[2]}`,
|
|
561
|
+
prop,
|
|
562
|
+
value: keys.base[prop],
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return extra;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/** The generated theme source. @param {string} slug @param {ThemeData} data @param {any} theme */
|
|
571
|
+
function settingsSource(slug, data, theme) {
|
|
572
|
+
const like = data.like;
|
|
573
|
+
return [
|
|
574
|
+
`// Generated by \`${P}\` from ${slug}.settings.json on every change: do not edit.`,
|
|
575
|
+
`// Change it with \`${P} set ${slug} ...\`; your own changes go in ${slug}.theme.ts.`,
|
|
576
|
+
"import {defineTheme} from '@astryxdesign/core/theme';",
|
|
577
|
+
...(like
|
|
578
|
+
? [
|
|
579
|
+
`import {${like}Theme as base} from '@astryxdesign/theme-${like}/built';`,
|
|
580
|
+
]
|
|
581
|
+
: []),
|
|
582
|
+
'',
|
|
583
|
+
`export const ${camel(slug)}Settings = defineTheme({`,
|
|
584
|
+
...(like ? [' extends: base,'] : []),
|
|
585
|
+
...JSON.stringify({...theme, name: `${slug}-settings`}, null, 2)
|
|
586
|
+
.split('\n')
|
|
587
|
+
.slice(1, -1),
|
|
588
|
+
'});',
|
|
589
|
+
'',
|
|
590
|
+
].join('\n');
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* The theme module, written once: it extends the generated settings and is
|
|
595
|
+
* the person's own from then on, so hand edits in it survive every change.
|
|
596
|
+
* @param {string} slug
|
|
597
|
+
*/
|
|
598
|
+
function themeModuleSource(slug) {
|
|
599
|
+
return [
|
|
600
|
+
`// The ${slug} theme. \`${P}\` wrote this file once and never changes it again.`,
|
|
601
|
+
`// The checked settings live in ${slug}.settings.json and the generated ${slug}.settings.ts;`,
|
|
602
|
+
`// change them with \`${P} set ${slug} ...\`. Add your own changes below: they win over the settings.`,
|
|
603
|
+
"import {defineTheme} from '@astryxdesign/core/theme';",
|
|
604
|
+
`import {${camel(slug)}Settings} from './${slug}.settings';`,
|
|
605
|
+
'',
|
|
606
|
+
`export const ${camel(slug)}Theme = defineTheme({`,
|
|
607
|
+
` extends: ${camel(slug)}Settings,`,
|
|
608
|
+
` name: '${slug}',`,
|
|
609
|
+
'});',
|
|
610
|
+
'',
|
|
611
|
+
].join('\n');
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Generate the theme source, build it with the theme build, then repeat any
|
|
616
|
+
* part setting a more specific rule would beat and build again.
|
|
617
|
+
* @param {Ctx} ctx
|
|
618
|
+
* @param {string} slug
|
|
619
|
+
* @param {ThemeData} data
|
|
620
|
+
* @returns {Promise<{warnings: string[], lifted: string[]}>}
|
|
621
|
+
*/
|
|
622
|
+
export async function buildTheme(ctx, slug, data) {
|
|
623
|
+
const {cwd} = ctx.project;
|
|
624
|
+
const f = themeFiles(cwd, slug);
|
|
625
|
+
if (data.like) baseThemeFile(ctx.project, data.like);
|
|
626
|
+
const theme = compileTheme(
|
|
627
|
+
ctx,
|
|
628
|
+
data,
|
|
629
|
+
await baseResolved(ctx.project, data.like),
|
|
630
|
+
);
|
|
631
|
+
fs.mkdirSync(f.dir, {recursive: true});
|
|
632
|
+
fs.writeFileSync(f.settingsTs, settingsSource(slug, data, theme));
|
|
633
|
+
if (!fs.existsSync(f.ts)) fs.writeFileSync(f.ts, themeModuleSource(slug));
|
|
634
|
+
let receipt = await runBuild(ctx, slug);
|
|
635
|
+
const found = findOverrides(fs.readFileSync(f.css, 'utf8'), theme).filter(
|
|
636
|
+
x => !(theme.components[x.target][x.key] ?? {})[x.prop],
|
|
637
|
+
);
|
|
638
|
+
/** @type {string[]} */
|
|
639
|
+
const lifted = [];
|
|
640
|
+
if (found.length) {
|
|
641
|
+
for (const x of found) {
|
|
642
|
+
const t = theme.components[x.target];
|
|
643
|
+
(t[x.key] ??= {})[x.prop] ??= x.value;
|
|
644
|
+
lifted.push(`${x.target} ${x.key} ${kebab(x.prop)}`);
|
|
645
|
+
}
|
|
646
|
+
fs.writeFileSync(f.settingsTs, settingsSource(slug, data, theme));
|
|
647
|
+
receipt = await runBuild(ctx, slug);
|
|
648
|
+
}
|
|
649
|
+
// Font notices are reported by the font dial itself, once.
|
|
650
|
+
const warnings = [
|
|
651
|
+
...(receipt?.data?.warnings ?? []),
|
|
652
|
+
...(receipt?.data?.notices ?? []),
|
|
653
|
+
]
|
|
654
|
+
.map(String)
|
|
655
|
+
.filter(w => !/^Font "/.test(w));
|
|
656
|
+
return {warnings, lifted};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** @param {Ctx} ctx @param {string} slug @returns {Promise<any>} */
|
|
660
|
+
async function runBuild(ctx, slug) {
|
|
661
|
+
const {cwd} = ctx.project;
|
|
662
|
+
const f = themeFiles(cwd, slug);
|
|
663
|
+
const silent = logger.silent;
|
|
664
|
+
logger.setSilent(true);
|
|
665
|
+
try {
|
|
666
|
+
const {receipt} = await themeBuildForReport(
|
|
667
|
+
shown(cwd, f.ts),
|
|
668
|
+
{out: shown(cwd, f.css)},
|
|
669
|
+
{cwd},
|
|
670
|
+
{compact: true},
|
|
671
|
+
);
|
|
672
|
+
return receipt;
|
|
673
|
+
} finally {
|
|
674
|
+
logger.setSilent(silent);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Resolved tokens of the built theme in both modes.
|
|
680
|
+
* @param {Ctx} ctx
|
|
681
|
+
* @param {string} slug
|
|
682
|
+
* @returns {Promise<{light: Record<string, string>, dark: Record<string, string>}>}
|
|
683
|
+
*/
|
|
684
|
+
export async function resolvedTokens(ctx, slug) {
|
|
685
|
+
const f = themeFiles(ctx.project.cwd, slug);
|
|
686
|
+
const mod = await import(
|
|
687
|
+
`${pathToFileURL(f.js).href}?t=${Date.now()}${Math.random()}`
|
|
688
|
+
);
|
|
689
|
+
const t = Object.values(mod).find(
|
|
690
|
+
v => v && typeof v === 'object' && typeof v.name === 'string',
|
|
691
|
+
);
|
|
692
|
+
const {core} = ctx.project;
|
|
693
|
+
return {
|
|
694
|
+
light: core.resolveThemeTokens(t, {mode: 'light'}),
|
|
695
|
+
dark: core.resolveThemeTokens(t, {mode: 'dark'}),
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Contrast problems this theme caused: text below 4.5:1 that was not as low
|
|
701
|
+
* in the base theme. A part's own color or background setting, or a token
|
|
702
|
+
* set on that part, counts for that part.
|
|
703
|
+
* @param {Ctx} ctx
|
|
704
|
+
* @param {ThemeData} data
|
|
705
|
+
* @param {{light: Record<string, string>, dark: Record<string, string>}} res
|
|
706
|
+
* @returns {Promise<string[]>}
|
|
707
|
+
*/
|
|
708
|
+
export async function contrastProblems(ctx, data, res) {
|
|
709
|
+
const base = await baseResolved(ctx.project, data.like);
|
|
710
|
+
/** @param {import('./_parts.mjs').PaintSide} side @param {'fg' | 'bg'} which @param {string} m */
|
|
711
|
+
const override = (side, which, m) => {
|
|
712
|
+
if (!side.target) return null;
|
|
713
|
+
const props =
|
|
714
|
+
which === 'fg' ? ['color'] : ['background', 'background-color'];
|
|
715
|
+
const hit = [...data.settings]
|
|
716
|
+
.reverse()
|
|
717
|
+
.find(
|
|
718
|
+
x =>
|
|
719
|
+
partsOf(ctx, x).includes(/** @type {string} */ (side.target)) &&
|
|
720
|
+
!x.when &&
|
|
721
|
+
!x.state &&
|
|
722
|
+
(!x.mode || x.mode === m) &&
|
|
723
|
+
((x.source === 'property' && props.includes(x.thing)) ||
|
|
724
|
+
(x.source === 'token' && side.tokens.includes(x.thing))),
|
|
725
|
+
);
|
|
726
|
+
return hit?.value ?? null;
|
|
727
|
+
};
|
|
728
|
+
/** @type {(p: import('./_parts.mjs').TextPair, side: 'fg' | 'bg', m: string) => string | null} */
|
|
729
|
+
const value = (p, side, m) => {
|
|
730
|
+
const s = p[side];
|
|
731
|
+
return (
|
|
732
|
+
override(s, side, m) ??
|
|
733
|
+
(s.tokens.length
|
|
734
|
+
? res[/** @type {'light' | 'dark'} */ (m)][s.tokens[0]]
|
|
735
|
+
: s.raw)
|
|
736
|
+
);
|
|
737
|
+
};
|
|
738
|
+
/** @type {(p: import('./_parts.mjs').TextPair, side: 'fg' | 'bg', m: string) => string | null} */
|
|
739
|
+
const baseValue = (p, side, m) => {
|
|
740
|
+
const s = p[side];
|
|
741
|
+
return s.tokens.length
|
|
742
|
+
? base[/** @type {'light' | 'dark'} */ (m)][s.tokens[0]]
|
|
743
|
+
: s.raw;
|
|
744
|
+
};
|
|
745
|
+
return ctx.C.contrast(value, baseValue, modesOf(data), {
|
|
746
|
+
parseColor,
|
|
747
|
+
ratio,
|
|
748
|
+
}).map(
|
|
749
|
+
x =>
|
|
750
|
+
`text in ${x.part} ("${x.sample}") is ${x.ratio}:1 in ${x.mode} (${x.fg} on ${x.bgPart ?? 'the page'}'s ${x.bg}); text needs 4.5:1`,
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Write the settings, build, and on a failed build put back what was there.
|
|
756
|
+
* Returns what changed in the resolved tokens and the lifted keys that are
|
|
757
|
+
* new since the last build.
|
|
758
|
+
* @param {Ctx} ctx
|
|
759
|
+
* @param {string} slug
|
|
760
|
+
* @param {ThemeData} data
|
|
761
|
+
* @param {{history: boolean}} options
|
|
762
|
+
*/
|
|
763
|
+
export async function commitTheme(ctx, slug, data, {history}) {
|
|
764
|
+
const {cwd} = ctx.project;
|
|
765
|
+
const f = themeFiles(cwd, slug);
|
|
766
|
+
const snapBefore = fs.existsSync(f.settings)
|
|
767
|
+
? fs.readFileSync(f.settings, 'utf8')
|
|
768
|
+
: null;
|
|
769
|
+
const resBefore =
|
|
770
|
+
snapBefore && fs.existsSync(f.js)
|
|
771
|
+
? await resolvedTokens(ctx, slug).catch(() => null)
|
|
772
|
+
: null;
|
|
773
|
+
const liftedBefore = new Set(
|
|
774
|
+
snapBefore ? (JSON.parse(snapBefore).lifted ?? []) : [],
|
|
775
|
+
);
|
|
776
|
+
fs.mkdirSync(f.dir, {recursive: true});
|
|
777
|
+
/** @type {{warnings: string[], lifted: string[]}} */
|
|
778
|
+
let built;
|
|
779
|
+
try {
|
|
780
|
+
built = await buildTheme(ctx, slug, data);
|
|
781
|
+
} catch (e) {
|
|
782
|
+
if (snapBefore) {
|
|
783
|
+
fs.writeFileSync(f.settings, snapBefore);
|
|
784
|
+
await buildTheme(ctx, slug, JSON.parse(snapBefore)).catch(() => null);
|
|
785
|
+
} else fs.rmSync(f.dir, {recursive: true, force: true});
|
|
786
|
+
if (e instanceof AstryxError && e.code === ERROR_CODES.ERR_UNKNOWN_THEME)
|
|
787
|
+
throw e;
|
|
788
|
+
const why = e instanceof Error ? e.message : String(e);
|
|
789
|
+
throw new AstryxError(
|
|
790
|
+
`The theme did not build, so the change was undone: ${why.split('\n').slice(0, 5).join(' | ')}`,
|
|
791
|
+
[],
|
|
792
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
data.lifted = built.lifted;
|
|
796
|
+
fs.writeFileSync(f.settings, `${JSON.stringify(data, null, 2)}\n`);
|
|
797
|
+
if (snapBefore && history) {
|
|
798
|
+
const h = fs.existsSync(f.history)
|
|
799
|
+
? JSON.parse(fs.readFileSync(f.history, 'utf8'))
|
|
800
|
+
: [];
|
|
801
|
+
h.push(snapBefore);
|
|
802
|
+
fs.writeFileSync(f.history, JSON.stringify(h.slice(-80)));
|
|
803
|
+
}
|
|
804
|
+
const res = await resolvedTokens(ctx, slug);
|
|
805
|
+
/** @type {string[]} */
|
|
806
|
+
const changed = [];
|
|
807
|
+
const modes = modesOf(data);
|
|
808
|
+
if (resBefore) {
|
|
809
|
+
for (const m of modes) {
|
|
810
|
+
for (const k of Object.keys(res[m])) {
|
|
811
|
+
if (resBefore[m][k] === res[m][k]) continue;
|
|
812
|
+
const modal = MODAL.has(ctx.C.typeOf(k));
|
|
813
|
+
if (!modal && m === 'dark' && modes.length > 1) continue;
|
|
814
|
+
changed.push(
|
|
815
|
+
`${k}${modal && modes.length > 1 ? ` (${m})` : ''}: ${resBefore[m][k]} -> ${res[m][k]}`,
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
return {
|
|
821
|
+
changed,
|
|
822
|
+
res,
|
|
823
|
+
problems: await contrastProblems(ctx, data, res),
|
|
824
|
+
warnings: built.warnings,
|
|
825
|
+
lifted: built.lifted.filter(x => !liftedBefore.has(x)),
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/** @param {Ctx} ctx @param {ThemeData} data */
|
|
830
|
+
export function fontPackages(ctx, data) {
|
|
831
|
+
/** @type {Array<{pkg: string, installed: boolean}>} */
|
|
832
|
+
const out = [];
|
|
833
|
+
for (const s of data.settings) {
|
|
834
|
+
if (s.thing !== 'font') continue;
|
|
835
|
+
const pkg = fontOf(s.value).pkg;
|
|
836
|
+
if (!pkg || out.some(x => x.pkg === pkg)) continue;
|
|
837
|
+
let installed = true;
|
|
838
|
+
try {
|
|
839
|
+
ctx.project.req.resolve(`${pkg}/package.json`);
|
|
840
|
+
} catch {
|
|
841
|
+
installed = false;
|
|
842
|
+
}
|
|
843
|
+
out.push({pkg, installed});
|
|
844
|
+
}
|
|
845
|
+
return out;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
export {partDefault};
|