@alis-build/design-system 0.18.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -1
- package/alisx.css +69 -0
- package/check.mjs +264 -0
- package/classes.json +587 -0
- package/components/AppAvatar.md +70 -0
- package/components/AppCheckbox.md +88 -0
- package/components/AppCombobox.md +117 -0
- package/components/AppDisclosure.md +61 -0
- package/components/AppMenu.md +2 -2
- package/components/AppSegmented.md +1 -0
- package/components/AppSelect.md +2 -2
- package/components/AppSelectTile.md +1 -1
- package/components/AppShell.md +1 -1
- package/components/AppSwitch.md +1 -0
- package/components/AppTextField.md +2 -2
- package/components/AppTurn.md +1 -1
- package/components/README.md +4 -0
- package/package.json +17 -1
- package/rules/chips-alerts.md +9 -0
- package/rules/colour.md +1 -1
- package/rules/known-gaps.md +3 -3
- package/rules/page.md +30 -2
- package/rules/quick-start.md +5 -1
- package/rules/selection.md +4 -2
- package/rules/styling.md +26 -9
- package/stylelint.mjs +167 -0
package/README.md
CHANGED
|
@@ -17,9 +17,12 @@ product screens in any framework: Svelte, Vue, or plain HTML.
|
|
|
17
17
|
| `tokens.json` | The source of every value: each colour tagged palette, product or narrative, each product role with a value per theme. References such as `{cyan-600}` are left in; read `tokens.css` or `themes.json` rather than resolving them. For code written before themes, each product role also keeps a `value` (its light one), and each shadow its light literal as `value` beside `values` per theme. |
|
|
18
18
|
| `marks.json` | The capability marks (`alis_*`): construction, stroke grade by size, geometry, in canonical order. |
|
|
19
19
|
| `assets/Brand/` | The Alis mark, product lockups, app icon and favicon. |
|
|
20
|
+
| `stylelint.mjs` | The style check that restates `rules/styling.md`, as a stylelint config your own extends. |
|
|
21
|
+
| `classes.json` | Every class and custom property `alisx.css` defines in this version. |
|
|
22
|
+
| `check.mjs` | `alisx-check`: runs the two checks below in your build. |
|
|
20
23
|
|
|
21
24
|
```bash
|
|
22
|
-
pnpm add @alis-build/design-system
|
|
25
|
+
pnpm add -E @alis-build/design-system
|
|
23
26
|
```
|
|
24
27
|
|
|
25
28
|
```js
|
|
@@ -32,6 +35,113 @@ import tokens from '@alis-build/design-system/tokens.json' with { type: 'json' }
|
|
|
32
35
|
`"system"` to follow the operating system, on `<html>` or on any element
|
|
33
36
|
([rules/themes.md](rules/themes.md)). Choosing the theme is your app's job.
|
|
34
37
|
|
|
38
|
+
## Keep an app in line
|
|
39
|
+
|
|
40
|
+
Three things, set up once, so the next change stays consistent whoever makes it.
|
|
41
|
+
|
|
42
|
+
**1. Point your agents at the rules.** In your app's `CLAUDE.md` or `AGENTS.md`:
|
|
43
|
+
|
|
44
|
+
```md
|
|
45
|
+
The UI is built from @alis-build/design-system. Before changing a screen, read
|
|
46
|
+
node_modules/@alis-build/design-system/rules/README.md and follow its "Where to
|
|
47
|
+
start". The rules there are the version this app has installed.
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Write the path from where that file sits: if your `package.json` is in a
|
|
51
|
+
subfolder, the rules are under that folder's `node_modules`. Never copy the
|
|
52
|
+
rules into your repo: a copy stops moving with the version.
|
|
53
|
+
|
|
54
|
+
**2. Check your styles.** Add `stylelint` (16.11 or later) as a dev dependency,
|
|
55
|
+
with `postcss-html` if you have `<style>` blocks in Svelte or Vue files and
|
|
56
|
+
`postcss-scss` if any of them is `lang="scss"` (without it those blocks are
|
|
57
|
+
skipped, silently). Then in `stylelint.config.js`:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
export default {
|
|
61
|
+
extends: ['@alis-build/design-system/stylelint'],
|
|
62
|
+
overrides: [{ files: ['**/*.svelte', '**/*.vue'], customSyntax: 'postcss-html' }],
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- **A rule you set under the same name replaces the system's**: stylelint never
|
|
67
|
+
merges two lists. To add to one, carry the system's part along. The config
|
|
68
|
+
exports its lists (`refusedValues`, `allowedValues`, `refusedProperties`,
|
|
69
|
+
`refusedSelectors`) and two builders:
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { refuse, refusedValues } from '@alis-build/design-system/stylelint'
|
|
73
|
+
export default {
|
|
74
|
+
extends: ['@alis-build/design-system/stylelint'],
|
|
75
|
+
rules: {
|
|
76
|
+
'declaration-property-value-disallowed-list': refuse([
|
|
77
|
+
...refusedValues,
|
|
78
|
+
{ properties: 'cursor', values: ['wait'], message: 'A busy control shows its own spinner.' },
|
|
79
|
+
]),
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- **A file that keeps one part of a rule picks that part by name**:
|
|
85
|
+
`refused.literalColour`, `refused.blend`, `refused.spacing`, `allowed.radius`,
|
|
86
|
+
`allowed.shadow`. Never by its message or its place in a list. A name a
|
|
87
|
+
release changes then stops the check instead of thinning the rule.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import { refuse, refused } from '@alis-build/design-system/stylelint'
|
|
91
|
+
// in overrides: the one file where a client's theme meets the system
|
|
92
|
+
{ files: ['**/theme-bridge.css'], rules: {
|
|
93
|
+
'declaration-property-value-disallowed-list': refuse([refused.literalColour]),
|
|
94
|
+
} }
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- **When nothing in `rules/styling.md` fits, the system has a gap.** Say so on
|
|
98
|
+
the line above, by rule and with a reason, and name it when you hand the work
|
|
99
|
+
back.
|
|
100
|
+
|
|
101
|
+
```css
|
|
102
|
+
/* stylelint-disable-next-line declaration-no-important -- GAP: a hidden element stays hidden */
|
|
103
|
+
[hidden] { display: none !important; }
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
A disable is itself reported when it names no rule (it would silence every
|
|
107
|
+
rule on that line), gives no reason, or silences nothing.
|
|
108
|
+
|
|
109
|
+
- It reads what is written in CSS. A value held in a Sass variable or a mixin,
|
|
110
|
+
or a literal behind a custom property of your own (`--pad: 13px`), is not seen.
|
|
111
|
+
|
|
112
|
+
**3. Run both checks in your build.**
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
"scripts": {
|
|
116
|
+
"lint:names": "alisx-check names src",
|
|
117
|
+
"lint:styles": "alisx-check styles \"src/**/*.{css,svelte,vue}\"",
|
|
118
|
+
"lint:styles:record": "alisx-check styles \"src/**/*.{css,svelte,vue}\" --update"
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
- `alisx-check names` fails on a system class (`app-*`, `text-*`) or a custom
|
|
123
|
+
property this version doesn't have: a misspelling, or a name a release
|
|
124
|
+
removed. Pass `--prefix app-` if your app has `text-*` utilities of its own.
|
|
125
|
+
For a property that arrives from outside your source, name the file that
|
|
126
|
+
declares it: `--declared e2e/fixtures/theme.json` reads any text file or
|
|
127
|
+
folder (a stylesheet inside JSON, server code) and counts its names as known
|
|
128
|
+
by their exact spelling. The path must exist wherever the build runs: an
|
|
129
|
+
image build may copy only the app. Where no file declares it (a library sets
|
|
130
|
+
it from script), pass `--allow` with the exact name; a pattern such as
|
|
131
|
+
`--allow '--brand-*'` lets a misspelling by.
|
|
132
|
+
- `alisx-check styles` is a gate: any violation fails. An app with a backlog
|
|
133
|
+
records it once (`lint:styles:record`) and commits
|
|
134
|
+
`alisx-style-baseline.json`; from then on it is a ratchet, failing only when a
|
|
135
|
+
file gains violations of a rule. The baseline holds the package version it was
|
|
136
|
+
recorded against: after an upgrade the check says so, because the rules may
|
|
137
|
+
have changed. Re-record only after an intentional change, never to clear a
|
|
138
|
+
failure, and say in the commit why the number moved.
|
|
139
|
+
- Both stop with exit code 2, never pass, when they could not run as set up: a
|
|
140
|
+
rule stylelint rejected, a file that didn't parse, no files matched.
|
|
141
|
+
|
|
142
|
+
Pin an exact version (`pnpm add -E`, `npm install -E`), so the rules, the
|
|
143
|
+
classes and the checks move together.
|
|
144
|
+
|
|
35
145
|
The package root has no JavaScript entry: import the files above by path.
|
|
36
146
|
Pin an exact version from a CDN with
|
|
37
147
|
`https://cdn.jsdelivr.net/npm/@alis-build/design-system@<version>/alisx.css`.
|
package/alisx.css
CHANGED
|
@@ -1068,6 +1068,75 @@ a.app-token,button.app-token{border:0;font:inherit;font-weight:600;cursor:pointe
|
|
|
1068
1068
|
.app-shortcuts__note[hidden]{display:none}
|
|
1069
1069
|
/* The foot stays in view while the groups scroll under it */
|
|
1070
1070
|
.app-shortcuts__foot{position:sticky;bottom:0;margin-top:auto;padding-top:var(--space-16);border-top:1px solid var(--outline-variant);background:var(--background)}
|
|
1071
|
+
/* ── Avatar · someone or something with a name: a person is round, a thing is a square tile ── */
|
|
1072
|
+
.app-avatar{display:inline-flex;align-items:center;justify-content:center;flex:none;vertical-align:middle;width:36px;height:36px;border-radius:var(--radius-pill);background:var(--surface-container-highest);color:var(--on-surface);font-family:var(--font-sans);font-size:14px;line-height:20px;font-weight:600;letter-spacing:0;overflow:hidden;object-fit:cover;user-select:none}
|
|
1073
|
+
.app-avatar--small{width:24px;height:24px;font-size:12px;line-height:16px}
|
|
1074
|
+
.app-avatar--large{width:80px;height:80px;font-size:28px;line-height:36px}
|
|
1075
|
+
.app-avatar--thing{border-radius:var(--radius-sm)}
|
|
1076
|
+
.app-avatar--thing.app-avatar--small{border-radius:var(--radius-xs)}
|
|
1077
|
+
.app-avatar--thing.app-avatar--large{border-radius:var(--radius-lg)}
|
|
1078
|
+
.app-avatar--large>.app-icon{font-size:40px}
|
|
1079
|
+
/* A logo and the Alis mark arrive with their own ground (rules/chips-alerts.md) */
|
|
1080
|
+
.app-avatar--logo,.app-avatar--alis{background:transparent}
|
|
1081
|
+
.app-avatar--logo{object-fit:contain}
|
|
1082
|
+
.app-avatar--cat1{background:var(--cat1-container);color:var(--on-cat1-container)}.app-avatar--cat2{background:var(--cat2-container);color:var(--on-cat2-container)}.app-avatar--cat3{background:var(--cat3-container);color:var(--on-cat3-container)}.app-avatar--cat4{background:var(--cat4-container);color:var(--on-cat4-container)}.app-avatar--cat5{background:var(--cat5-container);color:var(--on-cat5-container)}.app-avatar--cat6{background:var(--cat6-container);color:var(--on-cat6-container)}
|
|
1083
|
+
/* Several people: side by side, never overlapped, so no ring has to guess the ground under it */
|
|
1084
|
+
.app-avatar-group{display:inline-flex;align-items:center;gap:var(--space-4)}
|
|
1085
|
+
.app-avatar-group__more{font-variant-numeric:tabular-nums}
|
|
1086
|
+
|
|
1087
|
+
/* ── Checkbox and radio · native inputs; the row is the target ── */
|
|
1088
|
+
.app-check-group{display:flex;flex-direction:column}
|
|
1089
|
+
.app-check-row{position:relative;display:flex;align-items:flex-start;gap:var(--space-12);min-height:var(--control-height);padding:6px 0;color:var(--on-surface);cursor:pointer}
|
|
1090
|
+
@media (pointer:coarse){.app-check-row{min-height:var(--touch-target-coarse);padding:var(--space-12) 0}}
|
|
1091
|
+
.app-check-row__text{display:flex;flex-direction:column;flex:1;min-width:0}
|
|
1092
|
+
.app-check-row__text .text-supporting{color:var(--on-surface-variant)}
|
|
1093
|
+
.app-checkbox,.app-radio{position:relative;flex:none;width:18px;height:18px;margin:3px;border:2px solid var(--on-surface-variant);background:transparent;appearance:none;-webkit-appearance:none;cursor:pointer;transition:background-color var(--dur-effects) var(--motion-effects),border-color var(--dur-effects) var(--motion-effects),box-shadow var(--dur-effects) var(--motion-effects)}
|
|
1094
|
+
.app-checkbox{border-radius:var(--radius-xs)}
|
|
1095
|
+
.app-radio{width:20px;height:20px;margin:2px;border-radius:var(--radius-pill)}
|
|
1096
|
+
.app-checkbox:checked,.app-checkbox:indeterminate,.app-checkbox.is-checked{border-color:var(--primary);background:var(--primary)}
|
|
1097
|
+
.app-checkbox:checked::before,.app-checkbox.is-checked::before{content:"";position:absolute;left:3px;top:-1px;width:5px;height:10px;border:solid var(--on-primary);border-width:0 2px 2px 0;transform:rotate(45deg)}
|
|
1098
|
+
.app-checkbox:indeterminate::before{content:"";position:absolute;left:2px;right:2px;top:6px;height:2px;background:var(--on-primary)}
|
|
1099
|
+
.app-radio:checked{border-color:var(--primary)}
|
|
1100
|
+
.app-radio:checked::before{content:"";position:absolute;inset:3px;border-radius:inherit;background:var(--primary)}
|
|
1101
|
+
.app-checkbox:focus-visible,.app-radio:focus-visible{outline:2px solid var(--primary);outline-offset:1px}
|
|
1102
|
+
.app-check-row:hover :is(.app-checkbox,.app-radio):not(:disabled),:is(.app-checkbox,.app-radio):hover:not(:disabled){box-shadow:0 0 0 8px color-mix(in srgb,var(--on-surface) calc(var(--state-hover) * 100%),transparent)}
|
|
1103
|
+
.app-check-row:active :is(.app-checkbox,.app-radio):not(:disabled){box-shadow:0 0 0 8px color-mix(in srgb,var(--on-surface) calc(var(--state-pressed) * 100%),transparent)}
|
|
1104
|
+
.app-field.is-error :is(.app-checkbox,.app-radio):not(:checked),.app-fieldset.is-error :is(.app-checkbox,.app-radio):not(:checked){border-color:var(--error)}
|
|
1105
|
+
.app-fieldset.is-error .app-fieldset__support{color:var(--error)}
|
|
1106
|
+
:is(.app-checkbox,.app-radio):disabled{cursor:default}
|
|
1107
|
+
.app-check-row.is-disabled{opacity:.38;cursor:default}
|
|
1108
|
+
|
|
1109
|
+
/* ── Combobox · the select's box opens a menu with a search at its head ── */
|
|
1110
|
+
.app-combo{position:relative}
|
|
1111
|
+
.app-combo__trigger{width:100%;font-family:var(--font-sans);font-size:16px;line-height:24px;text-align:left;cursor:pointer;outline:none}
|
|
1112
|
+
.app-combo__trigger:focus-visible,.app-combo__trigger[aria-expanded="true"]{border-color:var(--primary);box-shadow:inset 0 0 0 1px var(--primary)}
|
|
1113
|
+
.app-combo__trigger>.app-icon{flex:none;transition:transform var(--dur-effects) var(--motion-effects)}
|
|
1114
|
+
.app-combo__trigger[aria-expanded="true"]>.app-icon{transform:rotate(180deg)}
|
|
1115
|
+
.app-combo__value{display:flex;align-items:center;gap:var(--space-8);flex:1;min-width:0;color:var(--on-surface)}
|
|
1116
|
+
.app-combo__value.is-placeholder{color:var(--on-surface-variant)}
|
|
1117
|
+
.app-combo__text{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
1118
|
+
.app-combo__panel{position:absolute;top:calc(100% + var(--space-4));left:0;right:0;z-index:2;min-width:240px;max-width:none;max-height:none;padding:0;overflow:visible}
|
|
1119
|
+
/* The search is the menu's first line: no box of its own, its glyph on the rows' leading edge */
|
|
1120
|
+
.app-combo__search{display:flex;align-items:center;gap:var(--space-12);flex:none;min-height:48px;padding:0 var(--space-16) 0 calc(var(--space-8) + var(--space-12));border-bottom:1px solid var(--outline-variant);color:var(--on-surface-variant)}
|
|
1121
|
+
.app-combo__input{flex:1;min-width:0;padding:0;border:0;background:transparent;color:var(--on-surface);font-family:var(--font-sans);font-size:14px;line-height:20px;outline:none}
|
|
1122
|
+
.app-combo__input::placeholder{color:var(--on-surface-variant);opacity:1}
|
|
1123
|
+
@media (pointer:coarse){.app-combo__input{font-size:16px;line-height:24px}}
|
|
1124
|
+
.app-combo__list{display:flex;flex-direction:column;max-height:min(280px,40vh);overflow-y:auto;overscroll-behavior:contain;margin:0;padding:var(--space-8);list-style:none}
|
|
1125
|
+
/* Several: each option leads with a drawn checkbox, and the box alone says it is chosen */
|
|
1126
|
+
.app-menu__item>.app-checkbox{margin:1px;cursor:inherit}
|
|
1127
|
+
|
|
1128
|
+
/* ── Disclosure · detail most people don't need, folded under its title ── */
|
|
1129
|
+
.app-disclosure{min-width:0;color:var(--on-surface)}
|
|
1130
|
+
.app-disclosure__summary{display:flex;align-items:center;gap:var(--space-12);min-height:max(48px,var(--touch-target));margin:0 calc(var(--space-8) * -1);padding:var(--space-4) var(--space-8);border-radius:var(--radius-sm);list-style:none}
|
|
1131
|
+
.app-disclosure__summary::-webkit-details-marker{display:none}
|
|
1132
|
+
.app-disclosure__text{display:flex;flex-direction:column;flex:1;min-width:0}
|
|
1133
|
+
.app-disclosure__text .text-supporting{color:var(--on-surface-variant)}
|
|
1134
|
+
.app-disclosure__summary>.app-icon:last-child{flex:none;color:var(--on-surface-variant);transition:transform var(--dur-effects) var(--motion-effects)}
|
|
1135
|
+
.app-disclosure[open]>.app-disclosure__summary>.app-icon:last-child{transform:rotate(180deg)}
|
|
1136
|
+
.app-disclosure__body{padding:var(--space-4) 0 var(--space-16)}
|
|
1137
|
+
.app-disclosure-group{display:flex;flex-direction:column}
|
|
1138
|
+
.app-disclosure-group>.app-disclosure+.app-disclosure{border-top:1px solid var(--outline-variant)}
|
|
1139
|
+
|
|
1071
1140
|
/* ── Drop target, while something is dragged over it ── */
|
|
1072
1141
|
.drop-target{outline:2px dashed var(--primary);outline-offset:-2px;background-color:color-mix(in srgb,var(--primary) calc(var(--state-dragged) * 100%),transparent)}
|
|
1073
1142
|
|
package/check.mjs
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// alisx-check: the two checks an app built on @alis-build/design-system runs in
|
|
3
|
+
// its build. Neither needs a framework; both read your source as text.
|
|
4
|
+
//
|
|
5
|
+
// alisx-check names <path…> [--prefix app-] [--declared <path>] [--allow '--brand-*']
|
|
6
|
+
// Fails on a system class or a custom property this version of the package
|
|
7
|
+
// doesn't have: a misspelling, or a name a release removed. Without it a
|
|
8
|
+
// wrong name renders unstyled and nothing says so.
|
|
9
|
+
// --prefix the class prefixes that belong to the system (default: app-
|
|
10
|
+
// and text-; an app with its own text-* utilities passes app-)
|
|
11
|
+
// --declared a file or folder outside your source that declares custom
|
|
12
|
+
// properties: a theme a server sends, a test fixture, server
|
|
13
|
+
// code. Any text file, JSON included. Its names count as
|
|
14
|
+
// known by their exact spelling; nothing in it is checked.
|
|
15
|
+
// --allow a custom property no file declares (a library sets it from
|
|
16
|
+
// script). Prefer --declared: a pattern lets a misspelling by.
|
|
17
|
+
// It reads a class where one is written (a selector, a class attribute, a
|
|
18
|
+
// class directive, classList), not a name held in a script string.
|
|
19
|
+
//
|
|
20
|
+
// alisx-check styles <glob…> [--baseline <file>] [--update]
|
|
21
|
+
// Runs your stylelint config (which extends ./stylelint, the check that
|
|
22
|
+
// restates rules/styling.md). With no baseline it is a gate: any violation
|
|
23
|
+
// fails. With one it is a ratchet: it fails only when a file gains
|
|
24
|
+
// violations of a rule, so a backlog doesn't block new work. --update
|
|
25
|
+
// records the baseline (default alisx-style-baseline.json, where you run
|
|
26
|
+
// it); do that only after an intentional change, never to clear a failure.
|
|
27
|
+
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
28
|
+
import { spawnSync } from 'node:child_process'
|
|
29
|
+
import { tmpdir } from 'node:os'
|
|
30
|
+
import { dirname, extname, join, relative, resolve, sep } from 'node:path'
|
|
31
|
+
import { fileURLToPath } from 'node:url'
|
|
32
|
+
|
|
33
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
34
|
+
const { version } = JSON.parse(readFileSync(join(here, 'package.json'), 'utf8'))
|
|
35
|
+
const usage = 'usage: alisx-check names <path…> [--prefix app-] [--declared <path>] [--allow <property>]\n alisx-check styles <glob…> [--baseline <file>] [--update]'
|
|
36
|
+
const [command, ...argv] = process.argv.slice(2)
|
|
37
|
+
|
|
38
|
+
const paths = []
|
|
39
|
+
const options = { prefix: [], allow: [], declared: [], baseline: 'alisx-style-baseline.json', update: false }
|
|
40
|
+
for (let i = 0; i < argv.length; i++) {
|
|
41
|
+
const arg = argv[i]
|
|
42
|
+
if (arg === '--update') options.update = true
|
|
43
|
+
else if (arg === '--baseline' || arg === '--prefix' || arg === '--allow' || arg === '--declared') {
|
|
44
|
+
const value = argv[++i]
|
|
45
|
+
// --allow takes a property, which itself starts with two dashes.
|
|
46
|
+
if (!value || (arg !== '--allow' && value.startsWith('--'))) stop(`${arg} needs a value.\n${usage}`)
|
|
47
|
+
if (arg === '--baseline') options.baseline = value
|
|
48
|
+
else options[arg.slice(2)].push(value)
|
|
49
|
+
} else if (arg.startsWith('--')) stop(`Unknown option ${arg}.\n${usage}`)
|
|
50
|
+
else paths.push(arg)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (command === 'names' && paths.length) names()
|
|
54
|
+
else if (command === 'styles' && paths.length) styles()
|
|
55
|
+
else stop(usage)
|
|
56
|
+
|
|
57
|
+
function names() {
|
|
58
|
+
const inventory = JSON.parse(readFileSync(join(here, 'classes.json'), 'utf8'))
|
|
59
|
+
const classes = new Set(inventory.classes)
|
|
60
|
+
const properties = new Set(inventory.properties)
|
|
61
|
+
const prefixes = (options.prefix.length ? options.prefix : ['app-', 'text-']).map(escape).join('|')
|
|
62
|
+
const allowed = options.allow.map((glob) => new RegExp(`^${glob.split('*').map(escape).join('.*')}$`))
|
|
63
|
+
|
|
64
|
+
const sources = ['.svelte', '.vue', '.html', '.jsx', '.tsx', '.js', '.ts', '.mjs', '.css', '.scss', '.less']
|
|
65
|
+
const missing = [...paths, ...options.declared].filter((path) => !existsSync(path))
|
|
66
|
+
if (missing.length) stop(`alisx-check names: no such path: ${missing.join(', ')}`)
|
|
67
|
+
const files = paths.flatMap((path) => walk(resolve(path))).filter((file) => sources.includes(extname(file)))
|
|
68
|
+
if (!files.length) stop(`alisx-check names: no source files under ${paths.join(', ')}.`)
|
|
69
|
+
const texts = new Map(files.map((file) => [file, readFileSync(file, 'utf8')]))
|
|
70
|
+
|
|
71
|
+
// A property your own source declares is yours, wherever it is used, and so
|
|
72
|
+
// is one a --declared file declares. Declared: `--x: …` in a rule or an
|
|
73
|
+
// object, style:--x on an element, setProperty('--x', …) in script.
|
|
74
|
+
const declared = [/(?:^|[\s{;"'`])(--[\w-]+)["'`]?\s*:/g, /\bstyle:(--[\w-]+)/g, /setProperty\(\s*["'`](--[\w-]+)/g]
|
|
75
|
+
for (const path of options.declared) {
|
|
76
|
+
let found = 0
|
|
77
|
+
for (const text of walk(resolve(path)).map(readDeclared))
|
|
78
|
+
for (const pattern of declared)
|
|
79
|
+
for (const [, name] of text.matchAll(pattern)) {
|
|
80
|
+
properties.add(name)
|
|
81
|
+
found++
|
|
82
|
+
}
|
|
83
|
+
// A path that declares nothing is a wrong path or a changed format, and the
|
|
84
|
+
// names it used to cover would fail below with no hint why.
|
|
85
|
+
if (!found) stop(`alisx-check names: no custom property is declared in ${path}.`)
|
|
86
|
+
}
|
|
87
|
+
for (const text of texts.values())
|
|
88
|
+
for (const pattern of declared) for (const [, name] of text.matchAll(pattern)) properties.add(name)
|
|
89
|
+
|
|
90
|
+
// A class is read where a class is written: a selector, a class attribute, a
|
|
91
|
+
// class directive, classList. Anywhere else the same letters are not a class.
|
|
92
|
+
const token = new RegExp(`(?<![\\w-])((?:${prefixes})[\\w-]*[A-Za-z0-9])(?![\\w-]|\\{|\\$)`, 'g')
|
|
93
|
+
const selector = new RegExp(`(?<![\\w/-])\\.((?:${prefixes})[\\w-]*[A-Za-z0-9])`, 'g')
|
|
94
|
+
const written = [
|
|
95
|
+
/\bclass(?:Name)?\s*=\s*(?:"([^"]*)"|'([^']*)'|\{([^}]*)\})/g,
|
|
96
|
+
/\bclass:([\w-]+)/g,
|
|
97
|
+
/\bclassList\.\w+\(([^)]*)\)/g,
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
const problems = []
|
|
101
|
+
let uses = 0
|
|
102
|
+
for (const [file, text] of texts) {
|
|
103
|
+
const line = (index) => text.slice(0, index).split('\n').length
|
|
104
|
+
const seen = (name, index, known, kind) => {
|
|
105
|
+
uses++
|
|
106
|
+
if (!known.has(name)) problems.push(`${relative(process.cwd(), file)}:${line(index)} ${kind} ${name}`)
|
|
107
|
+
}
|
|
108
|
+
for (const match of text.matchAll(selector)) seen(match[1], match.index, classes, 'class')
|
|
109
|
+
for (const pattern of written)
|
|
110
|
+
for (const match of text.matchAll(pattern))
|
|
111
|
+
for (const found of (match[1] ?? match[2] ?? match[3] ?? '').matchAll(token)) seen(found[1], match.index, classes, 'class')
|
|
112
|
+
for (const match of text.matchAll(/var\(\s*(--[\w-]+)/g))
|
|
113
|
+
if (!allowed.some((pattern) => pattern.test(match[1]))) seen(match[1], match.index, properties, 'property')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!problems.length) {
|
|
117
|
+
console.log(`alisx-check names: ok. ${uses} uses in ${files.length} files, all in @alis-build/design-system@${version}.`)
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
console.error(`\nalisx-check names: not in @alis-build/design-system@${version}, and not declared in your source.\n`)
|
|
121
|
+
for (const problem of [...new Set(problems)]) console.error(` ${problem}`)
|
|
122
|
+
console.error('\nA misspelling, or a name this version removed: components/ and tokens.css in the package have the names.\n')
|
|
123
|
+
process.exit(1)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function styles() {
|
|
127
|
+
const bin = findUp(join('node_modules', '.bin', 'stylelint'))
|
|
128
|
+
if (!bin) stop('alisx-check styles: stylelint is not installed in this app. Add stylelint 16.11 or later as a dev dependency.')
|
|
129
|
+
// The config asks for reports stylelint gained in 16.11. An older one skips
|
|
130
|
+
// them and says nothing, which would read as a clean result.
|
|
131
|
+
const installed = (spawnSync(bin, ['--version'], { encoding: 'utf8' }).stdout ?? '').trim()
|
|
132
|
+
const [major, minor] = installed.split('.').map(Number)
|
|
133
|
+
if (!(major > 16 || (major === 16 && minor >= 11))) stop(`alisx-check styles: stylelint ${installed || 'of an unknown version'} is installed, and the check needs 16.11 or later.`)
|
|
134
|
+
|
|
135
|
+
// The report goes to a file: stylelint's own stream for it changed between
|
|
136
|
+
// versions, and it also echoes the report, so the pipe must not cap it.
|
|
137
|
+
const report = join(mkdtempSync(join(tmpdir(), 'alisx-check-')), 'report.json')
|
|
138
|
+
const run = spawnSync(bin, ['--formatter', 'json', '--output-file', report, ...paths], { encoding: 'utf8', maxBuffer: Infinity })
|
|
139
|
+
let results
|
|
140
|
+
try {
|
|
141
|
+
results = JSON.parse(readFileSync(report, 'utf8'))
|
|
142
|
+
} catch {
|
|
143
|
+
// No report: stylelint itself failed (no files matched, a config that won't load).
|
|
144
|
+
}
|
|
145
|
+
rmSync(dirname(report), { recursive: true, force: true })
|
|
146
|
+
if (!results) stop(`alisx-check styles: stylelint did not run.\n\n${(run.stderr || run.stdout || String(run.error)).trim()}`)
|
|
147
|
+
|
|
148
|
+
// A rule that didn't run, or a file that didn't parse, reports nothing: that
|
|
149
|
+
// would read as an improvement. Stop instead.
|
|
150
|
+
const broken = []
|
|
151
|
+
for (const result of results) {
|
|
152
|
+
for (const each of result.invalidOptionWarnings ?? []) broken.push(each.text)
|
|
153
|
+
for (const each of result.parseErrors ?? []) broken.push(`${result.source}: ${each.text}`)
|
|
154
|
+
for (const each of result.warnings) if (each.rule === 'CssSyntaxError') broken.push(`${result.source}:${each.line}: ${each.text}`)
|
|
155
|
+
}
|
|
156
|
+
if (broken.length)
|
|
157
|
+
stop(
|
|
158
|
+
`alisx-check styles: the check could not run as configured, so nothing was compared.\n\n ${[...new Set(broken)].join('\n ')}\n\nA file with a <style> block needs its syntax in your stylelint config (postcss-html; postcss-scss for lang="scss").`,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
// Keyed by the file's path from the baseline's own directory, so the same
|
|
162
|
+
// baseline reads the same wherever the command is run from.
|
|
163
|
+
const root = dirname(resolve(options.baseline))
|
|
164
|
+
const key = (source) => relative(root, source).split(sep).join('/')
|
|
165
|
+
const now = {}
|
|
166
|
+
const linted = new Set()
|
|
167
|
+
for (const result of results) {
|
|
168
|
+
const path = key(result.source)
|
|
169
|
+
linted.add(path)
|
|
170
|
+
for (const { rule } of result.warnings) {
|
|
171
|
+
now[path] ??= {}
|
|
172
|
+
now[path][rule] = (now[path][rule] ?? 0) + 1
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const total = (files) => Object.values(files).reduce((sum, rules) => sum + Object.values(rules).reduce((a, b) => a + b, 0), 0)
|
|
176
|
+
|
|
177
|
+
let baseline = null
|
|
178
|
+
if (existsSync(options.baseline)) {
|
|
179
|
+
try {
|
|
180
|
+
baseline = JSON.parse(readFileSync(options.baseline, 'utf8'))
|
|
181
|
+
if (!baseline.files) throw new Error('it has no "files"')
|
|
182
|
+
} catch (error) {
|
|
183
|
+
stop(`alisx-check styles: ${options.baseline} can't be read (${error.message}). Restore it from version control, or re-record it with --update and say why in the commit.`)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// A file the baseline names that this run didn't lint keeps its entry while
|
|
187
|
+
// it exists: a run over part of the app says nothing about the rest.
|
|
188
|
+
const unseen = Object.fromEntries(Object.entries(baseline?.files ?? {}).filter(([path]) => !linted.has(path) && existsSync(join(root, path))))
|
|
189
|
+
|
|
190
|
+
if (options.update) {
|
|
191
|
+
const files = Object.fromEntries(Object.entries({ ...unseen, ...now }).sort(([a], [b]) => a.localeCompare(b)))
|
|
192
|
+
writeFileSync(options.baseline, `${JSON.stringify({ version, files }, null, 2)}\n`)
|
|
193
|
+
console.log(`alisx-check styles: recorded ${total(files)} violations across ${Object.keys(files).length} files in ${options.baseline}, against @alis-build/design-system@${version}.`)
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const was = baseline?.files ?? {}
|
|
198
|
+
const worse = []
|
|
199
|
+
for (const [path, rules] of Object.entries(now))
|
|
200
|
+
for (const [rule, count] of Object.entries(rules)) if (count > (was[path]?.[rule] ?? 0)) worse.push({ path, rule, from: was[path]?.[rule] ?? 0, to: count })
|
|
201
|
+
let saved = 0
|
|
202
|
+
for (const [path, rules] of Object.entries(was))
|
|
203
|
+
if (!(path in unseen)) for (const [rule, count] of Object.entries(rules)) saved += Math.max(0, count - (now[path]?.[rule] ?? 0))
|
|
204
|
+
|
|
205
|
+
if (baseline && baseline.version !== version)
|
|
206
|
+
console.log(
|
|
207
|
+
`alisx-check styles: the baseline was recorded against @alis-build/design-system@${baseline.version}; ${version} is installed, and its rules may differ. If a change below comes from the rules and not from your code, re-record with --update and say so in the commit.`,
|
|
208
|
+
)
|
|
209
|
+
if (saved) console.log(`alisx-check styles: ${saved} fewer violations than the baseline. Run with --update to lock that in.`)
|
|
210
|
+
if (!worse.length) {
|
|
211
|
+
console.log(`alisx-check styles: ok. ${total(now) ? `${total(now)} known violations, none new` : 'No violations'}.`)
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
console.error(`\nalisx-check styles: ${baseline ? 'more violations than the baseline' : 'violations'}.\n`)
|
|
216
|
+
for (const { path, rule, from, to } of worse) {
|
|
217
|
+
console.error(` ${path} ${rule} ${from} → ${to}`)
|
|
218
|
+
const warnings = results.find((result) => key(result.source) === path).warnings.filter((warning) => warning.rule === rule)
|
|
219
|
+
for (const warning of warnings.slice(0, 20)) console.error(` ${warning.line}:${warning.column} ${warning.text.replace(` (${rule})`, '')}`)
|
|
220
|
+
if (warnings.length > 20) console.error(` … and ${warnings.length - 20} more`)
|
|
221
|
+
}
|
|
222
|
+
console.error(
|
|
223
|
+
'\nEvery rule is a row of rules/styling.md in @alis-build/design-system. If nothing there fits, the\nsystem has a gap: say so on the line above, and name it when you hand the work back.\n\n /* stylelint-disable-next-line <rule> -- GAP: what this needs */\n',
|
|
224
|
+
)
|
|
225
|
+
process.exit(1)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// A --declared file, as text. In JSON a stylesheet is a string with its line
|
|
229
|
+
// breaks escaped, so its strings are read as they would arrive too.
|
|
230
|
+
function readDeclared(file) {
|
|
231
|
+
const text = readFileSync(file, 'utf8')
|
|
232
|
+
if (extname(file) !== '.json') return text
|
|
233
|
+
const strings = [text]
|
|
234
|
+
try {
|
|
235
|
+
JSON.parse(text, (key, value) => (typeof value === 'string' && strings.push(value), value))
|
|
236
|
+
} catch {
|
|
237
|
+
// Not JSON after all: the text as written is all there is.
|
|
238
|
+
}
|
|
239
|
+
return strings.join('\n')
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function walk(path) {
|
|
243
|
+
if (!statSync(path).isDirectory()) return [path]
|
|
244
|
+
return readdirSync(path, { withFileTypes: true })
|
|
245
|
+
.filter((entry) => !entry.name.startsWith('.') && entry.name !== 'node_modules' && entry.name !== 'dist')
|
|
246
|
+
.flatMap((entry) => walk(join(path, entry.name)))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function findUp(target) {
|
|
250
|
+
for (let dir = process.cwd(); ; dir = dirname(dir)) {
|
|
251
|
+
if (existsSync(join(dir, target))) return join(dir, target)
|
|
252
|
+
if (dir === dirname(dir)) return null
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function escape(text) {
|
|
257
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// A setup problem, as opposed to a finding: exit 2.
|
|
261
|
+
function stop(message) {
|
|
262
|
+
console.error(message)
|
|
263
|
+
process.exit(2)
|
|
264
|
+
}
|