@astryxdesign/cli 0.7.13 → 0.7.14-canary.108f354
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -18
- package/api/component/__fixtures__/resolutions/modules.txt +1 -1
- package/api/doctor/doctor.mjs +3 -7
- package/api/experimental.d.mts +2 -0
- package/api/experimental.mjs +12 -0
- package/api/icon/_adapter.d.mts +10 -0
- package/api/icon/_adapter.mjs +31 -2
- package/api/icon/_authoring.d.mts +2 -1
- package/api/icon/_authoring.mjs +91 -6
- package/api/icon/_options.mjs +1 -0
- package/api/icon/_records.d.mts +18 -0
- package/api/icon/_records.mjs +26 -0
- package/api/icon/add/add.mjs +7 -1
- package/api/icon/definitions/definitions.d.mts +12 -0
- package/api/icon/definitions/definitions.mjs +83 -0
- package/api/icon/icon.doc.mjs +5 -4
- package/api/icon/icon.mjs +5 -16
- package/api/icon/icon.type.d.mts +29 -2
- package/api/icon/icon.type.mjs +17 -3
- package/api/icon/iconAdd.doc.mjs +1 -1
- package/api/icon/iconMap.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +8 -2
- package/api/icon/map/map.mjs +21 -2
- package/api/icon/new-set/new-set.mjs +14 -2
- package/api/icon/search/search.mjs +2 -15
- package/api/icon/use/use.mjs +4 -3
- package/api/integration/validate-integration.mjs +6 -1
- package/api/integration/validate-integration.type.d.mts +12 -0
- package/api/integration/validate-integration.type.mjs +5 -0
- package/api/theme-edit/_adapter.d.mts +116 -0
- package/api/theme-edit/_adapter.mjs +1683 -0
- package/api/theme-edit/_catalog.d.mts +59 -0
- package/api/theme-edit/_catalog.mjs +564 -0
- package/api/theme-edit/_check.d.mts +49 -0
- package/api/theme-edit/_check.mjs +845 -0
- package/api/theme-edit/_color.d.mts +29 -0
- package/api/theme-edit/_color.mjs +300 -0
- package/api/theme-edit/_compiler.d.mts +61 -0
- package/api/theme-edit/_compiler.mjs +522 -0
- package/api/theme-edit/_css-properties.d.mts +10 -0
- package/api/theme-edit/_css-properties.mjs +556 -0
- package/api/theme-edit/_parts.d.mts +111 -0
- package/api/theme-edit/_parts.mjs +409 -0
- package/api/theme-edit/_png.d.mts +40 -0
- package/api/theme-edit/_png.mjs +187 -0
- package/api/theme-edit/_preview.d.mts +121 -0
- package/api/theme-edit/_preview.mjs +720 -0
- package/api/theme-edit/_project.d.mts +83 -0
- package/api/theme-edit/_project.mjs +289 -0
- package/api/theme-edit/_settings.d.mts +222 -0
- package/api/theme-edit/_settings.mjs +861 -0
- package/api/theme-edit/_tables.d.mts +132 -0
- package/api/theme-edit/_tables.mjs +700 -0
- package/api/theme-edit/theme-edit.d.mts +93 -0
- package/api/theme-edit/theme-edit.mjs +145 -0
- package/api/theme-edit/theme-edit.type.d.mts +413 -0
- package/api/theme-edit/theme-edit.type.mjs +233 -0
- package/api/theme-edit/themeEditHow.doc.d.mts +6 -0
- package/api/theme-edit/themeEditHow.doc.mjs +88 -0
- package/api/theme-edit/themeEditNew.doc.d.mts +6 -0
- package/api/theme-edit/themeEditNew.doc.mjs +89 -0
- package/api/theme-edit/themeEditPreviewAdd.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +100 -0
- package/api/theme-edit/themeEditPreviewApp.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +84 -0
- package/api/theme-edit/themeEditPreviewList.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewList.doc.mjs +53 -0
- package/api/theme-edit/themeEditPreviewRemove.doc.d.mts +6 -0
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +66 -0
- package/api/theme-edit/themeEditSet.doc.d.mts +6 -0
- package/api/theme-edit/themeEditSet.doc.mjs +165 -0
- package/api/theme-edit/themeEditShow.doc.d.mts +6 -0
- package/api/theme-edit/themeEditShow.doc.mjs +86 -0
- package/api/theme-edit/themeEditUndo.doc.d.mts +6 -0
- package/api/theme-edit/themeEditUndo.doc.mjs +124 -0
- package/assets/docs/color.doc.mjs +5 -5
- package/assets/docs/tree/icon-sets.doc.mjs +6 -2
- package/assets/docs/tree/tokens-border.doc.mjs +1 -1
- package/assets/docs/tree/tokens-color.doc.mjs +2 -2
- package/assets/docs/tree/tokens-data.doc.mjs +2 -2
- package/assets/docs/tree/tokens-duration.doc.mjs +1 -1
- package/assets/docs/tree/tokens-ease.doc.mjs +1 -1
- package/assets/docs/tree/tokens-focus.doc.mjs +1 -1
- package/assets/docs/tree/tokens-font-weight.doc.mjs +1 -1
- package/assets/docs/tree/tokens-radius.doc.mjs +1 -1
- package/assets/docs/tree/tokens-shadow.doc.mjs +1 -1
- package/assets/docs/tree/tokens-size.doc.mjs +1 -1
- package/assets/docs/tree/tokens-spacing.doc.mjs +1 -1
- package/assets/docs/tree/tokens-syntax.doc.mjs +1 -1
- package/assets/docs/tree/tokens-text-size.doc.mjs +1 -1
- package/assets/docs/tree/tokens-type-scale.doc.mjs +1 -1
- package/assets/docs/tree/tokens-typography.doc.mjs +1 -1
- package/assets/icons/definitions/bank.doc.mjs +1 -1
- package/assets/icons/definitions/save.doc.mjs +1 -1
- package/assets/preview-app/README.md +6 -0
- package/assets/preview-app/css.d.ts +5 -0
- package/assets/preview-app/gitignore +1 -0
- package/assets/preview-app/next.config.mjs +6 -0
- package/assets/preview-app/package.json +23 -0
- package/assets/preview-app/src/app/globals.css +5 -0
- package/assets/preview-app/src/app/layout.tsx +21 -0
- package/assets/preview-app/src/app/page.tsx +94 -0
- package/assets/preview-app/src/app/providers.tsx +16 -0
- package/assets/preview-app/tsconfig.json +27 -0
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider01ShippedLocale.doc.mjs +2 -1
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider02Overrides.doc.mjs +2 -1
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider03RtlDirection.doc.mjs +2 -1
- package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider04Calendar.doc.mjs +2 -1
- package/assets/templates/blocks/components/Text/useTruncationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Text/useTruncationHookUsage.tsx +112 -0
- package/authoring/doctypes/_schema.d.mts +1 -0
- package/authoring/doctypes/_schema.mjs +1 -0
- package/authoring/doctypes/command/command.doc.mjs +7 -0
- package/authoring/doctypes/command/type.ts +3 -0
- package/authoring/doctypes/icon-set/parse.d.mts +7 -0
- package/authoring/doctypes/icon-set/parse.mjs +18 -8
- package/clients/cli/commands/doctor.mjs +33 -3
- package/clients/cli/commands/experimental/icon.mjs +67 -14
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/theme.mjs +684 -0
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +7 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +3 -2
- package/clients/cli/commands/experimental-theme-add.doc.mjs +78 -0
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +97 -0
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +84 -0
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +66 -0
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +121 -0
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +66 -0
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +104 -0
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +49 -0
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +71 -0
- package/clients/cli/commands/experimental-theme-list.doc.mjs +49 -0
- package/clients/cli/commands/experimental-theme-palette-generate.doc.mjs +73 -0
- package/clients/cli/commands/experimental-theme-palette.doc.mjs +37 -0
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +86 -0
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +66 -0
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +48 -0
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +59 -0
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +40 -0
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +61 -0
- package/clients/cli/commands/experimental-theme-template.doc.mjs +61 -0
- package/clients/cli/commands/experimental-theme-use.doc.mjs +55 -0
- package/clients/cli/commands/experimental-theme.doc.mjs +57 -0
- package/clients/cli/commands/experimental.doc.mjs +10 -3
- package/clients/cli/commands/theme.mjs +101 -34
- package/clients/cli/index.mjs +18 -0
- package/clients/cli/lib/define-command.mjs +3 -0
- package/foundation/discovery/icon-set-checks.d.mts +16 -1
- package/foundation/discovery/icon-set-checks.mjs +68 -7
- package/foundation/discovery/icon-set-discovery.d.mts +21 -6
- package/foundation/discovery/icon-set-discovery.mjs +17 -8
- package/foundation/discovery/template-icon-meanings.d.mts +39 -0
- package/foundation/discovery/template-icon-meanings.mjs +95 -0
- package/foundation/discovery/theming-targets.d.mts +6 -0
- package/foundation/discovery/theming-targets.mjs +33 -0
- package/foundation/response/error-codes.d.mts +12 -1
- package/foundation/response/error-codes.doc.mjs +66 -0
- package/foundation/response/error-codes.mjs +55 -0
- package/foundation/response/response-types.doc.mjs +55 -0
- package/foundation/text/template-meanings-line.d.mts +16 -0
- package/foundation/text/template-meanings-line.mjs +37 -0
- package/foundation/xle/browser.d.mts +2 -2
- package/package.json +6 -6
|
@@ -0,0 +1,1683 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The theme-edit subject's adapter: the work behind `astryx experimental
|
|
5
|
+
* theme editor`, and its only access to the environment (files, the installed Core,
|
|
6
|
+
* the renderer). theme-edit.mjs is the public API over it.
|
|
7
|
+
*
|
|
8
|
+
* themeEditNew('brand', {like: 'neutral'}) themeEdit.new
|
|
9
|
+
* themeEditSet('brand', 'corners', 'pill') themeEdit.set
|
|
10
|
+
* themeEditUndo('brand') themeEdit.undo (one step)
|
|
11
|
+
* themeEditUndo('brand', {thing: 'corners'}) themeEdit.undo (one setting)
|
|
12
|
+
* themeEditShow('brand') themeEdit.show
|
|
13
|
+
* themeEditPreviewAdd('a sales dashboard') themeEdit.previewAdd
|
|
14
|
+
* themeEditPreviewList() themeEdit.previewList
|
|
15
|
+
* themeEditPreviewRemove('components') themeEdit.previewRemove
|
|
16
|
+
* themeEditHow('banner') themeEdit.how
|
|
17
|
+
* themeEditHow('white text in the banner') themeEdit.how (a question)
|
|
18
|
+
* themeEditHow('target.png', {at: ['4,8']}) themeEdit.how (a picture)
|
|
19
|
+
* themeEditHow('accent', {list: 'tokens'}) themeEdit.how (a list)
|
|
20
|
+
*
|
|
21
|
+
* Every change is checked first against what the installed Core's parts
|
|
22
|
+
* really read; a change that cannot take effect is refused and nothing is
|
|
23
|
+
* written. An accepted change rebuilds the theme with the theme build and
|
|
24
|
+
* writes a preview.
|
|
25
|
+
*
|
|
26
|
+
* @input a theme name and a change, a question, or a picture
|
|
27
|
+
* @output themeEdit.* envelopes
|
|
28
|
+
* @position api/theme-edit — adapter for api/theme-edit/theme-edit.mjs
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import * as fs from 'node:fs';
|
|
32
|
+
import * as path from 'node:path';
|
|
33
|
+
import {fileURLToPath} from 'node:url';
|
|
34
|
+
import {AstryxError} from '../error.mjs';
|
|
35
|
+
import {cliVersion} from '../../foundation/release/run-context.mjs';
|
|
36
|
+
import {isOutsideProject} from '../../foundation/env/project-scope.mjs';
|
|
37
|
+
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
38
|
+
import {
|
|
39
|
+
assertWithin,
|
|
40
|
+
PathSafetyError,
|
|
41
|
+
} from '../../foundation/fs/path-safety.mjs';
|
|
42
|
+
import {CSS_PROPERTIES} from './_css-properties.mjs';
|
|
43
|
+
import {
|
|
44
|
+
AUTO_SPREAD,
|
|
45
|
+
CLOSEST_OPEN_FONT,
|
|
46
|
+
COMMON_PROPERTIES,
|
|
47
|
+
DIALS,
|
|
48
|
+
FAMILIES,
|
|
49
|
+
GROUPS,
|
|
50
|
+
KEY_TOKENS,
|
|
51
|
+
LIKE,
|
|
52
|
+
OPEN_FONTS,
|
|
53
|
+
HOW,
|
|
54
|
+
P,
|
|
55
|
+
SYSTEM_FONTS,
|
|
56
|
+
familyOf,
|
|
57
|
+
} from './_tables.mjs';
|
|
58
|
+
import {accentSet} from './_color.mjs';
|
|
59
|
+
import {colorsAt, decodePng, mainColors} from './_png.mjs';
|
|
60
|
+
import {makeCompiler} from './_compiler.mjs';
|
|
61
|
+
import {openProject, installLine} from './_project.mjs';
|
|
62
|
+
import {partData} from './_parts.mjs';
|
|
63
|
+
import {
|
|
64
|
+
commitTheme,
|
|
65
|
+
compileTheme,
|
|
66
|
+
contrastProblems,
|
|
67
|
+
describe,
|
|
68
|
+
fontOf,
|
|
69
|
+
fontPackages,
|
|
70
|
+
keyOf,
|
|
71
|
+
loadTheme,
|
|
72
|
+
modesOf,
|
|
73
|
+
nearest,
|
|
74
|
+
partsOf,
|
|
75
|
+
refuse,
|
|
76
|
+
resolvedTokens,
|
|
77
|
+
shown,
|
|
78
|
+
slugOk,
|
|
79
|
+
themeFiles,
|
|
80
|
+
buildTheme,
|
|
81
|
+
} from './_settings.mjs';
|
|
82
|
+
import {ASPECTS, answerHow, checkSetting, wholeThemeTokens} from './_check.mjs';
|
|
83
|
+
import {MODAL} from './_compiler.mjs';
|
|
84
|
+
import {
|
|
85
|
+
SHOWCASE_GROUPS,
|
|
86
|
+
componentsPageSource,
|
|
87
|
+
overviewPageSource,
|
|
88
|
+
previewRouteSource,
|
|
89
|
+
renderPreview,
|
|
90
|
+
renderSheet,
|
|
91
|
+
screenshot,
|
|
92
|
+
themeImports,
|
|
93
|
+
} from './_preview.mjs';
|
|
94
|
+
import {build} from '../build/build.mjs';
|
|
95
|
+
import {template} from '../template/template.mjs';
|
|
96
|
+
import {analyzeTemplateNeeds} from '../../foundation/discovery/template-needs.mjs';
|
|
97
|
+
import {createFileProtectionResolver} from '../../foundation/fs/file-protection.mjs';
|
|
98
|
+
import {baseResolved} from './_project.mjs';
|
|
99
|
+
|
|
100
|
+
const E = ERROR_CODES;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @typedef {import('./theme-edit.type.mjs').ThemeEditChangeOptions} ChangeOptions
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Refuse outside a project: a theme is written into an app, and every check
|
|
108
|
+
* reads the app's own Core.
|
|
109
|
+
* @param {string} cwd
|
|
110
|
+
*/
|
|
111
|
+
function requireProject(cwd) {
|
|
112
|
+
if (!isOutsideProject(cwd)) return;
|
|
113
|
+
throw new AstryxError(
|
|
114
|
+
'No package.json found. astryx experimental theme works inside an app project, which it writes the theme into and whose installed Core it checks every change against. Nothing was written.',
|
|
115
|
+
[],
|
|
116
|
+
E.ERR_PROJECT_REQUIRED,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** @param {string} cwd */
|
|
121
|
+
async function context(cwd) {
|
|
122
|
+
requireProject(cwd);
|
|
123
|
+
const project = await openProject(cwd);
|
|
124
|
+
const parts = await partData(project);
|
|
125
|
+
return {project, C: makeCompiler(project, parts), parts};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Pictures of HTML files, on request.
|
|
130
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
131
|
+
* @param {Array<{html: string, png: string}>} shots
|
|
132
|
+
* @param {Array<'light' | 'dark'>} modes
|
|
133
|
+
* @returns {Promise<{files: string[], problem: string | null}>}
|
|
134
|
+
*/
|
|
135
|
+
async function pictures(project, shots, modes) {
|
|
136
|
+
/** @type {string[]} */
|
|
137
|
+
const files = [];
|
|
138
|
+
for (const {html, png} of shots) {
|
|
139
|
+
for (const m of modes) {
|
|
140
|
+
const out =
|
|
141
|
+
modes.length > 1 && m === 'dark'
|
|
142
|
+
? png.replace(/\.png$/, '-dark.png')
|
|
143
|
+
: png;
|
|
144
|
+
const r = await screenshot(project, html, out, m);
|
|
145
|
+
if (!r.ok) return {files, problem: r.reason};
|
|
146
|
+
files.push(r.file);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return {files, problem: null};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Font notes for the settings: the import line for an open font, the
|
|
154
|
+
* install line when its package is missing, and a warning for a family no
|
|
155
|
+
* package loads, naming the closest open font.
|
|
156
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
157
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
158
|
+
* @returns {string[]}
|
|
159
|
+
*/
|
|
160
|
+
function fontNotes(ctx, data) {
|
|
161
|
+
/** @type {string[]} */
|
|
162
|
+
const notes = [];
|
|
163
|
+
for (const {pkg, installed} of fontPackages(ctx, data)) {
|
|
164
|
+
notes.push(
|
|
165
|
+
installed
|
|
166
|
+
? `the app loads this font with one import: import '${pkg}';`
|
|
167
|
+
: `${pkg} is not installed, so previews fall back to the next font: ${installLine(ctx.project.cwd, [pkg])}; then the app imports it once: import '${pkg}';`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
for (const s of data.settings) {
|
|
171
|
+
if (
|
|
172
|
+
s.thing !== 'font' ||
|
|
173
|
+
SYSTEM_FONTS.includes(s.value) ||
|
|
174
|
+
OPEN_FONTS[s.value] ||
|
|
175
|
+
fontOf(s.value).pkg ||
|
|
176
|
+
s.value === 'rounded' ||
|
|
177
|
+
s.value === 'comic'
|
|
178
|
+
)
|
|
179
|
+
continue;
|
|
180
|
+
const family = fontOf(s.value).family.replace(/["']/g, '').toLowerCase();
|
|
181
|
+
const near = CLOSEST_OPEN_FONT[family];
|
|
182
|
+
notes.push(
|
|
183
|
+
`"${fontOf(s.value).family.replace(/["']/g, '')}" is not a system font and no package loads it: the app must load it, or it falls back.${near ? ` The closest open font is ${near}: ${P} set ${data.name} font ${near}${s.on ? ` --on ${s.on}` : ''}` : ''}`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return notes;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The accent note: accent text in each mode, made readable in the direction
|
|
191
|
+
* that mode needs, while buttons keep the color exactly.
|
|
192
|
+
* @param {string} value
|
|
193
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
194
|
+
* @param {'light' | 'dark' | undefined} only
|
|
195
|
+
*/
|
|
196
|
+
function accentNote(value, data, only) {
|
|
197
|
+
const a = accentSet(value);
|
|
198
|
+
const ta = /** @type {string[]} */ (
|
|
199
|
+
[].concat(/** @type {any} */ (a['--color-text-accent']))
|
|
200
|
+
);
|
|
201
|
+
if (ta.length < 2) return null;
|
|
202
|
+
const modes = only ? [only] : modesOf(data);
|
|
203
|
+
/** @type {string[]} */
|
|
204
|
+
const parts = [];
|
|
205
|
+
if (modes.includes('light') && ta[0].toLowerCase() !== value.toLowerCase())
|
|
206
|
+
parts.push(
|
|
207
|
+
`in light, accent-colored text is ${ta[0]} (darker, so it reads at 4.5:1 on light surfaces)`,
|
|
208
|
+
);
|
|
209
|
+
if (modes.includes('dark') && ta[1].toLowerCase() !== value.toLowerCase())
|
|
210
|
+
parts.push(
|
|
211
|
+
`in dark, accent-colored text and icons are ${ta[1]} (lighter, so they read at 4.5:1 on dark surfaces)`,
|
|
212
|
+
);
|
|
213
|
+
return parts.length
|
|
214
|
+
? `${parts.join('; ')}; buttons and fills keep ${value} exactly`
|
|
215
|
+
: null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* What a change did to part styles (component settings), one line each.
|
|
220
|
+
* @param {any} before
|
|
221
|
+
* @param {any} after
|
|
222
|
+
* @returns {string[]}
|
|
223
|
+
*/
|
|
224
|
+
function styledChanges(before, after) {
|
|
225
|
+
/** @type {string[]} */
|
|
226
|
+
const out = [];
|
|
227
|
+
const a = before?.components ?? {};
|
|
228
|
+
const b = after?.components ?? {};
|
|
229
|
+
for (const target of new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
230
|
+
for (const key of new Set([
|
|
231
|
+
...Object.keys(a[target] ?? {}),
|
|
232
|
+
...Object.keys(b[target] ?? {}),
|
|
233
|
+
])) {
|
|
234
|
+
const x = a[target]?.[key] ?? {};
|
|
235
|
+
const y = b[target]?.[key] ?? {};
|
|
236
|
+
for (const prop of new Set([...Object.keys(x), ...Object.keys(y)])) {
|
|
237
|
+
if (JSON.stringify(x[prop]) === JSON.stringify(y[prop])) continue;
|
|
238
|
+
const name = prop.startsWith('--')
|
|
239
|
+
? prop
|
|
240
|
+
: prop.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
|
|
241
|
+
out.push(
|
|
242
|
+
`${target}${key === 'base' ? '' : ` ${key}`}: ${name} ${y[prop] === undefined ? '(removed)' : typeof y[prop] === 'string' ? y[prop] : JSON.stringify(y[prop])}`,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const ta = JSON.stringify(before?.typography ?? {});
|
|
248
|
+
const tb = JSON.stringify(after?.typography ?? {});
|
|
249
|
+
if (ta !== tb) {
|
|
250
|
+
for (const role of ['body', 'heading', 'code']) {
|
|
251
|
+
const x = after?.typography?.[role];
|
|
252
|
+
if (
|
|
253
|
+
JSON.stringify(before?.typography?.[role] ?? null) !==
|
|
254
|
+
JSON.stringify(x ?? null)
|
|
255
|
+
)
|
|
256
|
+
out.push(
|
|
257
|
+
`typography ${role}: ${
|
|
258
|
+
x
|
|
259
|
+
? Object.entries(x)
|
|
260
|
+
.map(([k, v]) => `${k} ${v}`)
|
|
261
|
+
.join(', ')
|
|
262
|
+
: '(removed)'
|
|
263
|
+
}`,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
if (
|
|
267
|
+
JSON.stringify(before?.typography?.scale ?? null) !==
|
|
268
|
+
JSON.stringify(after?.typography?.scale ?? null)
|
|
269
|
+
)
|
|
270
|
+
out.push(
|
|
271
|
+
`typography scale: base ${after?.typography?.scale?.base ?? 'default'}`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Previews after a change: the sample page always, pictures on request.
|
|
279
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
280
|
+
* @param {string} slug
|
|
281
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
282
|
+
* @param {boolean} png
|
|
283
|
+
*/
|
|
284
|
+
async function previewAfter(ctx, slug, data, png) {
|
|
285
|
+
const preview = await renderPreview(ctx, slug, data);
|
|
286
|
+
const f = themeFiles(ctx.project.cwd, slug);
|
|
287
|
+
const shots = png
|
|
288
|
+
? await pictures(
|
|
289
|
+
ctx.project,
|
|
290
|
+
[{html: f.preview, png: f.previewPng}],
|
|
291
|
+
modesOf(data),
|
|
292
|
+
)
|
|
293
|
+
: {files: [], problem: null};
|
|
294
|
+
return {preview, pictures: shots.files, pictureProblem: shots.problem};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Start a theme in themes/<name>/, optionally on a built-in theme.
|
|
299
|
+
* @experimental
|
|
300
|
+
* @param {string} name
|
|
301
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditNewOptions} [options]
|
|
302
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditNewResponse>}
|
|
303
|
+
*/
|
|
304
|
+
export async function themeEditNew(name, options = {}) {
|
|
305
|
+
const {cwd = process.cwd(), like, png = false} = options;
|
|
306
|
+
requireProject(cwd);
|
|
307
|
+
if (!slugOk(name))
|
|
308
|
+
refuse(
|
|
309
|
+
E.ERR_INVALID_ARGUMENT,
|
|
310
|
+
`"${name ?? ''}" is not a theme name (lowercase letters, digits, dashes, starting with a letter).`,
|
|
311
|
+
{run: [`${P} new my-theme`]},
|
|
312
|
+
);
|
|
313
|
+
const f = themeFiles(cwd, name);
|
|
314
|
+
if (fs.existsSync(f.settings))
|
|
315
|
+
refuse(E.ERR_FILE_EXISTS, `Theme "${name}" already exists.`, {
|
|
316
|
+
run: [`${P} show ${name}`, `${P} set ${name} base <theme>`],
|
|
317
|
+
});
|
|
318
|
+
if (fs.existsSync(f.dir) && fs.readdirSync(f.dir).length)
|
|
319
|
+
refuse(
|
|
320
|
+
E.ERR_FILE_EXISTS,
|
|
321
|
+
`themes/${name}/ already holds files this command did not make; choose another name.`,
|
|
322
|
+
{run: [`${P} new ${name}-2`]},
|
|
323
|
+
);
|
|
324
|
+
if (like && !LIKE.includes(like))
|
|
325
|
+
refuse(
|
|
326
|
+
E.ERR_THEME_BAD_VALUE,
|
|
327
|
+
`--like takes ${LIKE.join(', ')}, not "${like}".`,
|
|
328
|
+
{didYouMean: nearest(like, LIKE)},
|
|
329
|
+
);
|
|
330
|
+
const ctx = await context(cwd);
|
|
331
|
+
/** @type {import('./_settings.mjs').ThemeData} */
|
|
332
|
+
const data = {name, ...(like ? {like} : {}), mode: 'both', settings: []};
|
|
333
|
+
const r = await commitTheme(ctx, name, data, {history: false});
|
|
334
|
+
const p = await previewAfter(ctx, name, data, png);
|
|
335
|
+
return {
|
|
336
|
+
type: 'themeEdit.new',
|
|
337
|
+
data: {
|
|
338
|
+
theme: name,
|
|
339
|
+
like: like ?? null,
|
|
340
|
+
files: {
|
|
341
|
+
settings: shown(cwd, f.settings),
|
|
342
|
+
source: shown(cwd, f.ts),
|
|
343
|
+
css: shown(cwd, f.css),
|
|
344
|
+
module: shown(cwd, f.js),
|
|
345
|
+
},
|
|
346
|
+
problems: r.problems,
|
|
347
|
+
...p,
|
|
348
|
+
},
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Change one thing in a theme: checked, built, previewed.
|
|
354
|
+
* @experimental
|
|
355
|
+
* @param {string} theme
|
|
356
|
+
* @param {string} thing a dial, a token, or a CSS property
|
|
357
|
+
* @param {string} value
|
|
358
|
+
* @param {ChangeOptions} [options]
|
|
359
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditChangeResponse>}
|
|
360
|
+
*/
|
|
361
|
+
export async function themeEditSet(theme, thing, value, options = {}) {
|
|
362
|
+
return change('set', theme, thing, value, options);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @param {'set' | 'undo'} sub
|
|
367
|
+
* @param {string} slug
|
|
368
|
+
* @param {string} thing
|
|
369
|
+
* @param {string | undefined} value
|
|
370
|
+
* @param {ChangeOptions} options
|
|
371
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditChangeResponse>}
|
|
372
|
+
*/
|
|
373
|
+
async function change(sub, slug, thing, value, options) {
|
|
374
|
+
const {cwd = process.cwd(), png = false} = options;
|
|
375
|
+
requireProject(cwd);
|
|
376
|
+
const data = loadTheme(cwd, slug);
|
|
377
|
+
const ctx = await context(cwd);
|
|
378
|
+
const flags = {
|
|
379
|
+
on: options.on,
|
|
380
|
+
when: options.when,
|
|
381
|
+
state: options.state,
|
|
382
|
+
mode: options.mode,
|
|
383
|
+
only: options.only,
|
|
384
|
+
};
|
|
385
|
+
const s = checkSetting(ctx, slug, data, thing, value, flags, sub === 'undo');
|
|
386
|
+
const base = await baseResolved(ctx.project, data.like);
|
|
387
|
+
const compiledBefore = compileTheme(ctx, data, base);
|
|
388
|
+
if (sub === 'set' && (s.thing === 'base' || s.thing === 'mode')) {
|
|
389
|
+
const before = s.thing === 'base' ? (data.like ?? 'none') : data.mode;
|
|
390
|
+
if (s.thing === 'base') {
|
|
391
|
+
if (s.value === 'none') delete data.like;
|
|
392
|
+
else data.like = s.value;
|
|
393
|
+
} else
|
|
394
|
+
data.mode = /** @type {'both' | 'light-only' | 'dark-only'} */ (s.value);
|
|
395
|
+
const r = await commitTheme(ctx, slug, data, {history: true});
|
|
396
|
+
const p = await previewAfter(ctx, slug, data, png);
|
|
397
|
+
return {
|
|
398
|
+
type: 'themeEdit.set',
|
|
399
|
+
data: {
|
|
400
|
+
theme: slug,
|
|
401
|
+
action: 'set',
|
|
402
|
+
setting: s,
|
|
403
|
+
replaced: null,
|
|
404
|
+
was: before,
|
|
405
|
+
parts: [],
|
|
406
|
+
family: null,
|
|
407
|
+
changedTokens: r.changed,
|
|
408
|
+
styled: [],
|
|
409
|
+
kept: [],
|
|
410
|
+
lifted: r.lifted,
|
|
411
|
+
notes: [`your ${data.settings.length} settings are kept`],
|
|
412
|
+
problems: r.problems,
|
|
413
|
+
warnings: r.warnings,
|
|
414
|
+
...p,
|
|
415
|
+
},
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
const k = keyOf(s);
|
|
419
|
+
const prevIdx = data.settings.findIndex(x => keyOf(x) === k);
|
|
420
|
+
const prev = prevIdx >= 0 ? data.settings[prevIdx] : null;
|
|
421
|
+
if (sub === 'undo') {
|
|
422
|
+
if (!prev) {
|
|
423
|
+
const near = data.settings.filter(x => x.thing === s.thing).map(describe);
|
|
424
|
+
refuse(
|
|
425
|
+
E.ERR_THEME_NOT_SET,
|
|
426
|
+
`The theme does not set ${describe({...s, value: '?'}).replace(' = ?', '')}.`,
|
|
427
|
+
{
|
|
428
|
+
also: near.length
|
|
429
|
+
? [`settings for ${s.thing}: ${near.join('; ')}`]
|
|
430
|
+
: [],
|
|
431
|
+
run: [`${P} show ${slug}`],
|
|
432
|
+
},
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
data.settings.splice(prevIdx, 1);
|
|
436
|
+
} else if (prev) data.settings[prevIdx] = s;
|
|
437
|
+
else data.settings.push(s);
|
|
438
|
+
const r = await commitTheme(ctx, slug, data, {history: true});
|
|
439
|
+
const styled = styledChanges(compiledBefore, compileTheme(ctx, data, base));
|
|
440
|
+
const parts = partsOf(ctx, s);
|
|
441
|
+
const kept =
|
|
442
|
+
sub === 'set' && s.source === 'dial' && !s.on
|
|
443
|
+
? data.settings.filter(x => x.thing === s.thing && x.on).map(describe)
|
|
444
|
+
: [];
|
|
445
|
+
/** @type {string[]} */
|
|
446
|
+
const notes = [];
|
|
447
|
+
const fam = s.on
|
|
448
|
+
? FAMILIES[s.on]
|
|
449
|
+
? s.on
|
|
450
|
+
: parts.length > 1
|
|
451
|
+
? familyOf(s.on)
|
|
452
|
+
: null
|
|
453
|
+
: null;
|
|
454
|
+
if (s.note) notes.push(s.note);
|
|
455
|
+
if (s.thing === 'accent' && sub === 'set' && !s.on) {
|
|
456
|
+
const n = accentNote(s.value, data, s.mode);
|
|
457
|
+
if (n) notes.push(n);
|
|
458
|
+
}
|
|
459
|
+
if (s.thing === 'font' && sub === 'set')
|
|
460
|
+
notes.push(...fontNotes(ctx, {...data, settings: [s]}));
|
|
461
|
+
const p = await previewAfter(ctx, slug, data, png);
|
|
462
|
+
return {
|
|
463
|
+
type: sub === 'set' ? 'themeEdit.set' : 'themeEdit.undo',
|
|
464
|
+
data: {
|
|
465
|
+
theme: slug,
|
|
466
|
+
action: sub,
|
|
467
|
+
setting:
|
|
468
|
+
sub === 'set'
|
|
469
|
+
? s
|
|
470
|
+
: /** @type {import('./_settings.mjs').Setting} */ (prev),
|
|
471
|
+
replaced: sub === 'set' ? prev : null,
|
|
472
|
+
was: null,
|
|
473
|
+
parts,
|
|
474
|
+
family: fam
|
|
475
|
+
? {
|
|
476
|
+
name: fam,
|
|
477
|
+
members: parts,
|
|
478
|
+
onlyHint: !FAMILIES[s.on ?? ''] && AUTO_SPREAD.includes(fam),
|
|
479
|
+
}
|
|
480
|
+
: null,
|
|
481
|
+
changedTokens: r.changed,
|
|
482
|
+
styled,
|
|
483
|
+
kept,
|
|
484
|
+
lifted: r.lifted,
|
|
485
|
+
notes,
|
|
486
|
+
problems: r.problems,
|
|
487
|
+
warnings: r.warnings,
|
|
488
|
+
...p,
|
|
489
|
+
},
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Every setting of a theme, key resolved values, problems it caused, the
|
|
495
|
+
* sample page and the theme-building page with every component.
|
|
496
|
+
* @experimental
|
|
497
|
+
* @param {string} theme
|
|
498
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditShowOptions} [options]
|
|
499
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditShowResponse>}
|
|
500
|
+
*/
|
|
501
|
+
export async function themeEditShow(theme, options = {}) {
|
|
502
|
+
const {cwd = process.cwd(), png = false, group} = options;
|
|
503
|
+
requireProject(cwd);
|
|
504
|
+
checkGroup(group);
|
|
505
|
+
const data = loadTheme(cwd, theme);
|
|
506
|
+
const ctx = await context(cwd);
|
|
507
|
+
const f = themeFiles(cwd, theme);
|
|
508
|
+
if (!fs.existsSync(f.js)) await buildTheme(ctx, theme, data);
|
|
509
|
+
const res = await resolvedTokens(ctx, theme);
|
|
510
|
+
const problems = await contrastProblems(ctx, data, res);
|
|
511
|
+
const preview = await renderPreview(ctx, theme, data);
|
|
512
|
+
const sheet = await renderSheet(ctx, theme, data, {group});
|
|
513
|
+
const modes = modesOf(data);
|
|
514
|
+
const shots = png
|
|
515
|
+
? await pictures(
|
|
516
|
+
ctx.project,
|
|
517
|
+
[
|
|
518
|
+
{html: f.preview, png: f.previewPng},
|
|
519
|
+
{html: f.sheet, png: f.sheetPng},
|
|
520
|
+
],
|
|
521
|
+
modes,
|
|
522
|
+
)
|
|
523
|
+
: {files: [], problem: null};
|
|
524
|
+
return {
|
|
525
|
+
type: 'themeEdit.show',
|
|
526
|
+
data: {
|
|
527
|
+
theme,
|
|
528
|
+
like: data.like ?? null,
|
|
529
|
+
mode: data.mode,
|
|
530
|
+
settings: data.settings.map(s => ({...s, parts: partsOf(ctx, s)})),
|
|
531
|
+
resolved: Object.fromEntries(
|
|
532
|
+
KEY_TOKENS.map(k => [
|
|
533
|
+
k,
|
|
534
|
+
Object.fromEntries(modes.map(m => [m, res[m][k] ?? null])),
|
|
535
|
+
]),
|
|
536
|
+
),
|
|
537
|
+
problems,
|
|
538
|
+
notes: fontNotes(ctx, data),
|
|
539
|
+
preview,
|
|
540
|
+
sheet: sheet.file,
|
|
541
|
+
sheetComponents: sheet.components,
|
|
542
|
+
skipped: sheet.skipped,
|
|
543
|
+
pictures: shots.files,
|
|
544
|
+
pictureProblem: shots.problem,
|
|
545
|
+
},
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Put the theme back as it was before its last change. With `thing`, remove
|
|
551
|
+
* that one setting instead, wherever it is in the settings: it goes back to
|
|
552
|
+
* the base theme's value.
|
|
553
|
+
* @experimental
|
|
554
|
+
* @param {string} theme
|
|
555
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditUndoOptions} [options]
|
|
556
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditUndoResponse | import('./theme-edit.type.mjs').ThemeEditChangeResponse>}
|
|
557
|
+
*/
|
|
558
|
+
export async function themeEditUndo(theme, options = {}) {
|
|
559
|
+
const {cwd = process.cwd(), png = false, thing} = options;
|
|
560
|
+
if (thing != null)
|
|
561
|
+
return change('undo', theme, thing, options.value, options);
|
|
562
|
+
requireProject(cwd);
|
|
563
|
+
// A damaged settings file is what undo recovers from: the last history
|
|
564
|
+
// entry replaces it without the current file being read.
|
|
565
|
+
/** @type {import('./_settings.mjs').ThemeData | null} */
|
|
566
|
+
let current = null;
|
|
567
|
+
try {
|
|
568
|
+
current = loadTheme(cwd, theme);
|
|
569
|
+
} catch (e) {
|
|
570
|
+
if (!(e instanceof AstryxError && e.code === E.ERR_THEME_INVALID)) throw e;
|
|
571
|
+
}
|
|
572
|
+
const f = themeFiles(cwd, theme);
|
|
573
|
+
const history = fs.existsSync(f.history)
|
|
574
|
+
? JSON.parse(fs.readFileSync(f.history, 'utf8'))
|
|
575
|
+
: [];
|
|
576
|
+
if (!history.length)
|
|
577
|
+
refuse(
|
|
578
|
+
E.ERR_THEME_NOTHING_TO_UNDO,
|
|
579
|
+
`Nothing to undo: ${theme} has no earlier change.`,
|
|
580
|
+
{run: [`${P} show ${theme}`]},
|
|
581
|
+
);
|
|
582
|
+
const ctx = await context(cwd);
|
|
583
|
+
const prevText = history.pop();
|
|
584
|
+
const prev = /** @type {import('./_settings.mjs').ThemeData} */ (
|
|
585
|
+
JSON.parse(prevText)
|
|
586
|
+
);
|
|
587
|
+
await commitTheme(ctx, theme, prev, {history: false});
|
|
588
|
+
fs.writeFileSync(f.history, JSON.stringify(history));
|
|
589
|
+
/** @type {string[]} */
|
|
590
|
+
const undone = current
|
|
591
|
+
? []
|
|
592
|
+
: [
|
|
593
|
+
`the damaged ${theme}.settings.json is replaced by the settings before the last change`,
|
|
594
|
+
];
|
|
595
|
+
const now = new Map(prev.settings.map(s => [keyOf(s), s]));
|
|
596
|
+
const was = new Map((current?.settings ?? []).map(s => [keyOf(s), s]));
|
|
597
|
+
for (const [k, s] of was) {
|
|
598
|
+
const back = now.get(k);
|
|
599
|
+
if (!back) undone.push(`removed ${describe(s)}`);
|
|
600
|
+
else if (back.value !== s.value)
|
|
601
|
+
undone.push(`${describe(s)} is back to ${back.value}`);
|
|
602
|
+
}
|
|
603
|
+
if (current)
|
|
604
|
+
for (const [k, s] of now)
|
|
605
|
+
if (!was.has(k)) undone.push(`put back ${describe(s)}`);
|
|
606
|
+
if (current && (current.like ?? 'none') !== (prev.like ?? 'none'))
|
|
607
|
+
undone.push(
|
|
608
|
+
`base ${current.like ?? 'none'} is back to ${prev.like ?? 'none'}`,
|
|
609
|
+
);
|
|
610
|
+
if (current && current.mode !== prev.mode)
|
|
611
|
+
undone.push(`mode ${current.mode} is back to ${prev.mode}`);
|
|
612
|
+
const p = await previewAfter(ctx, theme, prev, png);
|
|
613
|
+
return {
|
|
614
|
+
type: 'themeEdit.undo',
|
|
615
|
+
data: {
|
|
616
|
+
theme,
|
|
617
|
+
action: 'step',
|
|
618
|
+
undone,
|
|
619
|
+
settings: prev.settings.length,
|
|
620
|
+
stepsLeft: history.length,
|
|
621
|
+
...p,
|
|
622
|
+
},
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** @param {string | undefined} group */
|
|
627
|
+
function checkGroup(group) {
|
|
628
|
+
if (group != null && !SHOWCASE_GROUPS.includes(group))
|
|
629
|
+
refuse(
|
|
630
|
+
E.ERR_INVALID_ARGUMENT,
|
|
631
|
+
`--group takes ${SHOWCASE_GROUPS.join(', ')}, not "${group}".`,
|
|
632
|
+
{didYouMean: nearest(group, SHOWCASE_GROUPS)},
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** The preview's data file: the pages it shows and the files it wrote. */
|
|
637
|
+
const PREVIEW_DATA = 'theme-preview.json';
|
|
638
|
+
const PREVIEW = 'astryx experimental theme preview';
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* @typedef {{id: string, label: string, file: string}} PreviewPage
|
|
642
|
+
* @typedef {{version: 1, theme: string | null, pages: PreviewPage[], files: string[]}} PreviewData
|
|
643
|
+
*/
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Where the preview route lives. `--path` names it (inside the project);
|
|
647
|
+
* else an existing preview in the app's routes folder; else the routes
|
|
648
|
+
* folder of a Next app router (`app/` or `src/app/`); else
|
|
649
|
+
* `./theme-preview/` at the project root, with no app around it.
|
|
650
|
+
* @param {string} cwd
|
|
651
|
+
* @param {string | undefined} where
|
|
652
|
+
* @param {{optional?: boolean}} [opts] `optional`: null instead of a refusal when there is no routes folder
|
|
653
|
+
* @returns {{dir: string, app: string | null}} `app`: the routes folder, when the route is in one
|
|
654
|
+
*/
|
|
655
|
+
function previewDir(cwd, where, opts = {}) {
|
|
656
|
+
const routes = ['app', path.join('src', 'app')]
|
|
657
|
+
.map(r => path.join(cwd, r))
|
|
658
|
+
.filter(r => fs.existsSync(r) && fs.statSync(r).isDirectory());
|
|
659
|
+
const appOf = (/** @type {string} */ dir) =>
|
|
660
|
+
routes.find(r => dir.startsWith(r + path.sep)) ?? null;
|
|
661
|
+
if (where != null) {
|
|
662
|
+
try {
|
|
663
|
+
const dir = assertWithin(path.resolve(cwd, where), cwd, {
|
|
664
|
+
allowAbsolute: true,
|
|
665
|
+
label: '--path',
|
|
666
|
+
});
|
|
667
|
+
return {dir, app: appOf(dir)};
|
|
668
|
+
} catch (e) {
|
|
669
|
+
if (e instanceof PathSafetyError)
|
|
670
|
+
refuse(
|
|
671
|
+
E.ERR_PATH_TRAVERSAL,
|
|
672
|
+
`--path ${where} is outside the project; put the preview inside the folder you run the command from.`,
|
|
673
|
+
);
|
|
674
|
+
throw e;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
const candidates = routes.map(r => path.join(r, 'theme-preview'));
|
|
678
|
+
const known = candidates.find(d => fs.existsSync(path.join(d, PREVIEW_DATA)));
|
|
679
|
+
const dir = known ?? candidates[0];
|
|
680
|
+
if (!dir && opts.optional) return /** @type {any} */ (null);
|
|
681
|
+
if (!dir)
|
|
682
|
+
refuse(
|
|
683
|
+
E.ERR_NOT_FOUND,
|
|
684
|
+
'No app routes folder here (app/ or src/app/), so there is no app to put the preview in.',
|
|
685
|
+
{
|
|
686
|
+
run: [
|
|
687
|
+
`${PREVIEW} add --path <dir> (a folder inside your app's routes)`,
|
|
688
|
+
`${PREVIEW} app <dir> (a new starter app with the preview in it)`,
|
|
689
|
+
],
|
|
690
|
+
},
|
|
691
|
+
);
|
|
692
|
+
return {dir, app: appOf(dir)};
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
const CLI_PACKAGE = path.resolve(
|
|
696
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
697
|
+
'../..',
|
|
698
|
+
);
|
|
699
|
+
const STARTER = path.join(CLI_PACKAGE, 'assets', 'preview-app');
|
|
700
|
+
/** Starter files a folder may already have: they are the app's own, so never replaced. */
|
|
701
|
+
const STARTER_KEEP = new Set(['package.json', '.gitignore']);
|
|
702
|
+
/** What the starter's own package.json depends on. */
|
|
703
|
+
const STARTER_DEPENDENCIES = Object.keys(
|
|
704
|
+
JSON.parse(fs.readFileSync(path.join(STARTER, 'package.json'), 'utf8'))
|
|
705
|
+
.dependencies,
|
|
706
|
+
);
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* `file` when it is inside `root`, also through links; else a refusal.
|
|
710
|
+
* @param {string} file absolute
|
|
711
|
+
* @param {string} root absolute
|
|
712
|
+
* @param {string} what what the path is, for the refusal
|
|
713
|
+
* @returns {string}
|
|
714
|
+
*/
|
|
715
|
+
function confined(file, root, what) {
|
|
716
|
+
try {
|
|
717
|
+
return assertWithin(file, root, {allowAbsolute: true, label: what});
|
|
718
|
+
} catch (e) {
|
|
719
|
+
if (e instanceof PathSafetyError)
|
|
720
|
+
refuse(
|
|
721
|
+
E.ERR_PATH_TRAVERSAL,
|
|
722
|
+
`${what} ${file} is outside ${root}; nothing was written or removed.`,
|
|
723
|
+
);
|
|
724
|
+
throw e;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* The files a new starter app (Next.js app router, Core and the neutral
|
|
730
|
+
* theme) writes into `where`, with `extra` files beside them, checked before
|
|
731
|
+
* anything is written: every file stays inside the folder, also through
|
|
732
|
+
* links; with `overwrite`, a protected file there is refused and the app's
|
|
733
|
+
* own package.json and .gitignore are kept.
|
|
734
|
+
* @param {string} cwd
|
|
735
|
+
* @param {string} where
|
|
736
|
+
* @param {boolean} overwrite
|
|
737
|
+
* @param {(root: string) => Array<{rel: string, text: string}>} extra files relative to the app folder
|
|
738
|
+
* @param {Record<string, string>} dependencies added to the starter's package.json
|
|
739
|
+
* @returns {{root: string, writes: Array<{file: string, text: string}>, kept: string[]}}
|
|
740
|
+
*/
|
|
741
|
+
function planStarterApp(cwd, where, overwrite, extra, dependencies) {
|
|
742
|
+
/** @type {string} */
|
|
743
|
+
let root;
|
|
744
|
+
try {
|
|
745
|
+
root = assertWithin(path.resolve(cwd, where), cwd, {
|
|
746
|
+
allowAbsolute: true,
|
|
747
|
+
label: 'the app folder',
|
|
748
|
+
});
|
|
749
|
+
} catch (e) {
|
|
750
|
+
if (e instanceof PathSafetyError)
|
|
751
|
+
refuse(
|
|
752
|
+
E.ERR_PATH_TRAVERSAL,
|
|
753
|
+
`${where} is outside the current folder; make the app inside the folder you run the command from.`,
|
|
754
|
+
);
|
|
755
|
+
throw e;
|
|
756
|
+
}
|
|
757
|
+
if (fs.existsSync(root) && fs.readdirSync(root).length && !overwrite)
|
|
758
|
+
refuse(
|
|
759
|
+
E.ERR_FILE_EXISTS,
|
|
760
|
+
`${shown(cwd, root) || '.'} exists and is not empty; a starter app goes into a new or empty folder (--overwrite replaces its files).`,
|
|
761
|
+
);
|
|
762
|
+
/** @type {Array<{file: string, text: string}>} */
|
|
763
|
+
const writes = [];
|
|
764
|
+
/** @type {string[]} */
|
|
765
|
+
const kept = [];
|
|
766
|
+
/** @param {string} from */
|
|
767
|
+
const walk = from => {
|
|
768
|
+
for (const entry of fs.readdirSync(from, {withFileTypes: true})) {
|
|
769
|
+
const src = path.join(from, entry.name);
|
|
770
|
+
if (entry.isDirectory()) walk(src);
|
|
771
|
+
else if (entry.name !== 'README.md') {
|
|
772
|
+
const rel = path.relative(STARTER, src);
|
|
773
|
+
const name = rel === 'gitignore' ? '.gitignore' : rel;
|
|
774
|
+
const file = confined(
|
|
775
|
+
path.join(root, name),
|
|
776
|
+
root,
|
|
777
|
+
'a starter app file',
|
|
778
|
+
);
|
|
779
|
+
if (STARTER_KEEP.has(name) && fs.existsSync(file)) {
|
|
780
|
+
kept.push(shown(cwd, file));
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
783
|
+
const text = fs.readFileSync(src, 'utf8');
|
|
784
|
+
writes.push({
|
|
785
|
+
file,
|
|
786
|
+
text:
|
|
787
|
+
rel === 'package.json'
|
|
788
|
+
? starterPackageJson(text, dependencies)
|
|
789
|
+
: text,
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
walk(STARTER);
|
|
795
|
+
for (const x of extra(root))
|
|
796
|
+
writes.push({
|
|
797
|
+
file: confined(path.join(root, x.rel), root, 'an app file'),
|
|
798
|
+
text: x.text,
|
|
799
|
+
});
|
|
800
|
+
const existing = writes.map(w => w.file).filter(f => fs.existsSync(f));
|
|
801
|
+
if (existing.length) {
|
|
802
|
+
/** @type {ReturnType<typeof createFileProtectionResolver>} */
|
|
803
|
+
let resolver;
|
|
804
|
+
try {
|
|
805
|
+
resolver = createFileProtectionResolver(cwd);
|
|
806
|
+
} catch (e) {
|
|
807
|
+
refuse(
|
|
808
|
+
E.ERR_FILE_EXISTS,
|
|
809
|
+
`Could not read this project's file protection (${e instanceof Error ? e.message : String(e)}), so no existing file is replaced; nothing was written.`,
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
const blocked = existing.flatMap(f => {
|
|
813
|
+
const protection = resolver.classify(f)[0];
|
|
814
|
+
return protection ? [`${shown(cwd, f)} (${protection.declaration})`] : [];
|
|
815
|
+
});
|
|
816
|
+
if (blocked.length)
|
|
817
|
+
refuse(
|
|
818
|
+
E.ERR_FILE_EXISTS,
|
|
819
|
+
`${blocked.join(', ')} ${blocked.length === 1 ? 'is' : 'are'} protected, so never replaced, even with --overwrite; nothing was written. Choose another app folder.`,
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
return {root, writes, kept: kept.sort()};
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* The starter's package.json with the Astryx packages pinned to this CLI's
|
|
827
|
+
* release, and `dependencies` added.
|
|
828
|
+
* @param {string} text
|
|
829
|
+
* @param {Record<string, string>} dependencies
|
|
830
|
+
*/
|
|
831
|
+
function starterPackageJson(text, dependencies) {
|
|
832
|
+
const pinned = text.replaceAll('{{astryxVersion}}', cliVersion());
|
|
833
|
+
if (!Object.keys(dependencies).length) return pinned;
|
|
834
|
+
const pkg = JSON.parse(pinned);
|
|
835
|
+
pkg.dependencies = Object.fromEntries(
|
|
836
|
+
Object.entries({...pkg.dependencies, ...dependencies}).sort(([a], [b]) =>
|
|
837
|
+
a.localeCompare(b),
|
|
838
|
+
),
|
|
839
|
+
);
|
|
840
|
+
return `${JSON.stringify(pkg, null, 2)}\n`;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* Write a planned starter app. No install runs here.
|
|
845
|
+
* @param {string} cwd
|
|
846
|
+
* @param {{writes: Array<{file: string, text: string}>}} plan
|
|
847
|
+
* @returns {string[]} the files written
|
|
848
|
+
*/
|
|
849
|
+
function writeStarterApp(cwd, plan) {
|
|
850
|
+
for (const w of plan.writes) {
|
|
851
|
+
fs.mkdirSync(path.dirname(w.file), {recursive: true});
|
|
852
|
+
fs.writeFileSync(w.file, w.text);
|
|
853
|
+
}
|
|
854
|
+
return plan.writes.map(w => shown(cwd, w.file)).sort();
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/** @param {string} dir @returns {PreviewData | null} */
|
|
858
|
+
function readPreview(dir) {
|
|
859
|
+
try {
|
|
860
|
+
return JSON.parse(fs.readFileSync(path.join(dir, PREVIEW_DATA), 'utf8'));
|
|
861
|
+
} catch {
|
|
862
|
+
return null;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* A preview file, checked: inside the route folder, also through links, and
|
|
868
|
+
* not a file there that the preview did not write, which is never replaced.
|
|
869
|
+
* @param {string} cwd
|
|
870
|
+
* @param {string} dir
|
|
871
|
+
* @param {PreviewData} data
|
|
872
|
+
* @param {string} rel relative to dir
|
|
873
|
+
* @returns {string} the absolute file
|
|
874
|
+
*/
|
|
875
|
+
function previewFile(cwd, dir, data, rel) {
|
|
876
|
+
const file = confined(path.join(dir, rel), dir, 'a preview file');
|
|
877
|
+
const key = rel.split(path.sep).join('/');
|
|
878
|
+
if (fs.existsSync(file) && !data.files.includes(key))
|
|
879
|
+
refuse(
|
|
880
|
+
E.ERR_FILE_EXISTS,
|
|
881
|
+
`${shown(cwd, file)} exists and the preview did not write it; it is never replaced. Move it, or put the preview elsewhere with --path.`,
|
|
882
|
+
);
|
|
883
|
+
return file;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Write one file of the preview, checked as {@link previewFile} checks it.
|
|
888
|
+
* @param {string} cwd
|
|
889
|
+
* @param {string} dir
|
|
890
|
+
* @param {PreviewData} data
|
|
891
|
+
* @param {string} rel relative to dir
|
|
892
|
+
* @param {string} text
|
|
893
|
+
*/
|
|
894
|
+
function writePreviewFile(cwd, dir, data, rel, text) {
|
|
895
|
+
const file = previewFile(cwd, dir, data, rel);
|
|
896
|
+
fs.mkdirSync(path.dirname(file), {recursive: true});
|
|
897
|
+
fs.writeFileSync(file, text);
|
|
898
|
+
const key = rel.split(path.sep).join('/');
|
|
899
|
+
if (!data.files.includes(key)) data.files.push(key);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/** The themes this command made, by name. @param {string} cwd */
|
|
903
|
+
function editedThemes(cwd) {
|
|
904
|
+
const root = path.join(cwd, 'themes');
|
|
905
|
+
return fs.existsSync(root)
|
|
906
|
+
? fs
|
|
907
|
+
.readdirSync(root)
|
|
908
|
+
.filter(d => fs.existsSync(themeFiles(cwd, d).settings))
|
|
909
|
+
.sort()
|
|
910
|
+
: [];
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Write the route and the data file from the page list.
|
|
915
|
+
* @param {string} cwd
|
|
916
|
+
* @param {string} dir
|
|
917
|
+
* @param {PreviewData} data
|
|
918
|
+
*/
|
|
919
|
+
async function writeRoute(cwd, dir, data) {
|
|
920
|
+
/** @type {{name: string, module: string, css: string[]} | null} */
|
|
921
|
+
let theme = null;
|
|
922
|
+
if (data.theme) {
|
|
923
|
+
const t = loadTheme(cwd, data.theme);
|
|
924
|
+
const ctx = await context(cwd);
|
|
925
|
+
if (!fs.existsSync(themeFiles(cwd, data.theme).js))
|
|
926
|
+
await buildTheme(ctx, data.theme, t);
|
|
927
|
+
const ti = themeImports(ctx, data.theme, t, dir);
|
|
928
|
+
theme = {name: data.theme, module: ti.module, css: ti.css};
|
|
929
|
+
}
|
|
930
|
+
writePreviewFile(
|
|
931
|
+
cwd,
|
|
932
|
+
dir,
|
|
933
|
+
data,
|
|
934
|
+
'page.tsx',
|
|
935
|
+
previewRouteSource({pages: data.pages, theme}),
|
|
936
|
+
);
|
|
937
|
+
if (!data.files.includes(PREVIEW_DATA)) data.files.push(PREVIEW_DATA);
|
|
938
|
+
fs.writeFileSync(
|
|
939
|
+
path.join(dir, PREVIEW_DATA),
|
|
940
|
+
`${JSON.stringify(data, null, 2)}\n`,
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* How to open the preview, from where it is.
|
|
946
|
+
* @param {string} cwd
|
|
947
|
+
* @param {{dir: string, app: string | null}} at
|
|
948
|
+
*/
|
|
949
|
+
function openLine(cwd, at) {
|
|
950
|
+
if (at.app)
|
|
951
|
+
return `open /${path.relative(at.app, at.dir).split(path.sep).join('/')} in the running app`;
|
|
952
|
+
return `no app routes folder (app/ or src/app/) was found, so the preview is in ${shown(cwd, at.dir)}/; a starter app around it is not set up yet: serve page.tsx from a route of your app, or move it there with --path`;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* Add pages to the in-app theme preview, and write the preview route the
|
|
957
|
+
* first time. With a description, the build ranking picks the template
|
|
958
|
+
* pages that fit it best (`top` of them); `template` names one instead.
|
|
959
|
+
* With neither, the default pages: the sample app page and every component.
|
|
960
|
+
* Every check runs before the first write, so a refusal changes nothing.
|
|
961
|
+
* @experimental
|
|
962
|
+
* @param {string} [description]
|
|
963
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewAddOptions} [options]
|
|
964
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAddResponse>}
|
|
965
|
+
*/
|
|
966
|
+
export async function themeEditPreviewAdd(description, options = {}) {
|
|
967
|
+
const {cwd = process.cwd(), top = 1} = options;
|
|
968
|
+
if (isOutsideProject(cwd))
|
|
969
|
+
refuse(
|
|
970
|
+
E.ERR_PROJECT_REQUIRED,
|
|
971
|
+
'No package.json here, so there is no app to put the preview in.',
|
|
972
|
+
{
|
|
973
|
+
run: [
|
|
974
|
+
`${PREVIEW} add --path <dir> (run in your app: a folder inside its routes)`,
|
|
975
|
+
`${PREVIEW} app <dir> (a new starter app with the preview in it)`,
|
|
976
|
+
],
|
|
977
|
+
},
|
|
978
|
+
);
|
|
979
|
+
const words = (description ?? '').trim();
|
|
980
|
+
if (words && options.template != null)
|
|
981
|
+
refuse(
|
|
982
|
+
E.ERR_INVALID_ARGUMENT,
|
|
983
|
+
'--template names the page; a description asks the ranking to pick one, so drop one of them.',
|
|
984
|
+
);
|
|
985
|
+
if (!Number.isInteger(top) || top < 1 || top > 3)
|
|
986
|
+
refuse(E.ERR_INVALID_ARGUMENT, `--top takes 1, 2 or 3, not "${top}".`);
|
|
987
|
+
const at = previewDir(cwd, options.path);
|
|
988
|
+
const data = readPreview(at.dir) ?? {
|
|
989
|
+
version: 1,
|
|
990
|
+
theme: null,
|
|
991
|
+
pages: [],
|
|
992
|
+
files: [],
|
|
993
|
+
};
|
|
994
|
+
/** @type {string[]} */
|
|
995
|
+
const notes = [];
|
|
996
|
+
if (options.theme != null) {
|
|
997
|
+
loadTheme(cwd, options.theme);
|
|
998
|
+
data.theme = options.theme;
|
|
999
|
+
} else if (!data.theme) {
|
|
1000
|
+
const themes = editedThemes(cwd);
|
|
1001
|
+
data.theme = themes.length === 1 ? themes[0] : null;
|
|
1002
|
+
}
|
|
1003
|
+
if (data.theme) loadTheme(cwd, data.theme);
|
|
1004
|
+
const project = await openProject(cwd);
|
|
1005
|
+
/** @type {Array<{id: string, label: string, source?: string, template?: string}>} */
|
|
1006
|
+
const add = [];
|
|
1007
|
+
/** @type {Array<{page: string, missingPackages: string[], installCommand: string | null}>} */
|
|
1008
|
+
const skipped = [];
|
|
1009
|
+
if (!words && options.template == null) {
|
|
1010
|
+
add.push(
|
|
1011
|
+
{id: 'overview', label: 'Overview', source: overviewPageSource(project)},
|
|
1012
|
+
{
|
|
1013
|
+
id: 'components',
|
|
1014
|
+
label: 'Components',
|
|
1015
|
+
source: componentsPageSource(project).source,
|
|
1016
|
+
},
|
|
1017
|
+
);
|
|
1018
|
+
} else {
|
|
1019
|
+
/** @type {Array<{name: string, displayName: string}>} */
|
|
1020
|
+
let picks;
|
|
1021
|
+
if (options.template != null)
|
|
1022
|
+
picks = [{name: options.template, displayName: options.template}];
|
|
1023
|
+
else {
|
|
1024
|
+
const kit =
|
|
1025
|
+
/** @type {import('../build/build.type.mjs').BuildKitResponse} */ (
|
|
1026
|
+
await build(words, {cwd})
|
|
1027
|
+
);
|
|
1028
|
+
const start = kit.data.start;
|
|
1029
|
+
picks = start ? [start, ...start.alternatives].slice(0, top) : [];
|
|
1030
|
+
if (!picks.length)
|
|
1031
|
+
refuse(
|
|
1032
|
+
E.ERR_NOT_FOUND,
|
|
1033
|
+
`No page template to show for "${words}": this project has none.`,
|
|
1034
|
+
{run: [`${PREVIEW} add`]},
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
1037
|
+
for (const pick of picks) {
|
|
1038
|
+
const shownTemplate =
|
|
1039
|
+
/** @type {import('../template/template.type.mjs').TemplateShowResponse} */ (
|
|
1040
|
+
await template(pick.name, {cwd, type: 'page'})
|
|
1041
|
+
);
|
|
1042
|
+
const needs = analyzeTemplateNeeds(shownTemplate.data.source, cwd);
|
|
1043
|
+
if (needs.missingPackages.length) {
|
|
1044
|
+
skipped.push({
|
|
1045
|
+
page: pick.name,
|
|
1046
|
+
missingPackages: needs.missingPackages,
|
|
1047
|
+
installCommand: needs.installCommand,
|
|
1048
|
+
});
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
add.push({id: pick.name, label: pick.displayName, template: pick.name});
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
for (const s of skipped)
|
|
1055
|
+
notes.push(
|
|
1056
|
+
`the ${s.page} page needs ${s.missingPackages.join(', ')}, so it was not added${s.installCommand ? `: ${s.installCommand}, then add it again` : ''}`,
|
|
1057
|
+
);
|
|
1058
|
+
if (!add.length)
|
|
1059
|
+
refuse(
|
|
1060
|
+
E.ERR_DEP_MISSING,
|
|
1061
|
+
`No page was added: ${skipped.map(s => `the ${s.page} page needs ${s.missingPackages.join(', ')}`).join('; ')}. Nothing was written.`,
|
|
1062
|
+
{
|
|
1063
|
+
run: skipped.flatMap(s => (s.installCommand ? [s.installCommand] : [])),
|
|
1064
|
+
},
|
|
1065
|
+
);
|
|
1066
|
+
for (const page of add)
|
|
1067
|
+
previewFile(cwd, at.dir, data, `pages/${page.id}.tsx`);
|
|
1068
|
+
previewFile(cwd, at.dir, data, 'page.tsx');
|
|
1069
|
+
previewFile(cwd, at.dir, data, PREVIEW_DATA);
|
|
1070
|
+
|
|
1071
|
+
/** @type {string[]} */
|
|
1072
|
+
const added = [];
|
|
1073
|
+
for (const page of add) {
|
|
1074
|
+
const file = `pages/${page.id}`;
|
|
1075
|
+
if (page.template != null) {
|
|
1076
|
+
const target = previewFile(cwd, at.dir, data, `${file}.tsx`);
|
|
1077
|
+
await template(page.template, {
|
|
1078
|
+
cwd,
|
|
1079
|
+
type: 'page',
|
|
1080
|
+
targetPath: path.relative(cwd, target),
|
|
1081
|
+
overwrite: true,
|
|
1082
|
+
});
|
|
1083
|
+
if (!data.files.includes(`${file}.tsx`)) data.files.push(`${file}.tsx`);
|
|
1084
|
+
} else
|
|
1085
|
+
writePreviewFile(
|
|
1086
|
+
cwd,
|
|
1087
|
+
at.dir,
|
|
1088
|
+
data,
|
|
1089
|
+
`${file}.tsx`,
|
|
1090
|
+
/** @type {string} */ (page.source),
|
|
1091
|
+
);
|
|
1092
|
+
if (!data.pages.some(x => x.id === page.id)) {
|
|
1093
|
+
data.pages.push({id: page.id, label: page.label, file});
|
|
1094
|
+
added.push(page.id);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
await writeRoute(cwd, at.dir, data);
|
|
1098
|
+
return {
|
|
1099
|
+
type: 'themeEdit.previewAdd',
|
|
1100
|
+
data: {
|
|
1101
|
+
folder: shown(cwd, at.dir),
|
|
1102
|
+
theme: data.theme,
|
|
1103
|
+
added,
|
|
1104
|
+
skipped,
|
|
1105
|
+
notes,
|
|
1106
|
+
pages: data.pages.map(x => x.id),
|
|
1107
|
+
files: data.files.map(f => shown(cwd, path.join(at.dir, f))),
|
|
1108
|
+
open: openLine(cwd, at),
|
|
1109
|
+
},
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* Make a new starter app (Next.js app router, Core and the neutral theme,
|
|
1115
|
+
* with the Astryx packages pinned to this CLI's release) in `dir`, with the
|
|
1116
|
+
* theme preview route and its default pages inside. With `theme`, the theme
|
|
1117
|
+
* folder is copied into the app and the pages wear it. No install runs.
|
|
1118
|
+
* Every check runs before the first write, so a refusal changes nothing.
|
|
1119
|
+
* @experimental
|
|
1120
|
+
* @param {string} dir
|
|
1121
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewAppOptions} [options]
|
|
1122
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAppResponse>}
|
|
1123
|
+
*/
|
|
1124
|
+
export async function themeEditPreviewApp(dir, options = {}) {
|
|
1125
|
+
const {cwd = process.cwd(), overwrite = false} = options;
|
|
1126
|
+
if (typeof dir !== 'string' || !dir.trim())
|
|
1127
|
+
refuse(
|
|
1128
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1129
|
+
'Name the folder for the new app: a new or empty folder inside the current one.',
|
|
1130
|
+
{run: [`${PREVIEW} app <dir>`]},
|
|
1131
|
+
);
|
|
1132
|
+
const slug = options.theme ?? null;
|
|
1133
|
+
const theme = slug == null ? null : loadTheme(cwd, slug);
|
|
1134
|
+
// A new app has no Core installed yet: its pages come from this CLI's own
|
|
1135
|
+
// Core when the current folder has none.
|
|
1136
|
+
const project = await openProject(cwd).catch(() => openProject(CLI_PACKAGE));
|
|
1137
|
+
/** @type {Record<string, string>} */
|
|
1138
|
+
const dependencies = {};
|
|
1139
|
+
/** @type {Array<{rel: string, text: string}>} */
|
|
1140
|
+
const themeCopy = [];
|
|
1141
|
+
/** @type {{name: string, module: string, css: string[]} | null} */
|
|
1142
|
+
let routeTheme = null;
|
|
1143
|
+
const routeDir = path.join('src', 'app', 'theme-preview');
|
|
1144
|
+
if (slug != null && theme) {
|
|
1145
|
+
const from = themeFiles(cwd, slug);
|
|
1146
|
+
if (!fs.existsSync(from.js) || !fs.existsSync(from.css))
|
|
1147
|
+
await buildTheme(await context(cwd), slug, theme);
|
|
1148
|
+
const to = themeFiles('', slug);
|
|
1149
|
+
// The theme and its build, with the type declarations the build writes;
|
|
1150
|
+
// not its history, preview pages or pictures.
|
|
1151
|
+
const skip = new Set(
|
|
1152
|
+
[
|
|
1153
|
+
from.history,
|
|
1154
|
+
from.preview,
|
|
1155
|
+
from.sheet,
|
|
1156
|
+
from.previewPng,
|
|
1157
|
+
from.sheetPng,
|
|
1158
|
+
].map(f => path.basename(f)),
|
|
1159
|
+
);
|
|
1160
|
+
for (const entry of fs.readdirSync(from.dir, {withFileTypes: true}))
|
|
1161
|
+
if (entry.isFile() && !skip.has(entry.name))
|
|
1162
|
+
themeCopy.push({
|
|
1163
|
+
rel: path.join(to.dir, entry.name),
|
|
1164
|
+
text: fs.readFileSync(path.join(from.dir, entry.name), 'utf8'),
|
|
1165
|
+
});
|
|
1166
|
+
if (theme.like && theme.like !== 'neutral')
|
|
1167
|
+
dependencies[`@astryxdesign/theme-${theme.like}`] = cliVersion();
|
|
1168
|
+
/** @param {string} rel */
|
|
1169
|
+
const fromRoute = rel => {
|
|
1170
|
+
const r = path.relative(routeDir, rel).split(path.sep).join('/');
|
|
1171
|
+
return r.startsWith('.') ? r : `./${r}`;
|
|
1172
|
+
};
|
|
1173
|
+
const fonts = fontPackages(await context(cwd), theme).filter(
|
|
1174
|
+
x => x.installed,
|
|
1175
|
+
);
|
|
1176
|
+
for (const font of fonts)
|
|
1177
|
+
dependencies[font.pkg] = readVersion(project, font.pkg) ?? 'latest';
|
|
1178
|
+
routeTheme = {
|
|
1179
|
+
name: slug,
|
|
1180
|
+
module: fromRoute(to.js),
|
|
1181
|
+
css: [fromRoute(to.css), ...fonts.map(x => `${x.pkg}/index.css`)],
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
/** @type {PreviewData} */
|
|
1185
|
+
const data = {version: 1, theme: slug, pages: [], files: []};
|
|
1186
|
+
/** @type {Array<{rel: string, text: string}>} */
|
|
1187
|
+
const preview = [];
|
|
1188
|
+
for (const [id, label, source] of /** @type {const} */ ([
|
|
1189
|
+
['overview', 'Overview', overviewPageSource(project)],
|
|
1190
|
+
['components', 'Components', componentsPageSource(project).source],
|
|
1191
|
+
])) {
|
|
1192
|
+
data.pages.push({id, label, file: `pages/${id}`});
|
|
1193
|
+
preview.push({rel: `pages/${id}.tsx`, text: source});
|
|
1194
|
+
}
|
|
1195
|
+
preview.push({
|
|
1196
|
+
rel: 'page.tsx',
|
|
1197
|
+
text: previewRouteSource({pages: data.pages, theme: routeTheme}),
|
|
1198
|
+
});
|
|
1199
|
+
data.files = [...preview.map(x => x.rel), PREVIEW_DATA];
|
|
1200
|
+
preview.push({rel: PREVIEW_DATA, text: `${JSON.stringify(data, null, 2)}\n`});
|
|
1201
|
+
const plan = planStarterApp(
|
|
1202
|
+
cwd,
|
|
1203
|
+
dir,
|
|
1204
|
+
overwrite,
|
|
1205
|
+
() => [
|
|
1206
|
+
...themeCopy,
|
|
1207
|
+
...preview.map(x => ({rel: path.join(routeDir, x.rel), text: x.text})),
|
|
1208
|
+
],
|
|
1209
|
+
dependencies,
|
|
1210
|
+
);
|
|
1211
|
+
const files = writeStarterApp(cwd, plan);
|
|
1212
|
+
const folder = shown(cwd, plan.root) || '.';
|
|
1213
|
+
/** @type {string[]} */
|
|
1214
|
+
const notes = [];
|
|
1215
|
+
if (plan.kept.length)
|
|
1216
|
+
notes.push(
|
|
1217
|
+
`kept the app's own ${plan.kept.join(', ')}: the starter needs ${[...STARTER_DEPENDENCIES, ...Object.keys(dependencies)].join(', ')} in package.json`,
|
|
1218
|
+
);
|
|
1219
|
+
return {
|
|
1220
|
+
type: 'themeEdit.previewApp',
|
|
1221
|
+
data: {
|
|
1222
|
+
folder,
|
|
1223
|
+
theme: slug,
|
|
1224
|
+
pages: data.pages.map(x => x.id),
|
|
1225
|
+
files,
|
|
1226
|
+
notes,
|
|
1227
|
+
open: `cd ${folder} && npm install && npm run dev, then open /theme-preview`,
|
|
1228
|
+
},
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/**
|
|
1233
|
+
* The installed version of a package, as the project resolves it.
|
|
1234
|
+
* @param {import('./_project.mjs').ThemeProject} project
|
|
1235
|
+
* @param {string} pkg
|
|
1236
|
+
* @returns {string | null}
|
|
1237
|
+
*/
|
|
1238
|
+
function readVersion(project, pkg) {
|
|
1239
|
+
try {
|
|
1240
|
+
return JSON.parse(
|
|
1241
|
+
fs.readFileSync(project.req.resolve(`${pkg}/package.json`), 'utf8'),
|
|
1242
|
+
).version;
|
|
1243
|
+
} catch {
|
|
1244
|
+
return null;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* The pages of the in-app theme preview. With no preview, and with no
|
|
1250
|
+
* routes folder for one, the list is empty.
|
|
1251
|
+
* @experimental
|
|
1252
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewListOptions} [options]
|
|
1253
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewListResponse>}
|
|
1254
|
+
*/
|
|
1255
|
+
export async function themeEditPreviewList(options = {}) {
|
|
1256
|
+
const {cwd = process.cwd()} = options;
|
|
1257
|
+
requireProject(cwd);
|
|
1258
|
+
const at = previewDir(cwd, options.path, {optional: true});
|
|
1259
|
+
const data = at && readPreview(at.dir);
|
|
1260
|
+
return {
|
|
1261
|
+
type: 'themeEdit.previewList',
|
|
1262
|
+
data: {
|
|
1263
|
+
folder: at && data ? shown(cwd, at.dir) : null,
|
|
1264
|
+
theme: data?.theme ?? null,
|
|
1265
|
+
pages: (data?.pages ?? []).map(x => ({id: x.id, label: x.label})),
|
|
1266
|
+
},
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
/**
|
|
1271
|
+
* Remove one page from the in-app theme preview, or with no page the whole
|
|
1272
|
+
* preview: only the files it wrote are deleted, and only inside the route
|
|
1273
|
+
* folder.
|
|
1274
|
+
* @experimental
|
|
1275
|
+
* @param {string} [page]
|
|
1276
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewListOptions} [options]
|
|
1277
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewRemoveResponse>}
|
|
1278
|
+
*/
|
|
1279
|
+
export async function themeEditPreviewRemove(page, options = {}) {
|
|
1280
|
+
const {cwd = process.cwd()} = options;
|
|
1281
|
+
requireProject(cwd);
|
|
1282
|
+
const at = previewDir(cwd, options.path);
|
|
1283
|
+
const data = readPreview(at.dir);
|
|
1284
|
+
if (!data)
|
|
1285
|
+
refuse(
|
|
1286
|
+
E.ERR_NOT_FOUND,
|
|
1287
|
+
`There is no theme preview in ${shown(cwd, at.dir)}/ (no ${PREVIEW_DATA}).`,
|
|
1288
|
+
{run: [`${PREVIEW} add`]},
|
|
1289
|
+
);
|
|
1290
|
+
// theme-preview.json is a file anyone can edit: every path in it must
|
|
1291
|
+
// stay in the route folder before anything is removed.
|
|
1292
|
+
const inside = (/** @type {string} */ rel) =>
|
|
1293
|
+
confined(path.join(at.dir, rel), at.dir, `${PREVIEW_DATA} names`);
|
|
1294
|
+
for (const rel of data.files) inside(rel);
|
|
1295
|
+
for (const p of data.pages) inside(`${p.file}.tsx`);
|
|
1296
|
+
/** @param {string} rel */
|
|
1297
|
+
const drop = rel => {
|
|
1298
|
+
const file = inside(rel);
|
|
1299
|
+
fs.rmSync(file, {force: true});
|
|
1300
|
+
return shown(cwd, file);
|
|
1301
|
+
};
|
|
1302
|
+
if (page != null && data.pages.length > 1) {
|
|
1303
|
+
const one = data.pages.find(x => x.id === page);
|
|
1304
|
+
if (!one)
|
|
1305
|
+
refuse(E.ERR_NOT_FOUND, `The preview has no page "${page}".`, {
|
|
1306
|
+
didYouMean: nearest(
|
|
1307
|
+
page,
|
|
1308
|
+
data.pages.map(x => x.id),
|
|
1309
|
+
),
|
|
1310
|
+
run: [`${PREVIEW} list`],
|
|
1311
|
+
});
|
|
1312
|
+
data.pages = data.pages.filter(x => x !== one);
|
|
1313
|
+
const rel = `${one.file}.tsx`;
|
|
1314
|
+
data.files = data.files.filter(f => f !== rel);
|
|
1315
|
+
const removed = [drop(rel)];
|
|
1316
|
+
await writeRoute(cwd, at.dir, data);
|
|
1317
|
+
return {
|
|
1318
|
+
type: 'themeEdit.previewRemove',
|
|
1319
|
+
data: {
|
|
1320
|
+
folder: shown(cwd, at.dir),
|
|
1321
|
+
removed,
|
|
1322
|
+
pages: data.pages.map(x => x.id),
|
|
1323
|
+
},
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
if (page != null && !data.pages.some(x => x.id === page))
|
|
1327
|
+
refuse(E.ERR_NOT_FOUND, `The preview has no page "${page}".`, {
|
|
1328
|
+
run: [`${PREVIEW} list`],
|
|
1329
|
+
});
|
|
1330
|
+
const removed = data.files.map(drop);
|
|
1331
|
+
for (const sub of [path.join(at.dir, 'pages'), at.dir])
|
|
1332
|
+
try {
|
|
1333
|
+
fs.rmdirSync(sub);
|
|
1334
|
+
} catch {
|
|
1335
|
+
// Not empty: something there is not the preview's.
|
|
1336
|
+
}
|
|
1337
|
+
return {
|
|
1338
|
+
type: 'themeEdit.previewRemove',
|
|
1339
|
+
data: {folder: shown(cwd, at.dir), removed, pages: []},
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/** What `how --list` takes. */
|
|
1344
|
+
const HOW_LISTS = ['dials', 'tokens', 'parts', 'families', 'properties'];
|
|
1345
|
+
|
|
1346
|
+
/**
|
|
1347
|
+
* Explain anything a theme change can name, or answer a question about one:
|
|
1348
|
+
* a part (what it reads for each aspect, its variants, states and the parts
|
|
1349
|
+
* inside it), a family (its members), a setting (a dial's rungs, a token's
|
|
1350
|
+
* type and readers, a CSS property), an aspect (colors, text, corners...: the
|
|
1351
|
+
* whole-theme tokens and dial), a plain-words question (exact commands), a
|
|
1352
|
+
* PNG picture (its main colors, or the color at points), or with `list` a
|
|
1353
|
+
* list of dials, tokens, parts, families or CSS properties.
|
|
1354
|
+
* @experimental
|
|
1355
|
+
* @param {string} subject a part, family, setting, aspect, question, or PNG file; with `list`, a filter
|
|
1356
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditHowOptions} [options]
|
|
1357
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditHowResponse>}
|
|
1358
|
+
*/
|
|
1359
|
+
export async function themeEditHow(subject, options = {}) {
|
|
1360
|
+
const {cwd = process.cwd(), list, at = [], count} = options;
|
|
1361
|
+
requireProject(cwd);
|
|
1362
|
+
const t = (subject ?? '').trim();
|
|
1363
|
+
if (list != null) {
|
|
1364
|
+
if (at.length)
|
|
1365
|
+
refuse(
|
|
1366
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1367
|
+
'--at reads a picture; --list lists names, so drop one of them.',
|
|
1368
|
+
);
|
|
1369
|
+
return {
|
|
1370
|
+
type: 'themeEdit.how',
|
|
1371
|
+
data: {kind: 'list', subject: t, ...(await listItems(list, t, cwd))},
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
if (/\.png$/i.test(t))
|
|
1375
|
+
return {
|
|
1376
|
+
type: 'themeEdit.how',
|
|
1377
|
+
data: {kind: 'picture', subject: t, ...pictureColors(t, cwd, at, count)},
|
|
1378
|
+
};
|
|
1379
|
+
if (at.length)
|
|
1380
|
+
refuse(
|
|
1381
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1382
|
+
'--at reads the color at points of a PNG picture; name the picture first.',
|
|
1383
|
+
{run: [`${HOW} picture.png --at "40,20"`]},
|
|
1384
|
+
);
|
|
1385
|
+
if (count != null)
|
|
1386
|
+
refuse(E.ERR_INVALID_ARGUMENT, 'A count applies only to a picture.', {
|
|
1387
|
+
run: [`${HOW} picture.png ${count}`],
|
|
1388
|
+
});
|
|
1389
|
+
if (!t) {
|
|
1390
|
+
refuse(
|
|
1391
|
+
E.ERR_MISSING_ARGUMENT,
|
|
1392
|
+
'Name a part, a family, a setting, an aspect, a question in quotes, or a PNG picture.',
|
|
1393
|
+
{
|
|
1394
|
+
run: [
|
|
1395
|
+
`${HOW} card`,
|
|
1396
|
+
`${HOW} "white text in the banner"`,
|
|
1397
|
+
`${HOW} --list dials`,
|
|
1398
|
+
],
|
|
1399
|
+
},
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
const ctx = await context(cwd);
|
|
1403
|
+
const {C} = ctx;
|
|
1404
|
+
if (FAMILIES[t]) {
|
|
1405
|
+
return {
|
|
1406
|
+
type: 'themeEdit.how',
|
|
1407
|
+
data: {
|
|
1408
|
+
kind: 'family',
|
|
1409
|
+
subject: t,
|
|
1410
|
+
family: t,
|
|
1411
|
+
members: FAMILIES[t],
|
|
1412
|
+
spreads: AUTO_SPREAD.includes(t),
|
|
1413
|
+
lines: [
|
|
1414
|
+
`${t} (family): ${FAMILIES[t].join(', ')}`,
|
|
1415
|
+
AUTO_SPREAD.includes(t)
|
|
1416
|
+
? ` styling one of them styles all (--only to opt out); ${HOW} <one of them> for details`
|
|
1417
|
+
: ` --on ${t} styles all of them; --on <one of them> styles just that one`,
|
|
1418
|
+
],
|
|
1419
|
+
},
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
if (ctx.project.target.has(t)) {
|
|
1423
|
+
const fam = familyOf(t);
|
|
1424
|
+
const lines = [...C.explain(t, P)];
|
|
1425
|
+
if (fam)
|
|
1426
|
+
lines.push(
|
|
1427
|
+
AUTO_SPREAD.includes(fam)
|
|
1428
|
+
? ` family: ${fam} (styling ${t} styles all of: ${FAMILIES[fam].join(', ')}; --only to opt out)`
|
|
1429
|
+
: ` family: ${fam} (--on ${fam} styles all of: ${FAMILIES[fam].join(', ')})`,
|
|
1430
|
+
);
|
|
1431
|
+
return {
|
|
1432
|
+
type: 'themeEdit.how',
|
|
1433
|
+
data: {
|
|
1434
|
+
kind: 'part',
|
|
1435
|
+
subject: t,
|
|
1436
|
+
family: fam,
|
|
1437
|
+
variants: C.variantValues(t),
|
|
1438
|
+
states: ctx.project.target.get(t)?.states ?? [],
|
|
1439
|
+
reads: Object.fromEntries(
|
|
1440
|
+
Object.keys(GROUPS).map(g => [g, C.partReads(t, g)]),
|
|
1441
|
+
),
|
|
1442
|
+
contains: C.MAP[t]?.contains ?? [],
|
|
1443
|
+
lines,
|
|
1444
|
+
},
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
if (ASPECTS.includes(t)) {
|
|
1448
|
+
const aspects = t === 'colors' ? ['text', 'background', 'border'] : [t];
|
|
1449
|
+
const results = aspects.flatMap(a => wholeThemeTokens(ctx, a));
|
|
1450
|
+
const dial = /** @type {Record<string, string>} */ ({
|
|
1451
|
+
corners: 'corners',
|
|
1452
|
+
spacing: 'spacing',
|
|
1453
|
+
font: 'font',
|
|
1454
|
+
size: 'text-size',
|
|
1455
|
+
})[t];
|
|
1456
|
+
if (t === 'colors' || t === 'text' || t === 'background')
|
|
1457
|
+
results.unshift({
|
|
1458
|
+
text: DIALS.accent.help,
|
|
1459
|
+
commands: DIALS.accent.examples.map(e => `${P} ${e}`),
|
|
1460
|
+
});
|
|
1461
|
+
if (dial)
|
|
1462
|
+
results.unshift({
|
|
1463
|
+
text: DIALS[dial].help,
|
|
1464
|
+
commands: DIALS[dial].examples.map(e => `${P} ${e}`),
|
|
1465
|
+
});
|
|
1466
|
+
results.push({
|
|
1467
|
+
text: `for one part: ${HOW} <part> lists what it reads for ${t}`,
|
|
1468
|
+
commands: [`${HOW} button`],
|
|
1469
|
+
});
|
|
1470
|
+
return {
|
|
1471
|
+
type: 'themeEdit.how',
|
|
1472
|
+
data: {kind: 'aspect', subject: t, results, lines: []},
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
if (DIALS[t]) {
|
|
1476
|
+
const d = DIALS[t];
|
|
1477
|
+
return {
|
|
1478
|
+
type: 'themeEdit.how',
|
|
1479
|
+
data: {
|
|
1480
|
+
kind: 'setting',
|
|
1481
|
+
subject: t,
|
|
1482
|
+
source: 'dial',
|
|
1483
|
+
values: d.values,
|
|
1484
|
+
lines: [
|
|
1485
|
+
`${t} (dial): ${d.values}`,
|
|
1486
|
+
` ${d.help}`,
|
|
1487
|
+
...d.examples.map(e => ` e.g. ${P} ${e}`),
|
|
1488
|
+
],
|
|
1489
|
+
},
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1492
|
+
if (t.startsWith('--') && t in ctx.project.core.tokenDefaults) {
|
|
1493
|
+
const readers = C.anyReader(t);
|
|
1494
|
+
const type = C.typeOf(t);
|
|
1495
|
+
return {
|
|
1496
|
+
type: 'themeEdit.how',
|
|
1497
|
+
data: {
|
|
1498
|
+
kind: 'setting',
|
|
1499
|
+
subject: t,
|
|
1500
|
+
source: 'token',
|
|
1501
|
+
tokenType: type,
|
|
1502
|
+
default: String(ctx.project.core.tokenDefaults[t]),
|
|
1503
|
+
readBy: readers,
|
|
1504
|
+
lines: [
|
|
1505
|
+
`${t} (token): a ${type}, default ${ctx.project.core.tokenDefaults[t]}${MODAL.has(type) ? '; may differ in light and dark (--mode)' : '; the same in light and dark'}`,
|
|
1506
|
+
` read by ${readers.length} part${readers.length === 1 ? '' : 's'}${readers.length ? `: ${readers.slice(0, 12).join(', ')}${readers.length > 12 ? ', ...' : ''}` : ' (setting it changes nothing)'}`,
|
|
1507
|
+
` e.g. ${P} set <theme> ${t} <value>${readers[0] ? ` or only inside one part: --on ${readers[0]}` : ''}`,
|
|
1508
|
+
],
|
|
1509
|
+
},
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
if (CSS_PROPERTIES.has(t)) {
|
|
1513
|
+
const setters = Object.keys(C.MAP).filter(p => t in C.MAP[p].own);
|
|
1514
|
+
return {
|
|
1515
|
+
type: 'themeEdit.how',
|
|
1516
|
+
data: {
|
|
1517
|
+
kind: 'setting',
|
|
1518
|
+
subject: t,
|
|
1519
|
+
source: 'property',
|
|
1520
|
+
setBy: setters,
|
|
1521
|
+
lines: [
|
|
1522
|
+
`${t} (CSS property): set it on a part or a family with --on; any CSS value works.`,
|
|
1523
|
+
` parts that set it now: ${setters.length ? `${setters.slice(0, 12).join(', ')}${setters.length > 12 ? ', ...' : ''}` : 'none (it still applies when you set it)'}`,
|
|
1524
|
+
` e.g. ${P} set <theme> ${t} <value> --on ${setters[0] ?? '<part>'}`,
|
|
1525
|
+
],
|
|
1526
|
+
},
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
1529
|
+
const h = answerHow(ctx, t);
|
|
1530
|
+
return {
|
|
1531
|
+
type: 'themeEdit.how',
|
|
1532
|
+
data: {
|
|
1533
|
+
kind: 'question',
|
|
1534
|
+
subject: t,
|
|
1535
|
+
understood: {parts: h.parts, aspects: h.aspects},
|
|
1536
|
+
results: h.results,
|
|
1537
|
+
lines: [],
|
|
1538
|
+
},
|
|
1539
|
+
};
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
/**
|
|
1543
|
+
* The items of `how --list`.
|
|
1544
|
+
* @param {string} kind
|
|
1545
|
+
* @param {string} filter
|
|
1546
|
+
* @param {string} cwd
|
|
1547
|
+
* @returns {Promise<{list: string, items: Array<Record<string, unknown>>, common?: boolean}>}
|
|
1548
|
+
*/
|
|
1549
|
+
async function listItems(kind, filter, cwd) {
|
|
1550
|
+
if (!HOW_LISTS.includes(kind))
|
|
1551
|
+
refuse(
|
|
1552
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1553
|
+
`--list takes ${HOW_LISTS.join(', ')}${kind ? `, not "${kind}"` : ''}.`,
|
|
1554
|
+
{
|
|
1555
|
+
didYouMean: kind ? nearest(kind, HOW_LISTS) : [],
|
|
1556
|
+
run: [`${HOW} --list dials`],
|
|
1557
|
+
},
|
|
1558
|
+
);
|
|
1559
|
+
const f = (/** @type {string} */ x) =>
|
|
1560
|
+
x.toLowerCase().includes(filter.toLowerCase());
|
|
1561
|
+
if (kind === 'dials')
|
|
1562
|
+
return {
|
|
1563
|
+
list: kind,
|
|
1564
|
+
items: Object.entries(DIALS)
|
|
1565
|
+
.filter(([k]) => f(k))
|
|
1566
|
+
.map(([name, d]) => ({
|
|
1567
|
+
name,
|
|
1568
|
+
values: d.values,
|
|
1569
|
+
help: d.help,
|
|
1570
|
+
examples: d.examples.map(e => `${P} ${e}`),
|
|
1571
|
+
})),
|
|
1572
|
+
};
|
|
1573
|
+
if (kind === 'families')
|
|
1574
|
+
return {
|
|
1575
|
+
list: kind,
|
|
1576
|
+
items: Object.entries(FAMILIES)
|
|
1577
|
+
.filter(([k, v]) => f(k) || v.some(f))
|
|
1578
|
+
.map(([name, members]) => ({
|
|
1579
|
+
name,
|
|
1580
|
+
members,
|
|
1581
|
+
spreads: AUTO_SPREAD.includes(name),
|
|
1582
|
+
})),
|
|
1583
|
+
};
|
|
1584
|
+
if (kind === 'properties') {
|
|
1585
|
+
const props = filter
|
|
1586
|
+
? [...CSS_PROPERTIES].filter(x => f(x)).slice(0, 60)
|
|
1587
|
+
: COMMON_PROPERTIES;
|
|
1588
|
+
return {list: kind, items: props.map(name => ({name})), common: !filter};
|
|
1589
|
+
}
|
|
1590
|
+
const ctx = await context(cwd);
|
|
1591
|
+
const {C} = ctx;
|
|
1592
|
+
if (kind === 'tokens') {
|
|
1593
|
+
const toks = Object.keys(ctx.project.core.tokenDefaults).filter(k => f(k));
|
|
1594
|
+
return {
|
|
1595
|
+
list: kind,
|
|
1596
|
+
items: toks.map(k => ({
|
|
1597
|
+
name: k,
|
|
1598
|
+
type: C.typeOf(k),
|
|
1599
|
+
default: String(ctx.project.core.tokenDefaults[k]),
|
|
1600
|
+
readBy: C.anyReader(k).length,
|
|
1601
|
+
})),
|
|
1602
|
+
};
|
|
1603
|
+
}
|
|
1604
|
+
const targets = ctx.project.targets.filter(x => f(x.key) || f(x.component));
|
|
1605
|
+
return {
|
|
1606
|
+
list: kind,
|
|
1607
|
+
items: targets.map(x => ({
|
|
1608
|
+
name: x.key,
|
|
1609
|
+
component: x.component,
|
|
1610
|
+
family: familyOf(x.key),
|
|
1611
|
+
variants: C.variantValues(x.key),
|
|
1612
|
+
states: x.states,
|
|
1613
|
+
recorded: Boolean(C.MAP[x.key]),
|
|
1614
|
+
})),
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
/**
|
|
1619
|
+
* Read `--at` points: "x,y;x,y", "x,y x,y", repeated, or a flat "a,b,c,d".
|
|
1620
|
+
* @param {string[]} at
|
|
1621
|
+
*/
|
|
1622
|
+
export function parsePoints(at) {
|
|
1623
|
+
const nums = at
|
|
1624
|
+
.join(' ')
|
|
1625
|
+
.split(/[^\d]+/)
|
|
1626
|
+
.filter(Boolean)
|
|
1627
|
+
.map(Number);
|
|
1628
|
+
if (nums.length % 2)
|
|
1629
|
+
refuse(
|
|
1630
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1631
|
+
`--at takes x,y pairs; "${at.join(' ')}" has an odd count of numbers.`,
|
|
1632
|
+
{run: [`${HOW} picture.png --at "40,20;300,180"`]},
|
|
1633
|
+
);
|
|
1634
|
+
/** @type {Array<{x: number, y: number}>} */
|
|
1635
|
+
const points = [];
|
|
1636
|
+
for (let i = 0; i < nums.length; i += 2)
|
|
1637
|
+
points.push({x: nums[i], y: nums[i + 1]});
|
|
1638
|
+
return points;
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
/**
|
|
1642
|
+
* The main colors of a PNG picture, or the exact color at points.
|
|
1643
|
+
* @param {string} file
|
|
1644
|
+
* @param {string} cwd
|
|
1645
|
+
* @param {string[]} at
|
|
1646
|
+
* @param {number | undefined} count
|
|
1647
|
+
*/
|
|
1648
|
+
function pictureColors(file, cwd, at, count = 14) {
|
|
1649
|
+
const abs = path.resolve(cwd, file);
|
|
1650
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isFile())
|
|
1651
|
+
throw new AstryxError(`${file} does not exist.`, [], E.ERR_FILE_NOT_FOUND);
|
|
1652
|
+
if (!Number.isInteger(count) || count <= 0)
|
|
1653
|
+
refuse(
|
|
1654
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1655
|
+
`The count must be a positive whole number, not "${count}".`,
|
|
1656
|
+
);
|
|
1657
|
+
/** @type {import('./_png.mjs').Picture} */
|
|
1658
|
+
let pic;
|
|
1659
|
+
try {
|
|
1660
|
+
pic = decodePng(fs.readFileSync(abs));
|
|
1661
|
+
} catch (e) {
|
|
1662
|
+
throw new AstryxError(
|
|
1663
|
+
`Could not read ${file} as a PNG picture: ${e instanceof Error ? e.message : String(e)}.`,
|
|
1664
|
+
[],
|
|
1665
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1666
|
+
);
|
|
1667
|
+
}
|
|
1668
|
+
if (at.length)
|
|
1669
|
+
return {
|
|
1670
|
+
file,
|
|
1671
|
+
width: pic.width,
|
|
1672
|
+
height: pic.height,
|
|
1673
|
+
colors: null,
|
|
1674
|
+
points: colorsAt(pic, parsePoints(at)),
|
|
1675
|
+
};
|
|
1676
|
+
return {
|
|
1677
|
+
file,
|
|
1678
|
+
width: pic.width,
|
|
1679
|
+
height: pic.height,
|
|
1680
|
+
colors: mainColors(pic, count),
|
|
1681
|
+
points: null,
|
|
1682
|
+
};
|
|
1683
|
+
}
|