@seanyeh77/ui-cli 0.39.0 → 0.41.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.
Files changed (2) hide show
  1. package/dist/cli.js +98 -13
  2. package/package.json +4 -3
package/dist/cli.js CHANGED
@@ -14,7 +14,9 @@ function measure() {
14
14
  markdown: [],
15
15
  native: [],
16
16
  wrapped: [],
17
- escaped: []
17
+ escaped: [],
18
+ mixed: [],
19
+ raised: []
18
20
  };
19
21
  const describe = (el) => {
20
22
  let s = el.tagName.toLowerCase();
@@ -137,36 +139,106 @@ function measure() {
137
139
  return new Set([...range.getClientRects()].filter((r) => r.width > 1).map((r) => Math.round(r.top / 4))).size > 1;
138
140
  }))) out.wrapped.push(describe(el));
139
141
  }
142
+ const bars = ".ui-dt-tools, .ui-dt-actions, .ui-card-actions, [role=toolbar]";
143
+ const controls = "button, a[href], input:not([type=hidden]), [role=button], [role=combobox], .ui-searchfield, .ui-pwfield, .ui-datefield, .ui-ms, .ui-select";
144
+ for (const bar of document.querySelectorAll(bars)) {
145
+ if (out.mixed.length >= 6) break;
146
+ if (bar.parentElement?.closest(bars) || !visible(bar)) continue;
147
+ const items = [];
148
+ for (const el of bar.querySelectorAll(controls)) {
149
+ const own = el.parentElement?.closest(controls);
150
+ if (own && bar.contains(own) || !visible(el) || el.closest("[aria-hidden=\"true\"]")) continue;
151
+ items.push({
152
+ el,
153
+ r: el.getBoundingClientRect()
154
+ });
155
+ }
156
+ let hit = "";
157
+ for (let i = 0; i < items.length && !hit; i++) for (let j = i + 1; j < items.length && !hit; j++) {
158
+ const a = items[i].r;
159
+ const b = items[j].r;
160
+ if (Math.min(a.bottom, b.bottom) > Math.max(a.top, b.top) && Math.abs(a.height - b.height) > 1) hit = `${describe(bar)}: ${describe(items[i].el)} ${Math.round(a.height)} px, ${describe(items[j].el)} ${Math.round(b.height)} px`;
161
+ }
162
+ if (hit) out.mixed.push(hit);
163
+ }
164
+ for (const el of document.querySelectorAll(".ui-dt-body .ui-badge, .ui-card .ui-badge")) {
165
+ if (out.raised.length >= 6) break;
166
+ const sh = getComputedStyle(el).boxShadow;
167
+ if (sh && sh !== "none" && visible(el)) out.raised.push(describe(el));
168
+ }
140
169
  return out;
141
170
  }
