@astryxdesign/cli 0.7.15 → 0.7.16-canary.g54dd64e
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 +5 -3
- package/api/component/_adapter.d.mts +1 -1
- package/api/component/_adapter.mjs +28 -1
- package/api/doctor/_adapter.d.mts +17 -0
- package/api/doctor/_adapter.mjs +35 -0
- package/api/doctor/doctor.mjs +18 -4
- package/api/integration/pack-check.mjs +13 -7
- package/api/integration/validate-integration.mjs +1 -1
- package/api/theme/_adapter.d.mts +5 -2
- package/api/theme/_adapter.mjs +43 -12
- package/api/theme/build/build.d.mts +64 -27
- package/api/theme/build/build.mjs +341 -69
- package/api/theme/eject/eject.mjs +22 -1
- package/api/theme/template/template.d.mts +19 -0
- package/api/theme/template/template.mjs +102 -25
- package/api/theme/theme.type.d.mts +5 -1
- package/api/theme/theme.type.mjs +2 -1
- package/api/theme/themeBuild.doc.mjs +3 -3
- package/api/theme/themeEject.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +9 -3
- package/api/theme-edit/_adapter.d.mts +33 -0
- package/api/theme-edit/_adapter.mjs +208 -31
- package/api/theme-edit/_check.d.mts +38 -0
- package/api/theme-edit/_check.mjs +1326 -211
- package/api/theme-edit/_color.d.mts +2 -8
- package/api/theme-edit/_color.mjs +292 -219
- package/api/theme-edit/_compiler.d.mts +1 -0
- package/api/theme-edit/_compiler.mjs +20 -1
- package/api/theme-edit/_context.d.mts +29 -0
- package/api/theme-edit/_context.mjs +329 -30
- package/api/theme-edit/_css.d.mts +60 -1
- package/api/theme-edit/_css.mjs +404 -28
- package/api/theme-edit/_edit.d.mts +9 -0
- package/api/theme-edit/_edit.mjs +1025 -126
- package/api/theme-edit/_how-commands.d.mts +11 -26
- package/api/theme-edit/_how-commands.mjs +30 -2
- package/api/theme-edit/_how-question.d.mts +7 -0
- package/api/theme-edit/_how-question.mjs +1304 -176
- package/api/theme-edit/_how.mjs +144 -30
- package/api/theme-edit/_overview.mjs +2 -0
- package/api/theme-edit/_palette.d.mts +11 -0
- package/api/theme-edit/_palette.mjs +194 -65
- package/api/theme-edit/_parts.d.mts +16 -0
- package/api/theme-edit/_parts.mjs +59 -7
- package/api/theme-edit/_png.mjs +19 -14
- package/api/theme-edit/_preview-app.d.mts +3 -0
- package/api/theme-edit/_preview-app.mjs +843 -45
- package/api/theme-edit/_preview.d.mts +16 -3
- package/api/theme-edit/_preview.mjs +310 -29
- package/api/theme-edit/_project.d.mts +11 -0
- package/api/theme-edit/_project.mjs +46 -12
- package/api/theme-edit/_settings.d.mts +178 -5
- package/api/theme-edit/_settings.mjs +2035 -86
- package/api/theme-edit/_tables.d.mts +24 -0
- package/api/theme-edit/_tables.mjs +211 -149
- package/api/theme-edit/_vet.d.mts +42 -0
- package/api/theme-edit/_vet.mjs +144 -0
- package/api/theme-edit/theme-edit.type.d.mts +2 -1
- package/api/theme-edit/theme-edit.type.mjs +1 -1
- package/api/theme-edit/themeEditNew.doc.mjs +1 -1
- package/api/theme-edit/themeEditShow.doc.mjs +11 -3
- package/api/upgrade/registry/registry.mjs +6 -1
- package/assets/docs/tree/single-component.doc.mjs +11 -2
- package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
- package/assets/docs.generated.json +2 -2
- package/assets/preview-app/next.config.mjs +7 -1
- package/assets/preview-app/package.json +1 -0
- package/assets/preview-app/src/app/globals.css +1 -0
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/dashboard/page.tsx +1 -1
- package/authoring/doctypes/_schema.d.mts +52 -5
- package/authoring/doctypes/_schema.mjs +62 -7
- package/authoring/doctypes/base/type.ts +10 -0
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/function/function.doc.mjs +6 -0
- package/authoring/doctypes/function/type.ts +3 -0
- package/authoring/doctypes/legacy.mjs +5 -1
- package/authoring/index.d.ts +1 -0
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/doctor.doc.mjs +2 -2
- package/clients/cli/commands/experimental/theme.mjs +190 -37
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/theme-template.doc.mjs +3 -1
- package/clients/cli/commands/theme.mjs +168 -45
- package/clients/cli/formatters/index.mjs +7 -4
- package/clients/cli/lib/cli-error.mjs +9 -3
- package/clients/cli/lib/component-format.mjs +27 -9
- package/clients/cli/lib/json-shim.mjs +40 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/clients/cli/run.mjs +49 -0
- package/foundation/config/project.mjs +50 -14
- package/foundation/discovery/theme-discovery.d.mts +5 -1
- package/foundation/discovery/theme-discovery.mjs +24 -3
- package/foundation/fs/gather-cache.d.mts +9 -2
- package/foundation/fs/gather-cache.mjs +79 -15
- package/foundation/fs/paths.d.mts +31 -0
- package/foundation/fs/paths.mjs +43 -10
- package/foundation/render/host-exit.d.mts +8 -0
- package/foundation/render/host-exit.mjs +11 -0
- package/foundation/render/page-host.mjs +56 -10
- package/foundation/render/page-worker.mjs +14 -0
- package/foundation/render/render-page.mjs +37 -2
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +5 -0
- package/foundation/response/error-codes.mjs +3 -0
- package/foundation/response/json.d.mts +15 -0
- package/foundation/response/json.mjs +52 -1
- package/foundation/response/response-types.doc.mjs +1 -1
- package/package.json +7 -6
|
@@ -10,11 +10,14 @@
|
|
|
10
10
|
* @position api/theme-edit — storage and compile for the theme commands
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {parse as parseModule} from '@babel/parser';
|
|
13
14
|
import {createHash} from 'node:crypto';
|
|
14
15
|
import * as fs from 'node:fs';
|
|
16
|
+
import {coversProperty, longhandsOf} from './_css.mjs';
|
|
15
17
|
import postcss from 'postcss';
|
|
16
18
|
import {createRequire} from 'node:module';
|
|
17
19
|
import * as path from 'node:path';
|
|
20
|
+
import {themeFileImports} from '../../foundation/discovery/theme-discovery.mjs';
|
|
18
21
|
import {pathToFileURL} from 'node:url';
|
|
19
22
|
import {cliVersion} from '../../foundation/release/run-context.mjs';
|
|
20
23
|
import {AstryxError} from '../error.mjs';
|
|
@@ -39,9 +42,12 @@ import {
|
|
|
39
42
|
TEXT_SIZE,
|
|
40
43
|
TYPE_STYLES,
|
|
41
44
|
familyOf,
|
|
45
|
+
WRAPPED_PARTS,
|
|
46
|
+
CARD_LIKE,
|
|
42
47
|
} from './_tables.mjs';
|
|
43
48
|
import {
|
|
44
49
|
alphaOf,
|
|
50
|
+
colorProblem,
|
|
45
51
|
compositeOver,
|
|
46
52
|
isColorValue,
|
|
47
53
|
isSafeValue,
|
|
@@ -63,11 +69,20 @@ import {
|
|
|
63
69
|
lockHeld,
|
|
64
70
|
removeEmptyFolder,
|
|
65
71
|
writeWhole,
|
|
72
|
+
setOwnTokenReader,
|
|
66
73
|
} from './_palette.mjs';
|
|
67
74
|
import {MODAL, isColorToken} from './_compiler.mjs';
|
|
68
|
-
import {
|
|
75
|
+
import {
|
|
76
|
+
baseInstalled,
|
|
77
|
+
baseModule,
|
|
78
|
+
baseResolved,
|
|
79
|
+
baseThemeFile,
|
|
80
|
+
} from './_project.mjs';
|
|
69
81
|
import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
|
|
70
82
|
|
|
83
|
+
// The palette reads the tokens the own source sets through this module.
|
|
84
|
+
setOwnTokenReader((cwd, slug) => ownSourceTokenValues(cwd, slug));
|
|
85
|
+
|
|
71
86
|
/**
|
|
72
87
|
* One setting the CLI owns.
|
|
73
88
|
* @typedef {object} Setting
|
|
@@ -97,7 +112,7 @@ import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
|
|
|
97
112
|
/** What lifted secondary text sits on: the contrast check measures it there. */
|
|
98
113
|
const LIFT_GROUND_TOKENS = new Set(LIFT_PARTS.map(([, , g]) => g));
|
|
99
114
|
|
|
100
|
-
/** @typedef {{project: import('./_project.mjs').ThemeProject, C: import('./_compiler.mjs').Compiler, unchecked?: string | null, uncheckedCode?: string | null, wrote?: Map<string, string | null>, base?: {light: Record<string, string>, dark: Record<string, string>, components?: Record<string, any>}, keepStops?: Record<string, Record<'light' | 'dark', {family: string, stop: number, alpha?: string}>> | null, keepRadii?: Record<string, string> | null}} Ctx */
|
|
115
|
+
/** @typedef {{project: import('./_project.mjs').ThemeProject, C: import('./_compiler.mjs').Compiler, unchecked?: string | null, uncheckedCode?: string | null, wrote?: Map<string, string | null>, base?: {light: Record<string, string>, dark: Record<string, string>, components?: Record<string, any>}, keepStops?: Record<string, Record<'light' | 'dark', {family: string, stop: number, alpha?: string}>> | null, keepRadii?: Record<string, string> | null, derived?: Record<string, Array<{property: string, vars?: string[], replaces?: boolean, expand?: string}>>}} Ctx */
|
|
101
116
|
/** @typedef {import('./_parts.mjs').PaintSide} PaintSide */
|
|
102
117
|
/** @typedef {import('./_parts.mjs').TextPair} TextPair */
|
|
103
118
|
|
|
@@ -113,12 +128,17 @@ export const slugOk = s =>
|
|
|
113
128
|
!windowsReserved(s);
|
|
114
129
|
|
|
115
130
|
/**
|
|
116
|
-
* A name Windows
|
|
117
|
-
*
|
|
131
|
+
* A name Windows does not allow for a file or folder, so it cannot be
|
|
132
|
+
* checked out there: a device name (con, nul, com1, com¹…), a character
|
|
133
|
+
* Windows reserves (`<>:"|?*` and control characters), or a dot or space
|
|
134
|
+
* at the end.
|
|
118
135
|
* @param {string} s
|
|
119
136
|
*/
|
|
120
137
|
export const windowsReserved = s =>
|
|
121
|
-
/^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i.test(s)
|
|
138
|
+
/^(con|prn|aux|nul|com[0-9¹²³]|lpt[0-9¹²³])(\..*)?$/i.test(s) ||
|
|
139
|
+
// eslint-disable-next-line no-control-regex
|
|
140
|
+
/[<>:"|?*\u0000-\u001f]/.test(s) ||
|
|
141
|
+
/[. ]$/.test(s);
|
|
122
142
|
/**
|
|
123
143
|
* The theme name closest to what was typed: spaces and accents dropped,
|
|
124
144
|
* lower case, anything else a dash.
|
|
@@ -171,6 +191,116 @@ export function themeFiles(cwd, slug) {
|
|
|
171
191
|
};
|
|
172
192
|
}
|
|
173
193
|
|
|
194
|
+
/**
|
|
195
|
+
* A name `new` takes, for a suggestion: `name` itself when it is valid, not a
|
|
196
|
+
* built-in theme and its folder is free; `my-<name>` for a built-in theme's
|
|
197
|
+
* name; else the first free numbered name.
|
|
198
|
+
* @param {string} cwd
|
|
199
|
+
* @param {string} name
|
|
200
|
+
*/
|
|
201
|
+
export function usableNewName(cwd, name) {
|
|
202
|
+
const base = LIKE.includes(name) ? `my-${name}` : name;
|
|
203
|
+
return slugOk(base) && !fs.existsSync(themeFiles(cwd, base).dir)
|
|
204
|
+
? base
|
|
205
|
+
: freeThemeName(cwd, base);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A theme name near `name` that is valid and whose folder is free: `name-2`,
|
|
210
|
+
* `name-3`, …, shortened to fit the name's length limit.
|
|
211
|
+
* @param {string} cwd
|
|
212
|
+
* @param {string} name
|
|
213
|
+
*/
|
|
214
|
+
export function freeThemeName(cwd, name) {
|
|
215
|
+
for (let n = 2; n < 1000; n += 1) {
|
|
216
|
+
const suffix = `-${n}`;
|
|
217
|
+
const candidate = `${name.slice(0, 64 - suffix.length).replace(/-+$/, '')}${suffix}`;
|
|
218
|
+
if (slugOk(candidate) && !fs.existsSync(themeFiles(cwd, candidate).dir))
|
|
219
|
+
return candidate;
|
|
220
|
+
}
|
|
221
|
+
return '<another name>';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Why the editor cannot read one of its files, or null when it can (or the
|
|
226
|
+
* file is not there): a folder in the file's place, or no permission to read
|
|
227
|
+
* it. Bad content is a different problem, judged where the file is parsed.
|
|
228
|
+
* @param {string} file
|
|
229
|
+
* @returns {string | null}
|
|
230
|
+
*/
|
|
231
|
+
export function unreadableReason(file) {
|
|
232
|
+
let st;
|
|
233
|
+
try {
|
|
234
|
+
st = fs.statSync(file);
|
|
235
|
+
} catch (e) {
|
|
236
|
+
const code = /** @type {any} */ (e)?.code;
|
|
237
|
+
return code === 'ENOENT' || code === 'ENOTDIR'
|
|
238
|
+
? null
|
|
239
|
+
: `it cannot be read (${code ?? 'unknown error'})`;
|
|
240
|
+
}
|
|
241
|
+
if (st.isDirectory()) return 'it is a folder, not a file';
|
|
242
|
+
try {
|
|
243
|
+
fs.accessSync(file, fs.constants.R_OK);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
return `it cannot be read (${/** @type {any} */ (e)?.code ?? 'no permission'}: permission denied)`;
|
|
246
|
+
}
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Refuse, by name, when a file of the theme is a folder or cannot be read:
|
|
252
|
+
* every command reads them, and none should crash on one. The fix is to
|
|
253
|
+
* look at the file and give it back its place; a file is never deleted.
|
|
254
|
+
* @param {string} cwd
|
|
255
|
+
* @param {string} slug
|
|
256
|
+
*/
|
|
257
|
+
export function refuseUnreadableFiles(cwd, slug) {
|
|
258
|
+
const f = themeFiles(cwd, slug);
|
|
259
|
+
// The files the editor reads (the settings, the source, the history) are
|
|
260
|
+
// named here; the files the build writes (with their types) by the
|
|
261
|
+
// build's own check, the same one show uses.
|
|
262
|
+
refuseUnreadable(cwd, [
|
|
263
|
+
...builtFiles(cwd, slug, {name: slug, mode: 'both', settings: []}),
|
|
264
|
+
path.join(f.dir, `${slug}.variants.d.ts`),
|
|
265
|
+
]);
|
|
266
|
+
for (const file of [f.settings, f.ts, f.history]) {
|
|
267
|
+
const why = unreadableReason(file);
|
|
268
|
+
if (why)
|
|
269
|
+
throw new AstryxError(
|
|
270
|
+
`${path.relative(cwd, file)} ${why}, so the theme cannot be read. Put the file back in its place (or give it read permission); nothing was changed.`,
|
|
271
|
+
[
|
|
272
|
+
{
|
|
273
|
+
name: `ls -la ${quoteCommandArgument(path.relative(cwd, file))}`,
|
|
274
|
+
reason: 'see what is there',
|
|
275
|
+
},
|
|
276
|
+
],
|
|
277
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Refuse when the app's themes/ is a file or a link to something other
|
|
284
|
+
* than a folder: every editor theme lives in themes/<name>/. The run lists
|
|
285
|
+
* what is there; the message says to rename or move it.
|
|
286
|
+
* @param {string} cwd
|
|
287
|
+
*/
|
|
288
|
+
export function refuseThemesNotFolder(cwd) {
|
|
289
|
+
const root = path.join(cwd, 'themes');
|
|
290
|
+
const st = fs.lstatSync(root, {throwIfNoEntry: false});
|
|
291
|
+
if (!st) return;
|
|
292
|
+
const isDir =
|
|
293
|
+
st.isDirectory() ||
|
|
294
|
+
(st.isSymbolicLink() &&
|
|
295
|
+
fs.statSync(root, {throwIfNoEntry: false})?.isDirectory());
|
|
296
|
+
if (isDir) return;
|
|
297
|
+
refuse(
|
|
298
|
+
ERROR_CODES.ERR_FILE_EXISTS,
|
|
299
|
+
`themes in this app is ${st.isSymbolicLink() ? 'a link that does not lead to a folder' : 'a file, not a folder'}, so no editor theme can be read or made: every theme goes in themes/<name>/. Rename or move it, then run the command again.`,
|
|
300
|
+
{run: ['ls -la themes']},
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
174
304
|
/**
|
|
175
305
|
* The themes the editor made in the app (a settings file of its own), by
|
|
176
306
|
* name, or with `changed` the one an editor command changed last first: the
|
|
@@ -183,6 +313,7 @@ export function themeFiles(cwd, slug) {
|
|
|
183
313
|
export function editedThemes(cwd, order = 'name') {
|
|
184
314
|
const root = path.join(cwd, 'themes');
|
|
185
315
|
if (!fs.existsSync(root)) return [];
|
|
316
|
+
refuseThemesNotFolder(cwd);
|
|
186
317
|
const names = fs
|
|
187
318
|
.readdirSync(root)
|
|
188
319
|
.filter(d => fs.existsSync(themeFiles(cwd, d).settings))
|
|
@@ -259,6 +390,29 @@ export function refuse(code, message, extra = {}) {
|
|
|
259
390
|
throw new AstryxError(`${message} ${tail}`, suggestions, code);
|
|
260
391
|
}
|
|
261
392
|
|
|
393
|
+
/**
|
|
394
|
+
* A theme name as typed, read the way the editor reads it: a path to its
|
|
395
|
+
* folder or a file in it (t.theme.ts, Brand.settings.json), in any case,
|
|
396
|
+
* names the theme. The result may still need slugFor.
|
|
397
|
+
* @param {unknown} s
|
|
398
|
+
* @returns {string}
|
|
399
|
+
*/
|
|
400
|
+
export function typedThemeName(s) {
|
|
401
|
+
return (
|
|
402
|
+
String(s ?? '')
|
|
403
|
+
.trim()
|
|
404
|
+
.toLowerCase()
|
|
405
|
+
// ./t and .\t name the theme t.
|
|
406
|
+
.replace(/^(\.[\\/])+/, '')
|
|
407
|
+
.replace(/^.*themes[\\/]/, '')
|
|
408
|
+
.replace(/[\\/].*$/, '')
|
|
409
|
+
.replace(
|
|
410
|
+
/\.(theme\.ts|settings\.(json|ts)|history\.json|css\.d\.ts|d\.ts|css|js|ts)$/,
|
|
411
|
+
'',
|
|
412
|
+
)
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
262
416
|
/**
|
|
263
417
|
* The settings of a theme, or a refusal naming the command that makes it.
|
|
264
418
|
* @param {string} cwd
|
|
@@ -273,19 +427,12 @@ export function loadTheme(
|
|
|
273
427
|
slug,
|
|
274
428
|
skip = /** @type {string | Partial<Setting>} */ (''),
|
|
275
429
|
) {
|
|
430
|
+
refuseThemesNotFolder(cwd);
|
|
276
431
|
if (!slugOk(slug)) {
|
|
277
432
|
// A path or a capital letter typed for a theme that exists names it.
|
|
278
433
|
// A file name typed for the theme (t.theme.ts, Brand.settings.json)
|
|
279
434
|
// names the theme.
|
|
280
|
-
const plain =
|
|
281
|
-
.trim()
|
|
282
|
-
.toLowerCase()
|
|
283
|
-
.replace(/^.*themes[\\/]/, '')
|
|
284
|
-
.replace(/[\\/].*$/, '')
|
|
285
|
-
.replace(
|
|
286
|
-
/\.(theme\.ts|settings\.(json|ts)|history\.json|css\.d\.ts|d\.ts|css|js|ts)$/,
|
|
287
|
-
'',
|
|
288
|
-
);
|
|
435
|
+
const plain = typedThemeName(slug);
|
|
289
436
|
const fixed = slugFor(plain);
|
|
290
437
|
const same = fs.existsSync(themeFiles(cwd, fixed).settings) ? fixed : null;
|
|
291
438
|
// A list where one theme goes (t,u or "t;u"): one theme at a time,
|
|
@@ -309,6 +456,27 @@ export function loadTheme(
|
|
|
309
456
|
},
|
|
310
457
|
);
|
|
311
458
|
}
|
|
459
|
+
// An empty name (an unset variable, a lone dash): no theme to guess
|
|
460
|
+
// from it, so the themes here are named instead of a new one.
|
|
461
|
+
if (!same && !/[a-z0-9]/i.test(String(slug ?? ''))) {
|
|
462
|
+
const have = fs.existsSync(themeFiles(cwd, 'x').root)
|
|
463
|
+
? fs
|
|
464
|
+
.readdirSync(themeFiles(cwd, 'x').root)
|
|
465
|
+
.filter(
|
|
466
|
+
d => slugOk(d) && fs.existsSync(themeFiles(cwd, d).settings),
|
|
467
|
+
)
|
|
468
|
+
.sort()
|
|
469
|
+
: [];
|
|
470
|
+
refuse(
|
|
471
|
+
ERROR_CODES.ERR_INVALID_ARGUMENT,
|
|
472
|
+
`The theme name is empty${String(slug ?? '').trim() ? ` ("${String(slug).slice(0, 20)}" has no letter or digit)` : ''}.${have.length ? ` Themes here: ${have.slice(0, 6).join(', ')}.` : ''}`,
|
|
473
|
+
{
|
|
474
|
+
run: have.length
|
|
475
|
+
? have.slice(0, 3).map(t => `${P} show ${t}`)
|
|
476
|
+
: [`${P} new my-theme`],
|
|
477
|
+
},
|
|
478
|
+
);
|
|
479
|
+
}
|
|
312
480
|
refuse(
|
|
313
481
|
ERROR_CODES.ERR_INVALID_ARGUMENT,
|
|
314
482
|
`"${String(slug ?? '').slice(0, 80)}" is not a theme name (${NAME_RULE}).`,
|
|
@@ -348,29 +516,76 @@ export function loadTheme(
|
|
|
348
516
|
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
349
517
|
stopped
|
|
350
518
|
? `themes/${slug} was left by a \`new\` that was stopped part way, so it has no settings yet. Run new again to finish it; files you added there are kept.`
|
|
351
|
-
: `themes/${slug} has lost its settings (${slug}.settings.json is gone), but the editor made it. Run new to make it again: your own files there are kept${fs.existsSync(f.history) ? `, and \`undo ${slug}\` then steps back to the
|
|
519
|
+
: `themes/${slug} has lost its settings (${slug}.settings.json is gone), but the editor made it. Run new to make it again: your own files there are kept${fs.existsSync(f.history) ? `, and \`undo ${slug}\` then steps back to the settings from before the last change (the last change itself is not kept)` : ''}.`,
|
|
352
520
|
{run: [`${P} new ${slug}`]},
|
|
353
521
|
);
|
|
522
|
+
// A folder copied or renamed by hand from an editor theme: its files
|
|
523
|
+
// still carry the old name (t.settings.json in themes/t2).
|
|
524
|
+
const copied =
|
|
525
|
+
!same && fs.existsSync(f.dir)
|
|
526
|
+
? fs
|
|
527
|
+
.readdirSync(f.dir)
|
|
528
|
+
.map(n => /^(.+)\.settings\.json$/.exec(n)?.[1])
|
|
529
|
+
.find(n => n && n !== slug)
|
|
530
|
+
: undefined;
|
|
531
|
+
if (copied) {
|
|
532
|
+
const files = fs
|
|
533
|
+
.readdirSync(f.dir)
|
|
534
|
+
.filter(n => n.startsWith(`${copied}.`))
|
|
535
|
+
.sort();
|
|
536
|
+
const original = fs.existsSync(themeFiles(cwd, copied).settings);
|
|
537
|
+
refuse(
|
|
538
|
+
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
539
|
+
`themes/${slug} holds the editor files of a theme named ${copied} (${files.slice(0, 4).join(', ')}${files.length > 4 ? ', …' : ''}): it looks like a copy or rename of ${copied}. The editor reads a theme's files by its folder's name, so to edit it as ${slug}, rename each ${copied}.* file there to ${slug}.* (${files
|
|
540
|
+
.slice(0, 2)
|
|
541
|
+
.map(n => `${n} to ${slug}${n.slice(copied.length)}`)
|
|
542
|
+
.join(
|
|
543
|
+
', ',
|
|
544
|
+
)}${files.length > 2 ? ', and so on' : ''})${original ? `; ${copied} itself is still in themes/${copied}` : ''}.`,
|
|
545
|
+
{
|
|
546
|
+
run: original
|
|
547
|
+
? [`${P} show ${copied}`, `ls -la themes/${slug}`]
|
|
548
|
+
: [`ls -la themes/${slug}`],
|
|
549
|
+
},
|
|
550
|
+
);
|
|
551
|
+
}
|
|
354
552
|
if (!same && (other || LIKE.includes(slug)))
|
|
355
553
|
refuse(
|
|
356
554
|
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
357
555
|
`${other ? `${other[1]} is a theme the editor did not make (written by hand, or ejected)` : `${slug} is a built-in theme`}, so the editor does not change it. Make an editor theme with its own name instead${LIKE.includes(slug) ? `, on top of ${slug}` : ''}.`,
|
|
358
556
|
{
|
|
359
557
|
run: [
|
|
360
|
-
|
|
558
|
+
// The user's own theme on that base, when there is one, then a
|
|
559
|
+
// free valid name for a new one.
|
|
560
|
+
...(fs.existsSync(themeFiles(cwd, `my-${slug}`).settings)
|
|
561
|
+
? [`${P} show my-${slug}`]
|
|
562
|
+
: []),
|
|
563
|
+
`${P} new ${usableNewName(cwd, `my-${slug}`)}${LIKE.includes(slug) ? ` --like ${slug}` : ''}`,
|
|
361
564
|
],
|
|
362
565
|
},
|
|
363
566
|
);
|
|
364
567
|
refuse(
|
|
365
568
|
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
366
569
|
`There is no theme "${slug}" made by this command (no themes/${slug}/${slug}.settings.json).`,
|
|
367
|
-
{
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
570
|
+
(() => {
|
|
571
|
+
const near = same ? [same] : nearest(slug, have);
|
|
572
|
+
// A theme near the name comes first; a new theme of the name as
|
|
573
|
+
// typed only after.
|
|
574
|
+
return {
|
|
575
|
+
didYouMean: near,
|
|
576
|
+
run: same
|
|
577
|
+
? [`${P} show ${same}`]
|
|
578
|
+
: near.length
|
|
579
|
+
? [`${P} show ${near[0]}`, `${P} new ${slug}`]
|
|
580
|
+
: [`${P} new ${slug}`],
|
|
581
|
+
};
|
|
582
|
+
})(),
|
|
371
583
|
);
|
|
372
584
|
}
|
|
373
585
|
refuseLinks(cwd, slug);
|
|
586
|
+
// A file of the theme that is a folder or cannot be read is named, never
|
|
587
|
+
// called damaged.
|
|
588
|
+
refuseUnreadableFiles(cwd, slug);
|
|
374
589
|
// undo is offered only when there is a step to go back to.
|
|
375
590
|
const steps = readHistory(f.history).steps.length;
|
|
376
591
|
const recover = steps
|
|
@@ -472,22 +687,73 @@ export function loadTheme(
|
|
|
472
687
|
);
|
|
473
688
|
}
|
|
474
689
|
// A bad mode or base is fixed by setting it.
|
|
690
|
+
// A value written in other letters (Dark-Only, Neutral) is the one meant.
|
|
691
|
+
const modeMeant = String(data?.mode ?? '')
|
|
692
|
+
.trim()
|
|
693
|
+
.toLowerCase();
|
|
694
|
+
const likeMeant = String(data?.like ?? '')
|
|
695
|
+
.trim()
|
|
696
|
+
.toLowerCase();
|
|
475
697
|
const fieldFix = /^(it has no mode|mode ")/.test(problem ?? '')
|
|
476
|
-
? `${P} set ${slug} mode both`
|
|
698
|
+
? `${P} set ${slug} mode ${['both', 'light-only', 'dark-only'].includes(modeMeant) ? modeMeant : 'both'}`
|
|
477
699
|
: /^base "/.test(problem ?? '')
|
|
478
|
-
? `${P} set ${slug} base none`
|
|
700
|
+
? `${P} set ${slug} base ${LIKE.includes(likeMeant) && baseInstalled(cwd, likeMeant) ? likeMeant : 'none'}`
|
|
479
701
|
: null;
|
|
702
|
+
// A setting listed twice: setting it to one of its values, or undoing
|
|
703
|
+
// it, keeps one entry or none.
|
|
704
|
+
const twice = problem?.includes(' is listed twice (')
|
|
705
|
+
? duplicated(data)
|
|
706
|
+
: null;
|
|
480
707
|
if (problem)
|
|
481
708
|
throw new AstryxError(
|
|
482
|
-
`themes/${slug}/${slug}.settings.json: ${problem}. ${fieldFix ? 'Set it again' : `Fix it by hand${recover.text}`}. Nothing was changed.`,
|
|
709
|
+
`themes/${slug}/${slug}.settings.json: ${problem}. ${fieldFix ? 'Set it again' : twice ? `Set it to one of its values or undo it, or fix it by hand${recover.text}` : `Fix it by hand${recover.text}`}. Nothing was changed.`,
|
|
483
710
|
fieldFix
|
|
484
711
|
? [{name: fieldFix, reason: 'do this instead'}, ...recover.run]
|
|
485
|
-
:
|
|
712
|
+
: twice
|
|
713
|
+
? [
|
|
714
|
+
...twice.values
|
|
715
|
+
.filter(
|
|
716
|
+
v =>
|
|
717
|
+
typeof v === 'string' &&
|
|
718
|
+
!valueProblem({...twice.setting, value: v}),
|
|
719
|
+
)
|
|
720
|
+
.slice(0, 2)
|
|
721
|
+
.map(v => ({
|
|
722
|
+
name: `${P} set ${slug} ${undoArgs(twice.setting).replace(twice.setting.thing, `${twice.setting.thing} ${quoteCommandArgument(String(v))}`)}`,
|
|
723
|
+
reason: `keep ${v}`,
|
|
724
|
+
})),
|
|
725
|
+
{
|
|
726
|
+
name: `${P} undo ${slug} ${undoArgs(twice.setting)}`,
|
|
727
|
+
reason: 'remove both',
|
|
728
|
+
},
|
|
729
|
+
...recover.run,
|
|
730
|
+
]
|
|
731
|
+
: recover.run,
|
|
486
732
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
487
733
|
);
|
|
488
734
|
return data;
|
|
489
735
|
}
|
|
490
736
|
|
|
737
|
+
/**
|
|
738
|
+
* The first setting listed more than once, with every value it is listed
|
|
739
|
+
* with, in order.
|
|
740
|
+
* @param {any} data
|
|
741
|
+
* @returns {{setting: any, values: unknown[]} | null}
|
|
742
|
+
*/
|
|
743
|
+
function duplicated(data) {
|
|
744
|
+
/** @type {Map<string, any[]>} */
|
|
745
|
+
const byKey = new Map();
|
|
746
|
+
for (const s of data.settings ?? []) {
|
|
747
|
+
if (!s || typeof s.thing !== 'string') continue;
|
|
748
|
+
const key = keyOf(s);
|
|
749
|
+
byKey.set(key, [...(byKey.get(key) ?? []), s]);
|
|
750
|
+
}
|
|
751
|
+
for (const list of byKey.values())
|
|
752
|
+
if (list.length > 1)
|
|
753
|
+
return {setting: list[0], values: [...new Set(list.map(x => x.value))]};
|
|
754
|
+
return null;
|
|
755
|
+
}
|
|
756
|
+
|
|
491
757
|
/**
|
|
492
758
|
* What is wrong with settings data, or null: its shape, and each value with
|
|
493
759
|
* the same rules `set` checks it by (without the installed Core), so a value
|
|
@@ -513,7 +779,15 @@ export function settingsProblem(
|
|
|
513
779
|
if (!field && data.like != null && !LIKE.includes(data.like))
|
|
514
780
|
return `base "${data.like}" is not a built-in theme (${LIKE.join(', ')})`;
|
|
515
781
|
if (!Array.isArray(data.settings)) return 'settings is not a list';
|
|
516
|
-
|
|
782
|
+
// `lifted` is written by the build: a list of text, or absent.
|
|
783
|
+
if (
|
|
784
|
+
data.lifted != null &&
|
|
785
|
+
(!Array.isArray(data.lifted) ||
|
|
786
|
+
data.lifted.some((/** @type {unknown} */ x) => typeof x !== 'string'))
|
|
787
|
+
)
|
|
788
|
+
return `lifted is ${JSON.stringify(data.lifted)}, not a list of text (the build writes it; remove the field and the next build writes it again)`;
|
|
789
|
+
/** @type {Map<string, any>} */
|
|
790
|
+
const keys = new Map();
|
|
517
791
|
for (const s of data.settings) {
|
|
518
792
|
// A value or scope that is not text is a bad setting `undo` removes;
|
|
519
793
|
// a setting with no name is damage.
|
|
@@ -524,8 +798,11 @@ export function settingsProblem(
|
|
|
524
798
|
if (s.mode != null && s.mode !== 'light' && s.mode !== 'dark')
|
|
525
799
|
return `${s.thing} has mode "${s.mode}"`;
|
|
526
800
|
const key = keyOf(s);
|
|
527
|
-
|
|
528
|
-
|
|
801
|
+
// The command that changes or removes a setting listed twice keeps one
|
|
802
|
+
// value, or none, so it fixes the list.
|
|
803
|
+
if (keys.has(key) && !skips(s, skip))
|
|
804
|
+
return `${describe({...s, value: '?'}).replace(' = ?', '')} is listed twice (${JSON.stringify(keys.get(key).value)}, ${JSON.stringify(s.value)})`;
|
|
805
|
+
keys.set(key, s);
|
|
529
806
|
if (skips(s, skip)) continue;
|
|
530
807
|
const problem = valueProblem(s);
|
|
531
808
|
if (problem) return problem;
|
|
@@ -636,6 +913,12 @@ export function valueProblem(s) {
|
|
|
636
913
|
if (s.source !== 'dial') return null;
|
|
637
914
|
const d = DIALS[s.thing];
|
|
638
915
|
if (!d) return `${s.thing} is not a dial`;
|
|
916
|
+
// A color dial needs a fixed opaque seed, as `set` checks it: a
|
|
917
|
+
// see-through, system or variable color generates no shades.
|
|
918
|
+
if (PALETTE_DIALS.includes(s.thing) && isColorValue(s.value)) {
|
|
919
|
+
const seed = colorProblem(s.value);
|
|
920
|
+
if (seed) return `${bad} ${seed.message}`;
|
|
921
|
+
}
|
|
639
922
|
const words = d.values.split('|').filter(w => !w.startsWith('<'));
|
|
640
923
|
const ok =
|
|
641
924
|
s.thing === 'corners'
|
|
@@ -658,6 +941,617 @@ export function valueProblem(s) {
|
|
|
658
941
|
return null;
|
|
659
942
|
}
|
|
660
943
|
|
|
944
|
+
/**
|
|
945
|
+
* The page-wide tokens a theme's own source sets: the keys of the top-level
|
|
946
|
+
* `tokens` map in its `defineTheme({...})`, read from the syntax tree, each
|
|
947
|
+
* with its value when that is plain text (else null). A token set only in a
|
|
948
|
+
* scope (`onDark`, `components`) is not one: it does not apply page-wide.
|
|
949
|
+
* @param {string} cwd
|
|
950
|
+
* @param {string} slug
|
|
951
|
+
* @returns {Map<string, string | null>}
|
|
952
|
+
*/
|
|
953
|
+
function ownPageTokens(cwd, slug) {
|
|
954
|
+
/** @type {Map<string, string | null>} */
|
|
955
|
+
const out = new Map();
|
|
956
|
+
/** @type {string} */
|
|
957
|
+
let code;
|
|
958
|
+
try {
|
|
959
|
+
code = fs.readFileSync(themeFiles(cwd, slug).ts, 'utf8');
|
|
960
|
+
} catch {
|
|
961
|
+
return out;
|
|
962
|
+
}
|
|
963
|
+
/** @type {any} */
|
|
964
|
+
let ast;
|
|
965
|
+
try {
|
|
966
|
+
ast = parseModule(code, {sourceType: 'module', plugins: ['typescript']});
|
|
967
|
+
} catch {
|
|
968
|
+
return out;
|
|
969
|
+
}
|
|
970
|
+
/** @param {any} node @returns {string | null} */
|
|
971
|
+
const keyOf = node =>
|
|
972
|
+
node?.type === 'StringLiteral'
|
|
973
|
+
? node.value
|
|
974
|
+
: node?.type === 'Identifier'
|
|
975
|
+
? node.name
|
|
976
|
+
: null;
|
|
977
|
+
/** @param {any} node @returns {string | null} */
|
|
978
|
+
const textOf = node =>
|
|
979
|
+
node?.type === 'StringLiteral'
|
|
980
|
+
? node.value
|
|
981
|
+
: node?.type === 'TemplateLiteral' && node.expressions.length === 0
|
|
982
|
+
? node.quasis.map((/** @type {any} */ q) => q.value.cooked).join('')
|
|
983
|
+
: null;
|
|
984
|
+
// Every const binding in the file, by name, so a value given by name
|
|
985
|
+
// (tokens: mine, ...mine) is read as written.
|
|
986
|
+
/** @type {Map<string, any>} */
|
|
987
|
+
const consts = new Map();
|
|
988
|
+
/** @type {any[]} */
|
|
989
|
+
const calls = [];
|
|
990
|
+
/** @param {any} node @param {any} owner the exported name, when exported */
|
|
991
|
+
const visit = (node, owner = null) => {
|
|
992
|
+
if (!node || typeof node !== 'object') return;
|
|
993
|
+
if (node.type === 'VariableDeclarator' && node.id?.type === 'Identifier')
|
|
994
|
+
consts.set(node.id.name, node.init);
|
|
995
|
+
if (
|
|
996
|
+
node.type === 'CallExpression' &&
|
|
997
|
+
node.callee?.type === 'Identifier' &&
|
|
998
|
+
node.callee.name === 'defineTheme' &&
|
|
999
|
+
node.arguments?.[0]
|
|
1000
|
+
)
|
|
1001
|
+
calls.push({call: node, owner});
|
|
1002
|
+
for (const [k, v] of Object.entries(node)) {
|
|
1003
|
+
const nextOwner =
|
|
1004
|
+
node.type === 'VariableDeclarator' && k === 'init'
|
|
1005
|
+
? (node.id?.name ?? owner)
|
|
1006
|
+
: node.type === 'ExportDefaultDeclaration'
|
|
1007
|
+
? 'default'
|
|
1008
|
+
: owner;
|
|
1009
|
+
if (Array.isArray(v)) v.forEach(c => visit(c, nextOwner));
|
|
1010
|
+
else if (v && typeof v === 'object' && v.type) visit(v, nextOwner);
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
visit(ast.program);
|
|
1014
|
+
/** @param {any} node @param {number} [depth] @returns {any} */
|
|
1015
|
+
const unwrap = (node, depth = 0) => {
|
|
1016
|
+
if (!node || depth > 20) return null;
|
|
1017
|
+
if (
|
|
1018
|
+
[
|
|
1019
|
+
'TSAsExpression',
|
|
1020
|
+
'TSSatisfiesExpression',
|
|
1021
|
+
'TSNonNullExpression',
|
|
1022
|
+
'TSTypeAssertion',
|
|
1023
|
+
'ParenthesizedExpression',
|
|
1024
|
+
].includes(node.type)
|
|
1025
|
+
)
|
|
1026
|
+
return unwrap(node.expression, depth + 1);
|
|
1027
|
+
if (node.type === 'Identifier' && consts.has(node.name))
|
|
1028
|
+
return unwrap(consts.get(node.name), depth + 1);
|
|
1029
|
+
return node;
|
|
1030
|
+
};
|
|
1031
|
+
// The theme the build loads: the defineTheme that names it, else the one
|
|
1032
|
+
// exported as <camel>Theme or by default, else the only one.
|
|
1033
|
+
const named = calls.find(({call}) => {
|
|
1034
|
+
const obj = unwrap(call.arguments[0]);
|
|
1035
|
+
return (
|
|
1036
|
+
obj?.type === 'ObjectExpression' &&
|
|
1037
|
+
obj.properties.some(
|
|
1038
|
+
(/** @type {any} */ p) =>
|
|
1039
|
+
p.type === 'ObjectProperty' &&
|
|
1040
|
+
keyOf(p.key) === 'name' &&
|
|
1041
|
+
textOf(unwrap(p.value)) === slug,
|
|
1042
|
+
)
|
|
1043
|
+
);
|
|
1044
|
+
});
|
|
1045
|
+
const chosen =
|
|
1046
|
+
named ??
|
|
1047
|
+
calls.find(
|
|
1048
|
+
c => c.owner === `${camel(slug)}Theme` || c.owner === 'default',
|
|
1049
|
+
) ??
|
|
1050
|
+
(calls.length === 1 ? calls[0] : null);
|
|
1051
|
+
if (!chosen) return out;
|
|
1052
|
+
const def = unwrap(chosen.call.arguments[0]);
|
|
1053
|
+
if (def?.type !== 'ObjectExpression') return out;
|
|
1054
|
+
const tokensProp = def.properties.find(
|
|
1055
|
+
(/** @type {any} */ p) =>
|
|
1056
|
+
p.type === 'ObjectProperty' && !p.computed && keyOf(p.key) === 'tokens',
|
|
1057
|
+
);
|
|
1058
|
+
if (!tokensProp) return out;
|
|
1059
|
+
let unresolved = false;
|
|
1060
|
+
/** @param {any} node @param {number} depth */
|
|
1061
|
+
const collect = (node, depth) => {
|
|
1062
|
+
const obj = unwrap(node);
|
|
1063
|
+
if (obj?.type !== 'ObjectExpression' || depth > 20) {
|
|
1064
|
+
unresolved = true;
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
// Spreads and keys merge in order: a later one wins.
|
|
1068
|
+
for (const p of obj.properties) {
|
|
1069
|
+
if (p.type === 'SpreadElement') collect(p.argument, depth + 1);
|
|
1070
|
+
else if (p.type === 'ObjectProperty' && !p.computed) {
|
|
1071
|
+
const k = keyOf(p.key);
|
|
1072
|
+
if (k?.startsWith('--')) out.set(k, textOf(unwrap(p.value)));
|
|
1073
|
+
} else unresolved = true;
|
|
1074
|
+
}
|
|
1075
|
+
};
|
|
1076
|
+
collect(tokensProp.value, 0);
|
|
1077
|
+
// A value no read of the file can resolve (a call, an import) is read
|
|
1078
|
+
// from the build (ownTokens), and checked again after the next one.
|
|
1079
|
+
void unresolved;
|
|
1080
|
+
return out;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/**
|
|
1084
|
+
* The var() loops among a theme's built top-level tokens, each with a key
|
|
1085
|
+
* that names it whatever token it starts from.
|
|
1086
|
+
* @param {string} cwd
|
|
1087
|
+
* @param {string} slug
|
|
1088
|
+
* @returns {Array<{key: string, path: string[], part?: string}>}
|
|
1089
|
+
*/
|
|
1090
|
+
function builtLoops(cwd, slug) {
|
|
1091
|
+
const tokens = builtTopTokens(cwd, slug);
|
|
1092
|
+
/** @param {string} t */
|
|
1093
|
+
const refs = t =>
|
|
1094
|
+
[...String(tokens.get(t) ?? '').matchAll(/var\(\s*(--[a-z0-9-]+)/gi)].map(
|
|
1095
|
+
m => m[1],
|
|
1096
|
+
);
|
|
1097
|
+
/** @type {Array<{key: string, path: string[], part?: string}>} */
|
|
1098
|
+
const loops = [];
|
|
1099
|
+
const seen = new Set();
|
|
1100
|
+
for (const start of tokens.keys()) {
|
|
1101
|
+
/** @type {string[]} */
|
|
1102
|
+
const path = [];
|
|
1103
|
+
/** @param {string} t @returns {boolean} */
|
|
1104
|
+
const walk = t => {
|
|
1105
|
+
const at = path.indexOf(t);
|
|
1106
|
+
if (at >= 0) {
|
|
1107
|
+
const cycle = path.slice(at);
|
|
1108
|
+
const key = [...cycle].sort().join(' ');
|
|
1109
|
+
if (!seen.has(key)) {
|
|
1110
|
+
seen.add(key);
|
|
1111
|
+
loops.push({key, path: [...cycle, t]});
|
|
1112
|
+
}
|
|
1113
|
+
return true;
|
|
1114
|
+
}
|
|
1115
|
+
if (!tokens.has(t) || path.length > 50) return false;
|
|
1116
|
+
path.push(t);
|
|
1117
|
+
for (const r of refs(t)) if (walk(r)) return true;
|
|
1118
|
+
path.pop();
|
|
1119
|
+
return false;
|
|
1120
|
+
};
|
|
1121
|
+
walk(start);
|
|
1122
|
+
}
|
|
1123
|
+
// Inside one part: a part's own token reads the part's other own tokens
|
|
1124
|
+
// first, and a whole-theme one only as already resolved, so a loop there
|
|
1125
|
+
// closes among the part's own tokens.
|
|
1126
|
+
for (const part of blockKeys(themeFiles(cwd, slug).js, ['components'])) {
|
|
1127
|
+
const block = blockOf(themeFiles(cwd, slug).js, [
|
|
1128
|
+
'components',
|
|
1129
|
+
part,
|
|
1130
|
+
'base',
|
|
1131
|
+
]);
|
|
1132
|
+
for (const start of block.keys()) {
|
|
1133
|
+
if (!start.startsWith('--')) continue;
|
|
1134
|
+
/** @type {string[]} */
|
|
1135
|
+
const path = [];
|
|
1136
|
+
/** @param {string} t @returns {boolean} */
|
|
1137
|
+
const walk = t => {
|
|
1138
|
+
const at = path.indexOf(t);
|
|
1139
|
+
if (at >= 0) {
|
|
1140
|
+
const cycle = path.slice(at);
|
|
1141
|
+
const key = `${part}: ${[...cycle].sort().join(' ')}`;
|
|
1142
|
+
if (!seen.has(key)) {
|
|
1143
|
+
seen.add(key);
|
|
1144
|
+
loops.push({key, path: [...cycle, t], part});
|
|
1145
|
+
}
|
|
1146
|
+
return true;
|
|
1147
|
+
}
|
|
1148
|
+
if (!block.has(t) || path.length > 50) return false;
|
|
1149
|
+
path.push(t);
|
|
1150
|
+
for (const m of String(block.get(t) ?? '').matchAll(
|
|
1151
|
+
/var\(\s*(--[a-z0-9-]+)/gi,
|
|
1152
|
+
))
|
|
1153
|
+
if (walk(m[1])) return true;
|
|
1154
|
+
path.pop();
|
|
1155
|
+
return false;
|
|
1156
|
+
};
|
|
1157
|
+
walk(start);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
return loops;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* The keys of one object in a built theme module (see blockOf).
|
|
1165
|
+
* @param {string} file
|
|
1166
|
+
* @param {string[]} keys
|
|
1167
|
+
* @returns {string[]}
|
|
1168
|
+
*/
|
|
1169
|
+
function blockKeys(file, keys) {
|
|
1170
|
+
return [...blockOf(file, keys).keys()];
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* The top-level tokens of a theme's built module (<slug>.js), which the
|
|
1175
|
+
* build wrote as plain data, or an empty map.
|
|
1176
|
+
* @param {string} cwd
|
|
1177
|
+
* @param {string} slug
|
|
1178
|
+
* @returns {Map<string, string | null>}
|
|
1179
|
+
*/
|
|
1180
|
+
function builtTopTokens(cwd, slug) {
|
|
1181
|
+
return topTokensOf(themeFiles(cwd, slug).js);
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
/**
|
|
1185
|
+
* The tokens a base theme's own component rules for a part read through
|
|
1186
|
+
* var() (neutral's progress-bar reads --color-border-emphasized), from its
|
|
1187
|
+
* built module; an empty set when it is not installed.
|
|
1188
|
+
* @param {string} cwd
|
|
1189
|
+
* @param {string | null | undefined} like
|
|
1190
|
+
* @param {string} target
|
|
1191
|
+
* @returns {Set<string>}
|
|
1192
|
+
*/
|
|
1193
|
+
export function baseComponentReads(cwd, like, target) {
|
|
1194
|
+
/** @type {Set<string>} */
|
|
1195
|
+
const out = new Set();
|
|
1196
|
+
const file = like ? baseModule(cwd, like) : null;
|
|
1197
|
+
if (!file) return out;
|
|
1198
|
+
try {
|
|
1199
|
+
const ast = /** @type {any} */ (
|
|
1200
|
+
parseModule(fs.readFileSync(file, 'utf8'), {sourceType: 'module'})
|
|
1201
|
+
);
|
|
1202
|
+
/** @param {any} node */
|
|
1203
|
+
const visit = node => {
|
|
1204
|
+
if (!node || typeof node !== 'object') return;
|
|
1205
|
+
if (
|
|
1206
|
+
node.type === 'ObjectProperty' &&
|
|
1207
|
+
(node.key?.name ?? node.key?.value) === 'components' &&
|
|
1208
|
+
node.value?.type === 'ObjectExpression'
|
|
1209
|
+
) {
|
|
1210
|
+
const part = node.value.properties.find(
|
|
1211
|
+
(/** @type {any} */ p) => (p.key?.value ?? p.key?.name) === target,
|
|
1212
|
+
);
|
|
1213
|
+
if (part) {
|
|
1214
|
+
const text = JSON.stringify(part.value);
|
|
1215
|
+
for (const m of text.matchAll(/var\(\s*(--[a-z0-9-]+)/gi))
|
|
1216
|
+
out.add(m[1]);
|
|
1217
|
+
}
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
for (const v of Object.values(node))
|
|
1221
|
+
if (Array.isArray(v)) v.forEach(visit);
|
|
1222
|
+
else if (v && typeof v === 'object' && v.type) visit(v);
|
|
1223
|
+
};
|
|
1224
|
+
visit(ast.program);
|
|
1225
|
+
} catch {
|
|
1226
|
+
// Not one this reads.
|
|
1227
|
+
}
|
|
1228
|
+
return out;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
/**
|
|
1232
|
+
* The top-level tokens a base theme's built module gives (what the
|
|
1233
|
+
* editor's settings extend), or an empty map when it is not installed.
|
|
1234
|
+
* @param {string} cwd
|
|
1235
|
+
* @param {string | null | undefined} like
|
|
1236
|
+
* @returns {Map<string, string | null>}
|
|
1237
|
+
*/
|
|
1238
|
+
export function baseTopTokens(cwd, like) {
|
|
1239
|
+
const file = like ? baseModule(cwd, like) : null;
|
|
1240
|
+
return file ? topTokensOf(file) : new Map();
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
/**
|
|
1244
|
+
* The top-level tokens of a built theme module (plain data the build
|
|
1245
|
+
* wrote), or an empty map.
|
|
1246
|
+
* @param {string} file
|
|
1247
|
+
* @returns {Map<string, string | null>}
|
|
1248
|
+
*/
|
|
1249
|
+
function topTokensOf(file) {
|
|
1250
|
+
/** @type {Map<string, string | null>} */
|
|
1251
|
+
const out = new Map();
|
|
1252
|
+
for (const [k, v] of blockOf(file, ['tokens']))
|
|
1253
|
+
if (k.startsWith('--')) out.set(k, v);
|
|
1254
|
+
return out;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* One block of a built theme module (plain data the build wrote), by its
|
|
1259
|
+
* path from the theme object (`['tokens']`, `['components', 'card',
|
|
1260
|
+
* 'base']`): every key with its text value, a [light, dark] pair as the
|
|
1261
|
+
* build writes it, else null. An empty map when it is not there.
|
|
1262
|
+
* @param {string} file
|
|
1263
|
+
* @param {string[]} keys
|
|
1264
|
+
* @returns {Map<string, string | null>}
|
|
1265
|
+
*/
|
|
1266
|
+
function blockOf(file, keys) {
|
|
1267
|
+
/** @type {Map<string, string | null>} */
|
|
1268
|
+
const out = new Map();
|
|
1269
|
+
try {
|
|
1270
|
+
const ast = /** @type {any} */ (
|
|
1271
|
+
parseModule(fs.readFileSync(file, 'utf8'), {
|
|
1272
|
+
sourceType: 'module',
|
|
1273
|
+
plugins: ['typescript'],
|
|
1274
|
+
})
|
|
1275
|
+
);
|
|
1276
|
+
for (const st of ast.program.body) {
|
|
1277
|
+
const decl = st.declaration?.declarations?.[0];
|
|
1278
|
+
/** @type {any} */
|
|
1279
|
+
let obj = decl?.init;
|
|
1280
|
+
// The generated settings module: defineTheme({...}).
|
|
1281
|
+
if (obj?.type === 'CallExpression') obj = obj.arguments?.[0];
|
|
1282
|
+
for (const key of keys)
|
|
1283
|
+
obj =
|
|
1284
|
+
obj?.type === 'ObjectExpression'
|
|
1285
|
+
? obj.properties.find(
|
|
1286
|
+
(/** @type {any} */ p) => (p.key?.name ?? p.key?.value) === key,
|
|
1287
|
+
)?.value
|
|
1288
|
+
: undefined;
|
|
1289
|
+
if (obj?.type !== 'ObjectExpression') continue;
|
|
1290
|
+
for (const p of obj.properties) {
|
|
1291
|
+
const k = p.key?.value ?? p.key?.name;
|
|
1292
|
+
if (typeof k === 'string')
|
|
1293
|
+
out.set(
|
|
1294
|
+
k,
|
|
1295
|
+
p.value?.type === 'StringLiteral'
|
|
1296
|
+
? p.value.value
|
|
1297
|
+
: // A [light, dark] pair, as the build writes it.
|
|
1298
|
+
p.value?.type === 'ArrayExpression' &&
|
|
1299
|
+
p.value.elements.length === 2 &&
|
|
1300
|
+
p.value.elements.every(
|
|
1301
|
+
(/** @type {any} */ e) => e?.type === 'StringLiteral',
|
|
1302
|
+
)
|
|
1303
|
+
? `light-dark(${p.value.elements.map((/** @type {any} */ e) => e.value).join(', ')})`
|
|
1304
|
+
: null,
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
if (out.size) break;
|
|
1308
|
+
}
|
|
1309
|
+
} catch {
|
|
1310
|
+
// No build yet, or not one this reads.
|
|
1311
|
+
}
|
|
1312
|
+
return out;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
/**
|
|
1316
|
+
* What a part's plain rule (its `base` block) holds in the theme as built
|
|
1317
|
+
* (the base theme's, the settings' and the own source's together), else in
|
|
1318
|
+
* the base theme alone when there is no build: tokens and properties by
|
|
1319
|
+
* name. Kebab-case property names, as the editor names them.
|
|
1320
|
+
* @param {string} cwd
|
|
1321
|
+
* @param {string} slug
|
|
1322
|
+
* @param {string | null | undefined} like
|
|
1323
|
+
* @param {string} part
|
|
1324
|
+
* @returns {Map<string, string | null>}
|
|
1325
|
+
*/
|
|
1326
|
+
export function partRuleTokens(cwd, slug, like, part) {
|
|
1327
|
+
const built = blockOf(themeFiles(cwd, slug).js, ['components', part, 'base']);
|
|
1328
|
+
const baseFile = like ? baseModule(cwd, like) : null;
|
|
1329
|
+
const base = baseFile
|
|
1330
|
+
? blockOf(baseFile, ['components', part, 'base'])
|
|
1331
|
+
: new Map();
|
|
1332
|
+
/** @type {Map<string, string | null>} */
|
|
1333
|
+
const out = new Map();
|
|
1334
|
+
for (const m of [base, built])
|
|
1335
|
+
for (const [k, v] of m)
|
|
1336
|
+
out.set(
|
|
1337
|
+
k.startsWith('--')
|
|
1338
|
+
? k
|
|
1339
|
+
: k.replace(
|
|
1340
|
+
/[A-Z]/g,
|
|
1341
|
+
(/** @type {string} */ c) => `-${c.toLowerCase()}`,
|
|
1342
|
+
),
|
|
1343
|
+
v,
|
|
1344
|
+
);
|
|
1345
|
+
return out;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/**
|
|
1349
|
+
* The tokens and properties a theme's own source sets on a part's plain
|
|
1350
|
+
* rule, read from the build as ownTokens reads page-wide ones: what the
|
|
1351
|
+
* built rule holds that the settings module and the base theme do not give
|
|
1352
|
+
* as they are, and that the person's files name.
|
|
1353
|
+
* @param {string} cwd
|
|
1354
|
+
* @param {string} slug
|
|
1355
|
+
* @param {string} part
|
|
1356
|
+
* @returns {Map<string, string | null>}
|
|
1357
|
+
*/
|
|
1358
|
+
export function ownPartTokens(cwd, slug, part) {
|
|
1359
|
+
/** @type {Map<string, string | null>} */
|
|
1360
|
+
const out = new Map();
|
|
1361
|
+
const f = themeFiles(cwd, slug);
|
|
1362
|
+
/** @type {string} */
|
|
1363
|
+
let module;
|
|
1364
|
+
try {
|
|
1365
|
+
module = fs.readFileSync(f.settingsTs, 'utf8');
|
|
1366
|
+
} catch {
|
|
1367
|
+
return out;
|
|
1368
|
+
}
|
|
1369
|
+
const stamp = /\/\/ Built with \S+\.theme\.ts ([0-9a-f]{16})\./.exec(
|
|
1370
|
+
module,
|
|
1371
|
+
)?.[1];
|
|
1372
|
+
if (!stamp || stamp !== themeSourceHash(cwd, slug)) return out;
|
|
1373
|
+
const like = /\/\/ Built on @astryxdesign\/theme-([a-z0-9-]+) /.exec(
|
|
1374
|
+
module,
|
|
1375
|
+
)?.[1];
|
|
1376
|
+
const baseFile = like ? baseModule(cwd, like) : null;
|
|
1377
|
+
const base = baseFile
|
|
1378
|
+
? blockOf(baseFile, ['components', part, 'base'])
|
|
1379
|
+
: new Map();
|
|
1380
|
+
const settings = blockOf(f.settingsTs, ['components', part, 'base']);
|
|
1381
|
+
const named = namedInOwnSource(cwd, slug);
|
|
1382
|
+
for (const [k, v] of blockOf(f.js, ['components', part, 'base'])) {
|
|
1383
|
+
const given = settings.has(k) ? settings.get(k) : base.get(k);
|
|
1384
|
+
if (given !== v && named(k))
|
|
1385
|
+
out.set(
|
|
1386
|
+
k.startsWith('--')
|
|
1387
|
+
? k
|
|
1388
|
+
: k.replace(
|
|
1389
|
+
/[A-Z]/g,
|
|
1390
|
+
(/** @type {string} */ c) => `-${c.toLowerCase()}`,
|
|
1391
|
+
),
|
|
1392
|
+
v,
|
|
1393
|
+
);
|
|
1394
|
+
}
|
|
1395
|
+
return out;
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
/**
|
|
1399
|
+
* The page-wide tokens the theme's own source sets, read from what the
|
|
1400
|
+
* build made of it: the built module's top-level tokens that the
|
|
1401
|
+
* generated settings module does not give as they are (the own source wins
|
|
1402
|
+
* over the settings it extends). The build evaluates the file, so any way
|
|
1403
|
+
* of writing it counts (a namespace import, a renamed defineTheme, a
|
|
1404
|
+
* template, a spread of a shared object). Only a build made from the
|
|
1405
|
+
* source as it is now is read; with none, the file is read as written.
|
|
1406
|
+
* Both are joined: a key either names is the source's.
|
|
1407
|
+
* @param {string} cwd
|
|
1408
|
+
* @param {string} slug
|
|
1409
|
+
* @returns {Map<string, string | null>}
|
|
1410
|
+
*/
|
|
1411
|
+
function ownTokens(cwd, slug) {
|
|
1412
|
+
const out = new Map(ownPageTokens(cwd, slug));
|
|
1413
|
+
const fromBuild = builtOwnTokens(cwd, slug);
|
|
1414
|
+
if (fromBuild) for (const [k, v] of fromBuild) out.set(k, v);
|
|
1415
|
+
return out;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* The tokens the own source sets, as the current build shows them, or null
|
|
1420
|
+
* when there is no build made from the source as it is now.
|
|
1421
|
+
* @param {string} cwd
|
|
1422
|
+
* @param {string} slug
|
|
1423
|
+
* @returns {Map<string, string | null> | null}
|
|
1424
|
+
*/
|
|
1425
|
+
function builtOwnTokens(cwd, slug) {
|
|
1426
|
+
const f = themeFiles(cwd, slug);
|
|
1427
|
+
/** @type {string} */
|
|
1428
|
+
let module;
|
|
1429
|
+
try {
|
|
1430
|
+
module = fs.readFileSync(f.settingsTs, 'utf8');
|
|
1431
|
+
} catch {
|
|
1432
|
+
return null;
|
|
1433
|
+
}
|
|
1434
|
+
const built = /\/\/ Built with \S+\.theme\.ts ([0-9a-f]{16})\./.exec(
|
|
1435
|
+
module,
|
|
1436
|
+
)?.[1];
|
|
1437
|
+
if (!built || !fs.existsSync(f.js) || built !== themeSourceHash(cwd, slug))
|
|
1438
|
+
return null;
|
|
1439
|
+
// What the settings module gives each top-level token: a text value as
|
|
1440
|
+
// it is, a [light, dark] pair as a pair.
|
|
1441
|
+
/** @type {Map<string, string | 'pair' | 'other'>} */
|
|
1442
|
+
const settings = new Map();
|
|
1443
|
+
try {
|
|
1444
|
+
const ast = /** @type {any} */ (
|
|
1445
|
+
parseModule(module, {sourceType: 'module', plugins: ['typescript']})
|
|
1446
|
+
);
|
|
1447
|
+
/** @param {any} node */
|
|
1448
|
+
const visit = node => {
|
|
1449
|
+
if (!node || typeof node !== 'object') return;
|
|
1450
|
+
if (
|
|
1451
|
+
node.type === 'CallExpression' &&
|
|
1452
|
+
node.callee?.name === 'defineTheme' &&
|
|
1453
|
+
node.arguments?.[0]?.type === 'ObjectExpression'
|
|
1454
|
+
) {
|
|
1455
|
+
const tokens = node.arguments[0].properties.find(
|
|
1456
|
+
(/** @type {any} */ p) =>
|
|
1457
|
+
(p.key?.value ?? p.key?.name) === 'tokens' &&
|
|
1458
|
+
p.value?.type === 'ObjectExpression',
|
|
1459
|
+
);
|
|
1460
|
+
for (const p of tokens?.value.properties ?? []) {
|
|
1461
|
+
const k = p.key?.value ?? p.key?.name;
|
|
1462
|
+
if (typeof k !== 'string') continue;
|
|
1463
|
+
settings.set(
|
|
1464
|
+
k,
|
|
1465
|
+
p.value?.type === 'StringLiteral'
|
|
1466
|
+
? p.value.value
|
|
1467
|
+
: p.value?.type === 'ArrayExpression'
|
|
1468
|
+
? 'pair'
|
|
1469
|
+
: 'other',
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
return;
|
|
1473
|
+
}
|
|
1474
|
+
for (const v of Object.values(node))
|
|
1475
|
+
if (Array.isArray(v)) v.forEach(visit);
|
|
1476
|
+
else if (v && typeof v === 'object' && v.type) visit(v);
|
|
1477
|
+
};
|
|
1478
|
+
visit(ast.program);
|
|
1479
|
+
} catch {
|
|
1480
|
+
return null;
|
|
1481
|
+
}
|
|
1482
|
+
// What the base theme's built module gives: the tokens the settings
|
|
1483
|
+
// extend, which are no more the source's than the settings' are.
|
|
1484
|
+
const like = /\/\/ Built on @astryxdesign\/theme-([a-z0-9-]+) /.exec(
|
|
1485
|
+
module,
|
|
1486
|
+
)?.[1];
|
|
1487
|
+
const baseFile = like ? baseModule(cwd, like) : null;
|
|
1488
|
+
const base = baseFile ? topTokensOf(baseFile) : new Map();
|
|
1489
|
+
const named = namedInOwnSource(cwd, slug);
|
|
1490
|
+
/** @type {Map<string, string | null>} */
|
|
1491
|
+
const out = new Map();
|
|
1492
|
+
for (const [k, v] of builtTopTokens(cwd, slug)) {
|
|
1493
|
+
const given = settings.get(k);
|
|
1494
|
+
const own =
|
|
1495
|
+
given === undefined
|
|
1496
|
+
? !base.has(k) || base.get(k) !== v
|
|
1497
|
+
: given === 'pair'
|
|
1498
|
+
? !/^light-dark\(/.test(String(v ?? ''))
|
|
1499
|
+
: given !== 'other' && given !== v;
|
|
1500
|
+
if (own && named(k)) out.set(k, v);
|
|
1501
|
+
}
|
|
1502
|
+
return out;
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
/**
|
|
1506
|
+
* Whether a token or property name appears in the person's theme files: a
|
|
1507
|
+
* value the build shows that no file of theirs names came from the
|
|
1508
|
+
* settings (a dial's typography, a lifted value), not from them.
|
|
1509
|
+
* @param {string} cwd
|
|
1510
|
+
* @param {string} slug
|
|
1511
|
+
* @returns {(name: string) => boolean}
|
|
1512
|
+
*/
|
|
1513
|
+
function namedInOwnSource(cwd, slug) {
|
|
1514
|
+
const text = ownSourceFiles(cwd, slug)
|
|
1515
|
+
.map(file => {
|
|
1516
|
+
try {
|
|
1517
|
+
return fs.readFileSync(file, 'utf8');
|
|
1518
|
+
} catch {
|
|
1519
|
+
return '';
|
|
1520
|
+
}
|
|
1521
|
+
})
|
|
1522
|
+
.join('\n');
|
|
1523
|
+
return name =>
|
|
1524
|
+
text.includes(name) ||
|
|
1525
|
+
// A property may be written camelCase (borderRadius).
|
|
1526
|
+
(!name.startsWith('--') &&
|
|
1527
|
+
text.includes(name.replace(/-([a-z])/g, (_, c) => c.toUpperCase())));
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
/**
|
|
1531
|
+
* The page-wide tokens a theme's own source sets itself (see ownTokens):
|
|
1532
|
+
* they win over the settings it extends.
|
|
1533
|
+
* @param {string} cwd
|
|
1534
|
+
* @param {string} slug
|
|
1535
|
+
* @returns {string[]}
|
|
1536
|
+
*/
|
|
1537
|
+
export function ownSourceTokens(cwd, slug) {
|
|
1538
|
+
return [...ownTokens(cwd, slug).keys()];
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
/**
|
|
1542
|
+
* The values a theme's own source gives page-wide tokens as plain text, by
|
|
1543
|
+
* token (see ownTokens).
|
|
1544
|
+
* @param {string} cwd
|
|
1545
|
+
* @param {string} slug
|
|
1546
|
+
* @returns {Record<string, string>}
|
|
1547
|
+
*/
|
|
1548
|
+
export function ownSourceTokenValues(cwd, slug) {
|
|
1549
|
+
/** @type {Record<string, string>} */
|
|
1550
|
+
const out = {};
|
|
1551
|
+
for (const [k, v] of ownTokens(cwd, slug)) if (v != null) out[k] = v;
|
|
1552
|
+
return out;
|
|
1553
|
+
}
|
|
1554
|
+
|
|
661
1555
|
/**
|
|
662
1556
|
* What is wrong with where a setting applies, or null: names only, as
|
|
663
1557
|
* `set` takes them.
|
|
@@ -723,7 +1617,11 @@ export {modesOf};
|
|
|
723
1617
|
* @returns {number | null}
|
|
724
1618
|
*/
|
|
725
1619
|
export function lengthPx(v) {
|
|
726
|
-
|
|
1620
|
+
// A CSS number: a sign, digits with or without a fraction, an exponent.
|
|
1621
|
+
const m =
|
|
1622
|
+
/^\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?)\s*(px|rem|em)?\s*$/i.exec(
|
|
1623
|
+
String(v),
|
|
1624
|
+
);
|
|
727
1625
|
if (!m) return null;
|
|
728
1626
|
const n = Number(m[1]);
|
|
729
1627
|
return (m[2] ?? 'px').toLowerCase() === 'px' ? n : n * 16;
|
|
@@ -768,7 +1666,10 @@ export function fontOf(value) {
|
|
|
768
1666
|
.split(',')
|
|
769
1667
|
.map(x => x.trim())
|
|
770
1668
|
.filter(Boolean);
|
|
771
|
-
|
|
1669
|
+
// Runs of spaces or tabs inside a family name are one space.
|
|
1670
|
+
const family = unquote(parts[0] ?? v)
|
|
1671
|
+
.replace(/[ \t]+/g, ' ')
|
|
1672
|
+
.trim();
|
|
772
1673
|
return {
|
|
773
1674
|
family: cssFamily(family),
|
|
774
1675
|
fallbacks:
|
|
@@ -789,6 +1690,7 @@ export function fontOf(value) {
|
|
|
789
1690
|
* @param {boolean} [spaced]
|
|
790
1691
|
*/
|
|
791
1692
|
export function cssFamily(name, spaced = false) {
|
|
1693
|
+
name = name.replace(/[ \t]+/g, ' ');
|
|
792
1694
|
const words = name.trim().split(/\s+/);
|
|
793
1695
|
const ident = (/** @type {string} */ w) =>
|
|
794
1696
|
/^-?(?:[A-Za-z_]|[\u0080-\uffff])(?:[\w-]|[\u0080-\uffff])*$/.test(w) &&
|
|
@@ -989,20 +1891,398 @@ export function compileTheme(ctx, data, base) {
|
|
|
989
1891
|
t.dark = v;
|
|
990
1892
|
return;
|
|
991
1893
|
}
|
|
992
|
-
t[/** @type {'light' | 'dark'} */ (mode)] = v;
|
|
1894
|
+
t[/** @type {'light' | 'dark'} */ (mode)] = v;
|
|
1895
|
+
};
|
|
1896
|
+
/** @param {Setting} s @param {string} target @returns {Record<string, any>} */
|
|
1897
|
+
const block = (s, target) => {
|
|
1898
|
+
const t = (theme.components[target] ??= {});
|
|
1899
|
+
let b = (t[s.when ?? 'base'] ??= {});
|
|
1900
|
+
if (s.state) {
|
|
1901
|
+
const tg = ctx.project.target.get(target);
|
|
1902
|
+
if (tg?.states.includes(s.state) && s.when == null) b = t[s.state] ??= {};
|
|
1903
|
+
else if (tg?.states.includes(s.state))
|
|
1904
|
+
b = t[`${s.when}+${s.state}`] ??= {};
|
|
1905
|
+
else b = b[stateKey(s.state, wrapsOneControl(ctx, target))] ??= {};
|
|
1906
|
+
}
|
|
1907
|
+
return b;
|
|
1908
|
+
};
|
|
1909
|
+
/**
|
|
1910
|
+
* A padding on a part whose padding Core writes one variable per side
|
|
1911
|
+
* (its derivedVarRegistry's container expansion), as each side's value,
|
|
1912
|
+
* split at top-level spaces by the CSS shorthand rules: Core splits a
|
|
1913
|
+
* shorthand at every space, which cuts calc(4px + 2px) and loses a
|
|
1914
|
+
* fourth value. On the part itself the sides go as logical longhands,
|
|
1915
|
+
* which Core carries as they are; for a variant or state they go to the
|
|
1916
|
+
* side variables themselves, since Core writes one variable for equal
|
|
1917
|
+
* sides there and the part's own side variables would win over it.
|
|
1918
|
+
* Null for any other property or part.
|
|
1919
|
+
* @param {string} target @param {Setting} s
|
|
1920
|
+
* @returns {Record<string, string> | null}
|
|
1921
|
+
*/
|
|
1922
|
+
const containerSides = (target, s) => {
|
|
1923
|
+
const prop = kebab(s.thing);
|
|
1924
|
+
if (
|
|
1925
|
+
s.mode ||
|
|
1926
|
+
!/^padding(-block|-inline|-left|-right|-top|-bottom|-block-start|-block-end|-inline-start|-inline-end)?$/.test(
|
|
1927
|
+
prop,
|
|
1928
|
+
)
|
|
1929
|
+
)
|
|
1930
|
+
return null;
|
|
1931
|
+
if (
|
|
1932
|
+
!(ctx.derived?.[target] ?? []).some(
|
|
1933
|
+
d => d.property === 'padding' && d.expand,
|
|
1934
|
+
)
|
|
1935
|
+
)
|
|
1936
|
+
return null;
|
|
1937
|
+
const scoped0 = s.when != null || s.state != null;
|
|
1938
|
+
// padding's initial value is 0, and padding does not inherit, so unset
|
|
1939
|
+
// is 0 too: on Core's side variables a keyword means no value at all.
|
|
1940
|
+
const value = /^(initial|unset)$/i.test(String(s.value).trim())
|
|
1941
|
+
? '0'
|
|
1942
|
+
: String(s.value).trim();
|
|
1943
|
+
// A physical left or right stays physical, as Core keeps it (it maps
|
|
1944
|
+
// no physical side to a logical one but top and bottom, which it maps
|
|
1945
|
+
// itself): it is written as it is, and a later padding that covers it
|
|
1946
|
+
// takes it out of the block (below).
|
|
1947
|
+
const physical = /** @type {Record<string, string>} */ ({
|
|
1948
|
+
// One logical side is that side (its keyword resolved too).
|
|
1949
|
+
'padding-block-start': 'block-start',
|
|
1950
|
+
'padding-block-end': 'block-end',
|
|
1951
|
+
'padding-inline-start': 'inline-start',
|
|
1952
|
+
'padding-inline-end': 'inline-end',
|
|
1953
|
+
'padding-top': 'block-start',
|
|
1954
|
+
'padding-bottom': 'block-end',
|
|
1955
|
+
})[prop];
|
|
1956
|
+
if (physical)
|
|
1957
|
+
return {
|
|
1958
|
+
[scoped0
|
|
1959
|
+
? `--astryx-${target}-padding-${physical}`
|
|
1960
|
+
: camel(`padding-${physical}`)]: value,
|
|
1961
|
+
};
|
|
1962
|
+
// A physical left or right is written as it is (its keyword resolved),
|
|
1963
|
+
// never split as an axis: there is no padding-left-start.
|
|
1964
|
+
if (prop === 'padding-left' || prop === 'padding-right')
|
|
1965
|
+
return {[camel(prop)]: value};
|
|
1966
|
+
const w = splitTop(value, ' ').map(x => x.trim());
|
|
1967
|
+
if (!w.length || w.length > (prop === 'padding' ? 4 : 2)) return null;
|
|
1968
|
+
const scoped = s.when != null || s.state != null;
|
|
1969
|
+
// Only what Core would split wrong is written side by side: math or a
|
|
1970
|
+
// function, four values with different left and right sides; and, for
|
|
1971
|
+
// a variant or state, any padding once the part sets a side itself.
|
|
1972
|
+
const coreSplitsWrong =
|
|
1973
|
+
value !== String(s.value).trim() ||
|
|
1974
|
+
/\(/.test(value) ||
|
|
1975
|
+
(prop === 'padding' && w.length === 4 && w[1] !== w[3]);
|
|
1976
|
+
const partSides = data.settings.some(
|
|
1977
|
+
x =>
|
|
1978
|
+
x !== s &&
|
|
1979
|
+
x.source === 'property' &&
|
|
1980
|
+
// The part's own, or a group's it is in.
|
|
1981
|
+
(x.on === s.on ||
|
|
1982
|
+
(FAMILIES[/** @type {string} */ (x.on)] ?? []).includes(target)) &&
|
|
1983
|
+
x.when == null &&
|
|
1984
|
+
x.state == null &&
|
|
1985
|
+
(/^padding-/.test(kebab(x.thing)) ||
|
|
1986
|
+
(kebab(x.thing) === 'padding' &&
|
|
1987
|
+
/\(|\S+\s+\S+\s+\S+\s+\S+/.test(x.value))),
|
|
1988
|
+
);
|
|
1989
|
+
// A side set on the part or its group: every side is written, so the
|
|
1990
|
+
// later setting wins as written.
|
|
1991
|
+
if (!coreSplitsWrong && !partSides) return null;
|
|
1992
|
+
/** @type {Record<string, string>} */
|
|
1993
|
+
const side = {};
|
|
1994
|
+
if (prop === 'padding') {
|
|
1995
|
+
const [top, right = top, bottom = top, left = right] = w;
|
|
1996
|
+
Object.assign(side, {
|
|
1997
|
+
'block-start': top,
|
|
1998
|
+
'inline-end': right,
|
|
1999
|
+
'block-end': bottom,
|
|
2000
|
+
'inline-start': left,
|
|
2001
|
+
});
|
|
2002
|
+
} else {
|
|
2003
|
+
const axis = prop.slice('padding-'.length);
|
|
2004
|
+
side[`${axis}-start`] = w[0];
|
|
2005
|
+
side[`${axis}-end`] = w[1] ?? w[0];
|
|
2006
|
+
}
|
|
2007
|
+
return Object.fromEntries(
|
|
2008
|
+
Object.entries(side).map(([k, v]) => [
|
|
2009
|
+
scoped ? `--astryx-${target}-padding-${k}` : camel(`padding-${k}`),
|
|
2010
|
+
v,
|
|
2011
|
+
]),
|
|
2012
|
+
);
|
|
2013
|
+
};
|
|
2014
|
+
/**
|
|
2015
|
+
* A radius of more than one value on a part whose radius Core copies
|
|
2016
|
+
* into a variable for the content inside it (a card's full-bleed
|
|
2017
|
+
* children), as each corner's own radius, so Core writes no copy of the
|
|
2018
|
+
* whole value there (where one corner reads it, and `10px / 20px` or four
|
|
2019
|
+
* values are wrong). The inner variable takes one radius, the first
|
|
2020
|
+
* corner's across: Core gives the content one radius for every corner.
|
|
2021
|
+
* Null for any other property, part or a single radius.
|
|
2022
|
+
* @param {string} target @param {Setting} s
|
|
2023
|
+
* @returns {Record<string, string> | null}
|
|
2024
|
+
*/
|
|
2025
|
+
const cornerRadii = (target, s) => {
|
|
2026
|
+
if (s.mode || kebab(s.thing) !== 'border-radius') return null;
|
|
2027
|
+
const vars = (ctx.derived?.[target] ?? []).find(
|
|
2028
|
+
d => d.property === 'borderRadius' && d.vars?.length,
|
|
2029
|
+
)?.vars;
|
|
2030
|
+
if (!vars) return null;
|
|
2031
|
+
const [across, down] = splitTop(String(s.value).trim(), '/').map(x =>
|
|
2032
|
+
splitTop(x.trim(), ' ').map(y => y.trim()),
|
|
2033
|
+
);
|
|
2034
|
+
if (!across?.length || (across.length === 1 && !down)) return null;
|
|
2035
|
+
/** @param {string[]} w top-left, top-right, bottom-right, bottom-left */
|
|
2036
|
+
const four = w => {
|
|
2037
|
+
const [tl, tr = tl, br = tl, bl = tr] = w;
|
|
2038
|
+
return [tl, tr, br, bl];
|
|
2039
|
+
};
|
|
2040
|
+
const h = four(across);
|
|
2041
|
+
const v = down?.length ? four(down) : null;
|
|
2042
|
+
const corner = (/** @type {number} */ i) =>
|
|
2043
|
+
v && v[i] !== h[i] ? `${h[i]} ${v[i]}` : h[i];
|
|
2044
|
+
return {
|
|
2045
|
+
borderTopLeftRadius: corner(0),
|
|
2046
|
+
borderTopRightRadius: corner(1),
|
|
2047
|
+
borderBottomRightRadius: corner(2),
|
|
2048
|
+
borderBottomLeftRadius: corner(3),
|
|
2049
|
+
...Object.fromEntries(vars.map(name => [name, h[0]])),
|
|
2050
|
+
};
|
|
2051
|
+
};
|
|
2052
|
+
/**
|
|
2053
|
+
* A one-side logical padding on a part whose whole padding Core copies
|
|
2054
|
+
* into a variable of its own (a composer's or a menu's inner padding),
|
|
2055
|
+
* written for each direction (a left-to-right box's left is a
|
|
2056
|
+
* right-to-left box's right): Core feeds every logical padding into that
|
|
2057
|
+
* variable, so one side would set the whole box. Null for any other
|
|
2058
|
+
* property or part.
|
|
2059
|
+
* @param {string} target @param {Setting} s
|
|
2060
|
+
* @returns {Record<string, string | Record<string, string>> | null}
|
|
2061
|
+
*/
|
|
2062
|
+
const boxSides = (target, s) => {
|
|
2063
|
+
const prop = kebab(s.thing);
|
|
2064
|
+
if (s.mode) return null;
|
|
2065
|
+
const derived = ctx.derived?.[target] ?? [];
|
|
2066
|
+
if (
|
|
2067
|
+
derived.some(d => d.property === 'padding' && d.expand) ||
|
|
2068
|
+
!derived.some(d => d.property === 'padding' && d.vars?.length)
|
|
2069
|
+
)
|
|
2070
|
+
return null;
|
|
2071
|
+
const SIDES = /** @type {Record<string, string[]>} */ ({
|
|
2072
|
+
'padding-block-start': ['block-start'],
|
|
2073
|
+
'padding-block-end': ['block-end'],
|
|
2074
|
+
'padding-inline-start': ['inline-start'],
|
|
2075
|
+
'padding-inline-end': ['inline-end'],
|
|
2076
|
+
'padding-block': ['block-start', 'block-end'],
|
|
2077
|
+
'padding-inline': ['inline-start', 'inline-end'],
|
|
2078
|
+
});
|
|
2079
|
+
const sides = SIDES[prop];
|
|
2080
|
+
if (!sides) return null;
|
|
2081
|
+
const w = splitTop(String(s.value).trim(), ' ').map(x => x.trim());
|
|
2082
|
+
// Each side for each direction: a block side is the top or bottom; an
|
|
2083
|
+
// inline side is the left in a left-to-right box and the right in a
|
|
2084
|
+
// right-to-left one, written under :dir(), where Core writes the
|
|
2085
|
+
// declarations as they are and feeds none into the whole-box variable.
|
|
2086
|
+
/** @type {Record<string, string>} */
|
|
2087
|
+
const plain = {};
|
|
2088
|
+
/** @type {Record<string, string>} */
|
|
2089
|
+
const ltr = {};
|
|
2090
|
+
/** @type {Record<string, string>} */
|
|
2091
|
+
const rtl = {};
|
|
2092
|
+
sides.forEach((side, i) => {
|
|
2093
|
+
const v = w[i] ?? w[0];
|
|
2094
|
+
if (side === 'block-start') plain.paddingTop = v;
|
|
2095
|
+
else if (side === 'block-end') plain.paddingBottom = v;
|
|
2096
|
+
else if (side === 'inline-start') {
|
|
2097
|
+
ltr.paddingLeft = v;
|
|
2098
|
+
rtl.paddingRight = v;
|
|
2099
|
+
} else {
|
|
2100
|
+
ltr.paddingRight = v;
|
|
2101
|
+
rtl.paddingLeft = v;
|
|
2102
|
+
}
|
|
2103
|
+
});
|
|
2104
|
+
return {
|
|
2105
|
+
...plain,
|
|
2106
|
+
...(Object.keys(ltr).length ? {':dir(ltr)': ltr, ':dir(rtl)': rtl} : {}),
|
|
2107
|
+
};
|
|
2108
|
+
};
|
|
2109
|
+
/**
|
|
2110
|
+
* A padding on a part whose visible box sits inside its themed wrapper,
|
|
2111
|
+
* as the padding variable that box reads, not the wrapper's own padding
|
|
2112
|
+
* (which would add a gap outside the box). Null for any other part.
|
|
2113
|
+
* @param {string} target @param {Setting} s
|
|
2114
|
+
* @returns {Record<string, string> | null}
|
|
2115
|
+
*/
|
|
2116
|
+
const wrappedPadding = (target, s) => {
|
|
2117
|
+
if (s.mode || !WRAPPED_PARTS[target] || kebab(s.thing) !== 'padding')
|
|
2118
|
+
return null;
|
|
2119
|
+
const name = (ctx.derived?.[target] ?? []).find(
|
|
2120
|
+
d => d.property === 'padding' && d.vars?.length,
|
|
2121
|
+
)?.vars?.[0];
|
|
2122
|
+
return name ? {[name]: String(s.value).trim()} : null;
|
|
2123
|
+
};
|
|
2124
|
+
/**
|
|
2125
|
+
* The prefix of the padding variables a part's box reads, for a part whose
|
|
2126
|
+
* padding Core writes to them (its derivedVarRegistry's container
|
|
2127
|
+
* expansion), or for one drawn by such a part (a clickable card is a
|
|
2128
|
+
* card); null for any other part.
|
|
2129
|
+
* @param {string} target
|
|
2130
|
+
*/
|
|
2131
|
+
const padVars = target => {
|
|
2132
|
+
if (
|
|
2133
|
+
(ctx.derived?.[target] ?? []).some(
|
|
2134
|
+
d => d.property === 'padding' && d.expand,
|
|
2135
|
+
)
|
|
2136
|
+
)
|
|
2137
|
+
return `--astryx-${target}-padding`;
|
|
2138
|
+
return CARD_LIKE.includes(target) ? '--astryx-card-padding' : null;
|
|
2139
|
+
};
|
|
2140
|
+
/**
|
|
2141
|
+
* Padding settings per part and per scope (the block a setting writes
|
|
2142
|
+
* to), in the order they were made.
|
|
2143
|
+
* @type {Map<string, {target: string, s: Setting, list: Setting[]}>}
|
|
2144
|
+
*/
|
|
2145
|
+
const padQueue = new Map();
|
|
2146
|
+
/** @param {Setting} s @param {string} target */
|
|
2147
|
+
const queuePadding = (s, target) => {
|
|
2148
|
+
const key = `${target}\0${s.when ?? ''}\0${s.state ?? ''}`;
|
|
2149
|
+
const q = padQueue.get(key) ?? {target, s, list: []};
|
|
2150
|
+
q.list.push(s);
|
|
2151
|
+
padQueue.set(key, q);
|
|
2152
|
+
};
|
|
2153
|
+
/**
|
|
2154
|
+
* One padding model: each scope's settings, in order, resolved to the
|
|
2155
|
+
* four logical sides for each direction (a physical left is the
|
|
2156
|
+
* inline-start in a left-to-right box and the inline-end in a
|
|
2157
|
+
* right-to-left one; the padding shorthand is physical too), then written
|
|
2158
|
+
* to the variables the part's box reads: a side the same in both
|
|
2159
|
+
* directions in the scope's own block, one that differs in a rule for
|
|
2160
|
+
* each direction (one level deep under a state). So the derived and
|
|
2161
|
+
* full-bleed values follow, and a variant or a state out-ranks the base
|
|
2162
|
+
* as Core's own rules do.
|
|
2163
|
+
*/
|
|
2164
|
+
const emitPadding = () => {
|
|
2165
|
+
for (const {target, s, list} of padQueue.values()) {
|
|
2166
|
+
const prefix = /** @type {string} */ (padVars(target));
|
|
2167
|
+
/** @type {Record<'ltr' | 'rtl', Record<string, string>>} */
|
|
2168
|
+
const side = {ltr: {}, rtl: {}};
|
|
2169
|
+
for (const x of list) {
|
|
2170
|
+
const prop = kebab(x.thing);
|
|
2171
|
+
const raw = String(x.value).trim();
|
|
2172
|
+
const value = /^(initial|unset)$/i.test(raw) ? '0' : raw;
|
|
2173
|
+
const w = splitTop(value, ' ').map(y => y.trim());
|
|
2174
|
+
for (const dir of /** @type {const} */ (['ltr', 'rtl'])) {
|
|
2175
|
+
const o = side[dir];
|
|
2176
|
+
const left = dir === 'ltr' ? 'inline-start' : 'inline-end';
|
|
2177
|
+
const right = dir === 'ltr' ? 'inline-end' : 'inline-start';
|
|
2178
|
+
if (prop === 'padding') {
|
|
2179
|
+
const [top, r = top, bottom = top, l = r] = w;
|
|
2180
|
+
Object.assign(o, {
|
|
2181
|
+
'block-start': top,
|
|
2182
|
+
[right]: r,
|
|
2183
|
+
'block-end': bottom,
|
|
2184
|
+
[left]: l,
|
|
2185
|
+
});
|
|
2186
|
+
} else if (prop === 'padding-block') {
|
|
2187
|
+
o['block-start'] = w[0];
|
|
2188
|
+
o['block-end'] = w[1] ?? w[0];
|
|
2189
|
+
} else if (prop === 'padding-inline') {
|
|
2190
|
+
o['inline-start'] = w[0];
|
|
2191
|
+
o['inline-end'] = w[1] ?? w[0];
|
|
2192
|
+
} else if (prop === 'padding-top') o['block-start'] = value;
|
|
2193
|
+
else if (prop === 'padding-bottom') o['block-end'] = value;
|
|
2194
|
+
else if (prop === 'padding-left') o[left] = value;
|
|
2195
|
+
else if (prop === 'padding-right') o[right] = value;
|
|
2196
|
+
else {
|
|
2197
|
+
const k = prop.slice('padding-'.length);
|
|
2198
|
+
if (/^(block|inline)-(start|end)$/.test(k)) o[k] = value;
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
/** @type {Record<string, string>} */
|
|
2203
|
+
const plain = {};
|
|
2204
|
+
/** @type {Record<string, string>} */
|
|
2205
|
+
const ltr = {};
|
|
2206
|
+
/** @type {Record<string, string>} */
|
|
2207
|
+
const rtl = {};
|
|
2208
|
+
for (const k of [
|
|
2209
|
+
'block-start',
|
|
2210
|
+
'block-end',
|
|
2211
|
+
'inline-start',
|
|
2212
|
+
'inline-end',
|
|
2213
|
+
]) {
|
|
2214
|
+
const [a, b] = [side.ltr[k], side.rtl[k]];
|
|
2215
|
+
if (a != null && a === b) plain[`${prefix}-${k}`] = a;
|
|
2216
|
+
else {
|
|
2217
|
+
if (a != null) ltr[`${prefix}-${k}`] = a;
|
|
2218
|
+
if (b != null) rtl[`${prefix}-${k}`] = b;
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
// Four equal sides are the whole padding too, as Core writes it.
|
|
2222
|
+
const vals = Object.values(plain);
|
|
2223
|
+
if (vals.length === 4 && vals.every(v => v === vals[0]))
|
|
2224
|
+
plain[prefix] = vals[0];
|
|
2225
|
+
for (const [k, v] of Object.entries(plain)) putComp(s, target, k, v);
|
|
2226
|
+
for (const [dir, rule] of /** @type {const} */ ([
|
|
2227
|
+
[':dir(ltr)', ltr],
|
|
2228
|
+
[':dir(rtl)', rtl],
|
|
2229
|
+
]))
|
|
2230
|
+
if (Object.keys(rule).length) {
|
|
2231
|
+
const [b, key] = dirBlock(s, target, dir);
|
|
2232
|
+
b[key] = {...(b[key] ?? {}), ...rule};
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
993
2235
|
};
|
|
994
|
-
/**
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
2236
|
+
/**
|
|
2237
|
+
* Each part's blocks with its base first, and in each block the rules for
|
|
2238
|
+
* a direction before a state's: Core writes them in this order, and a
|
|
2239
|
+
* variant or a state comes after the base it out-ranks (rules of the same
|
|
2240
|
+
* weight win by order).
|
|
2241
|
+
*/
|
|
2242
|
+
const orderBlocks = () => {
|
|
2243
|
+
/** @param {Record<string, any>} b @returns {Record<string, any>} */
|
|
2244
|
+
const dirFirst = b => {
|
|
2245
|
+
const keys = Object.keys(b);
|
|
2246
|
+
const plain = keys.filter(k => !k.startsWith(':'));
|
|
2247
|
+
const dirs = keys.filter(k => /^:dir\(/.test(k));
|
|
2248
|
+
const rest = keys.filter(k => k.startsWith(':') && !/^:dir\(/.test(k));
|
|
2249
|
+
return Object.fromEntries(
|
|
2250
|
+
[...plain, ...dirs, ...rest].map(k => [
|
|
2251
|
+
k,
|
|
2252
|
+
k.startsWith(':') && b[k] && typeof b[k] === 'object'
|
|
2253
|
+
? dirFirst(b[k])
|
|
2254
|
+
: b[k],
|
|
2255
|
+
]),
|
|
2256
|
+
);
|
|
2257
|
+
};
|
|
2258
|
+
for (const [target, blocks] of Object.entries(theme.components ?? {})) {
|
|
2259
|
+
const keys = Object.keys(blocks);
|
|
2260
|
+
const ordered = keys.includes('base')
|
|
2261
|
+
? ['base', ...keys.filter(k => k !== 'base')]
|
|
2262
|
+
: keys;
|
|
2263
|
+
theme.components[target] = Object.fromEntries(
|
|
2264
|
+
ordered.map(k => [
|
|
2265
|
+
k,
|
|
2266
|
+
blocks[k] && typeof blocks[k] === 'object'
|
|
2267
|
+
? dirFirst(blocks[k])
|
|
2268
|
+
: blocks[k],
|
|
2269
|
+
]),
|
|
2270
|
+
);
|
|
1004
2271
|
}
|
|
1005
|
-
|
|
2272
|
+
};
|
|
2273
|
+
/**
|
|
2274
|
+
* Where a per-direction rule goes for a setting, one level deep, as the
|
|
2275
|
+
* block and its key: in the setting's own block, or under a state Core
|
|
2276
|
+
* writes as a pseudo-class, beside that state with the direction after it.
|
|
2277
|
+
* @param {Setting} s @param {string} target @param {string} dir `:dir(ltr)`
|
|
2278
|
+
* @returns {[Record<string, any>, string]}
|
|
2279
|
+
*/
|
|
2280
|
+
const dirBlock = (s, target, dir) => {
|
|
2281
|
+
const tg = ctx.project.target.get(target);
|
|
2282
|
+
if (!s.state || tg?.states.includes(s.state))
|
|
2283
|
+
return [block(s, target), dir];
|
|
2284
|
+
const parent = block({...s, state: undefined}, target);
|
|
2285
|
+
return [parent, `${stateKey(s.state, wrapsOneControl(ctx, target))}${dir}`];
|
|
1006
2286
|
};
|
|
1007
2287
|
/** @param {Setting} s @param {string} target @param {string} prop @param {string} v */
|
|
1008
2288
|
const putComp = (s, target, prop, v) => {
|
|
@@ -1256,9 +2536,89 @@ export function compileTheme(ctx, data, base) {
|
|
|
1256
2536
|
if (parts.length) for (const t of parts) putComp(s, t, s.thing, s.value);
|
|
1257
2537
|
else putTok(s.thing, s.value, s.mode);
|
|
1258
2538
|
} else if (s.source === 'property') {
|
|
1259
|
-
for (const t of parts)
|
|
2539
|
+
for (const t of parts) {
|
|
2540
|
+
// A padding on a part whose box reads Core's padding variables is
|
|
2541
|
+
// resolved with the rest of that part's padding, below.
|
|
2542
|
+
if (padVars(t) && /^padding(-|$)/.test(kebab(s.thing))) {
|
|
2543
|
+
queuePadding(s, t);
|
|
2544
|
+
continue;
|
|
2545
|
+
}
|
|
2546
|
+
const sides =
|
|
2547
|
+
wrappedPadding(t, s) ??
|
|
2548
|
+
containerSides(t, s) ??
|
|
2549
|
+
cornerRadii(t, s) ??
|
|
2550
|
+
boxSides(t, s);
|
|
2551
|
+
if (sides)
|
|
2552
|
+
for (const [k, v] of Object.entries(sides)) {
|
|
2553
|
+
// A per-direction block joins what that direction already has,
|
|
2554
|
+
// one level deep: under a state it is the state's own key with
|
|
2555
|
+
// the direction after it (`:hover:dir(ltr)`), which Core writes
|
|
2556
|
+
// as one rule.
|
|
2557
|
+
if (k.startsWith(':') && typeof v === 'object') {
|
|
2558
|
+
const [b, key] = dirBlock(s, t, k);
|
|
2559
|
+
b[key] = {...(b[key] ?? {}), ...v};
|
|
2560
|
+
} else putComp(s, t, k, /** @type {string} */ (v));
|
|
2561
|
+
}
|
|
2562
|
+
else putComp(s, t, camel(s.thing), s.value);
|
|
2563
|
+
// A later padding takes out what it covers that would beat it: a
|
|
2564
|
+
// physical side written as it is (a group's left under a later part
|
|
2565
|
+
// padding; left-to-right, inline-start is the left), and a side
|
|
2566
|
+
// written per direction, whose :dir() rule is more specific.
|
|
2567
|
+
{
|
|
2568
|
+
const prop = kebab(s.thing);
|
|
2569
|
+
/** @type {Record<string, Array<'paddingLeft' | 'paddingRight'>>} */
|
|
2570
|
+
const COVERS = {
|
|
2571
|
+
padding: ['paddingLeft', 'paddingRight'],
|
|
2572
|
+
'padding-inline': ['paddingLeft', 'paddingRight'],
|
|
2573
|
+
'padding-inline-start': ['paddingLeft'],
|
|
2574
|
+
'padding-inline-end': ['paddingRight'],
|
|
2575
|
+
'padding-left': [],
|
|
2576
|
+
'padding-right': [],
|
|
2577
|
+
};
|
|
2578
|
+
const plain = COVERS[prop];
|
|
2579
|
+
if (plain) {
|
|
2580
|
+
const b = block(s, t);
|
|
2581
|
+
// A side written per direction leaves a physical side as it is:
|
|
2582
|
+
// its :dir(ltr) rule already wins in a left-to-right box, and in
|
|
2583
|
+
// a right-to-left one the physical left is still the left.
|
|
2584
|
+
const perDirection =
|
|
2585
|
+
!!sides && Object.keys(sides).some(x => x.startsWith(':'));
|
|
2586
|
+
if (!perDirection)
|
|
2587
|
+
for (const k of plain)
|
|
2588
|
+
if (b[k] != null && !(sides && k in sides)) delete b[k];
|
|
2589
|
+
// Per direction: a whole padding or inline padding covers both
|
|
2590
|
+
// sides in both directions; a physical side, that side in both.
|
|
2591
|
+
const dirSides =
|
|
2592
|
+
prop === 'padding' || prop === 'padding-inline'
|
|
2593
|
+
? ['paddingLeft', 'paddingRight']
|
|
2594
|
+
: prop === 'padding-left'
|
|
2595
|
+
? ['paddingLeft']
|
|
2596
|
+
: prop === 'padding-right'
|
|
2597
|
+
? ['paddingRight']
|
|
2598
|
+
: [];
|
|
2599
|
+
if (
|
|
2600
|
+
dirSides.length &&
|
|
2601
|
+
!(sides && Object.keys(sides).some(x => x.startsWith(':')))
|
|
2602
|
+
)
|
|
2603
|
+
for (const d of [':dir(ltr)', ':dir(rtl)']) {
|
|
2604
|
+
const [db, key] = dirBlock(s, t, d);
|
|
2605
|
+
const rule = db[key];
|
|
2606
|
+
if (!rule) continue;
|
|
2607
|
+
for (const x of dirSides) delete rule[x];
|
|
2608
|
+
if (!Object.keys(rule).length) delete db[key];
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
// A part's own corner radii replace a single radius a group or an
|
|
2613
|
+
// earlier setting left in the same block, which Core would copy into
|
|
2614
|
+
// the content's radius after the editor's own.
|
|
2615
|
+
if (sides && 'borderTopLeftRadius' in sides)
|
|
2616
|
+
delete block(s, t).borderRadius;
|
|
2617
|
+
}
|
|
1260
2618
|
}
|
|
1261
2619
|
}
|
|
2620
|
+
emitPadding();
|
|
2621
|
+
orderBlocks();
|
|
1262
2622
|
// Core gives headings the body font when a theme sets only the body's:
|
|
1263
2623
|
// a font for the body alone keeps the base theme's heading font.
|
|
1264
2624
|
if (theme.typography?.body && !theme.typography.heading) {
|
|
@@ -1323,8 +2683,9 @@ export function compileTheme(ctx, data, base) {
|
|
|
1323
2683
|
theme.tokens[k] = l === d ? l : [l, d];
|
|
1324
2684
|
}
|
|
1325
2685
|
// Base themes set the type tokens again for phones (a coarse pointer
|
|
1326
|
-
// below the lg width); an exact font family
|
|
1327
|
-
//
|
|
2686
|
+
// below the lg width); an exact font family or weight, and the headings
|
|
2687
|
+
// dial's weights, are written there too, so they apply on every screen.
|
|
2688
|
+
// Sizes are not: phones keep their reading base.
|
|
1328
2689
|
const phone = Object.fromEntries(
|
|
1329
2690
|
data.settings
|
|
1330
2691
|
.filter(
|
|
@@ -1332,12 +2693,18 @@ export function compileTheme(ctx, data, base) {
|
|
|
1332
2693
|
x.source === 'token' &&
|
|
1333
2694
|
!x.on &&
|
|
1334
2695
|
!x.mode &&
|
|
1335
|
-
|
|
1336
|
-
/^--font-family/.test(x.thing) &&
|
|
2696
|
+
/^--font-family|^--(text-[a-z0-9-]+|font)-weight/.test(x.thing) &&
|
|
1337
2697
|
!(modeOnly && x.mode && x.mode !== modeOnly),
|
|
1338
2698
|
)
|
|
1339
2699
|
.map(x => [x.thing, x.value]),
|
|
1340
2700
|
);
|
|
2701
|
+
// The headings dial's weights, unless an exact token sets one.
|
|
2702
|
+
const headingsWeight = [...data.settings]
|
|
2703
|
+
.reverse()
|
|
2704
|
+
.find(x => x.thing === 'headings' && x.axis !== 'tracking' && !x.on);
|
|
2705
|
+
if (headingsWeight && HEADING_WEIGHTS[headingsWeight.value])
|
|
2706
|
+
for (const k of headingWeightTokens)
|
|
2707
|
+
phone[k] ??= HEADING_WEIGHTS[headingsWeight.value];
|
|
1341
2708
|
if (Object.keys(phone).length)
|
|
1342
2709
|
theme.adaptations = {
|
|
1343
2710
|
rules: [
|
|
@@ -1367,9 +2734,10 @@ const SMALL_STEPS = /** @type {Array<[string, number]>} */ ([
|
|
|
1367
2734
|
* Settings whose effect a later or stronger setting hides, with why: `type`
|
|
1368
2735
|
* loses heading weight and tracking to `headings`.
|
|
1369
2736
|
* @param {ThemeData} data
|
|
2737
|
+
* @param {Ctx | null} [ctx] with it, a later setting that reaches the parts through a spread counts too
|
|
1370
2738
|
* @returns {Map<Setting, string>}
|
|
1371
2739
|
*/
|
|
1372
|
-
export function maskedSettings(data) {
|
|
2740
|
+
export function maskedSettings(data, ctx = null) {
|
|
1373
2741
|
/** @type {Map<Setting, string>} */
|
|
1374
2742
|
const out = new Map();
|
|
1375
2743
|
const axes = new Set(
|
|
@@ -1409,6 +2777,37 @@ export function maskedSettings(data) {
|
|
|
1409
2777
|
`${describe(later)} comes later and covers ${s.on}, so ${s.on} shows ${later.value}; set ${s.on} again to make it win`,
|
|
1410
2778
|
);
|
|
1411
2779
|
});
|
|
2780
|
+
// With the parts known: a later setting of the same thing that reaches
|
|
2781
|
+
// every part an earlier one styles (a member that spreads to its group,
|
|
2782
|
+
// the same part without --only), dial or CSS property, covers it too.
|
|
2783
|
+
if (ctx)
|
|
2784
|
+
data.settings.forEach((s, i) => {
|
|
2785
|
+
if (
|
|
2786
|
+
out.has(s) ||
|
|
2787
|
+
!s.on ||
|
|
2788
|
+
(s.source !== 'dial' && s.source !== 'property')
|
|
2789
|
+
)
|
|
2790
|
+
return;
|
|
2791
|
+
const mine = partsOf(ctx, s);
|
|
2792
|
+
if (!mine.length) return;
|
|
2793
|
+
const later = data.settings
|
|
2794
|
+
.slice(i + 1)
|
|
2795
|
+
.find(
|
|
2796
|
+
x =>
|
|
2797
|
+
x.thing === s.thing &&
|
|
2798
|
+
x.source === s.source &&
|
|
2799
|
+
x.on &&
|
|
2800
|
+
(x.when ?? '') === (s.when ?? '') &&
|
|
2801
|
+
(x.state ?? '') === (s.state ?? '') &&
|
|
2802
|
+
(x.mode ?? '') === (s.mode ?? '') &&
|
|
2803
|
+
mine.every(t => partsOf(ctx, x).includes(t)),
|
|
2804
|
+
);
|
|
2805
|
+
if (later)
|
|
2806
|
+
out.set(
|
|
2807
|
+
s,
|
|
2808
|
+
`${describe(later)} comes later and covers ${s.on}, so ${s.on} shows ${later.value}; set ${s.on} again to make it win`,
|
|
2809
|
+
);
|
|
2810
|
+
});
|
|
1412
2811
|
for (const s of data.settings) {
|
|
1413
2812
|
if (s.thing !== 'type' || !axes.size) continue;
|
|
1414
2813
|
const T = TYPE_STYLES[s.value];
|
|
@@ -1448,16 +2847,13 @@ export function findOverrides(cssText, theme) {
|
|
|
1448
2847
|
'g',
|
|
1449
2848
|
);
|
|
1450
2849
|
// A shorthand and its longhands are one property: the last one set
|
|
1451
|
-
// (background after background-color) is the one in effect.
|
|
1452
|
-
//
|
|
1453
|
-
//
|
|
1454
|
-
const NOT_LONGHAND =
|
|
1455
|
-
/^border-(radius|collapse|spacing)|^border-[a-z]+-radius$/;
|
|
2850
|
+
// (background after background-color) is the one in effect. Which
|
|
2851
|
+
// longhands a shorthand sets is MDN's data (background-blend-mode is
|
|
2852
|
+
// no part of background).
|
|
1456
2853
|
/** @param {string} a @param {string} b */
|
|
1457
2854
|
const sameProperty = (a, b) => {
|
|
1458
2855
|
const [x, y] = [kebab(a), kebab(b)];
|
|
1459
|
-
|
|
1460
|
-
return x.startsWith(`${y}-`) || y.startsWith(`${x}-`) || x === y;
|
|
2856
|
+
return x === y || coversProperty(x, y) || coversProperty(y, x);
|
|
1461
2857
|
};
|
|
1462
2858
|
const last = props.filter(
|
|
1463
2859
|
(p, i) => !props.slice(i + 1).some(later => sameProperty(later, p)),
|
|
@@ -1592,6 +2988,12 @@ function settingsSource(slug, data, theme, pal, cwd = '') {
|
|
|
1592
2988
|
[
|
|
1593
2989
|
`// Generated by \`${P}\` from ${slug}.settings.json on every change: do not edit.`,
|
|
1594
2990
|
`// Built from settings ${settingsStamp(data)}.`,
|
|
2991
|
+
// The theme source as it is when the build starts (the editor's
|
|
2992
|
+
// own when the build writes it), so an own change to it after the
|
|
2993
|
+
// build is told by content.
|
|
2994
|
+
...(cwd
|
|
2995
|
+
? [`// Built with ${slug}.theme.ts ${themeSourceHash(cwd, slug)}.`]
|
|
2996
|
+
: []),
|
|
1595
2997
|
...(like && cwd
|
|
1596
2998
|
? [
|
|
1597
2999
|
`// Built on @astryxdesign/theme-${like} ${baseVersionOf(cwd, like) ?? 'unknown'}.`,
|
|
@@ -1615,6 +3017,130 @@ function settingsSource(slug, data, theme, pal, cwd = '') {
|
|
|
1615
3017
|
);
|
|
1616
3018
|
}
|
|
1617
3019
|
|
|
3020
|
+
/**
|
|
3021
|
+
* A hash of a theme's source file as the next build reads it: the file, or
|
|
3022
|
+
* the source the editor writes when there is none.
|
|
3023
|
+
* @param {string} cwd
|
|
3024
|
+
* @param {string} slug
|
|
3025
|
+
*/
|
|
3026
|
+
function themeSourceHash(cwd, slug) {
|
|
3027
|
+
const f = themeFiles(cwd, slug);
|
|
3028
|
+
const hash = createHash('sha256');
|
|
3029
|
+
if (!fs.existsSync(f.ts)) {
|
|
3030
|
+
// The same as the file the build is about to write would hash.
|
|
3031
|
+
hash.update(`${path.basename(f.ts)}\0`);
|
|
3032
|
+
hash.update(themeModuleSource(slug));
|
|
3033
|
+
return hash.digest('hex').slice(0, 16);
|
|
3034
|
+
}
|
|
3035
|
+
for (const file of ownSourceFiles(cwd, slug)) {
|
|
3036
|
+
hash.update(`${path.relative(f.dir, file)}\0`);
|
|
3037
|
+
hash.update(fs.readFileSync(file));
|
|
3038
|
+
}
|
|
3039
|
+
return hash.digest('hex').slice(0, 16);
|
|
3040
|
+
}
|
|
3041
|
+
|
|
3042
|
+
/**
|
|
3043
|
+
* The theme source and every local module it reaches (an own helper such
|
|
3044
|
+
* as ./my-tokens), read the way theme discovery reads them: the person's
|
|
3045
|
+
* files. The editor's generated modules change with each build and are
|
|
3046
|
+
* judged by their own stamps.
|
|
3047
|
+
* @param {string} cwd
|
|
3048
|
+
* @param {string} slug
|
|
3049
|
+
* @returns {string[]}
|
|
3050
|
+
*/
|
|
3051
|
+
function ownSourceFiles(cwd, slug) {
|
|
3052
|
+
const f = themeFiles(cwd, slug);
|
|
3053
|
+
if (!fs.existsSync(f.ts)) return [];
|
|
3054
|
+
const generated = new Set([f.settingsTs]);
|
|
3055
|
+
const seen = new Set();
|
|
3056
|
+
/** @type {string[]} */
|
|
3057
|
+
const files = [];
|
|
3058
|
+
const queue = [f.ts];
|
|
3059
|
+
while (queue.length) {
|
|
3060
|
+
const file = /** @type {string} */ (queue.shift());
|
|
3061
|
+
if (seen.has(file)) continue;
|
|
3062
|
+
seen.add(file);
|
|
3063
|
+
files.push(file);
|
|
3064
|
+
/** @type {string[]} */
|
|
3065
|
+
let specifiers = [];
|
|
3066
|
+
try {
|
|
3067
|
+
specifiers = themeFileImports(file, {withRequire: true});
|
|
3068
|
+
} catch {
|
|
3069
|
+
// A file that does not parse is hashed as it is; the build names it.
|
|
3070
|
+
}
|
|
3071
|
+
for (const spec of specifiers) {
|
|
3072
|
+
if (!spec.startsWith('.')) continue;
|
|
3073
|
+
const target = localModuleFile(path.dirname(file), spec);
|
|
3074
|
+
// Only the editor's own generated palette files are judged by their
|
|
3075
|
+
// stamps; any other file the theme reaches, in tokens/ too, is the
|
|
3076
|
+
// person's.
|
|
3077
|
+
if (
|
|
3078
|
+
target &&
|
|
3079
|
+
!generated.has(target) &&
|
|
3080
|
+
!isGeneratedPaletteFile(f.dir, slug, target)
|
|
3081
|
+
)
|
|
3082
|
+
queue.push(target);
|
|
3083
|
+
}
|
|
3084
|
+
}
|
|
3085
|
+
return files;
|
|
3086
|
+
}
|
|
3087
|
+
|
|
3088
|
+
/** The extensions a theme's local module may have, as the build reads them. */
|
|
3089
|
+
const MODULE_EXTENSIONS = [
|
|
3090
|
+
'.ts',
|
|
3091
|
+
'.tsx',
|
|
3092
|
+
'.mts',
|
|
3093
|
+
'.cts',
|
|
3094
|
+
'.js',
|
|
3095
|
+
'.jsx',
|
|
3096
|
+
'.mjs',
|
|
3097
|
+
'.cjs',
|
|
3098
|
+
];
|
|
3099
|
+
|
|
3100
|
+
/**
|
|
3101
|
+
* The file a relative import names, resolved as TypeScript resolves it: a
|
|
3102
|
+
* JavaScript extension names its TypeScript source (`.js` → `.ts`/`.tsx`,
|
|
3103
|
+
* `.jsx` → `.tsx`, `.mjs` → `.mts`, `.cjs` → `.cts`), then the path as
|
|
3104
|
+
* written, with each module extension, and as a folder's index file.
|
|
3105
|
+
* @param {string} dir
|
|
3106
|
+
* @param {string} spec
|
|
3107
|
+
* @returns {string | undefined}
|
|
3108
|
+
*/
|
|
3109
|
+
function localModuleFile(dir, spec) {
|
|
3110
|
+
const base = path.resolve(dir, spec);
|
|
3111
|
+
const ext = path.extname(base);
|
|
3112
|
+
const stem = base.slice(0, base.length - ext.length);
|
|
3113
|
+
const sourceFor = /** @type {Record<string, string[]>} */ ({
|
|
3114
|
+
'.js': ['.ts', '.tsx'],
|
|
3115
|
+
'.jsx': ['.tsx'],
|
|
3116
|
+
'.mjs': ['.mts'],
|
|
3117
|
+
'.cjs': ['.cts'],
|
|
3118
|
+
})[ext];
|
|
3119
|
+
return [
|
|
3120
|
+
...(sourceFor ?? []).map(x => stem + x),
|
|
3121
|
+
base,
|
|
3122
|
+
...MODULE_EXTENSIONS.map(x => base + x),
|
|
3123
|
+
...MODULE_EXTENSIONS.map(x => path.join(base, `index${x}`)),
|
|
3124
|
+
].find(p => fs.existsSync(p) && fs.statSync(p).isFile());
|
|
3125
|
+
}
|
|
3126
|
+
|
|
3127
|
+
/**
|
|
3128
|
+
* Whether a file is one of the palette files the editor generates in a
|
|
3129
|
+
* theme's tokens/ folder (`tokens/<slug>.palette*`).
|
|
3130
|
+
* @param {string} dir the theme folder
|
|
3131
|
+
* @param {string} slug
|
|
3132
|
+
* @param {string} file
|
|
3133
|
+
*/
|
|
3134
|
+
function isGeneratedPaletteFile(dir, slug, file) {
|
|
3135
|
+
const rel = path.relative(path.join(dir, 'tokens'), file);
|
|
3136
|
+
return (
|
|
3137
|
+
!rel.startsWith('..') &&
|
|
3138
|
+
!path.isAbsolute(rel) &&
|
|
3139
|
+
!rel.includes(path.sep) &&
|
|
3140
|
+
rel.startsWith(`${slug}.palette`)
|
|
3141
|
+
);
|
|
3142
|
+
}
|
|
3143
|
+
|
|
1618
3144
|
/**
|
|
1619
3145
|
* The theme module, written once: it extends the generated settings and is
|
|
1620
3146
|
* the person's own from then on, so hand edits in it survive every change.
|
|
@@ -1666,6 +3192,52 @@ export function themeSourceProblem(cwd, slug) {
|
|
|
1666
3192
|
return null;
|
|
1667
3193
|
}
|
|
1668
3194
|
|
|
3195
|
+
/**
|
|
3196
|
+
* Why the person's own theme source does not parse (its line and the
|
|
3197
|
+
* parser's message), or null when it parses or is not there.
|
|
3198
|
+
* @param {string} cwd
|
|
3199
|
+
* @param {string} slug
|
|
3200
|
+
* @returns {string | null}
|
|
3201
|
+
*/
|
|
3202
|
+
export function themeSourceParseError(cwd, slug) {
|
|
3203
|
+
const f = themeFiles(cwd, slug);
|
|
3204
|
+
let code;
|
|
3205
|
+
try {
|
|
3206
|
+
code = fs.readFileSync(f.ts, 'utf8');
|
|
3207
|
+
} catch {
|
|
3208
|
+
return null;
|
|
3209
|
+
}
|
|
3210
|
+
try {
|
|
3211
|
+
parseModule(code, {sourceType: 'module', plugins: ['typescript']});
|
|
3212
|
+
return null;
|
|
3213
|
+
} catch (e) {
|
|
3214
|
+
const err = /** @type {any} */ (e);
|
|
3215
|
+
return `${slug}.theme.ts does not parse${err?.loc?.line ? ` (line ${err.loc.line})` : ''}: ${String(err?.message ?? e).replace(/\s*\(\d+:\d+\)\s*$/, '')}`;
|
|
3216
|
+
}
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
/**
|
|
3220
|
+
* Refuse, before anything is written or removed, a command whose build
|
|
3221
|
+
* would load the person's own theme source when it does not parse: the
|
|
3222
|
+
* build would fail part way.
|
|
3223
|
+
* @param {string} cwd
|
|
3224
|
+
* @param {string} slug
|
|
3225
|
+
*/
|
|
3226
|
+
export function refuseThemeParse(cwd, slug) {
|
|
3227
|
+
const why = themeSourceParseError(cwd, slug);
|
|
3228
|
+
if (why)
|
|
3229
|
+
throw new AstryxError(
|
|
3230
|
+
`${why}, so the theme cannot build. It is your own file: fix it, then run the command again. Nothing was changed.`,
|
|
3231
|
+
[
|
|
3232
|
+
{
|
|
3233
|
+
name: `cat ${path.posix.join('themes', slug, `${slug}.theme.ts`)}`,
|
|
3234
|
+
reason: 'see the file',
|
|
3235
|
+
},
|
|
3236
|
+
],
|
|
3237
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
3238
|
+
);
|
|
3239
|
+
}
|
|
3240
|
+
|
|
1669
3241
|
/**
|
|
1670
3242
|
* Refuse a build whose theme source cannot carry the settings, naming the
|
|
1671
3243
|
* lines that put it back.
|
|
@@ -1673,6 +3245,7 @@ export function themeSourceProblem(cwd, slug) {
|
|
|
1673
3245
|
* @param {string} slug
|
|
1674
3246
|
*/
|
|
1675
3247
|
export function refuseThemeSource(cwd, slug) {
|
|
3248
|
+
refuseThemeParse(cwd, slug);
|
|
1676
3249
|
const why = themeSourceProblem(cwd, slug);
|
|
1677
3250
|
if (why)
|
|
1678
3251
|
throw new AstryxError(
|
|
@@ -1764,7 +3337,16 @@ export async function buildTheme(ctx, slug, data) {
|
|
|
1764
3337
|
if (found.length) {
|
|
1765
3338
|
for (const x of found) {
|
|
1766
3339
|
const t = theme.components[x.target];
|
|
1767
|
-
|
|
3340
|
+
// The base value goes before the variant's own declarations, so a
|
|
3341
|
+
// longhand or shorthand the variant sets itself (border-color after
|
|
3342
|
+
// a base border) still wins over it.
|
|
3343
|
+
const own = t[x.key] ?? {};
|
|
3344
|
+
// A variant that sets every longhand of the property itself needs
|
|
3345
|
+
// none of the base value (and a lifted radius would feed Core's copy
|
|
3346
|
+
// of it into the content's radius, after the variant's own).
|
|
3347
|
+
const longs = [...longhandsOf(kebab(x.prop))];
|
|
3348
|
+
if (longs.length && longs.every(l => camel(l) in own)) continue;
|
|
3349
|
+
t[x.key] = {[x.prop]: x.value, ...own};
|
|
1768
3350
|
lifted.push(`${x.target} ${x.key} ${kebab(x.prop)}`);
|
|
1769
3351
|
}
|
|
1770
3352
|
await step(() => {
|
|
@@ -1808,6 +3390,46 @@ async function runBuild(ctx, slug) {
|
|
|
1808
3390
|
}
|
|
1809
3391
|
}
|
|
1810
3392
|
|
|
3393
|
+
/**
|
|
3394
|
+
* Why the built outputs differ from what the theme builds now: one that is
|
|
3395
|
+
* missing or changed by hand, found the way `compile --check` finds it
|
|
3396
|
+
* (the same build, compared and not written), or null when they match.
|
|
3397
|
+
* @param {Ctx} ctx
|
|
3398
|
+
* @param {string} slug
|
|
3399
|
+
* @returns {Promise<string | null>}
|
|
3400
|
+
*/
|
|
3401
|
+
export async function outputsDrift(ctx, slug) {
|
|
3402
|
+
const {cwd} = ctx.project;
|
|
3403
|
+
const f = themeFiles(cwd, slug);
|
|
3404
|
+
const silent = logger.silent;
|
|
3405
|
+
logger.setSilent(true);
|
|
3406
|
+
try {
|
|
3407
|
+
const {receipt} = await themeBuildForReport(
|
|
3408
|
+
shown(cwd, f.ts),
|
|
3409
|
+
{check: true},
|
|
3410
|
+
{cwd},
|
|
3411
|
+
{compact: true},
|
|
3412
|
+
);
|
|
3413
|
+
const first = /** @type {any} */ (receipt)?.data?.stale?.[0];
|
|
3414
|
+
if (!first) return null;
|
|
3415
|
+
const name = path.basename(String(first.path));
|
|
3416
|
+
return first.reason === 'missing'
|
|
3417
|
+
? `${name} was missing`
|
|
3418
|
+
: `${name} differs from what the settings build, after an edit by hand or by another tool`;
|
|
3419
|
+
} catch (e) {
|
|
3420
|
+
// A theme source that does not load is named by its own checks; any
|
|
3421
|
+
// other failure means the outputs could not be compared: built again.
|
|
3422
|
+
const err = /** @type {any} */ (e);
|
|
3423
|
+
if (err instanceof AstryxError && err.code === ERROR_CODES.ERR_THEME_LOAD)
|
|
3424
|
+
return null;
|
|
3425
|
+
return `the outputs could not be checked (${String(err?.message ?? e)
|
|
3426
|
+
.split('\n')[0]
|
|
3427
|
+
.slice(0, 160)})`;
|
|
3428
|
+
} finally {
|
|
3429
|
+
logger.setSilent(silent);
|
|
3430
|
+
}
|
|
3431
|
+
}
|
|
3432
|
+
|
|
1811
3433
|
/**
|
|
1812
3434
|
* Resolved tokens of the built theme in both modes.
|
|
1813
3435
|
* @param {Ctx} ctx
|
|
@@ -1855,10 +3477,41 @@ export async function resolvedTokens(ctx, slug) {
|
|
|
1855
3477
|
v => v && typeof v === 'object' && typeof v.name === 'string',
|
|
1856
3478
|
);
|
|
1857
3479
|
const {core} = ctx.project;
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
3480
|
+
// A token value an own theme source gives as a number (8 for 8px)
|
|
3481
|
+
// reads as its text, never as a crash.
|
|
3482
|
+
const asText = (/** @type {Record<string, unknown>} */ m) =>
|
|
3483
|
+
Object.fromEntries(
|
|
3484
|
+
Object.entries(m ?? {}).map(([k, v]) => [
|
|
3485
|
+
k,
|
|
3486
|
+
typeof v === 'string' ? v : String(v),
|
|
3487
|
+
]),
|
|
3488
|
+
);
|
|
3489
|
+
// A token value that is not text (a number in an own theme source) is
|
|
3490
|
+
// named, never a crash.
|
|
3491
|
+
const resolve = (/** @type {'light' | 'dark'} */ mode) => {
|
|
3492
|
+
try {
|
|
3493
|
+
return asText(core.resolveThemeTokens(t, {mode}));
|
|
3494
|
+
} catch (e) {
|
|
3495
|
+
const bad = nonTextToken(t, ctx.C.typeOf);
|
|
3496
|
+
throw new AstryxError(
|
|
3497
|
+
bad
|
|
3498
|
+
? nonTextMessage(ctx, slug, bad)
|
|
3499
|
+
: `${slug} could not be read: ${e instanceof Error ? e.message : String(e)}.`,
|
|
3500
|
+
[{name: `${P} how --list tokens`, reason: 'every token and its type'}],
|
|
3501
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
3502
|
+
);
|
|
3503
|
+
}
|
|
1861
3504
|
};
|
|
3505
|
+
// A pair that holds a value that is not text ([8, 8]) resolves, but
|
|
3506
|
+
// builds nothing a browser reads: named the same way.
|
|
3507
|
+
const pairBad = nonTextToken(t, ctx.C.typeOf);
|
|
3508
|
+
if (pairBad)
|
|
3509
|
+
throw new AstryxError(
|
|
3510
|
+
nonTextMessage(ctx, slug, pairBad),
|
|
3511
|
+
[{name: `${P} how --list tokens`, reason: 'every token and its type'}],
|
|
3512
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
3513
|
+
);
|
|
3514
|
+
const out = {light: resolve('light'), dark: resolve('dark')};
|
|
1862
3515
|
// The tokens each part sets for itself (the official themes lift
|
|
1863
3516
|
// secondary text on some parts), for the contrast check; kept out of the
|
|
1864
3517
|
// modes so nothing that lists the resolved tokens sees it.
|
|
@@ -1873,6 +3526,90 @@ export async function resolvedTokens(ctx, slug) {
|
|
|
1873
3526
|
return out;
|
|
1874
3527
|
}
|
|
1875
3528
|
|
|
3529
|
+
/**
|
|
3530
|
+
* The first token a built theme gives a value that is not text, alone or
|
|
3531
|
+
* in a [light, dark] pair, or null.
|
|
3532
|
+
* @param {any} t
|
|
3533
|
+
* @param {(name: string) => string} [typeOf] a token's type (size, color…)
|
|
3534
|
+
* @returns {[string, unknown, 'object' | 'pair' | 'unit' | 'value'] | null}
|
|
3535
|
+
*/
|
|
3536
|
+
function nonTextToken(t, typeOf = (/** @type {string} */ _k) => 'other') {
|
|
3537
|
+
for (const [k, raw] of Object.entries(t?.tokens ?? {})) {
|
|
3538
|
+
// An object ({light, dark}) is not a value Core builds: it would write
|
|
3539
|
+
// [object Object].
|
|
3540
|
+
if (raw && typeof raw === 'object' && !Array.isArray(raw))
|
|
3541
|
+
return [k, raw, 'object'];
|
|
3542
|
+
// A built theme writes a pair as light-dark(). A half that was not text
|
|
3543
|
+
// comes out as nothing, null or a boolean; a bare number is text the
|
|
3544
|
+
// source may have quoted, and is wrong only for a size, which needs a
|
|
3545
|
+
// unit.
|
|
3546
|
+
const pair =
|
|
3547
|
+
typeof raw === 'string' ? /^light-dark\((.*)\)$/s.exec(raw.trim()) : null;
|
|
3548
|
+
// Split at the top-level comma: a half may hold commas (rgb(0, 0, 0)).
|
|
3549
|
+
const parts = pair ? splitTop(pair[1], ',').map(x => x.trim()) : [];
|
|
3550
|
+
if (pair && parts.length === 2) {
|
|
3551
|
+
const halves = parts;
|
|
3552
|
+
const missing = halves.some(x =>
|
|
3553
|
+
/^(|null|undefined|true|false)$/.test(x),
|
|
3554
|
+
);
|
|
3555
|
+
const unitless =
|
|
3556
|
+
typeOf(k) === 'size' &&
|
|
3557
|
+
halves.some(x => /^-?\d+(\.\d+)?$/.test(x) && Number(x) !== 0);
|
|
3558
|
+
if (missing || unitless) {
|
|
3559
|
+
const half = (/** @type {string} */ x) =>
|
|
3560
|
+
/^-?\d+(\.\d+)?$/.test(x)
|
|
3561
|
+
? Number(x)
|
|
3562
|
+
: x === '' || x === 'undefined' || x === 'null'
|
|
3563
|
+
? null
|
|
3564
|
+
: x === 'true' || x === 'false'
|
|
3565
|
+
? x === 'true'
|
|
3566
|
+
: x;
|
|
3567
|
+
return [k, [half(parts[0]), half(parts[1])], missing ? 'pair' : 'unit'];
|
|
3568
|
+
}
|
|
3569
|
+
continue;
|
|
3570
|
+
}
|
|
3571
|
+
if (Array.isArray(raw)) {
|
|
3572
|
+
if (raw.length !== 2 || raw.some(x => typeof x !== 'string'))
|
|
3573
|
+
return [k, raw, 'pair'];
|
|
3574
|
+
continue;
|
|
3575
|
+
}
|
|
3576
|
+
if (typeof raw !== 'string') return [k, raw, 'value'];
|
|
3577
|
+
}
|
|
3578
|
+
return null;
|
|
3579
|
+
}
|
|
3580
|
+
|
|
3581
|
+
/**
|
|
3582
|
+
* Why a token value is not one, with a value of the token's own type to
|
|
3583
|
+
* write instead (a size in px, a weight as a number in quotes), or none.
|
|
3584
|
+
* @param {Ctx} ctx
|
|
3585
|
+
* @param {string} slug
|
|
3586
|
+
* @param {[string, unknown, ('object' | 'pair' | 'unit' | 'value')?]} bad
|
|
3587
|
+
*/
|
|
3588
|
+
function nonTextMessage(ctx, slug, [name, value, kind = 'value']) {
|
|
3589
|
+
const type = ctx.C.typeOf(name);
|
|
3590
|
+
if (kind === 'object')
|
|
3591
|
+
return `${slug}.theme.ts gives ${name} an object (${JSON.stringify(value)}); a token value is a CSS value in quotes, or a [light, dark] pair such as ['6px', '8px'].`;
|
|
3592
|
+
if (kind === 'unit') {
|
|
3593
|
+
const n = /** @type {unknown[]} */ (value).find(
|
|
3594
|
+
x => typeof x === 'number' && x !== 0,
|
|
3595
|
+
);
|
|
3596
|
+
return `${slug}.theme.ts gives ${name}, a size, a [light, dark] pair with a number without a unit (${/** @type {unknown[]} */ (value).join(', ')}); a size needs one, such as ['${n}px', '${n}px'].`;
|
|
3597
|
+
}
|
|
3598
|
+
const one = Array.isArray(value)
|
|
3599
|
+
? value.find(x => typeof x !== 'string')
|
|
3600
|
+
: value;
|
|
3601
|
+
const example =
|
|
3602
|
+
typeof one === 'number'
|
|
3603
|
+
? type === 'size'
|
|
3604
|
+
? `'${one}px'`
|
|
3605
|
+
: `'${one}'`
|
|
3606
|
+
: null;
|
|
3607
|
+
const shape = Array.isArray(value)
|
|
3608
|
+
? 'a [light, dark] pair of two CSS values in quotes'
|
|
3609
|
+
: 'a CSS value in quotes';
|
|
3610
|
+
return `${slug}.theme.ts gives ${name} the value ${JSON.stringify(value)}, which is not text: a token value is ${shape}${example ? ` (such as ${Array.isArray(value) ? `[${example}, ${example}]` : example})` : ''}.`;
|
|
3611
|
+
}
|
|
3612
|
+
|
|
1876
3613
|
/**
|
|
1877
3614
|
* Contrast problems this theme caused: text below 4.5:1 that was not as low
|
|
1878
3615
|
* in the base theme. A part's own color or background setting, or a token
|
|
@@ -2050,7 +3787,7 @@ export async function contrastProblems(ctx, data, res) {
|
|
|
2050
3787
|
})(),
|
|
2051
3788
|
...(unchecked.length
|
|
2052
3789
|
? [
|
|
2053
|
-
`not checked: text on or in ${unchecked.slice(0, 3).join(', ')}${unchecked.length > 3 ? ` and ${unchecked.length - 3} more` : ''}, which
|
|
3790
|
+
`not checked: text on or in ${unchecked.slice(0, 3).join(', ')}${unchecked.length > 3 ? ` and ${unchecked.length - 3} more` : ''}, which the editor cannot read as one fixed color`,
|
|
2054
3791
|
]
|
|
2055
3792
|
: []),
|
|
2056
3793
|
];
|
|
@@ -2118,7 +3855,9 @@ export function baseComponent(base, ctx, target, prop, s) {
|
|
|
2118
3855
|
const states = ctx.project.target.get(target)?.states ?? [];
|
|
2119
3856
|
b = states.includes(s.state)
|
|
2120
3857
|
? t[s.when == null ? s.state : `${s.when}+${s.state}`]
|
|
2121
|
-
: (b?.[stateKey(s.state)] ??
|
|
3858
|
+
: (b?.[stateKey(s.state, wrapsOneControl(ctx, target))] ??
|
|
3859
|
+
b?.[stateKey(s.state)] ??
|
|
3860
|
+
b?.[`:${s.state}`]);
|
|
2122
3861
|
}
|
|
2123
3862
|
const v = b?.[prop];
|
|
2124
3863
|
return typeof v === 'string' ? v : null;
|
|
@@ -2184,15 +3923,64 @@ export function clearNewMarker(dir) {
|
|
|
2184
3923
|
|
|
2185
3924
|
/**
|
|
2186
3925
|
* The pseudo key a state writes in a part's styles. Core marks a disabled
|
|
2187
|
-
* button, link
|
|
2188
|
-
*
|
|
3926
|
+
* button, link or tab with aria-disabled (it stays focusable). A form
|
|
3927
|
+
* control styled on a wrapper (a typeahead, a complex selector) has its
|
|
3928
|
+
* input or button inside marked, so it also matches a disabled form
|
|
3929
|
+
* control inside it (never a list row); an action part that holds
|
|
3930
|
+
* children (a button group, a segmented control) never does, so one
|
|
3931
|
+
* disabled child never makes the whole part look disabled.
|
|
2189
3932
|
* @param {string} state
|
|
3933
|
+
* @param {boolean} [wrapsControl]
|
|
2190
3934
|
* @returns {string}
|
|
2191
3935
|
*/
|
|
2192
|
-
export function stateKey(state) {
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
:
|
|
3936
|
+
export function stateKey(state, wrapsControl = false) {
|
|
3937
|
+
if (state !== 'disabled') return `:${state}`;
|
|
3938
|
+
return wrapsControl
|
|
3939
|
+
? ':is(:disabled,[aria-disabled="true"],:has(:is(input,button,select,textarea):is(:disabled,[aria-disabled="true"])))'
|
|
3940
|
+
: ':is(:disabled,[aria-disabled="true"])';
|
|
3941
|
+
}
|
|
3942
|
+
|
|
3943
|
+
/**
|
|
3944
|
+
* The component docs of a part: its own component's, and the one named like
|
|
3945
|
+
* the part (tab is Tab, under TabList), each with its name, for the ones the
|
|
3946
|
+
* project has.
|
|
3947
|
+
* @param {Ctx} ctx
|
|
3948
|
+
* @param {string} part
|
|
3949
|
+
* @returns {Array<{name: string, doc: any}>}
|
|
3950
|
+
*/
|
|
3951
|
+
export function partComponentDocs(ctx, part) {
|
|
3952
|
+
const comps = /** @type {Record<string, any>} */ (
|
|
3953
|
+
ctx.project.components ?? {}
|
|
3954
|
+
);
|
|
3955
|
+
const pascal = part.replace(/(^|-)([a-z0-9])/g, (_, __, c) =>
|
|
3956
|
+
c.toUpperCase(),
|
|
3957
|
+
);
|
|
3958
|
+
return [ctx.project.target.get(part)?.component, pascal]
|
|
3959
|
+
.filter(n => n != null && comps[n])
|
|
3960
|
+
.map(n => ({
|
|
3961
|
+
name: /** @type {string} */ (n),
|
|
3962
|
+
doc: comps[/** @type {string} */ (n)],
|
|
3963
|
+
}));
|
|
3964
|
+
}
|
|
3965
|
+
|
|
3966
|
+
/**
|
|
3967
|
+
* Whether a part's styled element wraps one control: its component (or the
|
|
3968
|
+
* one named like the part) is a form control (Core's own category) that
|
|
3969
|
+
* takes a disabled prop. An action part that holds children (a button
|
|
3970
|
+
* group) never does.
|
|
3971
|
+
* @param {Ctx} ctx
|
|
3972
|
+
* @param {string} target
|
|
3973
|
+
*/
|
|
3974
|
+
export function wrapsOneControl(ctx, target) {
|
|
3975
|
+
// One control: a form control with a disabled prop whose content is not
|
|
3976
|
+
// other elements (a radio list's items, an input group's or a field's
|
|
3977
|
+
// children each hold controls of their own).
|
|
3978
|
+
return partComponentDocs(ctx, target).some(
|
|
3979
|
+
({doc: d}) =>
|
|
3980
|
+
/form/i.test(String(d.category ?? '')) &&
|
|
3981
|
+
Object.keys(d.props ?? {}).some(p => /disabled/i.test(p)) &&
|
|
3982
|
+
d.props?.children?.type !== 'ReactNode',
|
|
3983
|
+
);
|
|
2196
3984
|
}
|
|
2197
3985
|
|
|
2198
3986
|
/**
|
|
@@ -2394,8 +4182,12 @@ function restoreWritten(was, wrote) {
|
|
|
2394
4182
|
continue;
|
|
2395
4183
|
}
|
|
2396
4184
|
const old = was.get(p);
|
|
2397
|
-
if (old)
|
|
2398
|
-
|
|
4185
|
+
if (old) {
|
|
4186
|
+
// A folder the change removed (tokens/ once its palette went) comes
|
|
4187
|
+
// back with the file.
|
|
4188
|
+
fs.mkdirSync(path.dirname(p), {recursive: true});
|
|
4189
|
+
writeWhole(p, old);
|
|
4190
|
+
} else fs.rmSync(p, {force: true});
|
|
2399
4191
|
} catch {
|
|
2400
4192
|
failed.push(path.basename(p));
|
|
2401
4193
|
}
|
|
@@ -2483,15 +4275,12 @@ export function settingsStamp(data) {
|
|
|
2483
4275
|
*/
|
|
2484
4276
|
export function staleBuild(cwd, slug, data) {
|
|
2485
4277
|
const f = themeFiles(cwd, slug);
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
const missing = [f.js, f.css, f.settingsTs, ...colors].find(
|
|
2493
|
-
p => !fs.existsSync(p),
|
|
2494
|
-
);
|
|
4278
|
+
const files = builtFiles(cwd, slug, data);
|
|
4279
|
+
// An output that cannot be read (its permissions, or a folder in its
|
|
4280
|
+
// place) is refused by name: the build cannot replace it either. That
|
|
4281
|
+
// includes the files a build writes only for some themes.
|
|
4282
|
+
refuseUnreadable(cwd, [...files, ...builtFiles(cwd, slug, data, true)]);
|
|
4283
|
+
const missing = files.find(p => !fs.existsSync(p));
|
|
2495
4284
|
if (missing) return `${path.relative(f.dir, missing)} was missing`;
|
|
2496
4285
|
const ts = fs.readFileSync(f.settingsTs, 'utf8');
|
|
2497
4286
|
const stamp =
|
|
@@ -2510,6 +4299,16 @@ export function staleBuild(cwd, slug, data) {
|
|
|
2510
4299
|
if (built && now && built !== now)
|
|
2511
4300
|
return `@astryxdesign/theme-${data.like} is ${now} now, and the build was made on ${built}`;
|
|
2512
4301
|
}
|
|
4302
|
+
// The generated module seals its own text: a hand edit inside it (the
|
|
4303
|
+
// file says not to) is built over from the settings, and said. Checked
|
|
4304
|
+
// after the stamps, whose reasons say more.
|
|
4305
|
+
if (moduleUnedited(ts) === false)
|
|
4306
|
+
return `${slug}.settings.ts was edited by hand, though it is generated from ${slug}.settings.json and says not to edit it`;
|
|
4307
|
+
// An own change to <slug>.theme.ts since the build (the file says it
|
|
4308
|
+
// wins over the settings) is built in.
|
|
4309
|
+
const source = /\/\/ Built with \S+\.theme\.ts ([0-9a-f]{16})\./.exec(ts);
|
|
4310
|
+
if (source && fs.existsSync(f.ts) && source[1] !== themeSourceHash(cwd, slug))
|
|
4311
|
+
return `${slug}.theme.ts or a module it imports changed since the last build`;
|
|
2513
4312
|
// By content, never by file times (an editor's re-save or a checkout
|
|
2514
4313
|
// changes those): a generated module cut short, or built outputs whose
|
|
2515
4314
|
// header names another CLI than the settings were written by (a build
|
|
@@ -2526,6 +4325,17 @@ export function staleBuild(cwd, slug, data) {
|
|
|
2526
4325
|
})
|
|
2527
4326
|
)
|
|
2528
4327
|
return 'the last build did not finish (a command stopped while building)';
|
|
4328
|
+
// Outputs another theme's build wrote over (`compile other.theme.ts --out
|
|
4329
|
+
// themes/g/g.css`): their header names another source.
|
|
4330
|
+
const builtFrom = (/** @type {string} */ file) =>
|
|
4331
|
+
/^ \* Source: (.+)$/m
|
|
4332
|
+
.exec(fs.readFileSync(file, 'utf8').slice(0, 600))?.[1]
|
|
4333
|
+
?.trim() ?? null;
|
|
4334
|
+
for (const file of [f.css, f.js]) {
|
|
4335
|
+
const from = builtFrom(file);
|
|
4336
|
+
if (from != null && path.basename(from) !== path.basename(f.ts))
|
|
4337
|
+
return `${path.basename(file)} was built from ${from}, not ${slug}.theme.ts`;
|
|
4338
|
+
}
|
|
2529
4339
|
// A built stylesheet cut short (a crash, a bad merge) is built again, as
|
|
2530
4340
|
// an unreadable built module is.
|
|
2531
4341
|
try {
|
|
@@ -2536,6 +4346,79 @@ export function staleBuild(cwd, slug, data) {
|
|
|
2536
4346
|
return null;
|
|
2537
4347
|
}
|
|
2538
4348
|
|
|
4349
|
+
/**
|
|
4350
|
+
* Every file the editor's build of a theme writes, the one list staleness
|
|
4351
|
+
* reads: the generated settings module, the built stylesheet, module and
|
|
4352
|
+
* their types, and, with a color dial set, the generated color files.
|
|
4353
|
+
* @param {string} cwd
|
|
4354
|
+
* @param {string} slug
|
|
4355
|
+
* @param {ThemeData} data
|
|
4356
|
+
* @param {boolean} [some] the files a build writes only for some themes
|
|
4357
|
+
* (the variants types, for a theme with component variants) instead
|
|
4358
|
+
* @returns {string[]}
|
|
4359
|
+
*/
|
|
4360
|
+
export function builtFiles(cwd, slug, data, some = false) {
|
|
4361
|
+
const f = themeFiles(cwd, slug);
|
|
4362
|
+
if (some) return [path.join(f.dir, `${slug}.variants.d.ts`)];
|
|
4363
|
+
const pal = paletteFiles(slug);
|
|
4364
|
+
const colors = data.settings.some(
|
|
4365
|
+
s => s.source === 'dial' && PALETTE_DIALS.includes(s.thing),
|
|
4366
|
+
)
|
|
4367
|
+
? [pal.palette, pal.refs, pal.receipt].map(p => path.join(cwd, p))
|
|
4368
|
+
: [];
|
|
4369
|
+
return [
|
|
4370
|
+
f.settingsTs,
|
|
4371
|
+
// The theme's own source: the editor writes it once, and every build
|
|
4372
|
+
// reads it, so one deleted by hand is written again.
|
|
4373
|
+
f.ts,
|
|
4374
|
+
f.css,
|
|
4375
|
+
f.js,
|
|
4376
|
+
path.join(f.dir, `${slug}.d.ts`),
|
|
4377
|
+
path.join(f.dir, `${slug}.css.d.ts`),
|
|
4378
|
+
...colors,
|
|
4379
|
+
];
|
|
4380
|
+
}
|
|
4381
|
+
|
|
4382
|
+
/**
|
|
4383
|
+
* Refuse, by name, a built file that is there but cannot be read: its
|
|
4384
|
+
* permissions, or a folder where the file goes.
|
|
4385
|
+
* @param {string} cwd
|
|
4386
|
+
* @param {string[]} files
|
|
4387
|
+
*/
|
|
4388
|
+
function refuseUnreadable(cwd, files) {
|
|
4389
|
+
for (const file of files) {
|
|
4390
|
+
const st = fs.statSync(file, {throwIfNoEntry: false});
|
|
4391
|
+
if (!st) continue;
|
|
4392
|
+
const rel = shown(cwd, file);
|
|
4393
|
+
if (st.isDirectory())
|
|
4394
|
+
throw new AstryxError(
|
|
4395
|
+
`${rel} is a folder where the build writes a file, so the theme cannot be built. Move or rename the folder, then run the command again. Nothing was changed.`,
|
|
4396
|
+
[
|
|
4397
|
+
{
|
|
4398
|
+
name: `ls -la ${quoteCommandArgument(rel)}`,
|
|
4399
|
+
reason: 'see what is there',
|
|
4400
|
+
},
|
|
4401
|
+
],
|
|
4402
|
+
ERROR_CODES.ERR_FILE_EXISTS,
|
|
4403
|
+
);
|
|
4404
|
+
try {
|
|
4405
|
+
fs.accessSync(file, fs.constants.R_OK | fs.constants.W_OK);
|
|
4406
|
+
} catch (e) {
|
|
4407
|
+
const code = /** @type {any} */ (e)?.code ?? 'EACCES';
|
|
4408
|
+
throw new AstryxError(
|
|
4409
|
+
`${rel} cannot be read or replaced (${code}: its permissions), so the theme cannot be built. Give yourself read and write permission on it (chmod u+rw ${rel}), then run the command again. Nothing was changed.`,
|
|
4410
|
+
[
|
|
4411
|
+
{
|
|
4412
|
+
name: `ls -la ${quoteCommandArgument(rel)}`,
|
|
4413
|
+
reason: 'see its permissions',
|
|
4414
|
+
},
|
|
4415
|
+
],
|
|
4416
|
+
ERROR_CODES.ERR_WRITE_FAILED,
|
|
4417
|
+
);
|
|
4418
|
+
}
|
|
4419
|
+
}
|
|
4420
|
+
}
|
|
4421
|
+
|
|
2539
4422
|
/**
|
|
2540
4423
|
* The installed version of a base theme package, or null.
|
|
2541
4424
|
* @param {string} cwd
|
|
@@ -2572,6 +4455,19 @@ export const HISTORY_STEPS = 80;
|
|
|
2572
4455
|
*/
|
|
2573
4456
|
export function readHistory(file) {
|
|
2574
4457
|
if (!fs.existsSync(file)) return {steps: [], dropped: 0};
|
|
4458
|
+
// A history that cannot be read is not damaged: it is refused by name.
|
|
4459
|
+
const why = unreadableReason(file);
|
|
4460
|
+
if (why)
|
|
4461
|
+
throw new AstryxError(
|
|
4462
|
+
`${path.basename(file)} ${why}, so the undo history cannot be read. Give it back its place or its read permission; nothing was changed.`,
|
|
4463
|
+
[
|
|
4464
|
+
{
|
|
4465
|
+
name: `ls -la ${quoteCommandArgument(file)}`,
|
|
4466
|
+
reason: 'see what is there',
|
|
4467
|
+
},
|
|
4468
|
+
],
|
|
4469
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
4470
|
+
);
|
|
2575
4471
|
/** @type {unknown} */
|
|
2576
4472
|
let h;
|
|
2577
4473
|
try {
|
|
@@ -2646,7 +4542,14 @@ export async function commitTheme(
|
|
|
2646
4542
|
// A no-op change still mends a build that does not match the settings
|
|
2647
4543
|
// (an undo stopped part way): it is built again, with no new step.
|
|
2648
4544
|
const noop = history && same(parseSettings(snapBefore));
|
|
2649
|
-
|
|
4545
|
+
const stale =
|
|
4546
|
+
noop && fs.existsSync(f.js) ? staleBuild(cwd, slug, data) : null;
|
|
4547
|
+
// The no-op that mends one says what it built again.
|
|
4548
|
+
if (noop && stale)
|
|
4549
|
+
notes.push(
|
|
4550
|
+
`the build was not current (${stale}), so it was built again from the settings`,
|
|
4551
|
+
);
|
|
4552
|
+
if (noop && fs.existsSync(f.js) && !stale)
|
|
2650
4553
|
return {
|
|
2651
4554
|
changed: [],
|
|
2652
4555
|
res: {light: {}, dark: {}},
|
|
@@ -2656,6 +4559,11 @@ export async function commitTheme(
|
|
|
2656
4559
|
notes,
|
|
2657
4560
|
unchanged: true,
|
|
2658
4561
|
};
|
|
4562
|
+
// With no last build (deleted, never committed), nothing is compared.
|
|
4563
|
+
if (snapBefore && !fs.existsSync(f.js))
|
|
4564
|
+
notes.push(
|
|
4565
|
+
`there was no last build (${path.basename(f.js)} is missing), so the token changes are not listed; the theme is built again`,
|
|
4566
|
+
);
|
|
2659
4567
|
const resBefore =
|
|
2660
4568
|
snapBefore && fs.existsSync(f.js)
|
|
2661
4569
|
? await resolvedTokens(ctx, slug).catch(() => {
|
|
@@ -2668,7 +4576,9 @@ export async function commitTheme(
|
|
|
2668
4576
|
})
|
|
2669
4577
|
: null;
|
|
2670
4578
|
const before = parseSettings(snapBefore);
|
|
2671
|
-
const liftedBefore = new Set(
|
|
4579
|
+
const liftedBefore = new Set(
|
|
4580
|
+
Array.isArray(before?.lifted) ? before.lifted : [],
|
|
4581
|
+
);
|
|
2672
4582
|
// A change that leaves every color setting as it was keeps the shades the
|
|
2673
4583
|
// theme already shows: a theme an earlier editor built keeps its colors
|
|
2674
4584
|
// until a color, the mode or the base changes, and undo brings them back.
|
|
@@ -2728,6 +4638,10 @@ export async function commitTheme(
|
|
|
2728
4638
|
fs.mkdirSync(f.dir, {recursive: true});
|
|
2729
4639
|
/** @type {{warnings: string[], lifted: string[], notes: string[]}} */
|
|
2730
4640
|
let built;
|
|
4641
|
+
// The token loops the last build had, so a change that adds one (through
|
|
4642
|
+
// tokens the own source sets in a way no read of the file follows) is
|
|
4643
|
+
// refused once the build shows it.
|
|
4644
|
+
const loopsBefore = new Set(builtLoops(cwd, slug).map(l => l.key));
|
|
2731
4645
|
try {
|
|
2732
4646
|
refuseIfLockLost();
|
|
2733
4647
|
try {
|
|
@@ -2738,6 +4652,18 @@ export async function commitTheme(
|
|
|
2738
4652
|
// The build writes its outputs itself: a lock lost meanwhile is seen
|
|
2739
4653
|
// before anything else is written.
|
|
2740
4654
|
refuseIfLockLost();
|
|
4655
|
+
const loop = builtLoops(cwd, slug).find(l => !loopsBefore.has(l.key));
|
|
4656
|
+
if (loop)
|
|
4657
|
+
throw new AstryxError(
|
|
4658
|
+
`${loop.path.join(' reads ')}${loop.part ? ` on ${loop.part}` : ''}: the change makes these tokens read each other (with what ${slug}.theme.ts or the base theme sets), so they would have no value at all. Nothing was changed.`,
|
|
4659
|
+
[
|
|
4660
|
+
{
|
|
4661
|
+
name: `${P} how ${loop.path[0].replace(/^--/, '')}`,
|
|
4662
|
+
reason: 'see what it takes',
|
|
4663
|
+
},
|
|
4664
|
+
],
|
|
4665
|
+
ERROR_CODES.ERR_THEME_BAD_VALUE,
|
|
4666
|
+
);
|
|
2741
4667
|
data.lifted = built.lifted;
|
|
2742
4668
|
// The undo step and the settings are part of the change: written whole,
|
|
2743
4669
|
// inside the rollback, the settings first. A command stopped between
|
|
@@ -2784,7 +4710,7 @@ export async function commitTheme(
|
|
|
2784
4710
|
const code = /** @type {any} */ (e)?.code;
|
|
2785
4711
|
if (['EACCES', 'EPERM', 'ENOSPC', 'EROFS', 'EIO', 'EISDIR'].includes(code))
|
|
2786
4712
|
throw new AstryxError(
|
|
2787
|
-
`Could not write the theme files (${code}), so the
|
|
4713
|
+
`Could not write the theme files (${code}), so the files were put back as they were. Free space or fix the permission, then run the command again. Nothing was changed.`,
|
|
2788
4714
|
[
|
|
2789
4715
|
{
|
|
2790
4716
|
name: `ls -ld ${quoteCommandArgument(themeFiles(ctx.project.cwd, slug).dir)}`,
|
|
@@ -2800,9 +4726,27 @@ export async function commitTheme(
|
|
|
2800
4726
|
e.code === ERROR_CODES.ERR_THEME_INVALID ||
|
|
2801
4727
|
e.code === ERROR_CODES.ERR_PALETTE_GENERATION ||
|
|
2802
4728
|
e.code === ERROR_CODES.ERR_WRITE_FAILED ||
|
|
2803
|
-
e.code === ERROR_CODES.ERR_THEME_BUSY
|
|
4729
|
+
e.code === ERROR_CODES.ERR_THEME_BUSY ||
|
|
4730
|
+
e.code === ERROR_CODES.ERR_THEME_BAD_VALUE)
|
|
2804
4731
|
)
|
|
2805
4732
|
throw e;
|
|
4733
|
+
// A folder where a built file goes: named for the editor, which has no
|
|
4734
|
+
// --out to pick.
|
|
4735
|
+
const folder =
|
|
4736
|
+
e instanceof AstryxError
|
|
4737
|
+
? /^The output (.+) is a folder\./.exec(e.message)?.[1]
|
|
4738
|
+
: undefined;
|
|
4739
|
+
if (folder)
|
|
4740
|
+
throw new AstryxError(
|
|
4741
|
+
`${folder} is a folder, not a file, so the theme cannot be built. Put the file back in its place; nothing was changed.`,
|
|
4742
|
+
[
|
|
4743
|
+
{
|
|
4744
|
+
name: `ls -la ${quoteCommandArgument(folder)}`,
|
|
4745
|
+
reason: 'see what is there',
|
|
4746
|
+
},
|
|
4747
|
+
],
|
|
4748
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
4749
|
+
);
|
|
2806
4750
|
const why = (e instanceof Error ? e.message : String(e))
|
|
2807
4751
|
.split(`${cwd}${path.sep}`)
|
|
2808
4752
|
.join('')
|
|
@@ -2812,12 +4756,17 @@ export async function commitTheme(
|
|
|
2812
4756
|
.join(' | ');
|
|
2813
4757
|
const own = why.includes(path.basename(f.ts));
|
|
2814
4758
|
throw new AstryxError(
|
|
2815
|
-
`The theme did not build, so the
|
|
4759
|
+
`The theme did not build, so the files were put back as they were: ${why}${own ? ` (${shown(cwd, f.ts)} is your own file: fix it, then run the command again)` : ''}. Nothing was changed.`,
|
|
2816
4760
|
[
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
4761
|
+
own
|
|
4762
|
+
? {
|
|
4763
|
+
name: `cat ${quoteCommandArgument(shown(cwd, f.ts))}`,
|
|
4764
|
+
reason: 'see your own file, then fix it',
|
|
4765
|
+
}
|
|
4766
|
+
: {
|
|
4767
|
+
name: `${P} show ${slug}`,
|
|
4768
|
+
reason: 'do this instead: see the settings as they are',
|
|
4769
|
+
},
|
|
2821
4770
|
],
|
|
2822
4771
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
2823
4772
|
);
|