@astryxdesign/cli 0.7.18-canary.gfca8508 → 0.7.18
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/CHANGELOG.md +185 -19
- package/README.md +19 -19
- package/api/component/_adapter.d.mts +2 -1
- package/api/component/_adapter.mjs +2 -1
- package/api/docs/_adapter.d.mts +6 -5
- package/api/docs/_adapter.mjs +36 -5
- package/api/doctor/_adapter.d.mts +6 -3
- package/api/doctor/_adapter.mjs +13 -5
- package/api/doctor/doctor.mjs +2 -2
- package/api/doctor/font-packages.d.mts +36 -0
- package/api/doctor/font-packages.mjs +124 -0
- package/api/doctor/theme-checks.mjs +110 -49
- package/api/experimental.d.mts +0 -2
- package/api/experimental.mjs +0 -12
- package/api/icon/_adapter.d.mts +1 -1
- package/api/icon/_adapter.mjs +1 -0
- package/api/icon/_authoring.mjs +17 -8
- package/api/icon/_default-look.mjs +15 -15
- package/api/icon/add/add.mjs +9 -4
- package/api/icon/exportIconIndex.doc.mjs +1 -1
- package/api/icon/icon.doc.mjs +1 -1
- package/api/icon/icon.type.d.mts +33 -33
- package/api/icon/icon.type.mjs +45 -45
- package/api/icon/iconAdd.doc.mjs +1 -1
- package/api/icon/iconNewSet.doc.mjs +3 -3
- package/api/icon/iconUse.doc.mjs +5 -5
- package/api/icon/rankIconIndex.doc.mjs +1 -1
- package/api/icon/use/use.mjs +4 -4
- package/api/init/run/run.mjs +0 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/render/_adapter.mjs +7 -1
- package/api/search/ORDERING.md +1 -1
- package/api/search/_candidate-score.mjs +20 -0
- package/api/search/_words.d.mts +8 -0
- package/api/search/_words.mjs +15 -0
- package/api/template/template.type.d.mts +2 -2
- package/api/template/template.type.mjs +2 -2
- package/api/theme/_adapter.mjs +19 -11
- package/api/theme/build/build.d.mts +19 -51
- package/api/theme/build/build.mjs +195 -189
- package/api/theme/build/font-warning.d.mts +19 -2
- package/api/theme/build/font-warning.mjs +46 -5
- package/api/theme/generateTonalPalette.doc.mjs +1 -1
- package/api/theme/palette/generate/derive-look.mjs +108 -37
- package/api/theme/palette/generate/mapping.mjs +2 -0
- package/api/theme/template/template.d.mts +0 -17
- package/api/theme/template/template.mjs +1 -42
- package/api/theme/theme.type.d.mts +5 -4
- package/api/theme/theme.type.mjs +5 -4
- package/api/theme/themePaletteGenerate.doc.mjs +1 -1
- package/api/theme/themeRemove.doc.mjs +1 -1
- package/assets/codemods/transforms/v0.7.0/preserve-bottom-sheet-content-padding.mjs +3 -1
- package/assets/component-list.generated.json +1 -1
- package/assets/discover/official-packages.json +26 -2
- package/assets/docs/author-a-theme.doc.mjs +6 -5
- package/assets/docs/browser-support.doc.mjs +1 -0
- package/assets/docs/icons.doc.mjs +8 -7
- package/assets/docs/motion.doc.mjs +46 -0
- package/assets/docs/theme.doc.dense.mjs +6 -17
- package/assets/docs/theme.doc.mjs +6 -16
- package/assets/docs/theme.doc.zh.mjs +6 -16
- package/assets/docs/tree/ci-and-containers.doc.mjs +1 -1
- package/assets/docs/tree/font-setup.doc.mjs +30 -0
- package/assets/docs/tree/icon-sets.doc.mjs +1 -1
- package/assets/docs/tree/template-icons.doc.mjs +3 -3
- package/assets/docs/tree/tokens-color.doc.mjs +5 -0
- package/assets/docs/tree/tokens.doc.mjs +4 -3
- package/assets/docs/tree/updates.doc.mjs +1 -1
- package/assets/docs-routes.generated.json +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/icons/definitions/drag.doc.mjs +1 -0
- package/assets/search-catalog.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardElevated.doc.mjs +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardElevated.tsx +20 -10
- package/assets/templates/blocks/components/Card/SelectableCardShowcase.doc.mjs +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardShowcase.tsx +36 -16
- package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMentionTrigger.tsx +6 -0
- package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMultipleTriggers.tsx +6 -0
- package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.tsx +63 -0
- package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.doc.mjs +29 -0
- package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.tsx +237 -0
- package/assets/templates/blocks/components/SideNav/SideNavShowcase.tsx +10 -72
- package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.doc.mjs +1 -1
- package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.tsx +7 -7
- package/assets/templates/blocks/components/Table/TableCustomRowTable.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/TableCustomRowTable.tsx +96 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat-landing/page.tsx +4 -0
- package/assets/templates/pages/dashboard-scorecard/page.tsx +26 -7
- package/assets/templates/pages/editor/page.tsx +13 -3
- package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/butter/butterTheme.ts +12 -10
- package/assets/templates/themes/butter/icons.tsx +2 -0
- package/assets/templates/themes/chocolate/chocolateTheme.ts +1 -0
- package/assets/templates/themes/chocolate/icons.tsx +2 -0
- package/assets/templates/themes/gothic/gothicPaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/gothic/gothicTheme.ts +1 -0
- package/assets/templates/themes/gothic/icons.tsx +2 -0
- package/assets/templates/themes/matcha/icons.tsx +2 -0
- package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/matcha/matchaTheme.ts +1 -0
- package/assets/templates/themes/neutral/icons.tsx +2 -0
- package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/neutral/neutralTheme.ts +1 -0
- package/assets/templates/themes/stone/icons.tsx +2 -0
- package/assets/templates/themes/stone/stonePaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/stone/stoneTheme.ts +1 -0
- package/assets/templates/themes/y2k/icons.tsx +2 -0
- package/assets/templates/themes/y2k/y2kTheme.ts +1 -0
- package/assets/theme.template.ts +9 -5
- package/authoring/config/parse.mjs +1 -1
- package/authoring/doctypes/base/type.ts +2 -3
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/icon-set/icon-names.mjs +1 -0
- package/authoring/integration/integration.doc.mjs +1 -1
- package/authoring/integration/type.ts +1 -1
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/experimental/icon.mjs +7 -7
- package/clients/cli/commands/experimental/index.mjs +0 -5
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +2 -2
- package/clients/cli/commands/experimental-icon-use.doc.mjs +4 -4
- package/clients/cli/commands/experimental-icon.doc.mjs +1 -1
- package/clients/cli/commands/experimental.doc.mjs +2 -6
- package/clients/cli/commands/template.mjs +1 -1
- package/clients/cli/commands/theme-build.doc.mjs +8 -1
- package/clients/cli/commands/theme-palette-generate.doc.mjs +1 -1
- package/clients/cli/commands/theme-remove.doc.mjs +1 -1
- package/clients/cli/commands/theme.doc.mjs +1 -11
- package/clients/cli/commands/theme.mjs +110 -475
- package/clients/cli/commands.generated.json +1 -1
- package/clients/cli/index.mjs +4 -34
- package/clients/cli/lib/base-gate.mjs +2 -3
- package/clients/cli/lib/cli-error.mjs +8 -72
- package/clients/cli/lib/define-command.mjs +0 -19
- package/clients/cli/lib/json-shim.mjs +11 -633
- package/clients/cli/lib/manifest.mjs +0 -4
- package/clients/cli/lib/stray-option.mjs +1 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/color/css-color.mjs +2 -3
- package/foundation/discovery/app-layout.d.mts +13 -0
- package/foundation/discovery/app-layout.mjs +35 -0
- package/foundation/discovery/fixture-refs.d.mts +51 -0
- package/foundation/discovery/fixture-refs.mjs +351 -0
- package/foundation/discovery/icon-definition-discovery.mjs +1 -1
- package/foundation/discovery/icon-ranking.mjs +2 -2
- package/foundation/discovery/icon-set-checks.mjs +1 -1
- package/foundation/discovery/icon-set-discovery.mjs +1 -1
- package/foundation/discovery/icon-set-writer.d.mts +24 -7
- package/foundation/discovery/icon-set-writer.mjs +66 -22
- package/foundation/discovery/static-descriptor.d.mts +49 -0
- package/foundation/discovery/static-descriptor.mjs +548 -0
- package/foundation/discovery/template-adapter.d.mts +0 -47
- package/foundation/discovery/template-adapter.mjs +1 -333
- package/foundation/discovery/theme-discovery.d.mts +6 -47
- package/foundation/discovery/theme-discovery.mjs +15 -537
- package/foundation/doc-compiler/import.mjs +1 -1
- package/foundation/env/user-dirs.d.mts +5 -2
- package/foundation/env/user-dirs.mjs +12 -5
- package/foundation/fonts/next-font.d.mts +26 -0
- package/foundation/fonts/next-font.mjs +149 -0
- package/foundation/fs/empty-folder.mjs +2 -2
- package/foundation/fs/read-record.mjs +11 -4
- package/foundation/pictures/pictures.d.mts +1 -45
- package/foundation/pictures/pictures.mjs +8 -8
- package/foundation/release/folder-lock.mjs +3 -2
- package/foundation/render/cut-short.d.mts +2 -2
- package/foundation/render/cut-short.mjs +2 -2
- package/foundation/render/drawn-contrast.d.mts +2 -2
- package/foundation/render/drawn-contrast.mjs +2 -2
- package/foundation/render/font-probe.d.mts +2 -2
- package/foundation/render/font-probe.mjs +2 -2
- package/foundation/render/layers.mjs +1 -2
- package/foundation/render/package-resolve-hooks.d.mts +25 -5
- package/foundation/render/package-resolve-hooks.mjs +77 -14
- package/foundation/render/page-worker.mjs +24 -12
- package/foundation/render/part-boxes.d.mts +2 -3
- package/foundation/render/part-boxes.mjs +2 -3
- package/foundation/render/picture-chrome.mjs +5 -0
- package/foundation/render/render-page.d.mts +1 -1
- package/foundation/render/render-page.mjs +4 -5
- package/foundation/render/single-copy-hooks.d.mts +2 -0
- package/foundation/render/single-copy-hooks.mjs +2 -0
- package/foundation/response/error-codes.doc.mjs +14 -14
- package/foundation/response/error-codes.mjs +13 -13
- package/foundation/response/response-types.doc.mjs +7 -68
- package/foundation/runtime/css-tree.mjs +2 -2
- package/foundation/runtime/word-table.d.mts +1 -1
- package/foundation/runtime/word-table.mjs +1 -1
- package/foundation/suggest/other-command.d.mts +52 -15
- package/foundation/suggest/other-command.mjs +168 -45
- package/foundation/text/suggest-order.d.mts +7 -0
- package/foundation/text/suggest-order.mjs +1 -1
- package/package.json +7 -8
- package/api/theme-edit/_adapter.d.mts +0 -228
- package/api/theme-edit/_adapter.mjs +0 -2025
- package/api/theme-edit/_catalog.d.mts +0 -172
- package/api/theme-edit/_catalog.mjs +0 -806
- package/api/theme-edit/_check.d.mts +0 -391
- package/api/theme-edit/_check.mjs +0 -9370
- package/api/theme-edit/_color.d.mts +0 -135
- package/api/theme-edit/_color.mjs +0 -480
- package/api/theme-edit/_compiler.d.mts +0 -77
- package/api/theme-edit/_compiler.mjs +0 -1053
- package/api/theme-edit/_context.d.mts +0 -399
- package/api/theme-edit/_context.mjs +0 -1778
- package/api/theme-edit/_css.d.mts +0 -169
- package/api/theme-edit/_css.mjs +0 -1167
- package/api/theme-edit/_edit.d.mts +0 -167
- package/api/theme-edit/_edit.mjs +0 -11220
- package/api/theme-edit/_how-commands.d.mts +0 -315
- package/api/theme-edit/_how-commands.mjs +0 -1664
- package/api/theme-edit/_how-question.d.mts +0 -341
- package/api/theme-edit/_how-question.mjs +0 -33339
- package/api/theme-edit/_how-sides.d.mts +0 -22
- package/api/theme-edit/_how-sides.mjs +0 -173
- package/api/theme-edit/_how-steps.d.mts +0 -42
- package/api/theme-edit/_how-steps.mjs +0 -208
- package/api/theme-edit/_how-values.d.mts +0 -29
- package/api/theme-edit/_how-values.mjs +0 -111
- package/api/theme-edit/_how-words.d.mts +0 -15
- package/api/theme-edit/_how-words.mjs +0 -239
- package/api/theme-edit/_how.d.mts +0 -55
- package/api/theme-edit/_how.mjs +0 -2388
- package/api/theme-edit/_importers.d.mts +0 -38
- package/api/theme-edit/_importers.mjs +0 -188
- package/api/theme-edit/_marks.d.mts +0 -39
- package/api/theme-edit/_marks.mjs +0 -67
- package/api/theme-edit/_missed.d.mts +0 -56
- package/api/theme-edit/_missed.mjs +0 -1128
- package/api/theme-edit/_name-rule.d.mts +0 -11
- package/api/theme-edit/_name-rule.mjs +0 -12
- package/api/theme-edit/_normal.d.mts +0 -365
- package/api/theme-edit/_normal.mjs +0 -1978
- package/api/theme-edit/_overview.d.mts +0 -9
- package/api/theme-edit/_overview.mjs +0 -293
- package/api/theme-edit/_palette.d.mts +0 -616
- package/api/theme-edit/_palette.mjs +0 -2776
- package/api/theme-edit/_part-pieces.d.mts +0 -54
- package/api/theme-edit/_part-pieces.mjs +0 -179
- package/api/theme-edit/_part-words.d.mts +0 -39
- package/api/theme-edit/_part-words.mjs +0 -242
- package/api/theme-edit/_parts.d.mts +0 -402
- package/api/theme-edit/_parts.mjs +0 -2322
- package/api/theme-edit/_picture-traits.d.mts +0 -259
- package/api/theme-edit/_picture-traits.mjs +0 -1030
- package/api/theme-edit/_png.d.mts +0 -94
- package/api/theme-edit/_png.mjs +0 -479
- package/api/theme-edit/_preview-app.d.mts +0 -172
- package/api/theme-edit/_preview-app.mjs +0 -6430
- package/api/theme-edit/_preview.d.mts +0 -484
- package/api/theme-edit/_preview.mjs +0 -2775
- package/api/theme-edit/_project.d.mts +0 -207
- package/api/theme-edit/_project.mjs +0 -643
- package/api/theme-edit/_resolved.d.mts +0 -46
- package/api/theme-edit/_resolved.mjs +0 -800
- package/api/theme-edit/_set-line.d.mts +0 -29
- package/api/theme-edit/_set-line.mjs +0 -100
- package/api/theme-edit/_settings.d.mts +0 -2426
- package/api/theme-edit/_settings.mjs +0 -18449
- package/api/theme-edit/_tables.d.mts +0 -227
- package/api/theme-edit/_tables.mjs +0 -1128
- package/api/theme-edit/_text-looks.d.mts +0 -78
- package/api/theme-edit/_text-looks.mjs +0 -376
- package/api/theme-edit/_vet.d.mts +0 -50
- package/api/theme-edit/_vet.mjs +0 -166
- package/api/theme-edit/how/how.d.mts +0 -17
- package/api/theme-edit/how/how.mjs +0 -28
- package/api/theme-edit/new/new.d.mts +0 -11
- package/api/theme-edit/new/new.mjs +0 -22
- package/api/theme-edit/preview-add/preview-add.d.mts +0 -14
- package/api/theme-edit/preview-add/preview-add.mjs +0 -25
- package/api/theme-edit/preview-app/preview-app.d.mts +0 -13
- package/api/theme-edit/preview-app/preview-app.mjs +0 -24
- package/api/theme-edit/preview-list/preview-list.d.mts +0 -10
- package/api/theme-edit/preview-list/preview-list.mjs +0 -21
- package/api/theme-edit/preview-remove/preview-remove.d.mts +0 -12
- package/api/theme-edit/preview-remove/preview-remove.mjs +0 -23
- package/api/theme-edit/set/set.d.mts +0 -13
- package/api/theme-edit/set/set.mjs +0 -24
- package/api/theme-edit/show/show.d.mts +0 -13
- package/api/theme-edit/show/show.mjs +0 -24
- package/api/theme-edit/theme-edit.d.mts +0 -12
- package/api/theme-edit/theme-edit.mjs +0 -36
- package/api/theme-edit/theme-edit.type.d.mts +0 -730
- package/api/theme-edit/theme-edit.type.mjs +0 -342
- package/api/theme-edit/themeEditHow.doc.d.mts +0 -6
- package/api/theme-edit/themeEditHow.doc.mjs +0 -105
- package/api/theme-edit/themeEditNew.doc.d.mts +0 -5
- package/api/theme-edit/themeEditNew.doc.mjs +0 -112
- package/api/theme-edit/themeEditPreviewAdd.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +0 -121
- package/api/theme-edit/themeEditPreviewApp.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +0 -87
- package/api/theme-edit/themeEditPreviewList.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewList.doc.mjs +0 -62
- package/api/theme-edit/themeEditPreviewRemove.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +0 -96
- package/api/theme-edit/themeEditSet.doc.d.mts +0 -6
- package/api/theme-edit/themeEditSet.doc.mjs +0 -175
- package/api/theme-edit/themeEditShow.doc.d.mts +0 -6
- package/api/theme-edit/themeEditShow.doc.mjs +0 -118
- package/api/theme-edit/themeEditUndo.doc.d.mts +0 -6
- package/api/theme-edit/themeEditUndo.doc.mjs +0 -133
- package/api/theme-edit/undo/undo.d.mts +0 -13
- package/api/theme-edit/undo/undo.mjs +0 -24
- package/assets/preview-app/README.md +0 -6
- package/assets/preview-app/css.d.ts +0 -5
- package/assets/preview-app/gitignore +0 -4
- package/assets/preview-app/next.config.mjs +0 -12
- package/assets/preview-app/package.json +0 -24
- package/assets/preview-app/src/app/globals.css +0 -6
- package/assets/preview-app/src/app/layout.tsx +0 -21
- package/assets/preview-app/src/app/page.tsx +0 -94
- package/assets/preview-app/src/app/providers.tsx +0 -16
- package/assets/preview-app/tsconfig.json +0 -27
- package/clients/cli/commands/experimental/theme.mjs +0 -2990
- package/clients/cli/commands/experimental-theme-add.doc.mjs +0 -80
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +0 -98
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +0 -196
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +0 -174
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +0 -139
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +0 -77
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +0 -105
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +0 -49
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +0 -72
- package/clients/cli/commands/experimental-theme-list.doc.mjs +0 -49
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +0 -88
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +0 -61
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +0 -48
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +0 -59
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +0 -41
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +0 -57
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +0 -62
- package/clients/cli/commands/experimental-theme-template.doc.mjs +0 -63
- package/clients/cli/commands/experimental-theme-use.doc.mjs +0 -57
- package/clients/cli/commands/experimental-theme.doc.mjs +0 -60
- package/foundation/fonts/fontsource-families.generated.d.mts +0 -5
- package/foundation/fonts/fontsource-families.generated.mjs +0 -2110
|
@@ -1,2776 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file The palette behind the color dials of `astryx experimental theme`:
|
|
5
|
-
* each color dial is one family in the theme's palette.config.json, grown by
|
|
6
|
-
* the palette generator (the same function as `theme palette generate`).
|
|
7
|
-
* Every color decision is generator config: the seed's exact color is an
|
|
8
|
-
* anchor at its stop, in dark too when it reads there. The theme's color
|
|
9
|
-
* tokens point at stops as the official themes map them, and contrast moves
|
|
10
|
-
* a token to a nearer stop that passes; it never makes a new color. The
|
|
11
|
-
* theme imports only the stops it uses. `vibrancy` is the generator's
|
|
12
|
-
* `vibrancy`. Users see "colors" and "shades": the words palette, family and
|
|
13
|
-
* stop stay in the code.
|
|
14
|
-
*
|
|
15
|
-
* @position api/theme-edit — imported by _settings.mjs, _check.mjs, _how.mjs
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import * as fs from 'node:fs';
|
|
19
|
-
import * as path from 'node:path';
|
|
20
|
-
import {
|
|
21
|
-
irregularFileKind,
|
|
22
|
-
readRegularFile,
|
|
23
|
-
} from '../../foundation/fs/regular-file.mjs';
|
|
24
|
-
import {isAlive} from '../../foundation/release/cache.mjs';
|
|
25
|
-
import {stillHolds} from '../../foundation/release/folder-lock.mjs';
|
|
26
|
-
import {AstryxError} from '../error.mjs';
|
|
27
|
-
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
28
|
-
import {
|
|
29
|
-
ANCHOR_INPUT_KEYS,
|
|
30
|
-
DEFAULT_21_STOPS,
|
|
31
|
-
FAMILY_INPUT_KEYS,
|
|
32
|
-
GENERATION_INPUT_KEYS,
|
|
33
|
-
PALETTE_BLACK,
|
|
34
|
-
PALETTE_GENERATOR_VERSION,
|
|
35
|
-
PALETTE_RECIPE,
|
|
36
|
-
PALETTE_RECIPE_V2,
|
|
37
|
-
PALETTE_RECIPES,
|
|
38
|
-
PALETTE_WHITE,
|
|
39
|
-
generateTonalPalette,
|
|
40
|
-
modesForStrategy,
|
|
41
|
-
} from '../theme/palette/generate/generator.mjs';
|
|
42
|
-
import {themePaletteGenerate} from '../theme/palette/generate/generate.mjs';
|
|
43
|
-
import {
|
|
44
|
-
ACCENT_MUTED,
|
|
45
|
-
BORDER,
|
|
46
|
-
DARK_FILL,
|
|
47
|
-
LIFT_GROUNDS,
|
|
48
|
-
LIFT_PARTS,
|
|
49
|
-
NEUTRAL_INSIDE,
|
|
50
|
-
NEUTRAL_STOPS,
|
|
51
|
-
NEUTRAL_TINTS,
|
|
52
|
-
NON_TEXT,
|
|
53
|
-
ON_MUTED,
|
|
54
|
-
PALETTE_DIALS,
|
|
55
|
-
STATUS_MUTED,
|
|
56
|
-
STATUS_TOKEN,
|
|
57
|
-
TEXT,
|
|
58
|
-
TEXT_STOP,
|
|
59
|
-
VIBRANCY_DEFAULT,
|
|
60
|
-
anchorDarkFills,
|
|
61
|
-
dialFamily,
|
|
62
|
-
fillAt,
|
|
63
|
-
grownGrays,
|
|
64
|
-
isGray,
|
|
65
|
-
isLookToken,
|
|
66
|
-
mapPalette,
|
|
67
|
-
nearestShade,
|
|
68
|
-
paletteToken,
|
|
69
|
-
sameColor,
|
|
70
|
-
shade,
|
|
71
|
-
} from '../theme/palette/generate/mapping.mjs';
|
|
72
|
-
import * as editorLook from '../theme/palette/generate/editor-look.mjs';
|
|
73
|
-
import {
|
|
74
|
-
cvdClashes,
|
|
75
|
-
isLookFamily,
|
|
76
|
-
} from '../theme/palette/generate/derive-look.mjs';
|
|
77
|
-
/**
|
|
78
|
-
* How the page-wide tokens a theme's own source sets are read: given by
|
|
79
|
-
* _settings (which imports this module) when it loads.
|
|
80
|
-
* @type {(cwd: string, slug: string) => Record<string, string>}
|
|
81
|
-
*/
|
|
82
|
-
let ownSourceTokenValues = () => ({});
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Give the reader of the tokens a theme's own source sets.
|
|
86
|
-
* @param {(cwd: string, slug: string) => Record<string, string>} read
|
|
87
|
-
*/
|
|
88
|
-
export function setOwnTokenReader(read) {
|
|
89
|
-
ownSourceTokenValues = read;
|
|
90
|
-
}
|
|
91
|
-
import {alphaOf, hex, isClipped, parseColor} from './_color.mjs';
|
|
92
|
-
import {
|
|
93
|
-
contrastRatio,
|
|
94
|
-
hexToOklch,
|
|
95
|
-
oklchClampedHex,
|
|
96
|
-
} from '../theme/palette/generate/color.mjs';
|
|
97
|
-
import {splitTop} from './_css.mjs';
|
|
98
|
-
import {isColorToken} from './_compiler.mjs';
|
|
99
|
-
import {P} from './_tables.mjs';
|
|
100
|
-
import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
|
|
101
|
-
import {sha256} from '../../foundation/render/source-block.mjs';
|
|
102
|
-
import {cliVersion} from '../../foundation/release/run-context.mjs';
|
|
103
|
-
import {
|
|
104
|
-
newerRecipe,
|
|
105
|
-
newerRecipeRefusal,
|
|
106
|
-
} from '../theme/palette/generate/newer-recipe.mjs';
|
|
107
|
-
|
|
108
|
-
export {readRegularFile};
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Write a file whole: a temp file beside it, renamed into place, so a link
|
|
112
|
-
* there is replaced, never written through, and a stopped write leaves the
|
|
113
|
-
* old file. The temp file goes whatever happens.
|
|
114
|
-
* @param {string} file
|
|
115
|
-
* @param {string | Buffer} text
|
|
116
|
-
*/
|
|
117
|
-
export function writeWhole(file, text) {
|
|
118
|
-
// A folder where a file goes (a folder named preview.html) is never
|
|
119
|
-
// written through or replaced.
|
|
120
|
-
if (fs.lstatSync(file, {throwIfNoEntry: false})?.isDirectory())
|
|
121
|
-
throw new AstryxError(
|
|
122
|
-
`${[path.dirname(path.dirname(file)), path.dirname(file), file].map(p => path.basename(p)).join('/')} is a folder, so the editor cannot write that file there. Rename or move the folder, then run the command again.`,
|
|
123
|
-
[
|
|
124
|
-
{
|
|
125
|
-
name: `ls -la ${quoteCommandArgument(file)}`,
|
|
126
|
-
reason:
|
|
127
|
-
'see what is in the folder; move or rename it, then run the same command again',
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
ERROR_CODES.ERR_WRITE_FAILED,
|
|
131
|
-
);
|
|
132
|
-
// A pipe, a device or a socket there is not the editor's: the rename
|
|
133
|
-
// below would replace it, so it is refused and left as it is.
|
|
134
|
-
const kind = irregularFileKind(file);
|
|
135
|
-
if (kind && kind !== 'a folder')
|
|
136
|
-
throw new AstryxError(
|
|
137
|
-
`${[path.dirname(path.dirname(file)), path.dirname(file), file].map(p => path.basename(p)).join('/')} is ${kind}, not a file, so the editor writes nothing over it. Move it, then run the command again.`,
|
|
138
|
-
[],
|
|
139
|
-
ERROR_CODES.ERR_WRITE_FAILED,
|
|
140
|
-
);
|
|
141
|
-
// The same bytes already there are not written again: a refused command
|
|
142
|
-
// that puts back what it built leaves the file as it was, time included.
|
|
143
|
-
if (sameBytes(file, text)) return;
|
|
144
|
-
sweepTemps(path.dirname(file));
|
|
145
|
-
refuseIfLockLost();
|
|
146
|
-
const tmp = tempFor(file);
|
|
147
|
-
try {
|
|
148
|
-
fs.writeFileSync(tmp, text);
|
|
149
|
-
fs.renameSync(tmp, file);
|
|
150
|
-
} finally {
|
|
151
|
-
fs.rmSync(tmp, {force: true});
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Whether a regular file (never a link) at `file` holds exactly `text`.
|
|
157
|
-
* @param {string} file
|
|
158
|
-
* @param {string | Buffer} text
|
|
159
|
-
*/
|
|
160
|
-
function sameBytes(file, text) {
|
|
161
|
-
const st = fs.lstatSync(file, {throwIfNoEntry: false});
|
|
162
|
-
if (!st?.isFile()) return false;
|
|
163
|
-
const want = Buffer.isBuffer(text) ? text : Buffer.from(text);
|
|
164
|
-
if (st.size !== want.length) return false;
|
|
165
|
-
try {
|
|
166
|
-
return readRegularFile(file).equals(want);
|
|
167
|
-
} catch {
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* A command stopped long enough for another to take its lock (Ctrl-Z, a
|
|
174
|
-
* sleeping computer, a debugger) writes nothing more: refuse. Called right
|
|
175
|
-
* before each write.
|
|
176
|
-
*/
|
|
177
|
-
export function refuseIfLockLost() {
|
|
178
|
-
if (lockHeld()) return;
|
|
179
|
-
const error = new AstryxError(
|
|
180
|
-
`Another command took over the theme's lock while this one was stopped (Ctrl-Z, a sleeping computer, a debugger). This command's change was not applied, and it touched nothing more: the files stay as the other command left them. Run it again if you still want it.`,
|
|
181
|
-
[{name: `${P} how --list dials`, reason: 'what you can set'}],
|
|
182
|
-
ERROR_CODES.ERR_THEME_BUSY,
|
|
183
|
-
);
|
|
184
|
-
/** @type {any} */ (error).lockLost = true;
|
|
185
|
-
throw error;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** Whether this command still holds its lock (true outside a lock). */
|
|
189
|
-
export function lockHeld() {
|
|
190
|
-
try {
|
|
191
|
-
stillHolds();
|
|
192
|
-
return true;
|
|
193
|
-
} catch {
|
|
194
|
-
return false;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/** When this process started, for telling its own temp files from an earlier one's with the same id. */
|
|
199
|
-
const STARTED_AT = Date.now() - process.uptime() * 1000;
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Remove the editor's own temp files a stopped command left in a folder:
|
|
203
|
-
* only names the editor makes (`.<name>.astryx-<pid>-<random>.tmp`) whose
|
|
204
|
-
* process is gone, or that are older than ten minutes, and a build's
|
|
205
|
-
* `.<name>.tmp-<id>` older than ten minutes. Links are never
|
|
206
|
-
* followed. A theme folder holds a few files, so every write looks.
|
|
207
|
-
* @param {string} dir
|
|
208
|
-
*/
|
|
209
|
-
function sweepTemps(dir) {
|
|
210
|
-
let names;
|
|
211
|
-
try {
|
|
212
|
-
names = fs.readdirSync(dir);
|
|
213
|
-
} catch {
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
for (const n of names) {
|
|
217
|
-
// A build's own temp (`.<name>.tmp-<id>`) names no process: one older
|
|
218
|
-
// than ten minutes is a leftover of a command stopped mid-build (a
|
|
219
|
-
// build renames it in moments).
|
|
220
|
-
if (/^\..+\.tmp-[\w-]+$/.test(n)) {
|
|
221
|
-
const p = path.join(dir, n);
|
|
222
|
-
try {
|
|
223
|
-
const st = fs.lstatSync(p);
|
|
224
|
-
if (st.isFile() && Date.now() - st.mtimeMs >= 10 * 60_000)
|
|
225
|
-
fs.rmSync(p, {force: true});
|
|
226
|
-
} catch {
|
|
227
|
-
// Gone already.
|
|
228
|
-
}
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
// A picture taken under its own name (`.<picture>.part-<pid>.png`) by a
|
|
232
|
-
// run stopped while the browser ran.
|
|
233
|
-
const m =
|
|
234
|
-
/^\..+\.astryx-(\d+)-[a-z0-9]+\.tmp$/.exec(n) ??
|
|
235
|
-
/^\.[\w-]+\.part-(\d+)\.png$/.exec(n);
|
|
236
|
-
if (!m) continue;
|
|
237
|
-
const p = path.join(dir, n);
|
|
238
|
-
try {
|
|
239
|
-
const st = fs.lstatSync(p);
|
|
240
|
-
// Writes into a theme folder run under its lock, so a temp file
|
|
241
|
-
// whose process is gone is never one a live command still renames.
|
|
242
|
-
if (!st.isFile()) continue;
|
|
243
|
-
// A process id from another process space (pid 1 in a container)
|
|
244
|
-
// may look alive here: past ten minutes, an editor temp file is a
|
|
245
|
-
// leftover whatever its id says (a live writer renames it in
|
|
246
|
-
// moments, and holds the lock this write holds now).
|
|
247
|
-
// A temp named with this process's own id but older than this process
|
|
248
|
-
// is another one's (pid 1 in each container start).
|
|
249
|
-
const own = Number(m[1]) === process.pid;
|
|
250
|
-
const young = Date.now() - st.mtimeMs < 10 * 60_000;
|
|
251
|
-
if (
|
|
252
|
-
own ? st.mtimeMs >= STARTED_AT && young : isAlive(Number(m[1])) && young
|
|
253
|
-
)
|
|
254
|
-
continue;
|
|
255
|
-
fs.rmSync(p, {force: true});
|
|
256
|
-
} catch {
|
|
257
|
-
// Gone already, or not ours to judge.
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Whether a file name is one of the editor's own temp files: a whole-file
|
|
264
|
-
* write's `.<name>.astryx-<pid>-<random>.tmp`, a build's `.<name>.tmp-<id>`,
|
|
265
|
-
* or a picture's `.<picture>.part-<pid>.png`. A command stopped part way can
|
|
266
|
-
* leave one behind.
|
|
267
|
-
* @param {string} name
|
|
268
|
-
*/
|
|
269
|
-
export const isEditorTemp = name =>
|
|
270
|
-
/^\..+\.(astryx-\d+-[a-z0-9]+\.tmp|tmp-[\w-]+)$/.test(name) ||
|
|
271
|
-
/^\.[\w-]+\.part-\d+\.png$/.test(name);
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* A temp file beside `file` for a whole write, named so only the editor
|
|
275
|
-
* makes such names: `.<name>.astryx-<pid>-<random>.tmp`. The write that
|
|
276
|
-
* makes one removes it.
|
|
277
|
-
* @param {string} file
|
|
278
|
-
*/
|
|
279
|
-
export function tempFor(file) {
|
|
280
|
-
return path.join(
|
|
281
|
-
path.dirname(file),
|
|
282
|
-
`.${path.basename(file)}.astryx-${process.pid}-${Math.random().toString(36).slice(2, 10)}.tmp`,
|
|
283
|
-
);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/** @typedef {'light' | 'dark'} Mode */
|
|
287
|
-
/** @typedef {import('./_settings.mjs').Setting} Setting */
|
|
288
|
-
/** @typedef {import('../theme/theme.type.mjs').TonalPaletteCandidate} Candidate */
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* One token pointed at a palette stop in each mode.
|
|
292
|
-
* @typedef {object} StopRef
|
|
293
|
-
* @property {number} stop
|
|
294
|
-
* @property {string} color - The stop's color, with `alpha` appended when set.
|
|
295
|
-
* @property {string} [alpha] - Two hex digits of opacity.
|
|
296
|
-
* @property {string} [family] - The family the stop is from when it is not the token's own: `neutral` for text on a fill, or `black` / `white`, the palette's own ends.
|
|
297
|
-
* @property {boolean} [exact] - The stop holds the seed's exact color (an exact anchor).
|
|
298
|
-
* @property {boolean} [base] - The half is the base theme's own value in that mode (family `literal`): the generated module reads it from the base, so the mode follows the base theme.
|
|
299
|
-
*/
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* One contrast check behind a stop choice.
|
|
303
|
-
* @typedef {object} ContrastCheck
|
|
304
|
-
* @property {string} token
|
|
305
|
-
* @property {Mode} mode
|
|
306
|
-
* @property {number} stop
|
|
307
|
-
* @property {number} preferred - The stop the mapping starts from.
|
|
308
|
-
* @property {string} against - What it was measured on.
|
|
309
|
-
* @property {number} ratio - The lowest ratio over everything it was measured on.
|
|
310
|
-
* @property {number} needs
|
|
311
|
-
* @property {boolean} pass
|
|
312
|
-
* @property {string} [note] - The whole sentence to report, when the check is not a plain stop move.
|
|
313
|
-
* @property {string[]} [dials] - The color dials whose notes report it, when its token belongs to none of them.
|
|
314
|
-
* @property {{thing: string, value: string, mode?: Mode}} [fix] - The nearest setting that passes it, when one does: a darker exact color for a pale fill.
|
|
315
|
-
*/
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* A color dial's family: its seed, and the source token that gives it when
|
|
319
|
-
* the theme's own source sets the family's fill (null when the setting does).
|
|
320
|
-
* An accent set as a token carries the one mode it is for.
|
|
321
|
-
* @typedef {{id: string, dial: string, seed: string, value: string, source: string | null, only?: 'light' | 'dark'}} DialFamily
|
|
322
|
-
*/
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* The palette mapping of a theme.
|
|
326
|
-
* @typedef {object} PaletteMapping
|
|
327
|
-
* @property {Candidate} candidate
|
|
328
|
-
* @property {DialFamily[]} families
|
|
329
|
-
* @property {Record<string, {family: string, light: StopRef, dark: StopRef}>} tokens
|
|
330
|
-
* @property {ContrastCheck[]} contrast
|
|
331
|
-
* @property {boolean} [stored] - Whether the palette is kept as saved (made with a recipe this CLI does not know): its colors cannot change here.
|
|
332
|
-
* @property {number} vibrancy - The generator's vibrancy, 0 to 100.
|
|
333
|
-
* @property {Record<string, Partial<Record<Mode, string>>>} exact - Main tokens the theme sets exactly, the value per mode: the dial's fill gives way to them.
|
|
334
|
-
* @property {Record<string, string>} [unread] - Main tokens the theme sets exactly to a value the editor cannot read as one color in some mode, as written: the text on them is not picked for that value.
|
|
335
|
-
* @property {string[]} notes - What the config writer kept or replaced of a hand edit.
|
|
336
|
-
* @property {{badges: 'derived' | 'none', chart: 'derived' | 'none', limits: string[], moved: string[], alike: boolean, unread: boolean}} [categories] - Whether the category colors (Badge, Token, Card tints) and the chart colors follow the look, and what they gave up to stay written.
|
|
337
|
-
* @property {Array<{a: string, b: string, vision: 'red-weak' | 'green-weak', mode: Mode, colors: [string, string], page?: string, pages?: string[]}>} [cvd] - Status fills someone red-weak or green-weak may not tell apart.
|
|
338
|
-
* @property {string[]} [code] - The code colors that keep the base theme's because none of their family's stops reads on the code background and the page.
|
|
339
|
-
* @property {Array<{token: string, mode: Mode, pair: import('../theme/palette/generate/mapping.mjs').BasePair, ratio: number}>} [kept] - The look's colors kept at the base theme's in a mode, because a text/fill pair the base theme draws with them would not read.
|
|
340
|
-
* @property {{roles: Record<string, 'derived'>, why: string[], short: string[]}} [statuses] - The status roles the theme leaves unset, each derived from the look, and what they gave up to stay written.
|
|
341
|
-
* @property {Record<string, Partial<Record<Mode, StopRef>>> | null} lift - Lifted secondary text per background token of LIFT_PARTS (the accent tint, the neutral track), per mode, from the neutral color; null without one.
|
|
342
|
-
*/
|
|
343
|
-
|
|
344
|
-
// The color dials, the vibrancy default, each dial's main token, the lifted
|
|
345
|
-
// parts and the nearest shade of a ramp: the palette mapping's, which other
|
|
346
|
-
// editor modules read through this one.
|
|
347
|
-
export {
|
|
348
|
-
LIFT_PARTS,
|
|
349
|
-
PALETTE_DIALS,
|
|
350
|
-
VIBRANCY_DEFAULT,
|
|
351
|
-
nearestShade,
|
|
352
|
-
paletteToken,
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* The vibrancy dial a settings list sets, or null.
|
|
357
|
-
* @param {Setting[]} settings
|
|
358
|
-
* @returns {number | null}
|
|
359
|
-
*/
|
|
360
|
-
function dialVibrancy(settings) {
|
|
361
|
-
const s = [...settings]
|
|
362
|
-
.reverse()
|
|
363
|
-
.find(x => x?.source === 'dial' && x.thing === 'vibrancy');
|
|
364
|
-
// A value edited by hand that is not one (`high`, `150`) counts as none,
|
|
365
|
-
// so the command that fixes it can still build the state before it.
|
|
366
|
-
return s && /^\d{1,3}$/.test(String(s.value)) && Number(s.value) <= 100
|
|
367
|
-
? Number(s.value)
|
|
368
|
-
: null;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/** A layout needs this many stops between black and white to map every token. */
|
|
372
|
-
const MIN_INNER_STOPS = 5;
|
|
373
|
-
/** The widest gap between two stops that still maps every token near its stop. */
|
|
374
|
-
const MAX_STOP_GAP = 25;
|
|
375
|
-
|
|
376
|
-
/** @param {string} slug */
|
|
377
|
-
export function paletteFiles(slug) {
|
|
378
|
-
return {
|
|
379
|
-
config: `themes/${slug}/palette.config.json`,
|
|
380
|
-
palette: `themes/${slug}/tokens/${slug}.palette.ts`,
|
|
381
|
-
receipt: `themes/${slug}/tokens/${slug}.palette.receipt.json`,
|
|
382
|
-
refs: `themes/${slug}/tokens/${slug}.palette-refs.ts`,
|
|
383
|
-
importPath: `./tokens/${slug}.palette`,
|
|
384
|
-
refsImportPath: `./tokens/${slug}.palette-refs`,
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/** The export name of a theme's selected stops. @param {string} slug */
|
|
389
|
-
const refsName = slug =>
|
|
390
|
-
`${slug.replace(/-+([a-z0-9])?/g, (_, c) => (c ? c.toUpperCase() : ''))}PaletteRefs`;
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* The import line the generated settings use for the stops they read.
|
|
394
|
-
* @param {string} slug
|
|
395
|
-
*/
|
|
396
|
-
export const paletteImport = slug =>
|
|
397
|
-
`import {${refsName(slug)} as palette} from '${paletteFiles(slug).refsImportPath}';`;
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* The seed a color dial value grows from, or null for a value with no fixed,
|
|
401
|
-
* opaque color (a variable, a keyword, `color-mix()`, a translucent color).
|
|
402
|
-
* @param {string} value
|
|
403
|
-
*/
|
|
404
|
-
export function seedOf(value) {
|
|
405
|
-
const rgb = parseColor(value);
|
|
406
|
-
return rgb && alphaOf(value) >= 1 ? hex(rgb) : null;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/** Whether a seed (or no seed) has no color to saturate. @param {string | null} seed */
|
|
410
|
-
export const isGraySeed = seed => !seed || isGray(seed);
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Whether a color dial's seed comes out different at two vibrancies: the
|
|
414
|
-
* ramp the generator grows from it, light and dark, compared shade by
|
|
415
|
-
* shade. A seed with no saturation to change (a pure gray) gives the same
|
|
416
|
-
* ramp at every vibrancy. A gray-ish seed is grown as the editor grows it
|
|
417
|
-
* (a neutral), so a warm or cool gray counts by what it shows.
|
|
418
|
-
* @param {string} dial
|
|
419
|
-
* @param {string | null} seed
|
|
420
|
-
* @param {number} from
|
|
421
|
-
* @param {number} to
|
|
422
|
-
*/
|
|
423
|
-
export function vibrancyMoves(dial, seed, from, to) {
|
|
424
|
-
if (!seed || from === to) return false;
|
|
425
|
-
const kind = dial === 'neutral' || isGray(seed) ? 'neutral' : 'chromatic';
|
|
426
|
-
const ramp = (/** @type {number} */ vibrancy) =>
|
|
427
|
-
JSON.stringify(
|
|
428
|
-
/** @type {any} */ (
|
|
429
|
-
generate({
|
|
430
|
-
vibrancy,
|
|
431
|
-
...(kind === 'neutral' ? {neutralProfile: 'custom'} : {}),
|
|
432
|
-
families: [{id: 'seed', seed, kind}],
|
|
433
|
-
}).palette.seed
|
|
434
|
-
),
|
|
435
|
-
);
|
|
436
|
-
return ramp(from) !== ramp(to);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* The modes a theme has: one for a one-mode theme, else light and dark.
|
|
441
|
-
* @param {{mode?: string}} data
|
|
442
|
-
* @returns {Mode[]}
|
|
443
|
-
*/
|
|
444
|
-
export function modesOf(data) {
|
|
445
|
-
return modesForStrategy(
|
|
446
|
-
data.mode === 'light-only' || data.mode === 'dark-only'
|
|
447
|
-
? data.mode
|
|
448
|
-
: 'light-and-dark',
|
|
449
|
-
);
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
* The light-mode color of a token value the theme's own source writes: a
|
|
454
|
-
* plain color, or the first half of a light-dark() pair.
|
|
455
|
-
* @param {string | undefined} value
|
|
456
|
-
*/
|
|
457
|
-
function lightHalf(value) {
|
|
458
|
-
const v = String(value ?? '').trim();
|
|
459
|
-
const pair = /^light-dark\((.*)\)$/is.exec(v);
|
|
460
|
-
return pair ? (splitTop(pair[1], ',')[0] ?? '').trim() : v;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* The color dial settings of a theme, in dial order. A family whose fill
|
|
465
|
-
* token the theme's own source sets exactly takes that color, the one in
|
|
466
|
-
* effect, as its seed: the shades its text, icons and tints read come from
|
|
467
|
-
* the color the fill shows, never from a dial value the source overrides.
|
|
468
|
-
* The neutral family always takes its setting.
|
|
469
|
-
* @param {{settings: Setting[]}} data
|
|
470
|
-
* @param {Record<string, string>} [own] the tokens the theme's own source sets
|
|
471
|
-
*/
|
|
472
|
-
function dialFamilies(data, own = {}) {
|
|
473
|
-
/** @type {DialFamily[]} */
|
|
474
|
-
const out = [];
|
|
475
|
-
for (const dial of PALETTE_DIALS) {
|
|
476
|
-
const s = [...data.settings]
|
|
477
|
-
.reverse()
|
|
478
|
-
.find(x => x?.source === 'dial' && x.thing === dial && !x.on && !x.mode);
|
|
479
|
-
// The neutral family keeps its setting: a gray the source sets exactly
|
|
480
|
-
// is one surface, not the color every gray grows from.
|
|
481
|
-
const inEffect =
|
|
482
|
-
dial === 'neutral' ? null : seedOf(lightHalf(own[paletteToken(dial)]));
|
|
483
|
-
const seed = inEffect ?? (s && seedOf(s.value));
|
|
484
|
-
if (s && seed)
|
|
485
|
-
out.push({
|
|
486
|
-
id: dial,
|
|
487
|
-
dial,
|
|
488
|
-
seed,
|
|
489
|
-
value: s.value,
|
|
490
|
-
source: inEffect ? paletteToken(dial) : null,
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
// A whole-theme --color-accent set as a token is the accent too: the
|
|
494
|
-
// accent's shades (links, selected rows, tints) grow from it as from the
|
|
495
|
-
// dial, and the token itself keeps the exact value it was given.
|
|
496
|
-
if (!out.some(f => f.id === 'accent')) {
|
|
497
|
-
const raws = data.settings.filter(
|
|
498
|
-
x =>
|
|
499
|
-
x?.source === 'token' &&
|
|
500
|
-
x.thing === '--color-accent' &&
|
|
501
|
-
!x.on &&
|
|
502
|
-
!x.when &&
|
|
503
|
-
!x.state,
|
|
504
|
-
);
|
|
505
|
-
const last = (/** @type {(m: unknown) => boolean} */ ok) =>
|
|
506
|
-
[...raws].reverse().find(x => ok(x.mode));
|
|
507
|
-
const light = last(m => !m || m === 'light') ?? last(m => m === 'dark');
|
|
508
|
-
const seed = light && seedOf(light.value);
|
|
509
|
-
// Set for one mode only, it moves that mode only.
|
|
510
|
-
const modes = new Set(raws.map(x => x.mode ?? 'both'));
|
|
511
|
-
const only =
|
|
512
|
-
modes.size === 1 && (modes.has('light') || modes.has('dark'))
|
|
513
|
-
? /** @type {'light' | 'dark'} */ ([...modes][0])
|
|
514
|
-
: null;
|
|
515
|
-
if (light && seed)
|
|
516
|
-
out.unshift({
|
|
517
|
-
id: 'accent',
|
|
518
|
-
dial: 'accent',
|
|
519
|
-
seed,
|
|
520
|
-
value: light.value,
|
|
521
|
-
source: null,
|
|
522
|
-
...(only ? {only} : {}),
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
return out;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* The seed each color family grows from now: the fill the theme's own source
|
|
530
|
-
* sets as a fixed color, else the color setting.
|
|
531
|
-
* @param {string} cwd
|
|
532
|
-
* @param {string} slug
|
|
533
|
-
* @param {{settings: Setting[]}} data
|
|
534
|
-
*/
|
|
535
|
-
export function seedsInEffect(cwd, slug, data) {
|
|
536
|
-
return seedsOfSettings(data, ownSourceTokenValues(cwd, slug));
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* The seeds of a theme's color settings, its own source's values read:
|
|
541
|
-
* what seedsInEffect returns, for a caller that holds those values.
|
|
542
|
-
* @param {{settings: Setting[]}} data
|
|
543
|
-
* @param {Record<string, string>} own the theme source's own token values
|
|
544
|
-
*/
|
|
545
|
-
export function seedsOfSettings(data, own) {
|
|
546
|
-
// (A color setting's families only: an accent set as a token keeps its
|
|
547
|
-
// exact value, so it is not a seed a dial such as vibrancy moves.)
|
|
548
|
-
return dialFamilies(data, own).filter(f =>
|
|
549
|
-
data.settings.some(x => x?.source === 'dial' && x.thing === f.dial),
|
|
550
|
-
);
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Whether `set <theme> vibrancy` changes anything: a color setting's seed
|
|
555
|
-
* that vibrancy moves, the check set runs.
|
|
556
|
-
* @param {{settings: Setting[]}} data
|
|
557
|
-
* @param {Record<string, string>} own the theme source's own token values
|
|
558
|
-
*/
|
|
559
|
-
export function vibrancyTakes(data, own) {
|
|
560
|
-
return vibrancyMovesAny(seedsOfSettings(data, own));
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
/**
|
|
564
|
-
* Whether vibrancy changes any of these seeds: the one test `set <theme>
|
|
565
|
-
* vibrancy`, `how` and the color notes share.
|
|
566
|
-
* @param {DialFamily[]} seeds
|
|
567
|
-
*/
|
|
568
|
-
export function vibrancyMovesAny(seeds) {
|
|
569
|
-
return seeds.some(f => vibrancyMoves(f.dial, f.seed, 0, 100));
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
/**
|
|
573
|
-
* The token words of the status roles a theme's look derives: every role
|
|
574
|
-
* it leaves unset, once it sets a color.
|
|
575
|
-
* @param {{settings: Setting[]}} data
|
|
576
|
-
* @returns {string[]}
|
|
577
|
-
*/
|
|
578
|
-
export function derivedStatusWords(data) {
|
|
579
|
-
const fams = dialFamilies(data);
|
|
580
|
-
if (!fams.length) return [];
|
|
581
|
-
return Object.keys(STATUS_TOKEN)
|
|
582
|
-
.filter(d => !fams.some(f => f.id === d))
|
|
583
|
-
.map(d => STATUS_TOKEN[d]);
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
/**
|
|
587
|
-
* The tokens a theme sets to a value of its own for the whole theme: the
|
|
588
|
-
* change names each, apart from the colors that move with the look.
|
|
589
|
-
* @param {{settings: Setting[]}} data
|
|
590
|
-
* @returns {string[]}
|
|
591
|
-
*/
|
|
592
|
-
export function exactTokens(data) {
|
|
593
|
-
return data.settings
|
|
594
|
-
.filter(x => x?.source === 'token' && !x.on && !x.when && !x.state)
|
|
595
|
-
.map(x => x.thing);
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
export {isLookToken, STATUS_TOKEN};
|
|
599
|
-
|
|
600
|
-
/** A code color or the overlay scrim. @param {string} token */
|
|
601
|
-
const isCodeToken = token => /^--color-(?:syntax-[a-z]+|overlay)$/.test(token);
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* A change's token lines with the look's own (category and chart colors)
|
|
605
|
-
* said in one line each, as counts: they move together with every look
|
|
606
|
-
* change, and one line per value would bury the rest.
|
|
607
|
-
* @param {unknown[]} lines - `token (mode): before -> after` lines.
|
|
608
|
-
* @param {string[]} [statusWords] - The status words whose colors the look derives (derivedStatusWords): theirs are counted too.
|
|
609
|
-
* @param {string[]} [exact] - Tokens the theme sets to a value of its own (exactTokens): each is named, never counted, as it did not move with the look.
|
|
610
|
-
*/
|
|
611
|
-
export function lookCollapsed(lines, statusWords = [], exact = []) {
|
|
612
|
-
const tokenOf = (/** @type {unknown} */ c) => String(c).split(/[\s:]/)[0];
|
|
613
|
-
const status = new RegExp(
|
|
614
|
-
`^--color-(?:(?:on|text|icon|border)-)?(?:${statusWords.join('|') || '(?!)'})(?:-muted)?$`,
|
|
615
|
-
);
|
|
616
|
-
const folded = (/** @type {unknown} */ c) =>
|
|
617
|
-
status.test(tokenOf(c)) && !exact.includes(tokenOf(c));
|
|
618
|
-
const statusLines = lines.filter(folded);
|
|
619
|
-
const rest = lines.filter(c => !folded(c));
|
|
620
|
-
lines = rest;
|
|
621
|
-
// (A look token the theme sets to a value of its own is named, never
|
|
622
|
-
// counted: it did not move with the look.)
|
|
623
|
-
const counted = (/** @type {unknown} */ c) =>
|
|
624
|
-
isLookToken(tokenOf(c)) && !exact.includes(tokenOf(c));
|
|
625
|
-
const look = lines.filter(counted).map(String);
|
|
626
|
-
const code = look.filter(c => isCodeToken(tokenOf(c))).length;
|
|
627
|
-
const badges = look.filter(
|
|
628
|
-
c => !tokenOf(c).startsWith('--color-data-') && !isCodeToken(tokenOf(c)),
|
|
629
|
-
);
|
|
630
|
-
const chart = look.length - badges.length - code;
|
|
631
|
-
return {
|
|
632
|
-
lines: [
|
|
633
|
-
...lines.filter(c => !counted(c)).map(String),
|
|
634
|
-
...(badges.length
|
|
635
|
-
? [
|
|
636
|
-
`category colors (badges, tokens and card tints): ${badges.length} values moved with the look`,
|
|
637
|
-
]
|
|
638
|
-
: []),
|
|
639
|
-
...(chart ? [`chart colors: ${chart} values moved with the look`] : []),
|
|
640
|
-
...(code
|
|
641
|
-
? [`code colors and the overlay: ${code} values moved with the look`]
|
|
642
|
-
: []),
|
|
643
|
-
...(statusLines.length
|
|
644
|
-
? [
|
|
645
|
-
`status colors (${statusWords.join(', ')}): ${statusLines.length} values moved with the look`,
|
|
646
|
-
]
|
|
647
|
-
: []),
|
|
648
|
-
],
|
|
649
|
-
look: [...look, ...statusLines.map(String)],
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
/** Notes for damaged color configs kept aside during one sync. @type {string[]} */
|
|
654
|
-
const keptDamaged = [];
|
|
655
|
-
|
|
656
|
-
/** @param {string} file */
|
|
657
|
-
function readConfig(file) {
|
|
658
|
-
if (!fs.existsSync(file)) return null;
|
|
659
|
-
try {
|
|
660
|
-
return JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
|
|
661
|
-
} catch {
|
|
662
|
-
// A file that does not parse holds nothing the editor can keep: it is
|
|
663
|
-
// read as no config. Only a command that writes the theme keeps its
|
|
664
|
-
// text aside (keepDamaged), so a read writes nothing.
|
|
665
|
-
damagedConfigs.add(file);
|
|
666
|
-
return null;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
/** Damaged configs read in this run, kept aside when a change is written. */
|
|
671
|
-
const damagedConfigs = new Set();
|
|
672
|
-
|
|
673
|
-
/**
|
|
674
|
-
* Keep a damaged config's text beside it (palette.config.damaged.json,
|
|
675
|
-
* then -2, -3 …; the same text once) before the editor writes the config
|
|
676
|
-
* again, and say where.
|
|
677
|
-
* @param {string} file
|
|
678
|
-
*/
|
|
679
|
-
function keepDamaged(file) {
|
|
680
|
-
if (!damagedConfigs.has(file) || !fs.existsSync(file)) return;
|
|
681
|
-
damagedConfigs.delete(file);
|
|
682
|
-
{
|
|
683
|
-
const text = fs.readFileSync(file);
|
|
684
|
-
let n = 1;
|
|
685
|
-
const name = (/** @type {number} */ k) =>
|
|
686
|
-
`palette.config.damaged${k > 1 ? `-${k}` : ''}.json`;
|
|
687
|
-
const at = (/** @type {number} */ k) =>
|
|
688
|
-
path.join(path.dirname(file), name(k));
|
|
689
|
-
// The same damaged text is kept once.
|
|
690
|
-
while (fs.existsSync(at(n)) && !fs.readFileSync(at(n)).equals(text)) n++;
|
|
691
|
-
if (!fs.existsSync(at(n))) fs.copyFileSync(file, at(n));
|
|
692
|
-
keptDamaged.push(
|
|
693
|
-
`palette.config.json was damaged, so it was made again from the settings; its text is kept in ${name(n)}`,
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
/**
|
|
699
|
-
* A theme's settings file as it is on disk, or null when it does not read.
|
|
700
|
-
* @param {string} cwd
|
|
701
|
-
* @param {string} slug
|
|
702
|
-
* @returns {any}
|
|
703
|
-
*/
|
|
704
|
-
function savedTheme(cwd, slug) {
|
|
705
|
-
try {
|
|
706
|
-
return JSON.parse(
|
|
707
|
-
fs.readFileSync(
|
|
708
|
-
path.join(cwd, 'themes', slug, `${slug}.settings.json`),
|
|
709
|
-
'utf8',
|
|
710
|
-
),
|
|
711
|
-
);
|
|
712
|
-
} catch {
|
|
713
|
-
return null;
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* The settings saved on disk, before the change being built: what the
|
|
719
|
-
* editor wrote into the config last time, so a value it did not write is
|
|
720
|
-
* known to be a hand edit.
|
|
721
|
-
* @param {string} cwd
|
|
722
|
-
* @param {string} slug
|
|
723
|
-
* @returns {{settings: Setting[], mode: string | null}}
|
|
724
|
-
*/
|
|
725
|
-
function savedSettings(cwd, slug) {
|
|
726
|
-
const data = savedTheme(cwd, slug);
|
|
727
|
-
return {
|
|
728
|
-
settings: Array.isArray(data?.settings) ? data.settings : [],
|
|
729
|
-
mode: typeof data?.mode === 'string' ? data.mode : null,
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
/** The generator's mode strategy for a list of modes. @param {Mode[]} modes */
|
|
734
|
-
const strategyOf = modes =>
|
|
735
|
-
modes.length > 1 ? 'light-and-dark' : `${modes[0]}-only`;
|
|
736
|
-
|
|
737
|
-
/**
|
|
738
|
-
* The fields of a config the generator never reads, by path.
|
|
739
|
-
* @param {any} config
|
|
740
|
-
* @returns {string[]}
|
|
741
|
-
*/
|
|
742
|
-
function deadKeys(config) {
|
|
743
|
-
/** @type {string[]} */
|
|
744
|
-
const out = Object.keys(config).filter(
|
|
745
|
-
k => !GENERATION_INPUT_KEYS.includes(k),
|
|
746
|
-
);
|
|
747
|
-
(Array.isArray(config.families) ? config.families : []).forEach(
|
|
748
|
-
(/** @type {any} */ f, /** @type {number} */ i) => {
|
|
749
|
-
if (!f || typeof f !== 'object') return;
|
|
750
|
-
for (const k of Object.keys(f))
|
|
751
|
-
if (!FAMILY_INPUT_KEYS.includes(k)) out.push(`families[${i}].${k}`);
|
|
752
|
-
(Array.isArray(f.anchors) ? f.anchors : []).forEach(
|
|
753
|
-
(/** @type {any} */ a, /** @type {number} */ j) => {
|
|
754
|
-
if (a && typeof a === 'object')
|
|
755
|
-
for (const k of Object.keys(a))
|
|
756
|
-
if (!ANCHOR_INPUT_KEYS.includes(k))
|
|
757
|
-
out.push(`families[${i}].anchors[${j}].${k}`);
|
|
758
|
-
},
|
|
759
|
-
);
|
|
760
|
-
},
|
|
761
|
-
);
|
|
762
|
-
return out;
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
/**
|
|
766
|
-
* Refuse a stop layout the token mapping cannot use: numbers only, black
|
|
767
|
-
* (0) and white (100), enough shades between them, and no gap so wide that
|
|
768
|
-
* tokens land far from where they belong. The generator checks the order.
|
|
769
|
-
* @param {any} stops
|
|
770
|
-
* @param {string} slug
|
|
771
|
-
* @returns {readonly number[]}
|
|
772
|
-
*/
|
|
773
|
-
function usableStops(stops, slug) {
|
|
774
|
-
if (stops == null) return DEFAULT_21_STOPS;
|
|
775
|
-
const list = Array.isArray(stops) ? stops : [];
|
|
776
|
-
const shown = Array.isArray(stops)
|
|
777
|
-
? stops.map(x => JSON.stringify(x)).join(', ')
|
|
778
|
-
: JSON.stringify(stops);
|
|
779
|
-
const fix = `Remove "stops" from it to use the default 21 shades, or give 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100. Nothing was changed.`;
|
|
780
|
-
/** @param {string} why */
|
|
781
|
-
const refuse = why => {
|
|
782
|
-
throw new AstryxError(
|
|
783
|
-
`themes/${slug}/palette.config.json: its stops (${shown}) ${why}. ${fix}`,
|
|
784
|
-
[{name: `${P} how colors`, reason: 'how the shades are made'}],
|
|
785
|
-
ERROR_CODES.ERR_THEME_INVALID,
|
|
786
|
-
);
|
|
787
|
-
};
|
|
788
|
-
if (!Array.isArray(stops) || !list.length)
|
|
789
|
-
refuse('are not a list of numbers');
|
|
790
|
-
const bad = list.filter(x => typeof x !== 'number' || !Number.isFinite(x));
|
|
791
|
-
if (bad.length)
|
|
792
|
-
refuse(
|
|
793
|
-
`hold ${bad.map(x => JSON.stringify(x)).join(', ')}, which ${bad.length > 1 ? 'are not numbers' : 'is not a number'}`,
|
|
794
|
-
);
|
|
795
|
-
if (!list.includes(0) || !list.includes(100))
|
|
796
|
-
refuse('need 0 (black) and 100 (white) at the ends');
|
|
797
|
-
const inner = list.filter(s => s > 0 && s < 100);
|
|
798
|
-
if (inner.length < MIN_INNER_STOPS)
|
|
799
|
-
refuse(
|
|
800
|
-
`leave ${inner.length} shades between black and white, and the color tokens need at least ${MIN_INNER_STOPS}`,
|
|
801
|
-
);
|
|
802
|
-
const sorted = [...list].sort((a, b) => a - b);
|
|
803
|
-
const gap = Math.max(...sorted.slice(1).map((x, i) => x - sorted[i]));
|
|
804
|
-
if (gap > MAX_STOP_GAP)
|
|
805
|
-
refuse(
|
|
806
|
-
`leave a gap of ${gap} between two shades, and the color tokens need one at least every ${MAX_STOP_GAP}`,
|
|
807
|
-
);
|
|
808
|
-
return list;
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
/**
|
|
812
|
-
* What the palette mapping reads of a theme: its base colors, the tokens it
|
|
813
|
-
* has, its exact token settings (a page-wide token its own source sets wins,
|
|
814
|
-
* as the build shows it), the fixed color a value gives in a mode, and the
|
|
815
|
-
* command that sets a value, and the modes the theme shows.
|
|
816
|
-
* @param {string} cwd
|
|
817
|
-
* @param {string} slug
|
|
818
|
-
* @param {{mode?: string, settings: Setting[]}} data
|
|
819
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>} | undefined} base
|
|
820
|
-
* @param {Record<string, unknown>} [tokenDefaults]
|
|
821
|
-
* @returns {import('../theme/palette/generate/mapping.mjs').PaletteMappingContext}
|
|
822
|
-
*/
|
|
823
|
-
function paletteContext(cwd, slug, data, base, tokenDefaults = {}) {
|
|
824
|
-
const own = ownSourceTokenValues(cwd, slug);
|
|
825
|
-
/** @param {string} token @param {Mode} m @returns {string | undefined} */
|
|
826
|
-
const settingOf = (token, m) =>
|
|
827
|
-
(Object.hasOwn(own, token) ? own[token] : undefined) ??
|
|
828
|
-
[...data.settings]
|
|
829
|
-
.reverse()
|
|
830
|
-
.find(
|
|
831
|
-
s =>
|
|
832
|
-
s.source === 'token' &&
|
|
833
|
-
s.thing === token &&
|
|
834
|
-
!s.on &&
|
|
835
|
-
!s.when &&
|
|
836
|
-
!s.state &&
|
|
837
|
-
(!s.mode || s.mode === m),
|
|
838
|
-
)?.value;
|
|
839
|
-
/**
|
|
840
|
-
* The fixed color a value gives in a mode, as written when it is one
|
|
841
|
-
* already, or null: `light-dark()` takes its side for the mode, and a
|
|
842
|
-
* `var()` reads the theme's own setting, else the base theme's value.
|
|
843
|
-
* @param {unknown} value @param {Mode} m @param {Set<string>} [seen]
|
|
844
|
-
* @returns {string | null}
|
|
845
|
-
*/
|
|
846
|
-
const colorIn = (value, m, seen = new Set()) => {
|
|
847
|
-
const v = String(value ?? '').trim();
|
|
848
|
-
if (!v) return null;
|
|
849
|
-
if (parseColor(v)) return v;
|
|
850
|
-
const fn = /^([a-z-]+)\((.*)\)$/is.exec(v);
|
|
851
|
-
if (!fn) return null;
|
|
852
|
-
const name = fn[1].toLowerCase();
|
|
853
|
-
if (name === 'light-dark') {
|
|
854
|
-
const halves = splitTop(fn[2], ',');
|
|
855
|
-
return halves.length === 2
|
|
856
|
-
? colorIn(halves[m === 'dark' ? 1 : 0], m, seen)
|
|
857
|
-
: null;
|
|
858
|
-
}
|
|
859
|
-
if (name !== 'var') {
|
|
860
|
-
// A color function of tokens (a color-mix() of two) reads each one.
|
|
861
|
-
const put = v.replace(
|
|
862
|
-
/var\(\s*(--[a-z0-9-]+)\s*\)/gi,
|
|
863
|
-
(whole, t) => colorIn(`var(${t})`, m, new Set(seen)) ?? whole,
|
|
864
|
-
);
|
|
865
|
-
return put !== v && parseColor(put) ? put : null;
|
|
866
|
-
}
|
|
867
|
-
const [ref, ...rest] = splitTop(fn[2], ',');
|
|
868
|
-
const t = ref.trim();
|
|
869
|
-
if (!/^--[a-z0-9-]+$/i.test(t) || seen.has(t)) return null;
|
|
870
|
-
seen.add(t);
|
|
871
|
-
const set = settingOf(t, m);
|
|
872
|
-
const read =
|
|
873
|
-
set != null ? colorIn(set, m, seen) : colorIn(base?.[m]?.[t], m, seen);
|
|
874
|
-
return read ?? (rest.length ? colorIn(rest.join(','), m, seen) : null);
|
|
875
|
-
};
|
|
876
|
-
return {
|
|
877
|
-
base,
|
|
878
|
-
has: token => token in tokenDefaults,
|
|
879
|
-
// The editor's own rules (Core's context.editor), which only the
|
|
880
|
-
// editor loads.
|
|
881
|
-
editor: editorLook,
|
|
882
|
-
shown: modesOf(data),
|
|
883
|
-
defaults: tokenDefaults,
|
|
884
|
-
reader: {
|
|
885
|
-
rgb: value =>
|
|
886
|
-
/** @type {[number, number, number] | null} */ (parseColor(value)),
|
|
887
|
-
alpha: value => alphaOf(value),
|
|
888
|
-
},
|
|
889
|
-
settingOf,
|
|
890
|
-
colorIn: (value, m) => colorIn(value, m),
|
|
891
|
-
setExactly: token =>
|
|
892
|
-
data.settings.some(
|
|
893
|
-
s =>
|
|
894
|
-
s.source === 'token' &&
|
|
895
|
-
s.thing === token &&
|
|
896
|
-
!s.on &&
|
|
897
|
-
!s.when &&
|
|
898
|
-
!s.state,
|
|
899
|
-
),
|
|
900
|
-
setLine: (thing, value, mode) =>
|
|
901
|
-
`${P} set ${slug} ${thing} ${quoteCommandArgument(value)}${mode ? ` --mode ${mode}` : ''}`,
|
|
902
|
-
};
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
/**
|
|
906
|
-
* What the config writer decided: the config, what it kept or replaced of
|
|
907
|
-
* a hand edit, and for each accent or status color the stop of its exact
|
|
908
|
-
* color and whether dark mode holds it too (why not, when not).
|
|
909
|
-
* @typedef {object} PalettePlan
|
|
910
|
-
* @property {any} config
|
|
911
|
-
* @property {string[]} notes
|
|
912
|
-
* @property {Record<string, {stop: number, dark: boolean, why: string, ratio: number}>} fills
|
|
913
|
-
* @property {boolean} [stored] the saved palette, kept as it is (a recipe this CLI does not know)
|
|
914
|
-
*/
|
|
915
|
-
|
|
916
|
-
/**
|
|
917
|
-
* Whether a theme's palette names a recipe this CLI does not know (its
|
|
918
|
-
* colors are kept as saved, made by a newer CLI).
|
|
919
|
-
* @param {string} cwd
|
|
920
|
-
* @param {string} slug
|
|
921
|
-
*/
|
|
922
|
-
export function paletteRecipeUnknown(cwd, slug) {
|
|
923
|
-
const config = readConfig(path.join(cwd, paletteFiles(slug).config));
|
|
924
|
-
return newerRecipe(config?.recipe) != null;
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
/**
|
|
928
|
-
* What a palette kept as saved is, in words: whose colors use which recipe,
|
|
929
|
-
* that this CLI does not make it, and what still works. No update step: a
|
|
930
|
-
* CLI that makes the recipe may not be released yet.
|
|
931
|
-
* @param {string} slug
|
|
932
|
-
* @param {unknown} recipe
|
|
933
|
-
*/
|
|
934
|
-
const keptPaletteWords = (slug, recipe) =>
|
|
935
|
-
`${slug}'s colors use the ${String(recipe)} recipe, which this Astryx CLI (${cliVersion()}) does not make; its palette is kept as saved. Changes to what the palette is made from (the accent, neutral and status colors, the vibrancy) need a CLI that makes that recipe; other changes, including exact color tokens, a part's colors, the mode and the base, save as usual.`;
|
|
936
|
-
|
|
937
|
-
/**
|
|
938
|
-
* The refusal of a color change to a palette kept as saved (a recipe a
|
|
939
|
-
* newer CLI names): what it is, and nothing to run.
|
|
940
|
-
* @param {string} slug
|
|
941
|
-
* @param {unknown} recipe
|
|
942
|
-
*/
|
|
943
|
-
function unknownRecipe(slug, recipe) {
|
|
944
|
-
return new AstryxError(
|
|
945
|
-
`${keptPaletteWords(slug, recipe)} Nothing was changed.`,
|
|
946
|
-
[],
|
|
947
|
-
ERROR_CODES.ERR_PALETTE_GENERATION,
|
|
948
|
-
);
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
/**
|
|
952
|
-
* Each theme's colors as saved (its {@link paletteMadeFromKey}), as the
|
|
953
|
-
* settings reader read the file: a change is compared with this.
|
|
954
|
-
* @type {Map<string, string | null>}
|
|
955
|
-
*/
|
|
956
|
-
const SAVED_COLORS = new Map();
|
|
957
|
-
|
|
958
|
-
/**
|
|
959
|
-
* Each theme's settings as the settings reader read the file, before any
|
|
960
|
-
* change: what a hand edit is checked against.
|
|
961
|
-
* @type {Map<string, any>}
|
|
962
|
-
*/
|
|
963
|
-
const LOADED_SETTINGS = new Map();
|
|
964
|
-
|
|
965
|
-
/**
|
|
966
|
-
* Note what a theme's colors come from as saved, read as the settings
|
|
967
|
-
* reader reads the file (its base word, its field and entry names).
|
|
968
|
-
* @param {string} cwd
|
|
969
|
-
* @param {string} slug
|
|
970
|
-
* @param {any} data the settings as read, before any change
|
|
971
|
-
*/
|
|
972
|
-
export function noteSavedColors(cwd, slug, data) {
|
|
973
|
-
SAVED_COLORS.set(path.join(cwd, 'themes', slug), paletteMadeFromKey(data));
|
|
974
|
-
LOADED_SETTINGS.set(path.join(cwd, 'themes', slug), data);
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
/**
|
|
978
|
-
* A theme's settings as read before any change (the ones its settings
|
|
979
|
-
* module was built from), else as the file holds them now.
|
|
980
|
-
* @param {string} cwd
|
|
981
|
-
* @param {string} slug
|
|
982
|
-
* @returns {any}
|
|
983
|
-
*/
|
|
984
|
-
export function settingsBeforeChange(cwd, slug) {
|
|
985
|
-
const dir = path.join(cwd, 'themes', slug);
|
|
986
|
-
return LOADED_SETTINGS.has(dir)
|
|
987
|
-
? LOADED_SETTINGS.get(dir)
|
|
988
|
-
: savedTheme(cwd, slug);
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
/**
|
|
992
|
-
* What a palette is made from, as one key to compare: the color dials and
|
|
993
|
-
* vibrancy, and a whole-theme accent token, as {@link dialFamilies} reads
|
|
994
|
-
* them. A change to these makes the palette again; the mode, the base and
|
|
995
|
-
* any other color token only map it.
|
|
996
|
-
* @param {{settings?: Setting[]} | null} d
|
|
997
|
-
*/
|
|
998
|
-
export function paletteMadeFromKey(d) {
|
|
999
|
-
// (Beside an accent dial, a whole-theme accent token is an exact value
|
|
1000
|
-
// the palette never reads, as dialFamilies reads it.)
|
|
1001
|
-
const accentDial = (d?.settings ?? []).some(
|
|
1002
|
-
x => x?.source === 'dial' && x.thing === 'accent' && !x.on && !x.mode,
|
|
1003
|
-
);
|
|
1004
|
-
return d
|
|
1005
|
-
? JSON.stringify(
|
|
1006
|
-
(d.settings ?? [])
|
|
1007
|
-
.filter(
|
|
1008
|
-
x =>
|
|
1009
|
-
(x?.source === 'dial' &&
|
|
1010
|
-
!x.on &&
|
|
1011
|
-
(PALETTE_DIALS.includes(x.thing) || x.thing === 'vibrancy')) ||
|
|
1012
|
-
(!accentDial &&
|
|
1013
|
-
x?.source === 'token' &&
|
|
1014
|
-
x.thing === '--color-accent' &&
|
|
1015
|
-
!x.on &&
|
|
1016
|
-
!x.when &&
|
|
1017
|
-
!x.state),
|
|
1018
|
-
)
|
|
1019
|
-
.map(x =>
|
|
1020
|
-
[x.source, x.thing, x.value, x.mode]
|
|
1021
|
-
.map(v => String(v ?? '').toLowerCase())
|
|
1022
|
-
.join('|'),
|
|
1023
|
-
),
|
|
1024
|
-
)
|
|
1025
|
-
: null;
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
/**
|
|
1029
|
-
* What a theme's colors come from, as one key to compare: its mode, its
|
|
1030
|
-
* base, its color dials (and vibrancy) and its color tokens. A change that
|
|
1031
|
-
* leaves this as it was leaves the palette's colors alone.
|
|
1032
|
-
* @param {{mode?: string | null, like?: string | null, settings?: Setting[]} | null} d
|
|
1033
|
-
*/
|
|
1034
|
-
export function paletteColorKey(d) {
|
|
1035
|
-
return d
|
|
1036
|
-
? JSON.stringify({
|
|
1037
|
-
mode: d.mode ?? 'both',
|
|
1038
|
-
like: d.like ?? null,
|
|
1039
|
-
// (Each by what it says.)
|
|
1040
|
-
colors: (d.settings ?? [])
|
|
1041
|
-
.filter(
|
|
1042
|
-
x =>
|
|
1043
|
-
(x?.source === 'dial' &&
|
|
1044
|
-
(PALETTE_DIALS.includes(x.thing) || x.thing === 'vibrancy')) ||
|
|
1045
|
-
(x?.source === 'token' && isColorToken(x.thing)),
|
|
1046
|
-
)
|
|
1047
|
-
.map(x =>
|
|
1048
|
-
[x.source, x.thing, x.value, x.on, x.when, x.state, x.mode]
|
|
1049
|
-
.map(v => String(v ?? '').toLowerCase())
|
|
1050
|
-
.join('|'),
|
|
1051
|
-
),
|
|
1052
|
-
})
|
|
1053
|
-
: null;
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
/**
|
|
1057
|
-
* The palette as its files hold it, read as the generator's candidate
|
|
1058
|
-
* (its stops, families and ends), for a palette this CLI cannot make
|
|
1059
|
-
* again; null when the palette file is missing or does not read.
|
|
1060
|
-
* @param {string} cwd
|
|
1061
|
-
* @param {string} slug
|
|
1062
|
-
* @param {any} config
|
|
1063
|
-
*/
|
|
1064
|
-
function storedCandidate(cwd, slug, config) {
|
|
1065
|
-
const f = paletteFiles(slug);
|
|
1066
|
-
let data;
|
|
1067
|
-
try {
|
|
1068
|
-
data = paletteData(fs.readFileSync(path.join(cwd, f.palette), 'utf8'));
|
|
1069
|
-
} catch {
|
|
1070
|
-
return null;
|
|
1071
|
-
}
|
|
1072
|
-
if (!data) return null;
|
|
1073
|
-
const first = /** @type {any} */ (Object.values(data.palette)[0]);
|
|
1074
|
-
const stops = (
|
|
1075
|
-
Array.isArray(config?.stops)
|
|
1076
|
-
? config.stops
|
|
1077
|
-
: Object.keys(first?.light ?? first?.dark ?? {})
|
|
1078
|
-
)
|
|
1079
|
-
.map(Number)
|
|
1080
|
-
.sort((/** @type {number} */ a, /** @type {number} */ b) => a - b);
|
|
1081
|
-
return {
|
|
1082
|
-
schemaVersion: 1,
|
|
1083
|
-
status: 'candidate',
|
|
1084
|
-
recipe: config?.recipe,
|
|
1085
|
-
black: data.black ?? PALETTE_BLACK,
|
|
1086
|
-
white: data.white ?? PALETTE_WHITE,
|
|
1087
|
-
stops,
|
|
1088
|
-
palette: data.palette,
|
|
1089
|
-
};
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
/**
|
|
1093
|
-
* Whether a theme's palette is kept exactly as saved, as one made with a
|
|
1094
|
-
* recipe this CLI does not know is ({@link planPalette}): its build keeps
|
|
1095
|
-
* what that palette made.
|
|
1096
|
-
* @param {string} cwd
|
|
1097
|
-
* @param {string} slug
|
|
1098
|
-
* @param {{mode?: string, settings: Setting[]}} data
|
|
1099
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} [base]
|
|
1100
|
-
*/
|
|
1101
|
-
export function paletteKeptAsSaved(cwd, slug, data, base) {
|
|
1102
|
-
try {
|
|
1103
|
-
return Boolean(planPalette(cwd, slug, data, base)?.stored);
|
|
1104
|
-
} catch {
|
|
1105
|
-
return false;
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
/**
|
|
1110
|
-
* The candidate a plan's palette is: the saved one for a palette this CLI
|
|
1111
|
-
* cannot make again, else the generator's.
|
|
1112
|
-
* @param {string} cwd
|
|
1113
|
-
* @param {string} slug
|
|
1114
|
-
* @param {PalettePlan} plan
|
|
1115
|
-
*/
|
|
1116
|
-
function candidateOf(cwd, slug, plan) {
|
|
1117
|
-
return plan.stored
|
|
1118
|
-
? /** @type {Candidate} */ (storedCandidate(cwd, slug, plan.config))
|
|
1119
|
-
: generate(plan.config, paletteFiles(slug).config, cwd);
|
|
1120
|
-
}
|
|
1121
|
-
|
|
1122
|
-
/**
|
|
1123
|
-
* The palette config a theme's settings ask for. The existing config is
|
|
1124
|
-
* kept, and each color dial changes only what it owns: its family's seed,
|
|
1125
|
-
* the exact anchors of its seed (light, and dark when the color reads
|
|
1126
|
-
* there; both modes for neutral), and a gray seed's `kind`. `modeStrategy`
|
|
1127
|
-
* follows the theme's modes; `vibrancy` follows the vibrancy dial and is
|
|
1128
|
-
* kept when written by hand; anchors, names, profile, stops and families of
|
|
1129
|
-
* the author's own stay. Null when the config would hold no family.
|
|
1130
|
-
* @param {string} cwd
|
|
1131
|
-
* @param {string} slug
|
|
1132
|
-
* @param {{mode?: string, like?: string, settings: Setting[], palette?: string}} data
|
|
1133
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} [base] the base theme's resolved colors, which the dark check reads
|
|
1134
|
-
* @returns {PalettePlan | null}
|
|
1135
|
-
*/
|
|
1136
|
-
function planPalette(cwd, slug, data, base) {
|
|
1137
|
-
const file = paletteFiles(slug).config;
|
|
1138
|
-
const existing = readConfig(path.join(cwd, file));
|
|
1139
|
-
const fams = dialFamilies(data, ownSourceTokenValues(cwd, slug));
|
|
1140
|
-
if (!existing && !fams.length) return null;
|
|
1141
|
-
/** @type {string[]} */
|
|
1142
|
-
const notes = [];
|
|
1143
|
-
const own =
|
|
1144
|
-
existing && typeof existing === 'object' && !Array.isArray(existing);
|
|
1145
|
-
// A palette made with a recipe this CLI does not know (a newer CLI's):
|
|
1146
|
-
// its saved colors stay exactly as stored, so opening the theme never
|
|
1147
|
-
// recolors it; only a change to its colors needs a CLI that knows it.
|
|
1148
|
-
if (own && newerRecipe(existing.recipe)) {
|
|
1149
|
-
const stored = storedCandidate(cwd, slug, existing);
|
|
1150
|
-
const dir = path.join(cwd, 'themes', slug);
|
|
1151
|
-
const savedKey = SAVED_COLORS.has(dir)
|
|
1152
|
-
? SAVED_COLORS.get(dir)
|
|
1153
|
-
: paletteMadeFromKey(savedTheme(cwd, slug));
|
|
1154
|
-
/** The seed the saved palette made a family from. @param {string} id */
|
|
1155
|
-
const seedOfSaved = id =>
|
|
1156
|
-
/** @type {any[]} */ (
|
|
1157
|
-
Array.isArray(existing.families) ? existing.families : []
|
|
1158
|
-
).find(x => x?.id === id)?.seed;
|
|
1159
|
-
// (A color changed in the settings file by hand is a change too: each
|
|
1160
|
-
// color the settings name is checked against the seed the saved
|
|
1161
|
-
// palette was made from, where it names that family. A color the
|
|
1162
|
-
// saved palette has no shades for is a change too.)
|
|
1163
|
-
/** @param {DialFamily[]} list */
|
|
1164
|
-
const differing = list =>
|
|
1165
|
-
list.find(f => {
|
|
1166
|
-
if (!(/** @type {any} */ (stored?.palette)?.[f.id])) return true;
|
|
1167
|
-
const was = seedOfSaved(f.id);
|
|
1168
|
-
return Boolean(was) && !sameColor(String(was), f.seed);
|
|
1169
|
-
}) ?? null;
|
|
1170
|
-
const madeFrom = !differing(fams);
|
|
1171
|
-
// Settings that already disagree with the saved palette as read (a
|
|
1172
|
-
// color changed or added in the file by hand).
|
|
1173
|
-
const loaded = LOADED_SETTINGS.has(dir)
|
|
1174
|
-
? LOADED_SETTINGS.get(dir)
|
|
1175
|
-
: savedTheme(cwd, slug);
|
|
1176
|
-
const hand =
|
|
1177
|
-
stored && Array.isArray(loaded?.settings)
|
|
1178
|
-
? differing(dialFamilies(loaded, ownSourceTokenValues(cwd, slug)))
|
|
1179
|
-
: null;
|
|
1180
|
-
// (From such a file, any change that brings every color back to the
|
|
1181
|
-
// seeds the saved palette was made from is the way back, an undo or a
|
|
1182
|
-
// `set` of the saved seed alike, as long as the vibrancy is the saved
|
|
1183
|
-
// one.)
|
|
1184
|
-
const back =
|
|
1185
|
-
hand != null &&
|
|
1186
|
-
// (Every color the file names still named, so none falls back to the
|
|
1187
|
-
// base theme's.)
|
|
1188
|
-
// (A color the saved palette has no shades for may go.)
|
|
1189
|
-
dialFamilies(loaded, ownSourceTokenValues(cwd, slug)).every(
|
|
1190
|
-
l =>
|
|
1191
|
-
!(/** @type {any} */ (stored?.palette)?.[l.id]) ||
|
|
1192
|
-
fams.some(f => f.id === l.id),
|
|
1193
|
-
) &&
|
|
1194
|
-
(dialVibrancy(data.settings) ?? VIBRANCY_DEFAULT) ===
|
|
1195
|
-
(existing.vibrancy ?? VIBRANCY_DEFAULT);
|
|
1196
|
-
if (stored && madeFrom && (paletteMadeFromKey(data) === savedKey || back))
|
|
1197
|
-
return {
|
|
1198
|
-
config: existing,
|
|
1199
|
-
// Each accent or status color at the stop its saved palette holds
|
|
1200
|
-
// it, light (its exact anchor, else the nearest shade), and dark
|
|
1201
|
-
// when an exact dark anchor holds it there too.
|
|
1202
|
-
fills: Object.fromEntries(
|
|
1203
|
-
fams
|
|
1204
|
-
.filter(f => f.id !== 'neutral')
|
|
1205
|
-
.map(f => {
|
|
1206
|
-
const anchors = /** @type {any[]} */ (
|
|
1207
|
-
existing.families?.find(
|
|
1208
|
-
(/** @type {any} */ x) => x?.id === f.id,
|
|
1209
|
-
)?.anchors ?? []
|
|
1210
|
-
);
|
|
1211
|
-
const exact = (/** @type {Mode} */ m) =>
|
|
1212
|
-
anchors.find(
|
|
1213
|
-
a =>
|
|
1214
|
-
a?.mode === m &&
|
|
1215
|
-
a.policy === 'exact' &&
|
|
1216
|
-
sameColor(a.color, f.seed),
|
|
1217
|
-
);
|
|
1218
|
-
const ramp = /** @type {any} */ (stored.palette)[f.id]?.light;
|
|
1219
|
-
const stop =
|
|
1220
|
-
exact('light')?.stop ??
|
|
1221
|
-
(ramp ? nearestShade(ramp, f.seed) : null) ??
|
|
1222
|
-
50;
|
|
1223
|
-
return [
|
|
1224
|
-
f.id,
|
|
1225
|
-
{
|
|
1226
|
-
stop: Number(stop),
|
|
1227
|
-
dark: Boolean(exact('dark')),
|
|
1228
|
-
why: '',
|
|
1229
|
-
ratio: 0,
|
|
1230
|
-
},
|
|
1231
|
-
];
|
|
1232
|
-
}),
|
|
1233
|
-
),
|
|
1234
|
-
notes: [`${file}: ${keptPaletteWords(slug, existing.recipe)}`],
|
|
1235
|
-
stored: true,
|
|
1236
|
-
};
|
|
1237
|
-
// (A palette file gone or unreadable leaves nothing to keep: every
|
|
1238
|
-
// command needs it back, or a CLI that can make the colors again.)
|
|
1239
|
-
if (!stored)
|
|
1240
|
-
throw new AstryxError(
|
|
1241
|
-
`${file}: ${slug}'s colors use the ${String(existing.recipe)} recipe, which this Astryx CLI (${cliVersion()}) does not make, and its palette file (${paletteFiles(slug).palette}) is missing or does not read, so the theme cannot be built until that file is back. Nothing was changed.`,
|
|
1242
|
-
[],
|
|
1243
|
-
ERROR_CODES.ERR_PALETTE_GENERATION,
|
|
1244
|
-
);
|
|
1245
|
-
// Settings that already disagree with the saved palette as read refuse
|
|
1246
|
-
// every other command, so the refusal names that setting and the way
|
|
1247
|
-
// back.
|
|
1248
|
-
if (hand) {
|
|
1249
|
-
const name =
|
|
1250
|
-
hand.source ??
|
|
1251
|
-
(loaded.settings.some(
|
|
1252
|
-
(/** @type {any} */ x) =>
|
|
1253
|
-
x?.source === 'dial' && x.thing === hand.dial,
|
|
1254
|
-
)
|
|
1255
|
-
? hand.dial
|
|
1256
|
-
: '--color-accent');
|
|
1257
|
-
const seed = /** @type {any} */ (stored?.palette)?.[hand.id]
|
|
1258
|
-
? String(seedOfSaved(hand.id))
|
|
1259
|
-
: null;
|
|
1260
|
-
const was = seed
|
|
1261
|
-
? `the saved palette was made from ${seed}`
|
|
1262
|
-
: `the saved palette has no shades for ${hand.dial}`;
|
|
1263
|
-
// (The way back named is the one that opens the theme: `set` of the
|
|
1264
|
-
// saved seed when that setting is the only difference, else each
|
|
1265
|
-
// difference to put back by hand.)
|
|
1266
|
-
const own = ownSourceTokenValues(cwd, slug);
|
|
1267
|
-
const diffs = dialFamilies(loaded, own).filter(f => differing([f]));
|
|
1268
|
-
const vibrancyOff =
|
|
1269
|
-
(dialVibrancy(loaded.settings) ?? VIBRANCY_DEFAULT) !==
|
|
1270
|
-
(existing.vibrancy ?? VIBRANCY_DEFAULT);
|
|
1271
|
-
const nameOf = (/** @type {DialFamily} */ f) =>
|
|
1272
|
-
f.source ??
|
|
1273
|
-
(loaded.settings.some(
|
|
1274
|
-
(/** @type {any} */ x) => x?.source === 'dial' && x.thing === f.dial,
|
|
1275
|
-
)
|
|
1276
|
-
? f.dial
|
|
1277
|
-
: '--color-accent');
|
|
1278
|
-
// (A color the theme's own source sets wins over the settings: it is
|
|
1279
|
-
// put back in that file, never with set.)
|
|
1280
|
-
const sourceFile = `themes/${slug}/${slug}.theme.ts`;
|
|
1281
|
-
const backOf = (/** @type {DialFamily} */ f) =>
|
|
1282
|
-
/** @type {any} */ (stored?.palette)?.[f.id]
|
|
1283
|
-
? `${nameOf(f)} back to ${String(seedOfSaved(f.id))}${f.source ? ` in ${sourceFile}` : ''}`
|
|
1284
|
-
: `${nameOf(f)} out${f.source ? ` of ${sourceFile}` : ''}`;
|
|
1285
|
-
const way =
|
|
1286
|
-
diffs.length === 1 && !vibrancyOff
|
|
1287
|
-
? hand.source
|
|
1288
|
-
? `Change it back in ${sourceFile} to open the theme.`
|
|
1289
|
-
: seed
|
|
1290
|
-
? `Put it back by hand, or with set ${slug} ${name} ${seed}, to open the theme.`
|
|
1291
|
-
: `Take it out of the file by hand to open the theme.`
|
|
1292
|
-
: `Put every one back by hand to open the theme: ${[
|
|
1293
|
-
...diffs.map(backOf),
|
|
1294
|
-
...(vibrancyOff
|
|
1295
|
-
? [
|
|
1296
|
-
`vibrancy back to ${String(existing.vibrancy ?? VIBRANCY_DEFAULT)}`,
|
|
1297
|
-
]
|
|
1298
|
-
: []),
|
|
1299
|
-
].join(', ')}.`;
|
|
1300
|
-
throw new AstryxError(
|
|
1301
|
-
`${slug}'s colors use the ${String(existing.recipe)} recipe, which this Astryx CLI (${cliVersion()}) does not make; its palette is kept as saved. ${hand.source ? sourceFile : `themes/${slug}/${slug}.settings.json`} sets ${name} to ${hand.source ? hand.seed : hand.value}, but ${was}. ${way} Nothing was changed.`,
|
|
1302
|
-
[],
|
|
1303
|
-
ERROR_CODES.ERR_PALETTE_GENERATION,
|
|
1304
|
-
);
|
|
1305
|
-
}
|
|
1306
|
-
throw unknownRecipe(slug, existing.recipe);
|
|
1307
|
-
}
|
|
1308
|
-
// A new palette is made with the newest recipe; a palette the theme
|
|
1309
|
-
// already has keeps the one it was made with (none named is the first),
|
|
1310
|
-
// so its colors never move with a CLI upgrade. Moving it is a choice
|
|
1311
|
-
// made in its config.
|
|
1312
|
-
const config = own ? {...existing} : {recipe: PALETTE_RECIPE_V2};
|
|
1313
|
-
const {settings: saved, mode: savedMode} = savedSettings(cwd, slug);
|
|
1314
|
-
// Every theme's palette has both modes: a one-mode theme keeps both
|
|
1315
|
-
// halves of its colors (its mode is the one the app's <Theme> is given),
|
|
1316
|
-
// and a region Core draws in the other mode reads the other half.
|
|
1317
|
-
const modes = modesForStrategy('light-and-dark');
|
|
1318
|
-
|
|
1319
|
-
const vibrancy = dialVibrancy(data.settings);
|
|
1320
|
-
const savedVibrancy = dialVibrancy(saved);
|
|
1321
|
-
if (vibrancy != null) {
|
|
1322
|
-
if (
|
|
1323
|
-
config.vibrancy != null &&
|
|
1324
|
-
savedVibrancy == null &&
|
|
1325
|
-
config.vibrancy !== vibrancy
|
|
1326
|
-
)
|
|
1327
|
-
notes.push(
|
|
1328
|
-
`${file}: your vibrancy ${config.vibrancy} there is replaced by the vibrancy setting, ${vibrancy}`,
|
|
1329
|
-
);
|
|
1330
|
-
config.vibrancy = vibrancy;
|
|
1331
|
-
} else if (savedVibrancy != null) delete config.vibrancy;
|
|
1332
|
-
else if (config.vibrancy != null)
|
|
1333
|
-
notes.push(
|
|
1334
|
-
`${file} keeps the vibrancy ${config.vibrancy} written there by hand; ${P} set ${slug} vibrancy ${config.vibrancy} makes it a setting`,
|
|
1335
|
-
);
|
|
1336
|
-
|
|
1337
|
-
const strategy = strategyOf(modes);
|
|
1338
|
-
// Only a value the editor did not write is a hand edit worth naming: the
|
|
1339
|
-
// one it wrote follows the theme's mode as it was saved.
|
|
1340
|
-
const wrote = savedMode ? strategyOf(modesOf({mode: savedMode})) : null;
|
|
1341
|
-
if (
|
|
1342
|
-
config.modeStrategy != null &&
|
|
1343
|
-
config.modeStrategy !== strategy &&
|
|
1344
|
-
config.modeStrategy !== wrote
|
|
1345
|
-
)
|
|
1346
|
-
notes.push(
|
|
1347
|
-
`${file}: modeStrategy ${config.modeStrategy} is now ${strategy}, the modes the theme has`,
|
|
1348
|
-
);
|
|
1349
|
-
config.modeStrategy = strategy;
|
|
1350
|
-
usableStops(config.stops, slug);
|
|
1351
|
-
|
|
1352
|
-
if (config.families != null && !Array.isArray(config.families))
|
|
1353
|
-
notes.push(
|
|
1354
|
-
`${file}: families was not a list, so it is made again from your color settings`,
|
|
1355
|
-
);
|
|
1356
|
-
/** @type {any[]} */
|
|
1357
|
-
const before = Array.isArray(config.families) ? config.families : [];
|
|
1358
|
-
const savedSeeds = dialFamilies(
|
|
1359
|
-
{settings: saved},
|
|
1360
|
-
ownSourceTokenValues(cwd, slug),
|
|
1361
|
-
);
|
|
1362
|
-
// A config the editor wrote holds the seeds it gave, from the settings and
|
|
1363
|
-
// the source as they were then: its seed is never a hand edit.
|
|
1364
|
-
const configMine = configWrittenByEditor(cwd, slug, data);
|
|
1365
|
-
|
|
1366
|
-
/**
|
|
1367
|
-
* One dial family: the author's family with the dial's seed and the
|
|
1368
|
-
* exact anchors of that seed in place of the old ones.
|
|
1369
|
-
* @param {any} prev
|
|
1370
|
-
* @param {DialFamily} f
|
|
1371
|
-
*/
|
|
1372
|
-
const familyOf = (prev, f) => {
|
|
1373
|
-
const old = prev && typeof prev === 'object' ? prev : {};
|
|
1374
|
-
const neutral = f.id === 'neutral';
|
|
1375
|
-
const wasSaved = configMine
|
|
1376
|
-
? old.seed
|
|
1377
|
-
: savedSeeds.find(x => x.id === f.id)?.seed;
|
|
1378
|
-
if (old.seed != null && wasSaved && !sameColor(old.seed, wasSaved))
|
|
1379
|
-
notes.push(
|
|
1380
|
-
`${file}: the ${f.id} seed ${old.seed} there is replaced by ${f.source ? `the ${f.source} your theme source sets` : `your ${f.id} setting`}, ${f.seed}`,
|
|
1381
|
-
);
|
|
1382
|
-
const {family: made, stop} = dialFamily(f, config, modes);
|
|
1383
|
-
// The setting's family as a fresh palette has it; of the author's
|
|
1384
|
-
// family, a chromatic kind stays.
|
|
1385
|
-
const kind =
|
|
1386
|
-
made.kind ?? (old.kind === 'chromatic' ? 'chromatic' : undefined);
|
|
1387
|
-
if (
|
|
1388
|
-
old.kind === 'neutral' &&
|
|
1389
|
-
!kind &&
|
|
1390
|
-
!neutral &&
|
|
1391
|
-
!(wasSaved && isGray(wasSaved))
|
|
1392
|
-
)
|
|
1393
|
-
notes.push(
|
|
1394
|
-
`${file}: the ${f.id} kind "neutral" is dropped: your ${f.id} is not a gray, and a neutral kind turns its shades gray where no exact color holds them`,
|
|
1395
|
-
);
|
|
1396
|
-
/** @type {any[]} */
|
|
1397
|
-
const mine = made.anchors ?? [];
|
|
1398
|
-
/** @type {any[]} */
|
|
1399
|
-
const kept = [];
|
|
1400
|
-
const anchors = Array.isArray(old.anchors) ? old.anchors : [];
|
|
1401
|
-
// The editor's own anchors: the seed's exact color at the one stop it
|
|
1402
|
-
// put it (light, else dark). The same color at another stop is the
|
|
1403
|
-
// author's.
|
|
1404
|
-
/** @param {any} a */
|
|
1405
|
-
const seedColored = a =>
|
|
1406
|
-
a?.policy === 'exact' &&
|
|
1407
|
-
(sameColor(a.color, old.seed) ||
|
|
1408
|
-
sameColor(a.color, f.seed) ||
|
|
1409
|
-
(wasSaved && sameColor(a.color, wasSaved)));
|
|
1410
|
-
const editorStop = (
|
|
1411
|
-
anchors.find(
|
|
1412
|
-
(/** @type {any} */ a) => seedColored(a) && a.mode === 'light',
|
|
1413
|
-
) ?? anchors.find((/** @type {any} */ a) => seedColored(a))
|
|
1414
|
-
)?.stop;
|
|
1415
|
-
for (const a of anchors) {
|
|
1416
|
-
if (seedColored(a) && a.stop === editorStop) continue;
|
|
1417
|
-
if (!modes.includes(a?.mode)) {
|
|
1418
|
-
notes.push(
|
|
1419
|
-
`${file}: the ${f.id} ${a?.mode} anchor at shade ${shade(a?.stop)} is dropped: the theme has no ${a?.mode} mode`,
|
|
1420
|
-
);
|
|
1421
|
-
continue;
|
|
1422
|
-
}
|
|
1423
|
-
if (mine.some(x => x.mode === a.mode && x.stop === a.stop)) {
|
|
1424
|
-
notes.push(
|
|
1425
|
-
`${file}: your ${a.mode} anchor at shade ${shade(a.stop)} of ${f.id} is replaced by your exact ${f.id} color`,
|
|
1426
|
-
);
|
|
1427
|
-
continue;
|
|
1428
|
-
}
|
|
1429
|
-
kept.push(a);
|
|
1430
|
-
}
|
|
1431
|
-
if (kept.length && wasSaved && !sameColor(wasSaved, f.seed))
|
|
1432
|
-
notes.push(
|
|
1433
|
-
`${file} keeps your own ${f.id} anchors (${kept.map(a => `${a.mode} ${a.stop} ${a.color}`).join(', ')}): they were set for your earlier ${f.id}, ${wasSaved}, and still pull its shades toward them`,
|
|
1434
|
-
);
|
|
1435
|
-
const out = {
|
|
1436
|
-
...old,
|
|
1437
|
-
id: f.id,
|
|
1438
|
-
name: old.name ?? made.name,
|
|
1439
|
-
seed: f.seed,
|
|
1440
|
-
...(kind ? {kind} : {}),
|
|
1441
|
-
anchors: [...mine, ...kept].sort((a, b) =>
|
|
1442
|
-
a.mode === b.mode ? a.stop - b.stop : a.mode === 'light' ? -1 : 1,
|
|
1443
|
-
),
|
|
1444
|
-
};
|
|
1445
|
-
if (!kind) delete out.kind;
|
|
1446
|
-
if (!out.anchors.length) delete out.anchors;
|
|
1447
|
-
// The keys always in one order, so a change and its undo write the
|
|
1448
|
-
// same bytes.
|
|
1449
|
-
/** @type {Record<string, unknown>} */
|
|
1450
|
-
const ordered = {};
|
|
1451
|
-
for (const k of FAMILY_INPUT_KEYS) if (k in out) ordered[k] = out[k];
|
|
1452
|
-
for (const k of Object.keys(out)) if (!(k in ordered)) ordered[k] = out[k];
|
|
1453
|
-
return {family: /** @type {typeof out} */ (ordered), stop};
|
|
1454
|
-
};
|
|
1455
|
-
|
|
1456
|
-
/** @type {PalettePlan['fills']} */
|
|
1457
|
-
const fills = {};
|
|
1458
|
-
const families = before
|
|
1459
|
-
.map(f => {
|
|
1460
|
-
if (!PALETTE_DIALS.includes(f?.id)) return f;
|
|
1461
|
-
const mine = fams.find(x => x.id === f.id);
|
|
1462
|
-
if (!mine) return null;
|
|
1463
|
-
const {family, stop} = familyOf(f, mine);
|
|
1464
|
-
if (mine.id !== 'neutral') fills[mine.id] = fillAt(stop);
|
|
1465
|
-
return family;
|
|
1466
|
-
})
|
|
1467
|
-
.filter(Boolean);
|
|
1468
|
-
for (const f of fams)
|
|
1469
|
-
if (!families.some(x => x?.id === f.id)) {
|
|
1470
|
-
const {family, stop} = familyOf(null, f);
|
|
1471
|
-
if (f.id !== 'neutral') fills[f.id] = fillAt(stop);
|
|
1472
|
-
families.push(family);
|
|
1473
|
-
}
|
|
1474
|
-
// The hues the editor grew for the look (lookSeeds) go, and are grown
|
|
1475
|
-
// again below while the theme has a color; any other hue the config
|
|
1476
|
-
// holds is the author's, and stays as written.
|
|
1477
|
-
const grown = isLookFamily;
|
|
1478
|
-
for (let i = families.length - 1; i >= 0; i--)
|
|
1479
|
-
if (grown(families[i])) families.splice(i, 1);
|
|
1480
|
-
if (!families.length) return null;
|
|
1481
|
-
// The editor's colors stand in the order of their settings, wherever they
|
|
1482
|
-
// sit among a config's own families, so a step back writes the same file.
|
|
1483
|
-
const slots = families
|
|
1484
|
-
.map((f, i) => (PALETTE_DIALS.includes(f?.id) ? i : -1))
|
|
1485
|
-
.filter(i => i >= 0);
|
|
1486
|
-
const inOrder = fams
|
|
1487
|
-
.map(x => families.find(f => f?.id === x.id))
|
|
1488
|
-
.filter(Boolean);
|
|
1489
|
-
if (inOrder.length === slots.length)
|
|
1490
|
-
slots.forEach((i, k) => (families[i] = inOrder[k]));
|
|
1491
|
-
config.families = families;
|
|
1492
|
-
// The category and chart hues grow with the look, after the theme's own
|
|
1493
|
-
// colors, and so do the status roles the theme leaves unset (the
|
|
1494
|
-
// editor's own grownFamilies); a family the author's config already holds stays
|
|
1495
|
-
// theirs. A status family that is not a setting is the editor's, grown
|
|
1496
|
-
// again at every plan.
|
|
1497
|
-
for (const family of editorLook.grownFamilies(fams, {
|
|
1498
|
-
base,
|
|
1499
|
-
reader: {rgb: parseColor, alpha: alphaOf},
|
|
1500
|
-
}))
|
|
1501
|
-
if (!families.some(f => f?.id === family.id)) families.push(family);
|
|
1502
|
-
// A color change on a palette made with the first recipe says what the
|
|
1503
|
-
// newer one does, and how to choose it.
|
|
1504
|
-
/** @param {any} c */
|
|
1505
|
-
const colorsOf = c =>
|
|
1506
|
-
JSON.stringify([
|
|
1507
|
-
c?.vibrancy ?? null,
|
|
1508
|
-
(Array.isArray(c?.families) ? c.families : []).map(
|
|
1509
|
-
(/** @type {any} */ f) => [f?.id, String(f?.seed ?? '').toLowerCase()],
|
|
1510
|
-
),
|
|
1511
|
-
]);
|
|
1512
|
-
if (
|
|
1513
|
-
own &&
|
|
1514
|
-
(config.recipe ?? PALETTE_RECIPE) === PALETTE_RECIPE &&
|
|
1515
|
-
colorsOf(existing) !== colorsOf(config)
|
|
1516
|
-
)
|
|
1517
|
-
notes.push(
|
|
1518
|
-
`${file} makes its colors with the ${PALETTE_RECIPE} recipe it was made with, whose pale shades stay at the edge of what a screen shows whatever the vibrancy; "recipe": "${PALETTE_RECIPE_V2}" there makes them follow the vibrancy and each color's own strength (its colors change once)`,
|
|
1519
|
-
);
|
|
1520
|
-
const dead = deadKeys(config);
|
|
1521
|
-
if (dead.length)
|
|
1522
|
-
notes.push(
|
|
1523
|
-
`${file}: the color generator does not read ${dead.join(', ')}, so ${dead.length > 1 ? 'they change' : 'it changes'} nothing`,
|
|
1524
|
-
);
|
|
1525
|
-
if (config.neutralProfile != null) {
|
|
1526
|
-
const grays = families.filter(f => f?.kind === 'neutral');
|
|
1527
|
-
const unheld = grays.filter(f =>
|
|
1528
|
-
modes.some(
|
|
1529
|
-
m => !(f.anchors ?? []).some((/** @type {any} */ a) => a.mode === m),
|
|
1530
|
-
),
|
|
1531
|
-
);
|
|
1532
|
-
if (!unheld.length)
|
|
1533
|
-
notes.push(
|
|
1534
|
-
`${file}: neutralProfile ${config.neutralProfile} changes nothing here: ${grays.length ? 'each gray color holds its exact color in every mode, and that sets its grays' : 'no color is a gray'}`,
|
|
1535
|
-
);
|
|
1536
|
-
}
|
|
1537
|
-
|
|
1538
|
-
// The exact color anchors dark mode too when it reads there, as the
|
|
1539
|
-
// official themes anchor a brand color in both modes.
|
|
1540
|
-
if (modes.includes('dark') && Object.keys(fills).length) {
|
|
1541
|
-
try {
|
|
1542
|
-
config.families = anchorDarkFills(
|
|
1543
|
-
config,
|
|
1544
|
-
fams,
|
|
1545
|
-
fills,
|
|
1546
|
-
paletteContext(cwd, slug, data, base),
|
|
1547
|
-
(id, stop) =>
|
|
1548
|
-
notes.push(
|
|
1549
|
-
`${file}: your dark anchor at shade ${shade(stop)} of ${id} is replaced by your exact ${id} color`,
|
|
1550
|
-
),
|
|
1551
|
-
);
|
|
1552
|
-
} catch (error) {
|
|
1553
|
-
// A config the generator refuses is refused as the editor refuses
|
|
1554
|
-
// it, naming the file.
|
|
1555
|
-
generate(config, file);
|
|
1556
|
-
throw error;
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
return {config, notes, fills};
|
|
1560
|
-
}
|
|
1561
|
-
|
|
1562
|
-
/**
|
|
1563
|
-
* The palette config a theme's settings ask for (see {@link planPalette}),
|
|
1564
|
-
* or null when it would hold no family.
|
|
1565
|
-
* @param {string} cwd
|
|
1566
|
-
* @param {string} slug
|
|
1567
|
-
* @param {{mode?: string, settings: Setting[]}} data
|
|
1568
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} [base]
|
|
1569
|
-
* @returns {any}
|
|
1570
|
-
*/
|
|
1571
|
-
export function paletteConfig(cwd, slug, data, base) {
|
|
1572
|
-
return planPalette(cwd, slug, data, base)?.config ?? null;
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
/**
|
|
1576
|
-
* Write the palette config, palette and receipt the settings ask for, with
|
|
1577
|
-
* the same generator as `theme palette generate --out`; remove them all
|
|
1578
|
-
* when no family is left. The palette is generated again when the config
|
|
1579
|
-
* changes, when a file is missing, or when it came from another generator
|
|
1580
|
-
* version. A palette edited by hand ({@link paletteEdited}) is never
|
|
1581
|
-
* overwritten: a change that would rewrite it is refused with the commands
|
|
1582
|
-
* that keep or discard the edit; a change that leaves it alone goes ahead. Gives back what the config writer kept or replaced
|
|
1583
|
-
* of a hand edit, for the result notes.
|
|
1584
|
-
* @param {string} cwd
|
|
1585
|
-
* @param {string} slug
|
|
1586
|
-
* @param {{mode?: string, settings: Setting[], palette?: string}} data
|
|
1587
|
-
* `palette`: the hash of the config the editor wrote, kept in the settings
|
|
1588
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} [base]
|
|
1589
|
-
* @returns {string[]}
|
|
1590
|
-
*/
|
|
1591
|
-
export function syncPalette(cwd, slug, data, base) {
|
|
1592
|
-
keptDamaged.length = 0;
|
|
1593
|
-
// The config is read first, so a damaged one is kept aside before the
|
|
1594
|
-
// sync writes it again.
|
|
1595
|
-
const config = path.join(cwd, paletteFiles(slug).config);
|
|
1596
|
-
readConfig(config);
|
|
1597
|
-
keepDamaged(config);
|
|
1598
|
-
const notes = syncPaletteFiles(cwd, slug, data, base);
|
|
1599
|
-
return [...keptDamaged.splice(0), ...notes];
|
|
1600
|
-
}
|
|
1601
|
-
|
|
1602
|
-
/**
|
|
1603
|
-
* Whether the editor wrote the theme's palette config as it is now: its hash
|
|
1604
|
-
* is the one the settings hold (`data.palette`, or the saved settings, which
|
|
1605
|
-
* an undo may carry from before the palette existed), or the one the editor
|
|
1606
|
-
* recorded writing (a color change stopped before its settings were saved).
|
|
1607
|
-
* @param {string} cwd
|
|
1608
|
-
* @param {string} slug
|
|
1609
|
-
* @param {{palette?: string}} data
|
|
1610
|
-
*/
|
|
1611
|
-
function configWrittenByEditor(cwd, slug, data) {
|
|
1612
|
-
const file = path.join(cwd, paletteFiles(slug).config);
|
|
1613
|
-
if (!fs.existsSync(file)) return false;
|
|
1614
|
-
const hash = sha256(fs.readFileSync(file));
|
|
1615
|
-
/** @type {string | undefined} */
|
|
1616
|
-
let saved;
|
|
1617
|
-
try {
|
|
1618
|
-
saved = JSON.parse(
|
|
1619
|
-
fs.readFileSync(
|
|
1620
|
-
path.join(cwd, 'themes', slug, `${slug}.settings.json`),
|
|
1621
|
-
'utf8',
|
|
1622
|
-
),
|
|
1623
|
-
).palette;
|
|
1624
|
-
} catch {
|
|
1625
|
-
saved = undefined;
|
|
1626
|
-
}
|
|
1627
|
-
return (
|
|
1628
|
-
data.palette === hash ||
|
|
1629
|
-
saved === hash ||
|
|
1630
|
-
readWrittenRecord(cwd, slug)['palette.config.json'] === hash
|
|
1631
|
-
);
|
|
1632
|
-
}
|
|
1633
|
-
|
|
1634
|
-
/**
|
|
1635
|
-
* @param {string} cwd
|
|
1636
|
-
* @param {string} slug
|
|
1637
|
-
* @param {any} data
|
|
1638
|
-
* @param {any} base
|
|
1639
|
-
* @returns {string[]}
|
|
1640
|
-
*/
|
|
1641
|
-
function syncPaletteFiles(cwd, slug, data, base) {
|
|
1642
|
-
const f = paletteFiles(slug);
|
|
1643
|
-
const abs = (/** @type {string} */ p) => path.join(cwd, p);
|
|
1644
|
-
const plan = planPalette(cwd, slug, data, base);
|
|
1645
|
-
if (!plan) {
|
|
1646
|
-
// Only palette files the editor wrote go: a hand-made palette.config.json
|
|
1647
|
-
// (and what it generated) stays.
|
|
1648
|
-
const mine = configWrittenByEditor(cwd, slug, data);
|
|
1649
|
-
delete data.palette;
|
|
1650
|
-
if (!mine) return [];
|
|
1651
|
-
// A palette file edited by hand is never deleted: it stays, and the
|
|
1652
|
-
// result names it. Its config and receipt go, so the next color change
|
|
1653
|
-
// asks whether to keep or discard it.
|
|
1654
|
-
// The selected-stops file is generated from the palette on every
|
|
1655
|
-
// change, so it always goes.
|
|
1656
|
-
const editedPalette = paletteEdited(cwd, slug);
|
|
1657
|
-
/** @type {string[]} */
|
|
1658
|
-
const notes = [];
|
|
1659
|
-
for (const p of [f.config, f.receipt, f.refs])
|
|
1660
|
-
fs.rmSync(abs(p), {force: true});
|
|
1661
|
-
noteWritten(cwd, slug, 'palette.config.json', null);
|
|
1662
|
-
if (editedPalette)
|
|
1663
|
-
notes.push(
|
|
1664
|
-
`${f.palette} was edited by hand, so it stays: the theme no longer reads it. Move it out of tokens/ to keep it as your own, or delete it`,
|
|
1665
|
-
);
|
|
1666
|
-
else fs.rmSync(abs(f.palette), {force: true});
|
|
1667
|
-
const tokensDir = path.dirname(abs(f.palette));
|
|
1668
|
-
if (fs.existsSync(tokensDir) && !fs.readdirSync(tokensDir).length)
|
|
1669
|
-
fs.rmdirSync(tokensDir);
|
|
1670
|
-
return notes;
|
|
1671
|
-
}
|
|
1672
|
-
// A palette this CLI cannot make again keeps its files as they are.
|
|
1673
|
-
// (Its note rides on the mapping, paletteMapping, once.)
|
|
1674
|
-
if (plan.stored) {
|
|
1675
|
-
data.palette = sha256(fs.readFileSync(abs(f.config)));
|
|
1676
|
-
return [];
|
|
1677
|
-
}
|
|
1678
|
-
const text = `${JSON.stringify(plan.config, null, 2)}\n`;
|
|
1679
|
-
/** @type {any} */
|
|
1680
|
-
let receipt;
|
|
1681
|
-
try {
|
|
1682
|
-
receipt = JSON.parse(fs.readFileSync(abs(f.receipt), 'utf8'));
|
|
1683
|
-
} catch {
|
|
1684
|
-
receipt = null;
|
|
1685
|
-
}
|
|
1686
|
-
const same =
|
|
1687
|
-
fs.existsSync(abs(f.config)) &&
|
|
1688
|
-
fs.readFileSync(abs(f.config), 'utf8') === text &&
|
|
1689
|
-
fs.existsSync(abs(f.palette)) &&
|
|
1690
|
-
receipt?.generatorVersion === PALETTE_GENERATOR_VERSION;
|
|
1691
|
-
// Nothing to rewrite: no notes either, so a kept hand edit is named once,
|
|
1692
|
-
// when a color change touches the config.
|
|
1693
|
-
if (same) {
|
|
1694
|
-
data.palette = sha256(Buffer.from(text));
|
|
1695
|
-
// The theme reads only the selected shades, which come from the
|
|
1696
|
-
// generator, so a hand edit to the palette file never shows: say so.
|
|
1697
|
-
return paletteEdited(cwd, slug)
|
|
1698
|
-
? [
|
|
1699
|
-
`${f.palette} was edited by hand, but the theme never reads it: it reads ${f.refs}, which the editor writes from the generator, so your edit does not show. The next color change asks whether to keep or discard it`,
|
|
1700
|
-
]
|
|
1701
|
-
: [];
|
|
1702
|
-
}
|
|
1703
|
-
if (paletteEdited(cwd, slug)) {
|
|
1704
|
-
throw new AstryxError(
|
|
1705
|
-
`${f.palette} was edited by hand, so the editor will not write over it. The theme never read the edit: it reads ${f.refs}, which the editor writes from the generator. To keep your edits, move the file out of tokens/ (it is then yours: import it in ${slug}.theme.ts); to discard them, delete it. Then run the command again, and the editor writes the generated shades back. Only color changes wait for this; other changes still work. Nothing was changed.`,
|
|
1706
|
-
[
|
|
1707
|
-
{
|
|
1708
|
-
name: `${P} show ${slug}`,
|
|
1709
|
-
reason: `do this next: after you move ${f.palette} out of tokens/ (keeps your edits) or delete it (discards them), run your command again; show lists the theme as it is now`,
|
|
1710
|
-
},
|
|
1711
|
-
],
|
|
1712
|
-
ERROR_CODES.ERR_THEME_INVALID,
|
|
1713
|
-
);
|
|
1714
|
-
}
|
|
1715
|
-
fs.mkdirSync(path.dirname(abs(f.config)), {recursive: true});
|
|
1716
|
-
// Recorded before it is written: a run stopped after this leaves a config
|
|
1717
|
-
// the next build knows is the editor's.
|
|
1718
|
-
noteWritten(cwd, slug, 'palette.config.json', sha256(Buffer.from(text)));
|
|
1719
|
-
writeWhole(abs(f.config), text);
|
|
1720
|
-
data.palette = sha256(Buffer.from(text));
|
|
1721
|
-
themePaletteGenerate(f.config, {out: f.palette, overwrite: true}, {cwd});
|
|
1722
|
-
return plan.notes;
|
|
1723
|
-
}
|
|
1724
|
-
|
|
1725
|
-
/**
|
|
1726
|
-
* The record of the files the editor wrote into a theme folder,
|
|
1727
|
-
* `themes/<theme>/<theme>.pictures.json`: each by name, with the hash of
|
|
1728
|
-
* what was written (its pictures and its color config). The one reader and
|
|
1729
|
-
* writer of that record.
|
|
1730
|
-
* @param {string} cwd
|
|
1731
|
-
* @param {string} slug
|
|
1732
|
-
* @returns {Record<string, string>}
|
|
1733
|
-
*/
|
|
1734
|
-
export function readWrittenRecord(cwd, slug) {
|
|
1735
|
-
return readRecordIn(path.join(cwd, 'themes', slug), slug);
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
/**
|
|
1739
|
-
* The record of what the editor wrote, read from a theme folder as it is
|
|
1740
|
-
* (a copy of one included); empty when there is none or it is damaged.
|
|
1741
|
-
* @param {string} dir
|
|
1742
|
-
* @param {string} slug
|
|
1743
|
-
* @returns {Record<string, string>}
|
|
1744
|
-
*/
|
|
1745
|
-
export function readRecordIn(dir, slug) {
|
|
1746
|
-
try {
|
|
1747
|
-
const json = JSON.parse(
|
|
1748
|
-
readRegularFile(path.join(dir, `${slug}.pictures.json`), 'utf8'),
|
|
1749
|
-
);
|
|
1750
|
-
return json && typeof json === 'object' && !Array.isArray(json) ? json : {};
|
|
1751
|
-
} catch {
|
|
1752
|
-
return {};
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
|
-
|
|
1756
|
-
/** @param {string} cwd @param {string} slug */
|
|
1757
|
-
const writtenRecordFile = (cwd, slug) =>
|
|
1758
|
-
path.join(cwd, 'themes', slug, `${slug}.pictures.json`);
|
|
1759
|
-
|
|
1760
|
-
/**
|
|
1761
|
-
* Write the record whole, or remove it when it names nothing. A record that
|
|
1762
|
-
* cannot be written leaves nothing removed later: the safe way round.
|
|
1763
|
-
* @param {string} cwd
|
|
1764
|
-
* @param {string} slug
|
|
1765
|
-
* @param {Record<string, string>} map
|
|
1766
|
-
*/
|
|
1767
|
-
export function writeWrittenRecord(cwd, slug, map) {
|
|
1768
|
-
const file = writtenRecordFile(cwd, slug);
|
|
1769
|
-
// A link of the user's at the record's name is left as it is.
|
|
1770
|
-
if (fs.lstatSync(file, {throwIfNoEntry: false})?.isSymbolicLink()) return;
|
|
1771
|
-
try {
|
|
1772
|
-
if (!Object.keys(map).length) fs.rmSync(file, {force: true});
|
|
1773
|
-
else {
|
|
1774
|
-
fs.mkdirSync(path.dirname(file), {recursive: true});
|
|
1775
|
-
writeWhole(file, `${JSON.stringify(map, null, 2)}\n`);
|
|
1776
|
-
}
|
|
1777
|
-
} catch {
|
|
1778
|
-
// Without the record, nothing it names is removed later.
|
|
1779
|
-
}
|
|
1780
|
-
}
|
|
1781
|
-
|
|
1782
|
-
/**
|
|
1783
|
-
* Note a file the editor wrote into a theme folder, by name with the hash
|
|
1784
|
-
* of what it wrote, or forget it (null).
|
|
1785
|
-
* @param {string} cwd
|
|
1786
|
-
* @param {string} slug
|
|
1787
|
-
* @param {string} name
|
|
1788
|
-
* @param {string | null} hash
|
|
1789
|
-
*/
|
|
1790
|
-
export function noteWritten(cwd, slug, name, hash) {
|
|
1791
|
-
const map = readWrittenRecord(cwd, slug);
|
|
1792
|
-
if (hash) map[name] = hash;
|
|
1793
|
-
else if (name in map) delete map[name];
|
|
1794
|
-
else return;
|
|
1795
|
-
writeWrittenRecord(cwd, slug, map);
|
|
1796
|
-
}
|
|
1797
|
-
|
|
1798
|
-
/**
|
|
1799
|
-
* The value of the object literal that starts at the first `{` from
|
|
1800
|
-
* `from`, read as data: string and numeric keys, string values, nested
|
|
1801
|
-
* objects; quotes, commas, comments and white space as any formatter writes
|
|
1802
|
-
* them. Null when it is not such a literal.
|
|
1803
|
-
* @param {string} text
|
|
1804
|
-
* @param {number} from
|
|
1805
|
-
* @returns {any}
|
|
1806
|
-
*/
|
|
1807
|
-
function objectLiteral(text, from) {
|
|
1808
|
-
const open = text.indexOf('{', from);
|
|
1809
|
-
if (open < 0) return null;
|
|
1810
|
-
/** @type {string[]} */
|
|
1811
|
-
const json = [];
|
|
1812
|
-
let depth = 0;
|
|
1813
|
-
let i = open;
|
|
1814
|
-
while (i < text.length) {
|
|
1815
|
-
const c = text[i];
|
|
1816
|
-
if (c === '/' && text[i + 1] === '/') {
|
|
1817
|
-
i = text.indexOf('\n', i);
|
|
1818
|
-
if (i < 0) return null;
|
|
1819
|
-
continue;
|
|
1820
|
-
}
|
|
1821
|
-
if (c === '/' && text[i + 1] === '*') {
|
|
1822
|
-
i = text.indexOf('*/', i + 2);
|
|
1823
|
-
if (i < 0) return null;
|
|
1824
|
-
i += 2;
|
|
1825
|
-
continue;
|
|
1826
|
-
}
|
|
1827
|
-
if (c === '"' || c === "'") {
|
|
1828
|
-
let j = i + 1;
|
|
1829
|
-
let v = '';
|
|
1830
|
-
while (j < text.length && text[j] !== c) {
|
|
1831
|
-
if (text[j] === '\\') j++;
|
|
1832
|
-
v += text[j++];
|
|
1833
|
-
}
|
|
1834
|
-
json.push(JSON.stringify(v));
|
|
1835
|
-
i = j + 1;
|
|
1836
|
-
continue;
|
|
1837
|
-
}
|
|
1838
|
-
if (/[\s]/.test(c)) {
|
|
1839
|
-
i++;
|
|
1840
|
-
continue;
|
|
1841
|
-
}
|
|
1842
|
-
if (c === '{' || c === '[') depth++;
|
|
1843
|
-
if (c === '}' || c === ']') {
|
|
1844
|
-
// A trailing comma before a closing bracket.
|
|
1845
|
-
if (json.at(-1) === ',') json.pop();
|
|
1846
|
-
depth--;
|
|
1847
|
-
}
|
|
1848
|
-
if ('{}[]:,'.includes(c)) {
|
|
1849
|
-
json.push(c);
|
|
1850
|
-
i++;
|
|
1851
|
-
if (depth === 0) break;
|
|
1852
|
-
continue;
|
|
1853
|
-
}
|
|
1854
|
-
const word = /^[\w.$+-]+/.exec(text.slice(i))?.[0];
|
|
1855
|
-
if (!word) return null;
|
|
1856
|
-
json.push(JSON.stringify(word));
|
|
1857
|
-
i += word.length;
|
|
1858
|
-
}
|
|
1859
|
-
try {
|
|
1860
|
-
return JSON.parse(json.join(''));
|
|
1861
|
-
} catch {
|
|
1862
|
-
return null;
|
|
1863
|
-
}
|
|
1864
|
-
}
|
|
1865
|
-
|
|
1866
|
-
/**
|
|
1867
|
-
* The colors a palette module holds, read as data so formatting never
|
|
1868
|
-
* counts: `palette` (family, mode, stop, color) and `black`/`white`. Null
|
|
1869
|
-
* when the file does not read as one.
|
|
1870
|
-
* @param {string} text
|
|
1871
|
-
* @returns {{palette: any, black: string | null, white: string | null} | null}
|
|
1872
|
-
*/
|
|
1873
|
-
function paletteData(text) {
|
|
1874
|
-
const t = text.replace(/^\uFEFF/, '');
|
|
1875
|
-
const at = /\bconst\s+palette\s*=/.exec(t);
|
|
1876
|
-
const palette = at ? objectLiteral(t, at.index) : null;
|
|
1877
|
-
if (!palette || typeof palette !== 'object') return null;
|
|
1878
|
-
/** @param {string} name */
|
|
1879
|
-
const end = name =>
|
|
1880
|
-
new RegExp(`\\bconst\\s+${name}\\s*=\\s*["'](#[0-9a-fA-F]{6})["']`)
|
|
1881
|
-
.exec(t)?.[1]
|
|
1882
|
-
?.toLowerCase() ?? null;
|
|
1883
|
-
return {palette, black: end('black'), white: end('white')};
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1886
|
-
/**
|
|
1887
|
-
* Whether two palettes hold the same colors at the same stops.
|
|
1888
|
-
* @param {any} a @param {any} b
|
|
1889
|
-
*/
|
|
1890
|
-
function samePalette(a, b) {
|
|
1891
|
-
/** @param {any} p */
|
|
1892
|
-
const flat = p => {
|
|
1893
|
-
/** @type {string[]} */
|
|
1894
|
-
const out = [];
|
|
1895
|
-
for (const [fam, f] of Object.entries(p ?? {}))
|
|
1896
|
-
for (const m of /** @type {Mode[]} */ (['light', 'dark']))
|
|
1897
|
-
for (const [stop, color] of Object.entries(
|
|
1898
|
-
/** @type {any} */ (f)?.[m] ?? {},
|
|
1899
|
-
))
|
|
1900
|
-
out.push(
|
|
1901
|
-
`${fam}.${m}.${Number(stop)}=${String(color).toLowerCase()}`,
|
|
1902
|
-
);
|
|
1903
|
-
return out.sort().join('|');
|
|
1904
|
-
};
|
|
1905
|
-
return flat(a) === flat(b);
|
|
1906
|
-
}
|
|
1907
|
-
|
|
1908
|
-
/**
|
|
1909
|
-
* The first line of the empty palette module the preview writes for a
|
|
1910
|
-
* Colors page it keeps (changed by hand) once the theme has no colors, so
|
|
1911
|
-
* the app still builds. The editor replaces it freely.
|
|
1912
|
-
*/
|
|
1913
|
-
export const PALETTE_STAND_IN =
|
|
1914
|
-
'// A stand-in for a preview page that imports this file: the theme has no generated colors.';
|
|
1915
|
-
|
|
1916
|
-
/**
|
|
1917
|
-
* Whether a theme's palette file was edited by hand. Its bytes match its
|
|
1918
|
-
* receipt, or its colors (read as data, so formatting, a BOM or semicolons
|
|
1919
|
-
* never count) are the ones the generator gives for the request it came
|
|
1920
|
-
* from: the receipt's, else the config the editor wrote. Without a receipt
|
|
1921
|
-
* and with a config edited by hand since, the request is unknown; then the
|
|
1922
|
-
* file counts as the editor's when it holds every shade the theme ships
|
|
1923
|
-
* (the selected-stops file) unchanged. False when there is no palette file.
|
|
1924
|
-
* @param {string} cwd
|
|
1925
|
-
* @param {string} slug
|
|
1926
|
-
*/
|
|
1927
|
-
export function paletteEdited(cwd, slug) {
|
|
1928
|
-
const f = paletteFiles(slug);
|
|
1929
|
-
const file = path.join(cwd, f.palette);
|
|
1930
|
-
if (!fs.existsSync(file)) return false;
|
|
1931
|
-
const text = fs.readFileSync(file, 'utf8');
|
|
1932
|
-
// The empty stand-in a kept preview page imports is the editor's own.
|
|
1933
|
-
if (text.startsWith(PALETTE_STAND_IN)) return false;
|
|
1934
|
-
/** @type {any} */
|
|
1935
|
-
let receipt;
|
|
1936
|
-
try {
|
|
1937
|
-
receipt = JSON.parse(fs.readFileSync(path.join(cwd, f.receipt), 'utf8'));
|
|
1938
|
-
} catch {
|
|
1939
|
-
receipt = null;
|
|
1940
|
-
}
|
|
1941
|
-
if (receipt?.candidateSha256 === sha256(Buffer.from(text))) return false;
|
|
1942
|
-
const data = paletteData(text);
|
|
1943
|
-
if (!data) return true;
|
|
1944
|
-
/** @type {any} */
|
|
1945
|
-
let request = receipt?.request ?? null;
|
|
1946
|
-
let configMine = false;
|
|
1947
|
-
if (!request)
|
|
1948
|
-
try {
|
|
1949
|
-
const configText = fs.readFileSync(path.join(cwd, f.config), 'utf8');
|
|
1950
|
-
const saved = JSON.parse(
|
|
1951
|
-
fs.readFileSync(
|
|
1952
|
-
path.join(cwd, 'themes', slug, `${slug}.settings.json`),
|
|
1953
|
-
'utf8',
|
|
1954
|
-
),
|
|
1955
|
-
)?.palette;
|
|
1956
|
-
configMine = saved == null || saved === sha256(Buffer.from(configText));
|
|
1957
|
-
request = JSON.parse(configText.replace(/^\uFEFF/, ''));
|
|
1958
|
-
} catch {
|
|
1959
|
-
request = null;
|
|
1960
|
-
}
|
|
1961
|
-
if (request && (receipt?.request || configMine))
|
|
1962
|
-
try {
|
|
1963
|
-
const candidate = generateTonalPalette(request);
|
|
1964
|
-
return !(
|
|
1965
|
-
samePalette(candidate.palette, data.palette) &&
|
|
1966
|
-
(data.black == null || data.black === candidate.black) &&
|
|
1967
|
-
(data.white == null || data.white === candidate.white)
|
|
1968
|
-
);
|
|
1969
|
-
} catch {
|
|
1970
|
-
return true;
|
|
1971
|
-
}
|
|
1972
|
-
return refsDiffer(cwd, slug, data.palette) !== false;
|
|
1973
|
-
}
|
|
1974
|
-
|
|
1975
|
-
/**
|
|
1976
|
-
* Whether the selected-stops file holds a shade that differs from the
|
|
1977
|
-
* palette's: true when one differs, false when every one matches, null
|
|
1978
|
-
* when there is no such file to compare.
|
|
1979
|
-
* @param {string} cwd
|
|
1980
|
-
* @param {string} slug
|
|
1981
|
-
* @param {any} palette the palette file's colors
|
|
1982
|
-
* @returns {boolean | null}
|
|
1983
|
-
*/
|
|
1984
|
-
function refsDiffer(cwd, slug, palette) {
|
|
1985
|
-
const file = path.join(cwd, paletteFiles(slug).refs);
|
|
1986
|
-
if (!fs.existsSync(file)) return null;
|
|
1987
|
-
const refs = objectLiteral(fs.readFileSync(file, 'utf8'), 0);
|
|
1988
|
-
if (!refs || typeof refs !== 'object') return true;
|
|
1989
|
-
for (const [fam, f] of Object.entries(refs)) {
|
|
1990
|
-
if (typeof f !== 'object' || !f) continue;
|
|
1991
|
-
for (const [m, ramp] of Object.entries(/** @type {any} */ (f)))
|
|
1992
|
-
for (const [stop, color] of Object.entries(/** @type {any} */ (ramp))) {
|
|
1993
|
-
const mine = palette?.[fam]?.[m];
|
|
1994
|
-
const at = mine
|
|
1995
|
-
? Object.entries(mine).find(([k]) => Number(k) === Number(stop))?.[1]
|
|
1996
|
-
: undefined;
|
|
1997
|
-
if (String(at ?? '').toLowerCase() !== String(color).toLowerCase())
|
|
1998
|
-
return true;
|
|
1999
|
-
}
|
|
2000
|
-
}
|
|
2001
|
-
return false;
|
|
2002
|
-
}
|
|
2003
|
-
|
|
2004
|
-
/**
|
|
2005
|
-
* The stops a source reads: `palette.<family>.<mode>[<stop>]`, any number
|
|
2006
|
-
* as the stop (a layout may have 12.5), and `palette.black` / `palette.white`.
|
|
2007
|
-
*/
|
|
2008
|
-
const READS =
|
|
2009
|
-
/\bpalette(?:\.([a-z]\w*)|\["([a-z][a-z0-9-]*)"\])\.(light|dark)\[(\d+(?:\.\d+)?)\]|\bpalette\.(black|white)\b/g;
|
|
2010
|
-
|
|
2011
|
-
/** What the generated settings write for a stop before it becomes a reference. */
|
|
2012
|
-
const PLACEHOLDER = '@@palette:';
|
|
2013
|
-
|
|
2014
|
-
/**
|
|
2015
|
-
* Refuse generated text that still holds a stop placeholder: the CSS would
|
|
2016
|
-
* carry it as a value no browser reads.
|
|
2017
|
-
* @param {string} file shown in the refusal
|
|
2018
|
-
* @param {string} text
|
|
2019
|
-
*/
|
|
2020
|
-
export function assertNoPlaceholder(file, text) {
|
|
2021
|
-
const at = text.indexOf(PLACEHOLDER);
|
|
2022
|
-
if (at < 0) return;
|
|
2023
|
-
const what = text.slice(at, at + 60).split(/["\s,)]/)[0];
|
|
2024
|
-
throw new AstryxError(
|
|
2025
|
-
`${file} would hold ${what}, a color reference that never resolved, so the change was not made. This is a bug in the editor; palette.config.json may hold a stop layout it cannot write. Nothing was changed.`,
|
|
2026
|
-
[],
|
|
2027
|
-
ERROR_CODES.ERR_THEME_INVALID,
|
|
2028
|
-
);
|
|
2029
|
-
}
|
|
2030
|
-
|
|
2031
|
-
/**
|
|
2032
|
-
* The module of only the stops a generated settings source reads, in the
|
|
2033
|
-
* shape `scripts/generate-selected-palette-refs.mjs` gives the official
|
|
2034
|
-
* themes, or null when it reads none.
|
|
2035
|
-
* @param {string} slug
|
|
2036
|
-
* @param {Candidate} candidate
|
|
2037
|
-
* @param {string} source
|
|
2038
|
-
* @returns {string | null}
|
|
2039
|
-
*/
|
|
2040
|
-
export function paletteRefsSource(slug, candidate, source) {
|
|
2041
|
-
/** @type {Map<string, Map<Mode, Set<number>>>} */
|
|
2042
|
-
const used = new Map();
|
|
2043
|
-
/** @type {Set<'black' | 'white'>} */
|
|
2044
|
-
const ends = new Set();
|
|
2045
|
-
for (const m of source.matchAll(READS)) {
|
|
2046
|
-
if (m[5]) {
|
|
2047
|
-
ends.add(/** @type {'black' | 'white'} */ (m[5]));
|
|
2048
|
-
continue;
|
|
2049
|
-
}
|
|
2050
|
-
const family = m[1] ?? m[2];
|
|
2051
|
-
const modes = used.get(family) ?? new Map();
|
|
2052
|
-
used.set(family, modes);
|
|
2053
|
-
const mode = /** @type {Mode} */ (m[3]);
|
|
2054
|
-
(modes.get(mode) ?? modes.set(mode, new Set()).get(mode))?.add(
|
|
2055
|
-
Number(m[4]),
|
|
2056
|
-
);
|
|
2057
|
-
}
|
|
2058
|
-
if (!used.size && !ends.size) return null;
|
|
2059
|
-
const key = (/** @type {string} */ k) =>
|
|
2060
|
-
/^[A-Za-z_$][\w$]*$/.test(k) ? k : JSON.stringify(k);
|
|
2061
|
-
/** @type {string[]} */
|
|
2062
|
-
const body = [];
|
|
2063
|
-
for (const [family, modes] of used) {
|
|
2064
|
-
body.push(` ${key(family)}: {`);
|
|
2065
|
-
for (const mode of /** @type {Mode[]} */ (['light', 'dark'])) {
|
|
2066
|
-
const stops = modes.get(mode);
|
|
2067
|
-
if (!stops) continue;
|
|
2068
|
-
const ramp = /** @type {any} */ (candidate.palette[family])?.[mode] ?? {};
|
|
2069
|
-
const pairs = [...stops]
|
|
2070
|
-
.sort((a, b) => a - b)
|
|
2071
|
-
.map(s => `${s}: '${ramp[s]}'`);
|
|
2072
|
-
body.push(` ${mode}: {${pairs.join(', ')}},`);
|
|
2073
|
-
}
|
|
2074
|
-
body.push(' },');
|
|
2075
|
-
}
|
|
2076
|
-
for (const end of ends)
|
|
2077
|
-
body.push(
|
|
2078
|
-
` ${end}: '${end === 'black' ? PALETTE_BLACK : PALETTE_WHITE}',`,
|
|
2079
|
-
);
|
|
2080
|
-
return [
|
|
2081
|
-
`// Generated by \`${P}\` on every change: do not edit.`,
|
|
2082
|
-
`// Only the shades ${slug}.settings.ts reads, from ${slug}.palette.ts, so the`,
|
|
2083
|
-
'// theme ships no shade it does not use.',
|
|
2084
|
-
'// prettier-ignore',
|
|
2085
|
-
`export const ${refsName(slug)} = {`,
|
|
2086
|
-
...body,
|
|
2087
|
-
'};',
|
|
2088
|
-
'',
|
|
2089
|
-
].join('\n');
|
|
2090
|
-
}
|
|
2091
|
-
|
|
2092
|
-
/**
|
|
2093
|
-
* Write the selected stops module beside the palette for a generated
|
|
2094
|
-
* settings source, or remove it when the source reads no stop. A source
|
|
2095
|
-
* that still holds a stop placeholder is refused.
|
|
2096
|
-
* @param {string} cwd
|
|
2097
|
-
* @param {string} slug
|
|
2098
|
-
* @param {string} source the generated settings source
|
|
2099
|
-
* @param {PaletteMapping | null} pal
|
|
2100
|
-
*/
|
|
2101
|
-
export function syncPaletteRefs(cwd, slug, source, pal) {
|
|
2102
|
-
assertNoPlaceholder(`themes/${slug}/${slug}.settings.ts`, source);
|
|
2103
|
-
const file = path.join(cwd, paletteFiles(slug).refs);
|
|
2104
|
-
const text = pal ? paletteRefsSource(slug, pal.candidate, source) : null;
|
|
2105
|
-
if (text == null) {
|
|
2106
|
-
fs.rmSync(file, {force: true});
|
|
2107
|
-
return;
|
|
2108
|
-
}
|
|
2109
|
-
fs.mkdirSync(path.dirname(file), {recursive: true});
|
|
2110
|
-
writeWhole(file, text);
|
|
2111
|
-
}
|
|
2112
|
-
|
|
2113
|
-
/**
|
|
2114
|
-
* A palette made with a recipe this CLI does not know, with the file left as
|
|
2115
|
-
* it is. A newer CLI's recipe is no fault to fix by hand: it gets the
|
|
2116
|
-
* refusal `theme palette generate` gives, with the update step that fits
|
|
2117
|
-
* where the CLI runs. Any other recipe is named as no recipe Astryx makes,
|
|
2118
|
-
* and the file is the fix: its `recipe` set to one this CLI knows.
|
|
2119
|
-
* @param {any} config
|
|
2120
|
-
* @param {string} file the config's path, for the refusal
|
|
2121
|
-
* @param {string} [cwd] the project, for the step that updates the CLI
|
|
2122
|
-
*/
|
|
2123
|
-
function refuseUnknownRecipe(config, file, cwd) {
|
|
2124
|
-
const recipe = config?.recipe;
|
|
2125
|
-
if (recipe == null || PALETTE_RECIPES.includes(recipe)) return;
|
|
2126
|
-
if (newerRecipe(recipe))
|
|
2127
|
-
throw newerRecipeRefusal({recipe, file, cwd: cwd ?? process.cwd()});
|
|
2128
|
-
// Every command that reads the theme builds this plan and meets this
|
|
2129
|
-
// refusal again, so the way out is the file itself, and no command is
|
|
2130
|
-
// offered.
|
|
2131
|
-
throw new AstryxError(
|
|
2132
|
-
`${file}: this theme's colors name the palette recipe ${String(recipe)}, which no Astryx CLI makes. Fix that part of the file by hand: set "recipe" to one of the recipes this CLI (${cliVersion()}) knows, ${PALETTE_RECIPES.join(', ')}. Nothing was changed.`,
|
|
2133
|
-
[],
|
|
2134
|
-
ERROR_CODES.ERR_PALETTE_GENERATION,
|
|
2135
|
-
);
|
|
2136
|
-
}
|
|
2137
|
-
|
|
2138
|
-
/**
|
|
2139
|
-
* @param {any} config
|
|
2140
|
-
* @param {string} [file] the config's path, for the refusal
|
|
2141
|
-
* @param {string} [cwd] the project, for the command that updates the CLI
|
|
2142
|
-
* @returns {Candidate}
|
|
2143
|
-
*/
|
|
2144
|
-
function generate(config, file = 'palette.config.json', cwd = undefined) {
|
|
2145
|
-
refuseUnknownRecipe(config, file, cwd);
|
|
2146
|
-
try {
|
|
2147
|
-
return generateTonalPalette(config);
|
|
2148
|
-
} catch (e) {
|
|
2149
|
-
throw new AstryxError(
|
|
2150
|
-
`${file}: the colors could not be generated: ${(e instanceof Error ? e.message : String(e)).replace(/\.$/, '')}. Fix that part of the file by hand, or remove it. Nothing was changed.`,
|
|
2151
|
-
[{name: `${P} how colors`, reason: 'how the colors are made'}],
|
|
2152
|
-
ERROR_CODES.ERR_PALETTE_GENERATION,
|
|
2153
|
-
);
|
|
2154
|
-
}
|
|
2155
|
-
}
|
|
2156
|
-
|
|
2157
|
-
/**
|
|
2158
|
-
* A family's ramp in each mode, as the theme makes it: the palette's own
|
|
2159
|
-
* family when its color settings define one; for the neutral family, else
|
|
2160
|
-
* the grays grown from the theme's secondary text (every theme has its
|
|
2161
|
-
* grays); null for another family the palette does not define.
|
|
2162
|
-
* @param {string} cwd
|
|
2163
|
-
* @param {string} slug
|
|
2164
|
-
* @param {import('./_settings.mjs').ThemeData} data
|
|
2165
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} base
|
|
2166
|
-
* @param {string} family
|
|
2167
|
-
* @returns {{light: Record<string, string>, dark: Record<string, string>} | null}
|
|
2168
|
-
*/
|
|
2169
|
-
export function familyRamp(cwd, slug, data, base, family) {
|
|
2170
|
-
if (dialFamilies(data).some(f => f.id === family)) {
|
|
2171
|
-
const plan = /** @type {PalettePlan} */ (
|
|
2172
|
-
planPalette(cwd, slug, data, base)
|
|
2173
|
-
);
|
|
2174
|
-
const ramp = /** @type {any} */ (candidateOf(cwd, slug, plan).palette)[
|
|
2175
|
-
family
|
|
2176
|
-
];
|
|
2177
|
-
if (ramp?.light && ramp?.dark) return ramp;
|
|
2178
|
-
}
|
|
2179
|
-
if (family !== 'neutral') return null;
|
|
2180
|
-
/** (The secondary text the theme sets exactly in a mode, else its base's.) @param {Mode} m */
|
|
2181
|
-
const grown = m => {
|
|
2182
|
-
const set = data.settings.find(
|
|
2183
|
-
s =>
|
|
2184
|
-
s.source === 'token' &&
|
|
2185
|
-
s.thing === '--color-text-secondary' &&
|
|
2186
|
-
!s.on &&
|
|
2187
|
-
!s.when &&
|
|
2188
|
-
!s.state &&
|
|
2189
|
-
(!s.mode || s.mode === m),
|
|
2190
|
-
)?.value;
|
|
2191
|
-
const v =
|
|
2192
|
-
(set && parseColor(set) ? set : null) ??
|
|
2193
|
-
base?.[m]?.['--color-text-secondary'];
|
|
2194
|
-
const rgb = v ? parseColor(v) : null;
|
|
2195
|
-
return rgb ? grownGrays(hex(/** @type {number[]} */ (rgb)), m) : null;
|
|
2196
|
-
};
|
|
2197
|
-
const light = grown('light');
|
|
2198
|
-
const dark = grown('dark');
|
|
2199
|
-
return light && dark ? {light, dark} : null;
|
|
2200
|
-
}
|
|
2201
|
-
|
|
2202
|
-
/**
|
|
2203
|
-
* The colors and token mapping of a theme's palette, or null when it sets no
|
|
2204
|
-
* color dial. Reads the config the settings ask for and runs the generator
|
|
2205
|
-
* in memory, so it matches the palette file `syncPalette` writes.
|
|
2206
|
-
* @param {string} cwd
|
|
2207
|
-
* @param {string} slug
|
|
2208
|
-
* @param {{mode?: string, settings: Setting[]}} data - The theme's settings (a whole ThemeData, or the settings with one left out).
|
|
2209
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} base
|
|
2210
|
-
* @param {Record<string, unknown>} tokenDefaults
|
|
2211
|
-
* @returns {PaletteMapping | null}
|
|
2212
|
-
*/
|
|
2213
|
-
export function paletteMapping(cwd, slug, data, base, tokenDefaults) {
|
|
2214
|
-
if (!dialFamilies(data).length) return null;
|
|
2215
|
-
// One change asks for the mapping several times (to build, to check, to
|
|
2216
|
-
// say what changed), each with the same inputs: what it reads (the
|
|
2217
|
-
// settings, the base, the color config, saved settings and generated
|
|
2218
|
-
// palette on disk, the theme's own token values) names it, and an answer is made once. A
|
|
2219
|
-
// config that does not parse is read again each time, so it is named.
|
|
2220
|
-
const config = readText(path.join(cwd, paletteFiles(slug).config));
|
|
2221
|
-
if (config != null && !parsesAsJson(config))
|
|
2222
|
-
return paletteMappingOf(cwd, slug, data, base, tokenDefaults);
|
|
2223
|
-
const key = sha256(
|
|
2224
|
-
JSON.stringify([
|
|
2225
|
-
cwd,
|
|
2226
|
-
slug,
|
|
2227
|
-
data,
|
|
2228
|
-
base,
|
|
2229
|
-
tokenDefaults,
|
|
2230
|
-
config,
|
|
2231
|
-
readText(path.join(cwd, 'themes', slug, `${slug}.settings.json`)),
|
|
2232
|
-
ownSourceTokenValues(cwd, slug),
|
|
2233
|
-
// (The generated palette and its receipt: a hand edit there is the
|
|
2234
|
-
// mapping's "was edited by hand" note.)
|
|
2235
|
-
readText(path.join(cwd, paletteFiles(slug).palette)),
|
|
2236
|
-
readText(path.join(cwd, paletteFiles(slug).receipt)),
|
|
2237
|
-
]),
|
|
2238
|
-
);
|
|
2239
|
-
const kept = MAPPINGS.get(key);
|
|
2240
|
-
if (kept) {
|
|
2241
|
-
MAPPINGS.delete(key);
|
|
2242
|
-
MAPPINGS.set(key, kept);
|
|
2243
|
-
return structuredClone(kept);
|
|
2244
|
-
}
|
|
2245
|
-
const made = paletteMappingOf(cwd, slug, data, base, tokenDefaults);
|
|
2246
|
-
if (made) {
|
|
2247
|
-
MAPPINGS.set(key, structuredClone(made));
|
|
2248
|
-
if (MAPPINGS.size > MAPPINGS_KEPT)
|
|
2249
|
-
MAPPINGS.delete(/** @type {string} */ (MAPPINGS.keys().next().value));
|
|
2250
|
-
}
|
|
2251
|
-
return made;
|
|
2252
|
-
}
|
|
2253
|
-
|
|
2254
|
-
/** The mappings made last, by what they were made from (paletteMapping). @type {Map<string, PaletteMapping>} */
|
|
2255
|
-
const MAPPINGS = new Map();
|
|
2256
|
-
/** How many mappings are kept. */
|
|
2257
|
-
const MAPPINGS_KEPT = 8;
|
|
2258
|
-
|
|
2259
|
-
/** A file's text, or null when it cannot be read. @param {string} file */
|
|
2260
|
-
function readText(file) {
|
|
2261
|
-
try {
|
|
2262
|
-
return fs.readFileSync(file, 'utf8');
|
|
2263
|
-
} catch {
|
|
2264
|
-
return null;
|
|
2265
|
-
}
|
|
2266
|
-
}
|
|
2267
|
-
|
|
2268
|
-
/** @param {string} text */
|
|
2269
|
-
function parsesAsJson(text) {
|
|
2270
|
-
try {
|
|
2271
|
-
JSON.parse(text.replace(/^\uFEFF/, ''));
|
|
2272
|
-
return true;
|
|
2273
|
-
} catch {
|
|
2274
|
-
return false;
|
|
2275
|
-
}
|
|
2276
|
-
}
|
|
2277
|
-
|
|
2278
|
-
/**
|
|
2279
|
-
* The mapping, made: see {@link paletteMapping}.
|
|
2280
|
-
* @param {string} cwd
|
|
2281
|
-
* @param {string} slug
|
|
2282
|
-
* @param {{mode?: string, settings: Setting[]}} data
|
|
2283
|
-
* @param {{light: Record<string, string>, dark: Record<string, string>}} base
|
|
2284
|
-
* @param {Record<string, unknown>} tokenDefaults
|
|
2285
|
-
* @returns {PaletteMapping | null}
|
|
2286
|
-
*/
|
|
2287
|
-
function paletteMappingOf(cwd, slug, data, base, tokenDefaults) {
|
|
2288
|
-
const fams = dialFamilies(data, ownSourceTokenValues(cwd, slug));
|
|
2289
|
-
if (!fams.length) return null;
|
|
2290
|
-
const plan = /** @type {PalettePlan} */ (planPalette(cwd, slug, data, base));
|
|
2291
|
-
const {config} = plan;
|
|
2292
|
-
const candidate = candidateOf(cwd, slug, plan);
|
|
2293
|
-
const mapping = mapPalette(
|
|
2294
|
-
candidate,
|
|
2295
|
-
fams,
|
|
2296
|
-
plan.fills,
|
|
2297
|
-
paletteContext(cwd, slug, data, base, tokenDefaults),
|
|
2298
|
-
);
|
|
2299
|
-
const {tokens} = mapping;
|
|
2300
|
-
// An accent set as a token for one mode moves that mode only: the other
|
|
2301
|
-
// mode keeps what the theme's palette gives it without that accent (its
|
|
2302
|
-
// other colors, a warm gray, still drawn), else follows the base theme.
|
|
2303
|
-
// (A palette kept as saved has no palette without that accent to make:
|
|
2304
|
-
// its saved shades stand for both modes.)
|
|
2305
|
-
const only = plan.stored ? null : fams.find(f => f.id === 'accent')?.only;
|
|
2306
|
-
if (only) {
|
|
2307
|
-
const other = only === 'light' ? 'dark' : 'light';
|
|
2308
|
-
// The palette's checks for that mode are those of the palette without
|
|
2309
|
-
// the accent, which this change does not move.
|
|
2310
|
-
mapping.contrast = mapping.contrast.filter(c => c.mode !== other);
|
|
2311
|
-
const rest = paletteMapping(
|
|
2312
|
-
cwd,
|
|
2313
|
-
slug,
|
|
2314
|
-
{
|
|
2315
|
-
...data,
|
|
2316
|
-
settings: data.settings.filter(
|
|
2317
|
-
x =>
|
|
2318
|
-
!(
|
|
2319
|
-
x.source === 'token' &&
|
|
2320
|
-
x.thing === '--color-accent' &&
|
|
2321
|
-
!x.on &&
|
|
2322
|
-
!x.when &&
|
|
2323
|
-
!x.state &&
|
|
2324
|
-
x.mode === only
|
|
2325
|
-
),
|
|
2326
|
-
),
|
|
2327
|
-
},
|
|
2328
|
-
base,
|
|
2329
|
-
tokenDefaults,
|
|
2330
|
-
);
|
|
2331
|
-
for (const [k, v] of Object.entries(tokens)) {
|
|
2332
|
-
const before = rest?.tokens?.[k]?.[other];
|
|
2333
|
-
if (before) {
|
|
2334
|
-
// (The same shade where the palette's color there is the same; its
|
|
2335
|
-
// color written out where it differs, as the accent moves the
|
|
2336
|
-
// shades it coordinates.)
|
|
2337
|
-
tokens[k] = {
|
|
2338
|
-
...v,
|
|
2339
|
-
[other]:
|
|
2340
|
-
String(v[other]?.color).toLowerCase() ===
|
|
2341
|
-
String(before.color).toLowerCase()
|
|
2342
|
-
? v[other]
|
|
2343
|
-
: {stop: 0, color: String(before.color), family: 'literal'},
|
|
2344
|
-
};
|
|
2345
|
-
continue;
|
|
2346
|
-
}
|
|
2347
|
-
const kept = base?.[other]?.[k];
|
|
2348
|
-
if (kept == null) {
|
|
2349
|
-
delete tokens[k];
|
|
2350
|
-
continue;
|
|
2351
|
-
}
|
|
2352
|
-
// (The base theme's own value: the module reads it from the base,
|
|
2353
|
-
// so the mode keeps following it.)
|
|
2354
|
-
tokens[k] = {
|
|
2355
|
-
...v,
|
|
2356
|
-
[other]: {stop: 0, color: String(kept), family: 'literal', base: true},
|
|
2357
|
-
};
|
|
2358
|
-
}
|
|
2359
|
-
}
|
|
2360
|
-
return {
|
|
2361
|
-
candidate,
|
|
2362
|
-
families: fams,
|
|
2363
|
-
...mapping,
|
|
2364
|
-
stored: Boolean(plan.stored),
|
|
2365
|
-
vibrancy: config.vibrancy ?? VIBRANCY_DEFAULT,
|
|
2366
|
-
notes: [
|
|
2367
|
-
...plan.notes,
|
|
2368
|
-
...(paletteEdited(cwd, slug)
|
|
2369
|
-
? [
|
|
2370
|
-
`${paletteFiles(slug).palette} was edited by hand, but the theme never reads it: it reads ${paletteFiles(slug).refs}, which the editor writes from the generator, so the edit does not show. The next color change asks whether to keep or discard it`,
|
|
2371
|
-
]
|
|
2372
|
-
: []),
|
|
2373
|
-
],
|
|
2374
|
-
};
|
|
2375
|
-
}
|
|
2376
|
-
|
|
2377
|
-
/**
|
|
2378
|
-
* The color dial whose generated colors a token belongs to, or null: an
|
|
2379
|
-
* exact setting of it changes what that dial's mapping picks around it.
|
|
2380
|
-
* @param {string} token
|
|
2381
|
-
* @returns {string | null}
|
|
2382
|
-
*/
|
|
2383
|
-
export function paletteDialOf(token) {
|
|
2384
|
-
if (!PALETTE_TOKENS.has(token)) return null;
|
|
2385
|
-
if (/accent|^--focus-outline-color$/.test(token)) return 'accent';
|
|
2386
|
-
for (const [dial, word] of Object.entries(STATUS_TOKEN))
|
|
2387
|
-
if (new RegExp(`-${word}(-muted)?$`).test(token)) return dial;
|
|
2388
|
-
return 'neutral';
|
|
2389
|
-
}
|
|
2390
|
-
|
|
2391
|
-
/** Every token a color dial can point at a generated shade. */
|
|
2392
|
-
export const PALETTE_TOKENS = new Set([
|
|
2393
|
-
...Object.keys(NEUTRAL_STOPS),
|
|
2394
|
-
...Object.keys(NEUTRAL_TINTS),
|
|
2395
|
-
'--color-accent',
|
|
2396
|
-
'--color-on-accent',
|
|
2397
|
-
'--color-text-accent',
|
|
2398
|
-
'--color-icon-accent',
|
|
2399
|
-
'--color-accent-muted',
|
|
2400
|
-
'--focus-outline-color',
|
|
2401
|
-
...Object.values(STATUS_TOKEN).flatMap(w => [
|
|
2402
|
-
`--color-${w}`,
|
|
2403
|
-
`--color-on-${w}`,
|
|
2404
|
-
`--color-text-${w}`,
|
|
2405
|
-
`--color-icon-${w}`,
|
|
2406
|
-
`--color-${w}-muted`,
|
|
2407
|
-
`--color-on-${w}-muted`,
|
|
2408
|
-
`--color-border-${w}`,
|
|
2409
|
-
]),
|
|
2410
|
-
]);
|
|
2411
|
-
|
|
2412
|
-
/**
|
|
2413
|
-
* Where each token starts, for `how`: `token: light stop L, dark stop D`.
|
|
2414
|
-
* @param {string} dial
|
|
2415
|
-
* @returns {string[]}
|
|
2416
|
-
*/
|
|
2417
|
-
export function mappingRows(dial) {
|
|
2418
|
-
if (dial === 'neutral')
|
|
2419
|
-
return [
|
|
2420
|
-
'your exact color sits at its own shade, light and dark, and the shades around it follow it (white and black are already the ends of every ramp)',
|
|
2421
|
-
...Object.entries(NEUTRAL_STOPS).map(
|
|
2422
|
-
([t, [l, d]]) =>
|
|
2423
|
-
`${t}: light shade ${l}, dark shade ${d}${t.endsWith('-secondary') ? ` (moves to a shade that reads at ${t.includes('icon') ? NON_TEXT : TEXT}:1 on the surfaces)` : NEUTRAL_INSIDE.includes(t) ? ' (the card surface of the other mode, for tooltips and toasts, at the nearest shade inside the ramp so it carries your color)' : ''}`,
|
|
2424
|
-
),
|
|
2425
|
-
...Object.entries(NEUTRAL_TINTS).map(
|
|
2426
|
-
([t, [[l, la], [d, da]]]) =>
|
|
2427
|
-
`${t}: light shade ${l} at opacity ${la}, dark shade ${d} at opacity ${da}`,
|
|
2428
|
-
),
|
|
2429
|
-
...Object.entries(LIFT_GROUNDS)
|
|
2430
|
-
.filter(([g]) => !g.startsWith('--color-background-'))
|
|
2431
|
-
.map(
|
|
2432
|
-
([, g]) =>
|
|
2433
|
-
`--color-text-secondary on ${g.parts}: the shade nearest the secondary text's that reads at ${TEXT}:1 on ${g.what} and under a hover, as the official theme lifts it`,
|
|
2434
|
-
),
|
|
2435
|
-
...(Object.keys(LIFT_GROUNDS).some(g =>
|
|
2436
|
-
g.startsWith('--color-background-'),
|
|
2437
|
-
)
|
|
2438
|
-
? [
|
|
2439
|
-
`--color-text-secondary on coloured cards: the shade nearest the secondary text's that reads at ${TEXT}:1 on each card's fill, as the official theme lifts it`,
|
|
2440
|
-
]
|
|
2441
|
-
: []),
|
|
2442
|
-
];
|
|
2443
|
-
const word = dial === 'accent' ? null : STATUS_TOKEN[dial];
|
|
2444
|
-
if (dial !== 'accent' && !word) return [];
|
|
2445
|
-
const t = (/** @type {string} */ a, /** @type {string} */ b) =>
|
|
2446
|
-
word ? a.replace('$', word) : b;
|
|
2447
|
-
const onFirst =
|
|
2448
|
-
dial === 'warning'
|
|
2449
|
-
? 'near-black first in both modes'
|
|
2450
|
-
: 'white first in light, near-black first in dark';
|
|
2451
|
-
const rows = [
|
|
2452
|
-
`${t('--color-$', '--color-accent')}: the color you gave, exactly, in light, and in dark when text on it reads at ${TEXT}:1 and it stands out from the dark page at ${NON_TEXT}:1; otherwise dark mode uses shade ${DARK_FILL}, or the shade nearest it that passes both, and the result says so. A light fill that stands out from the page at under ${NON_TEXT}:1 keeps your color, and the result says so`,
|
|
2453
|
-
`${t('--color-on-$', '--color-on-accent')}: the neutral color's white or near-black (${onFirst}; pure white or black without a neutral color), whichever reads at ${TEXT}:1 on the fill the theme uses`,
|
|
2454
|
-
`${t('--color-text-$', '--color-text-accent')} and ${t('--color-icon-$', '--color-icon-accent')}: light shade ${TEXT_STOP[0]}, dark shade ${TEXT_STOP[1]} (moves to the nearest shade, darker or lighter, that reads at ${TEXT}:1 on the surfaces and the muted tint)`,
|
|
2455
|
-
word
|
|
2456
|
-
? `--color-${word}-muted: light shade ${STATUS_MUTED[0]}, dark shade ${STATUS_MUTED[1][0]} at opacity ${STATUS_MUTED[1][1]} over the surface, never the fill's own shade`
|
|
2457
|
-
: `--color-accent-muted: light shade ${ACCENT_MUTED[0]}, dark shade ${ACCENT_MUTED[1]}, as the official neutral theme maps it, never the fill's own shade; secondary text on the parts that sit on it (selected items and rows, a dropzone, numbered citations) is lifted instead, to the gray nearest it that reads at ${TEXT}:1 on the tint and under a hover: the neutral color's shades, else grays grown from the base theme's own secondary text`,
|
|
2458
|
-
];
|
|
2459
|
-
if (word)
|
|
2460
|
-
rows.push(
|
|
2461
|
-
`--color-on-${word}-muted: light shade ${ON_MUTED[0]}, dark shade ${ON_MUTED[1]} (moves until it reads at ${TEXT}:1 on the muted tint)`,
|
|
2462
|
-
`--color-border-${word}: light shade ${BORDER[0]}, dark shade ${BORDER[1]}`,
|
|
2463
|
-
);
|
|
2464
|
-
else
|
|
2465
|
-
rows.push(
|
|
2466
|
-
`--focus-outline-color (the focus ring): the fill's shade, moved until it reads at ${NON_TEXT}:1 on the surfaces; with an exact --color-accent it follows that`,
|
|
2467
|
-
'links and selection read --color-text-accent and --color-accent; Core has no separate accent hover or pressed tokens (buttons tint with the overlay tokens, which the neutral dial sets)',
|
|
2468
|
-
);
|
|
2469
|
-
rows.push(
|
|
2470
|
-
`an exact ${t('--color-$', '--color-accent')} setting wins: the text on it is picked to read on your color, and the other tokens keep the generated shades`,
|
|
2471
|
-
);
|
|
2472
|
-
return rows;
|
|
2473
|
-
}
|
|
2474
|
-
|
|
2475
|
-
/**
|
|
2476
|
-
* The contrast checks of the colors a dial makes, in the theme's modes.
|
|
2477
|
-
* @param {PaletteMapping} pal
|
|
2478
|
-
* @param {string} dial
|
|
2479
|
-
* @param {Mode[]} modes
|
|
2480
|
-
*/
|
|
2481
|
-
function dialChecks(pal, dial, modes) {
|
|
2482
|
-
return pal.contrast.filter(
|
|
2483
|
-
c =>
|
|
2484
|
-
(pal.tokens[c.token]?.family === dial || c.dials?.includes(dial)) &&
|
|
2485
|
-
// A theme with one mode never shows the other one's shades.
|
|
2486
|
-
modes.includes(c.mode),
|
|
2487
|
-
);
|
|
2488
|
-
}
|
|
2489
|
-
|
|
2490
|
-
/**
|
|
2491
|
-
* What one contrast check says: its own note, or for a failed one the
|
|
2492
|
-
* ratio it reached.
|
|
2493
|
-
* @param {PaletteMapping['contrast'][number]} c
|
|
2494
|
-
* @param {string} dial
|
|
2495
|
-
*/
|
|
2496
|
-
function checkNote(c, dial) {
|
|
2497
|
-
return (
|
|
2498
|
-
c.note ||
|
|
2499
|
-
`${c.token} reads at only ${c.ratio}:1 in ${c.mode} on ${c.against} (needs ${c.needs}:1) even at shade ${shade(c.stop)}, the most contrasting one: pick a lighter or darker ${dial}, or set ${c.token} exactly`
|
|
2500
|
-
);
|
|
2501
|
-
}
|
|
2502
|
-
|
|
2503
|
-
/**
|
|
2504
|
-
* A contrast check's identity: its token, its mode and what it is measured
|
|
2505
|
-
* on, whatever ratio or shade it reaches. The same check before and after a
|
|
2506
|
-
* change has the same key, as a text or border problem's key does.
|
|
2507
|
-
* @param {ContrastCheck} c
|
|
2508
|
-
*/
|
|
2509
|
-
export function paletteCheckKey(c) {
|
|
2510
|
-
return `palette|${c.token}|${c.mode}|${c.against}`;
|
|
2511
|
-
}
|
|
2512
|
-
|
|
2513
|
-
/**
|
|
2514
|
-
* The contrast checks a dial's colors fail, as their notes say them, each
|
|
2515
|
-
* with its identity ({@link paletteCheckKey}), to tell the checks a change
|
|
2516
|
-
* made fail from the ones that failed before it.
|
|
2517
|
-
* @param {PaletteMapping | null} pal
|
|
2518
|
-
* @param {string} dial
|
|
2519
|
-
* @param {Mode[]} [modes] the modes the theme has
|
|
2520
|
-
* @returns {Array<{key: string, text: string}>}
|
|
2521
|
-
*/
|
|
2522
|
-
export function paletteFailures(pal, dial, modes = ['light', 'dark']) {
|
|
2523
|
-
if (!pal) return [];
|
|
2524
|
-
return dialChecks(pal, dial, modes)
|
|
2525
|
-
.filter(c => !c.pass)
|
|
2526
|
-
.map(c => ({
|
|
2527
|
-
key: paletteCheckKey(c),
|
|
2528
|
-
text: checkNote(c, dial),
|
|
2529
|
-
...(c.fix ? {fix: c.fix} : {}),
|
|
2530
|
-
}));
|
|
2531
|
-
}
|
|
2532
|
-
|
|
2533
|
-
/**
|
|
2534
|
-
* The identities of every contrast check a palette fails, whatever its
|
|
2535
|
-
* dial: what a change compares its own failures with.
|
|
2536
|
-
* @param {PaletteMapping | null} pal
|
|
2537
|
-
* @returns {Set<string>}
|
|
2538
|
-
*/
|
|
2539
|
-
export function paletteFailedKeys(pal) {
|
|
2540
|
-
return new Set(
|
|
2541
|
-
(pal?.contrast ?? []).filter(c => !c.pass).map(paletteCheckKey),
|
|
2542
|
-
);
|
|
2543
|
-
}
|
|
2544
|
-
|
|
2545
|
-
/**
|
|
2546
|
-
* A text/fill pair the base theme draws, as a person reads it: "the blue
|
|
2547
|
-
* card's primary text", "text on the error tint".
|
|
2548
|
-
* @param {import('../theme/palette/generate/mapping.mjs').BasePair} pair
|
|
2549
|
-
*/
|
|
2550
|
-
function pairWords(pair) {
|
|
2551
|
-
if (pair.kind === 'category') return `${pair.hue} category text`;
|
|
2552
|
-
if (pair.kind === 'status')
|
|
2553
|
-
return pair.tint ? `text on the ${pair.role} tint` : `text on ${pair.role}`;
|
|
2554
|
-
const which =
|
|
2555
|
-
/^(?:variant|color):(.+)$/.exec(pair.block)?.[1] ??
|
|
2556
|
-
pair.block.replace(':', ' ');
|
|
2557
|
-
const role =
|
|
2558
|
-
pair.prop === 'color'
|
|
2559
|
-
? 'text'
|
|
2560
|
-
: `${pair.prop.replace(/^--color-text-/, '')} text`;
|
|
2561
|
-
return `the ${which === 'base' ? '' : `${which} `}${pair.part.replace(/-/g, ' ')}'s ${role}`;
|
|
2562
|
-
}
|
|
2563
|
-
|
|
2564
|
-
/**
|
|
2565
|
-
* What the look did to the category colors (Badge, Token, Card tints), the
|
|
2566
|
-
* chart colors and the status roles: one line with the accent's notes, and
|
|
2567
|
-
* what a set gave up to stay written with every dial's.
|
|
2568
|
-
* @param {PaletteMapping} pal
|
|
2569
|
-
* @param {string} dial
|
|
2570
|
-
* @param {string} [slug]
|
|
2571
|
-
* @param {boolean} [vibrancyRuns] whether `set <theme> vibrancy` changes
|
|
2572
|
-
* anything (vibrancyTakes): only then is a higher vibrancy advised
|
|
2573
|
-
*/
|
|
2574
|
-
export function categoryNotes(
|
|
2575
|
-
pal,
|
|
2576
|
-
dial,
|
|
2577
|
-
slug = '<theme>',
|
|
2578
|
-
vibrancyRuns = false,
|
|
2579
|
-
) {
|
|
2580
|
-
const c = pal.categories;
|
|
2581
|
-
if (!c) return [];
|
|
2582
|
-
/** @type {string[]} */
|
|
2583
|
-
const out = [];
|
|
2584
|
-
const derived = [
|
|
2585
|
-
c.badges === 'derived'
|
|
2586
|
-
? 'category colors (badges, tokens and card tints)'
|
|
2587
|
-
: '',
|
|
2588
|
-
c.chart === 'derived' ? 'chart colors' : '',
|
|
2589
|
-
Object.keys(pal.tokens).some(k => k.startsWith('--color-syntax-'))
|
|
2590
|
-
? 'code colors'
|
|
2591
|
-
: '',
|
|
2592
|
-
].filter(Boolean);
|
|
2593
|
-
if (dial === 'accent' && derived.length)
|
|
2594
|
-
out.push(
|
|
2595
|
-
`${derived.join(', ').replace(/, ([^,]*)$/, ' and $1')} grow from Core's hues at your accent's strength and the palette's vibrancy, so they match the look; card tints stay tints that the page's text reads on where a shade allows, code reads at 4.5:1 on its background, and an exact --color-<role>-<hue>, --color-data-* or --color-syntax-* setting wins`,
|
|
2596
|
-
);
|
|
2597
|
-
// (Every limit no shade meets is said; the colors moved apart are
|
|
2598
|
-
// counted after the first.)
|
|
2599
|
-
if (c.limits.length || c.moved.length) {
|
|
2600
|
-
// (Hues drawn alike come from a low vibrancy: a higher one is advised,
|
|
2601
|
-
// with its command, only where set takes it.)
|
|
2602
|
-
// (A theme at 100 already has no higher vibrancy.)
|
|
2603
|
-
const apart =
|
|
2604
|
-
c.alike && vibrancyRuns && pal.vibrancy < 100
|
|
2605
|
-
? `; a higher vibrancy draws the hues apart: ${P} set ${slug} vibrancy more`
|
|
2606
|
-
: '';
|
|
2607
|
-
const moves = c.moved.length
|
|
2608
|
-
? `${c.moved[0]}${c.moved.length > 1 ? `, and ${c.moved.length - 1} more moved apart` : ''}`
|
|
2609
|
-
: '';
|
|
2610
|
-
out.push(
|
|
2611
|
-
`category and chart colors follow your look: ${[...c.limits, ...(moves ? [moves] : [])].join('; ')}${apart}${c.unread ? '; an exact setting of a color that does not read chooses it' : ''}`,
|
|
2612
|
-
);
|
|
2613
|
-
}
|
|
2614
|
-
// Status fills someone red-weak or green-weak may not tell apart: one
|
|
2615
|
-
// note per pair, with a line that sets one of them apart in lightness.
|
|
2616
|
-
/** @type {Set<string>} */
|
|
2617
|
-
const saidPairs = new Set();
|
|
2618
|
-
for (const c of pal.cvd ?? []) {
|
|
2619
|
-
const id = `${c.a}|${c.b}`;
|
|
2620
|
-
if (saidPairs.has(id)) continue;
|
|
2621
|
-
saidPairs.add(id);
|
|
2622
|
-
const visions = [
|
|
2623
|
-
...new Set(
|
|
2624
|
-
(pal.cvd ?? [])
|
|
2625
|
-
.filter(x => x.a === c.a && x.b === c.b)
|
|
2626
|
-
.map(x => `${x.vision} (${x.mode})`),
|
|
2627
|
-
),
|
|
2628
|
-
];
|
|
2629
|
-
// The one to move and its way: the second of the pair first, away
|
|
2630
|
-
// from the first in lightness, then the other way, then the first of
|
|
2631
|
-
// the pair; the first move that keeps the moved fill standing out from
|
|
2632
|
-
// the page (3:1, in every mode the theme shows) is the line. None
|
|
2633
|
-
// that does: the note names no line.
|
|
2634
|
-
const pages = [
|
|
2635
|
-
...new Set(
|
|
2636
|
-
(pal.cvd ?? [])
|
|
2637
|
-
.filter(z => z.a === c.a && z.b === c.b)
|
|
2638
|
-
.flatMap(z => z.pages ?? (z.page ? [z.page] : [])),
|
|
2639
|
-
),
|
|
2640
|
-
];
|
|
2641
|
-
const tries = /** @type {Array<[number, number]>} */ ([
|
|
2642
|
-
[1, 1],
|
|
2643
|
-
[1, -1],
|
|
2644
|
-
[0, 1],
|
|
2645
|
-
[0, -1],
|
|
2646
|
-
]);
|
|
2647
|
-
const [p0, p1] = c.colors.map(v => hexToOklch(v));
|
|
2648
|
-
// (The smallest move, in steps of lightness, that sets the pair apart
|
|
2649
|
-
// for color-blind readers and still stands out from every page.)
|
|
2650
|
-
const steps = [0.06, 0.09, 0.12, 0.15, 0.18, 0.21, 0.24, 0.27, 0.3];
|
|
2651
|
-
const move = steps
|
|
2652
|
-
.flatMap(d =>
|
|
2653
|
-
tries.map(([who, way]) => {
|
|
2654
|
-
const [me, other] = who === 1 ? [p1, p0] : [p0, p1];
|
|
2655
|
-
const sign = (me.L >= other.L ? 1 : -1) * way;
|
|
2656
|
-
const L = me.L + sign * d;
|
|
2657
|
-
if (L < 0.15 || L > 0.95) return null;
|
|
2658
|
-
const to = oklchClampedHex(L, me.C, me.H);
|
|
2659
|
-
if (!pages.every(pg => contrastRatio(to, pg) >= NON_TEXT))
|
|
2660
|
-
return null;
|
|
2661
|
-
const role = who === 1 ? c.b : c.a;
|
|
2662
|
-
const pair = {
|
|
2663
|
-
[c.a]: role === c.a ? to : c.colors[0],
|
|
2664
|
-
[c.b]: role === c.b ? to : c.colors[1],
|
|
2665
|
-
};
|
|
2666
|
-
if (cvdClashes(pair).some(x => x.a === c.a && x.b === c.b))
|
|
2667
|
-
return null;
|
|
2668
|
-
return {role, to, lighter: sign > 0};
|
|
2669
|
-
}),
|
|
2670
|
-
)
|
|
2671
|
-
.find(Boolean);
|
|
2672
|
-
const dial = move ? (move.role === 'error' ? 'danger' : move.role) : '';
|
|
2673
|
-
out.push(
|
|
2674
|
-
`for color-blind readers, the ${c.a} and ${c.b} fills may look alike (${visions.join(', ')}: ${c.colors.join(' and ')}); ${move ? `a ${move.lighter ? 'lighter' : 'darker'} ${dial} keeps the colors apart: ${P} set ${slug} ${dial} ${quoteCommandArgument(move.to)}` : 'no lighter or darker shade of either keeps both standing out from the page; pick a status color of another hue'}`,
|
|
2675
|
-
);
|
|
2676
|
-
}
|
|
2677
|
-
if (pal.code?.length)
|
|
2678
|
-
out.push(
|
|
2679
|
-
`${pal.code.join(', ')} ${pal.code.length > 1 ? 'keep' : 'keeps'} the base theme's: no shade of ${pal.code.length > 1 ? 'their' : 'its'} color reads at 4.5:1 on the code background and the page`,
|
|
2680
|
-
);
|
|
2681
|
-
const held = pal.kept ?? [];
|
|
2682
|
-
if (held.length) {
|
|
2683
|
-
const tokensHeld = [...new Set(held.map(h => h.token))];
|
|
2684
|
-
const pairs = [
|
|
2685
|
-
...new Set(held.map(h => `${pairWords(h.pair)} (${h.mode})`)),
|
|
2686
|
-
];
|
|
2687
|
-
out.push(
|
|
2688
|
-
`${tokensHeld.length} of the colors the look grows keep the base theme's where the base theme draws its own text with them, so it stays readable: ${pairs.slice(0, 3).join(', ')}${pairs.length > 3 ? `, and ${pairs.length - 3} more` : ''}`,
|
|
2689
|
-
);
|
|
2690
|
-
}
|
|
2691
|
-
const st = pal.statuses;
|
|
2692
|
-
const grown = ['info', 'success', 'warning', 'danger'].filter(
|
|
2693
|
-
role => st?.roles[role] === 'derived',
|
|
2694
|
-
);
|
|
2695
|
-
if (dial === 'accent' && grown.length)
|
|
2696
|
-
out.push(
|
|
2697
|
-
`${grown.join(', ')} ${grown.length > 1 ? 'grow' : 'grows'} from the base theme's ${grown.length > 1 ? 'hues' : 'hue'} at your accent's strength and the palette's vibrancy, kept apart from your accent where a shade allows (badges, banners, status dots, field status); an exact setting of ${grown.length > 1 ? 'one' : 'it'} chooses it`,
|
|
2698
|
-
);
|
|
2699
|
-
if (st?.why.length)
|
|
2700
|
-
out.push(
|
|
2701
|
-
`status colors follow your look: ${st.why.join('; ')}; to choose one: ${st.short.map(role => `${P} set ${slug} ${role} <color>`).join(', ')}`,
|
|
2702
|
-
);
|
|
2703
|
-
return out;
|
|
2704
|
-
}
|
|
2705
|
-
|
|
2706
|
-
/**
|
|
2707
|
-
* What one color dial did, for the result notes: what its main token uses
|
|
2708
|
-
* in each mode, every token contrast moved, and last the generated files.
|
|
2709
|
-
* What the config writer kept or replaced of a hand edit comes from the
|
|
2710
|
-
* build ({@link syncPalette}).
|
|
2711
|
-
* @param {PaletteMapping | null} pal
|
|
2712
|
-
* @param {string} dial
|
|
2713
|
-
* @param {string} slug
|
|
2714
|
-
* @param {Mode[]} [modes] the modes the theme has
|
|
2715
|
-
* @param {boolean} [vibrancyRuns] whether `set <theme> vibrancy` changes
|
|
2716
|
-
* anything (vibrancyTakes)
|
|
2717
|
-
* @returns {string[]}
|
|
2718
|
-
*/
|
|
2719
|
-
export function paletteNotes(
|
|
2720
|
-
pal,
|
|
2721
|
-
dial,
|
|
2722
|
-
slug,
|
|
2723
|
-
modes = ['light', 'dark'],
|
|
2724
|
-
vibrancyRuns = false,
|
|
2725
|
-
) {
|
|
2726
|
-
if (!pal) return [];
|
|
2727
|
-
const f = paletteFiles(slug);
|
|
2728
|
-
const token = paletteToken(dial);
|
|
2729
|
-
const main = pal.tokens[token];
|
|
2730
|
-
const exact = pal.exact[token];
|
|
2731
|
-
/** @type {string[]} */
|
|
2732
|
-
const notes = [];
|
|
2733
|
-
if (exact) {
|
|
2734
|
-
const shown = modes
|
|
2735
|
-
.filter(m => exact[m])
|
|
2736
|
-
.map(m => `${exact[m]}${modes.length > 1 ? ` in ${m}` : ''}`)
|
|
2737
|
-
.join(' and ');
|
|
2738
|
-
notes.push(
|
|
2739
|
-
`${token} uses your exact setting (${shown}), not the generated ${dial} shade: an exact setting wins${dial === 'neutral' ? '' : '; the text on it is picked to read on your color'}${dial === 'accent' ? ', and the focus ring keeps following it' : ''}`,
|
|
2740
|
-
);
|
|
2741
|
-
}
|
|
2742
|
-
const unread = pal.unread?.[token];
|
|
2743
|
-
if (unread) {
|
|
2744
|
-
notes.push(
|
|
2745
|
-
`${token} keeps your exact setting (${unread}), which the editor cannot read as one color${exact ? ' in every mode' : ''}: an exact setting wins, so the generated ${dial} shade does not show${dial === 'neutral' ? '' : ' there, and the text on it is not checked against your color: set a fixed color, or set the text color on it exactly'}`,
|
|
2746
|
-
);
|
|
2747
|
-
} else if (!exact && main) {
|
|
2748
|
-
/** @param {Mode} m */
|
|
2749
|
-
const part = m =>
|
|
2750
|
-
main[m].exact
|
|
2751
|
-
? m === 'dark' && modes.length > 1
|
|
2752
|
-
? 'the same exact color in dark'
|
|
2753
|
-
: `your exact color (${main[m].color}, ${dial} shade ${shade(main[m].stop)}) in ${m}`
|
|
2754
|
-
: `${dial} shade ${shade(main[m].stop)} (${main[m].color}) in ${m}`;
|
|
2755
|
-
notes.push(`${token} uses ${modes.map(part).join(' and ')}`);
|
|
2756
|
-
}
|
|
2757
|
-
const given = pal.families.find(x => x.dial === dial);
|
|
2758
|
-
if (given && isClipped(given.value))
|
|
2759
|
-
notes.push(
|
|
2760
|
-
given.source
|
|
2761
|
-
? `your ${dial} ${given.value} is outside the colors every screen shows (sRGB); the nearest one is ${seedOf(given.value)}, but the ${given.source} your theme source sets, ${given.seed}, is used`
|
|
2762
|
-
: `your ${dial} ${given.value} is outside the colors every screen shows (sRGB), so the nearest one, ${seedOf(given.value)}, is used`,
|
|
2763
|
-
);
|
|
2764
|
-
for (const c of dialChecks(pal, dial, modes)) {
|
|
2765
|
-
if (c.note || !c.pass) notes.push(checkNote(c, dial));
|
|
2766
|
-
else if (c.stop !== c.preferred)
|
|
2767
|
-
notes.push(
|
|
2768
|
-
`${c.token} uses shade ${shade(c.stop)} in ${c.mode}, not ${shade(c.preferred)}, so it reads at ${c.ratio}:1 on ${c.against}`,
|
|
2769
|
-
);
|
|
2770
|
-
}
|
|
2771
|
-
notes.push(...categoryNotes(pal, dial, slug, vibrancyRuns));
|
|
2772
|
-
notes.push(
|
|
2773
|
-
`the generated colors are in ${f.palette}, made from ${f.config} (receipt: ${f.receipt}); the theme imports only the shades it uses, from ${f.refs}`,
|
|
2774
|
-
);
|
|
2775
|
-
return notes;
|
|
2776
|
-
}
|