171
+ // src/usage.ts
172
+ const USAGE = /^## 用法[ \t]*$/m;
173
+ /** The title and the 用法 section, up to the next level-2 heading; null when the file has no 用法 section. */
174
+ function usageBlock(md) {
175
+ const start = USAGE.exec(md);
176
+ if (!start) return null;
177
+ const after = start.index + start[0].length;
178
+ let fenced = false;
179
+ let end = md.length;
180
+ let pos = after;
181
+ for (const line of md.slice(after).split("\n")) {
182
+ if (/^\s*(```|~~~)/.test(line)) fenced = !fenced;
183
+ else if (!fenced && /^## /.test(line)) {
184
+ end = pos;
185
+ break;
186
+ }
187
+ pos += line.length + 1;
188
+ }
189
+ return md.slice(0, Math.min(end, md.length)).trimEnd() + "\n";
190
+ }
191
+ /** One sentence about the file: the first sentence of the 用法 section, or of the first text line after the title. */
192
+ function summary(md) {
193
+ const start = USAGE.exec(md);
194
+ const line = (start ? md.slice(start.index + start[0].length) : md).split("\n").find((l) => l.trim() && !l.startsWith("#") && !l.startsWith("|"))?.trim() ?? "";
195
+ let code = false;
196
+ for (let i = 0; i < line.length; i++) if (line[i] === "`") code = !code;
197
+ else if (!code && line[i] === "。") return line.slice(0, i + 1);
198
+ return line;
199
+ }
142
200
  // src/cli.ts
143
201
  const HELP = `ui check <file or URL>... [--shots DIR] [--strict] [--wait MS]
144
202
  Opens each page at 390 px and 1280 px, light and dark, and fails (exit 1) on:
145
203
  console errors, horizontal overflow at 390 px, touch targets under 48 px, touch areas that spill past their control, a card inside a card,
146
- raw Markdown, native controls the design system replaces, and wrapped buttons, tabs, headers or headings.
204
+ raw Markdown, native controls the design system replaces, wrapped buttons, tabs, headers or headings, controls of
205
+ different heights in one toolbar row, and a status badge with a shadow in a table or card.
147
206
  Exits 2 when a page cannot be checked at all: it does not load, or the check itself crashes.
148
- ui docs [Name] prints the README of a component (Button, DataTable…) or a guide (guidelines, layout, motion)
149
- ui list lists every component`;
207
+ ui docs [Name] [--full]
208
+ Prints the 用法 block of a component (Button, DataTable…): what it is, a minimal example and the rules most often
209
+ gotten wrong. --full prints the whole README. Guides print whole: guidelines (the core rules every page needs),
210
+ layout, forms, data, monitoring, code, theme, motion. Without a name, lists every doc.
211
+ ui list lists every guide and component with its one-line description`;
150
212
  function docsDir() {
151
213
  const require = createRequire(import.meta.url);
152
214
  return join(dirname(require.resolve("@seanyeh77/ui/package.json")), "docs");
153
215
  }
154
- function docs(name) {
155
- const dir = docsDir();
216
+ /** Guides (lowercase file names) first, then the components. */
217
+ function docFiles(dir) {
156
218
  const files = readdirSync(dir).filter((f) => f.endsWith(".md"));
219
+ const guide = (f) => /^[a-z]/.test(f) ? 0 : 1;
220
+ return files.sort((a, b) => guide(a) - guide(b) || (a === "guidelines.md" ? -1 : b === "guidelines.md" ? 1 : a.localeCompare(b)));
221
+ }
222
+ function docs(name, full) {
223
+ const dir = docsDir();
224
+ const files = docFiles(dir);
157
225
  if (!name) return console.log(files.map((f) => f.slice(0, -3)).join("\n"));
158
226
  const hit = files.find((f) => f.slice(0, -3).toLowerCase() === name.toLowerCase());
159
227
  if (!hit) {
160
228
  console.error(`No docs for "${name}". Run "ui docs" for the list.`);
161
229
  process.exit(1);
162
230
  }
163
- console.log(readFileSync(join(dir, hit), "utf8"));
231
+ const md = readFileSync(join(dir, hit), "utf8");
232
+ const usage = full ? null : usageBlock(md);
233
+ if (!usage) return console.log(md);
234
+ console.log(usage + `\n(用法 only. The whole README: ui docs ${hit.slice(0, -3)} --full)`);
164
235
  }
165
236
  function list() {
166
237
  const dir = docsDir();
167
- for (const f of readdirSync(dir).filter((x) => x.endsWith(".md") && /^[A-Z]/.test(x))) {
168
- const first = readFileSync(join(dir, f), "utf8").split("\n").find((l) => l.trim() && !l.startsWith("#")) ?? "";
169
- console.log(`${f.slice(0, -3).padEnd(18)} ${first.trim()}`);
238
+ for (const f of docFiles(dir)) {
239
+ const md = readFileSync(join(dir, f), "utf8");
240
+ const line = /^[a-z]/.test(f) ? /^# (.+)$/m.exec(md)?.[1] ?? "" : summary(md);
241
+ console.log(`${f.slice(0, -3).padEnd(18)} ${line}`);
170
242
  }
171
243
  }
172
244
  /** The page itself could not be opened: a wrong path, no server, or an HTTP error. */
@@ -228,7 +300,9 @@ async function check(targets, opts) {
228
300
  };
229
301
  await run(() => page.evaluate((t) => {
230
302
  const r = document.documentElement;
231
- if (r.hasAttribute("data-theme") || "UI" in window) r.setAttribute("data-theme", t);
303
+ const ui = window.UI;
304
+ if (typeof ui?.setTheme === "function") ui.setTheme(t);
305
+ else if (r.hasAttribute("data-theme")) r.setAttribute("data-theme", t);
232
306
  }, theme));
233
307
  await page.waitForTimeout(opts.wait);
234
308
  await page.waitForLoadState("load");
@@ -240,6 +314,8 @@ async function check(targets, opts) {
240
314
  if (res.markdown.length) problems.push("raw Markdown shown: " + res.markdown.join("; "));
241
315
  if (res.native.length) problems.push("native controls: " + [...new Set(res.native)].join("; "));
242
316
  if (res.wrapped.length) problems.push("text that must stay on one line wraps: " + res.wrapped.join("; "));
317
+ if (res.mixed.length) problems.push("controls of different heights in one toolbar row: " + res.mixed.join("; ") + ". Use the system controls (Button, IconButton, SearchField, Select: 40 px) without your own height or padding.");
318
+ if (res.raised.length) problems.push("status badge with a shadow in a table or card: " + res.raised.join("; ") + ". Inside a table or card a Badge is a dot and text: remove your own box-shadow or chip styles.");
243
319
  if (phone) {
244
320
  if (res.overflow.length) problems.push("horizontal overflow: " + res.overflow.join("; "));
245
321
  if (res.escaped.length) problems.push("touch area larger than its control (covers other controls): " + res.escaped.join("; "));
@@ -328,8 +404,17 @@ if (cmd === "check") {
328
404
  strict: !!values.strict,
329
405
  wait
330
406
  });
331
- } else if (cmd === "docs") docs(rest[0]);
332
- else if (cmd === "list") list();
407
+ } else if (cmd === "docs") {
408
+ const { values, positionals } = parseArgs({
409
+ args: rest,
410
+ allowPositionals: true,
411
+ options: { full: {
412
+ type: "boolean",
413
+ default: false
414
+ } }
415
+ });
416
+ docs(positionals[0], !!values.full);
417
+ } else if (cmd === "list") list();
333
418
  else {
334
419
  console.log(HELP);
335
420
  process.exit(cmd ? 2 : 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seanyeh77/ui-cli",
3
- "version": "0.39.0",
3
+ "version": "0.41.0",
4
4
  "description": "Checks a page against SeanYeh's design system and prints component docs: ui check, ui docs, ui list.",
5
5
  "license": "MIT",
6
6
  "author": "Hsiu-An Yeh",
@@ -18,13 +18,14 @@
18
18
  ],
19
19
  "dependencies": {
20
20
  "playwright": "^1.65.0",
21
- "@seanyeh77/ui": "^0.39.0"
21
+ "@seanyeh77/ui": "^0.41.0"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=22"
25
25
  },
26
26
  "scripts": {
27
27
  "build": "tsdown",
28
- "typecheck": "tsc -p ."
28
+ "typecheck": "tsc -p .",
29
+ "test": "node --test src/*.test.ts"
29
30
  }
30
31
  }