@mark1russell7/site-pages 0.4.9 → 0.4.12

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 CHANGED
@@ -543,6 +543,49 @@ The theme `auto` removes `data-theme`, and the CSS of the site then obeys `prefe
543
543
  </script>
544
544
  ```
545
545
 
546
+ ## The parts of a live demo
547
+
548
+ A home page shows the package at work: a visitor types or clicks, and the page shows the result of the real functions. The entry `@mark1russell7/site-pages/demo` has the parts that each demo uses. Like `./chrome`, it imports no Node.js module. The file `@mark1russell7/site-pages/demo.css` has their style.
549
+
550
+ | Function | Operation |
551
+ | --- | --- |
552
+ | `examples(row, items, pick)` | Fills a row with one button for each example `[label, value]`. A click starts `pick` with the value. |
553
+ | `verdict(pass, title, ...reason)` | Makes the line of a verdict: a check or a cross, a title in bold and a reason. The mark has the label "Pass" or "Fail". |
554
+ | `setField(field, value)` | Sets the value of a field and sends an `input` event, as typing does. |
555
+ | `partsAt(text, ranges)` | Divides a text into parts at ranges `[start, end)`. Ranges that overlap become one part. |
556
+ | `partsOf(text, find)` | Divides a text into parts at each copy of `find`. |
557
+ | `markedText(parts, label)` | Shows a text in a box, with a `mark` element on each part with `hit`. |
558
+
559
+ The style gives these classes:
560
+
561
+ | Class | Element |
562
+ | --- | --- |
563
+ | `sp-demo` | A section of the page with one demo. |
564
+ | `sp-field` | A label with its input, its text box or its range. Put the text of the label in a `span`. |
565
+ | `sp-pair` | Two or three fields side by side. On a phone, they are in one column. |
566
+ | `sp-examples` | The row of `examples`. A button with `aria-pressed="true"` shows as chosen. |
567
+ | `sp-choices` | A row of choices, for example a radio group. The button with `aria-checked="true"` shows as chosen. |
568
+ | `sp-cards`, `sp-card` | One card for each version of a result. |
569
+
570
+ ```ts
571
+ import "@mark1russell7/site-pages/demo.css";
572
+ import { byId } from "@mark1russell7/site-pages/chrome";
573
+ import { examples, markedText, partsOf, setField, verdict } from "@mark1russell7/site-pages/demo";
574
+
575
+ const input = byId<HTMLInputElement>("find");
576
+ const result = byId("result");
577
+ const show = (): void => {
578
+ const parts = partsOf("Is 1+1=2? Yes.", input.value);
579
+ const found = parts.some((part) => part.hit);
580
+ result.replaceChildren(verdict(found, found ? "Found:" : "Not found:", `the text has "${input.value}".`), markedText(parts, "The text"));
581
+ };
582
+ input.addEventListener("input", show);
583
+ examples(byId("find-examples"), [["A question", "1+1=2?"], ["A word", "Yes"]], (value) => setField(input, value));
584
+ show();
585
+ ```
586
+
587
+ The style reads the tokens of a site when they exist, for example `--surface`, `--ink`, `--muted` and `--rule` of `shell.css`. Without them, it uses its own colors, in light and in dark. The tokens `--sp-pass`, `--sp-pass-bg`, `--sp-fail`, `--sp-fail-bg` and `--sp-hit` set the colors of a verdict and of a mark.
588
+
546
589
  ## The measures of a site
547
590
 
548
591
  The file `kpi.json` at the root of a site holds the measures of the project, in the format `kpi/1`. A reader outside the site takes the numbers from this file.
package/build/cli.js CHANGED
@@ -475,12 +475,16 @@ async function launch(executable, timeoutMs = 3e4, callMs = CALL_LIMIT_MS) {
475
475
  child.kill();
476
476
  await Promise.race([new Promise((resolve) => child.once("exit", resolve)), sleep(5e3)]);
477
477
  }
478
- rmSync(profile, {
479
- recursive: true,
480
- force: true,
481
- maxRetries: 5,
482
- retryDelay: 200
483
- });
478
+ try {
479
+ rmSync(profile, {
480
+ recursive: true,
481
+ force: true,
482
+ maxRetries: 10,
483
+ retryDelay: 200
484
+ });
485
+ } catch (error) {
486
+ process.stderr.write(`site-pages check: the browser profile ${profile} stays in the temporary folder: ${error instanceof Error ? error.message : String(error)}\n`);
487
+ }
484
488
  };
485
489
  try {
486
490
  const portFile = join(profile, "DevToolsActivePort");
@@ -954,27 +958,37 @@ async function checkSite(options) {
954
958
  };
955
959
  };
956
960
  let { page, events } = await start();
961
+ const restart = async () => {
962
+ await browser?.close();
963
+ browser = void 0;
964
+ return start();
965
+ };
957
966
  if (options.shots) mkdirSync(options.shots, { recursive: true });
958
967
  for (const [index, path] of options.pages.entries()) {
959
968
  const target = new URL(path.replace(/^\/+/, ""), homeUrl).href;
960
969
  for (const width of options.widths) for (const scheme of options.schemes) {
961
- const probe = scheme === options.schemes[0] ? probes[index] : void 0;
962
- const { view, hung } = await checkView(page, events, {
970
+ const input = {
963
971
  target,
964
972
  path,
965
973
  width,
966
974
  scheme,
967
- probe,
975
+ probe: scheme === options.schemes[0] ? probes[index] : void 0,
968
976
  options
969
- });
970
- views.push(view);
977
+ };
978
+ let { view, hung } = await checkView(page, events, input);
971
979
  if (hung) {
972
- log(`the browser did not answer at ${path} ${width} ${scheme}: the check starts a new browser`);
973
- await browser?.close();
974
- browser = void 0;
975
- ({page, events} = await start());
980
+ log(`the browser did not answer at ${path} ${width} ${scheme}: the check starts a new browser and tries the view again`);
981
+ ({page, events} = await restart());
982
+ const first = view.problems;
983
+ ({view, hung} = await checkView(page, events, input));
984
+ view.retried = first;
985
+ if (hung) {
986
+ log(`the browser did not answer again at ${path} ${width} ${scheme}: the check starts a new browser`);
987
+ ({page, events} = await restart());
988
+ }
976
989
  }
977
- log(`${view.ok ? "PASS" : "FAIL"} ${path} ${width} ${scheme}${view.measure ? ` scrollWidth=${view.measure.scrollWidth} innerWidth=${view.measure.innerWidth}` : ""}${view.probe ? ` probe ${view.probe.filter((c) => c.ok).length}/${view.probe.length}` : ""}${view.problems.length > 0 ? `\n - ${view.problems.join("\n - ")}` : ""}`);
990
+ views.push(view);
991
+ log(`${view.ok ? "PASS" : "FAIL"}${view.retried ? " (retried)" : ""} ${path} ${width} ${scheme}${view.measure ? ` scrollWidth=${view.measure.scrollWidth} innerWidth=${view.measure.innerWidth}` : ""}${view.probe ? ` probe ${view.probe.filter((c) => c.ok).length}/${view.probe.length}` : ""}${view.problems.length > 0 ? `\n - ${view.problems.join("\n - ")}` : ""}`);
978
992
  }
979
993
  }
980
994
  } finally {
@@ -1133,14 +1147,15 @@ function cell(text) {
1133
1147
  /** This function gives the Markdown summary of a result, for the job summary of a workflow. */
1134
1148
  function summary(result) {
1135
1149
  const failed = result.views.filter((view) => !view.ok);
1150
+ const retried = result.views.filter((view) => view.ok && view.retried !== void 0);
1136
1151
  return [
1137
1152
  `### Site check: ${result.ok ? "passed" : "failed"}`,
1138
1153
  "",
1139
- `${result.views.length - failed.length} of ${result.views.length} views passed, at ${result.url}.`,
1154
+ `${result.views.length - failed.length} of ${result.views.length} views passed, at ${result.url}.` + (retried.length > 0 ? ` ${retried.length} of them passed only on a second try, after the browser did not answer.` : ""),
1140
1155
  "",
1141
1156
  "| Page | Width | Scheme | Result | Problems |",
1142
1157
  "| --- | ---: | --- | --- | --- |",
1143
- ...result.views.map((view) => `| \`${view.page}\` | ${view.width} | ${view.scheme} | ${view.ok ? "pass" : "**fail**"}${view.probe ? ` (probe ${view.probe.filter((c) => c.ok).length}/${view.probe.length})` : ""} | ${view.problems.map(cell).join("<br>")} |`),
1158
+ ...result.views.map((view) => `| \`${view.page}\` | ${view.width} | ${view.scheme} | ${view.ok ? "pass" : "**fail**"}${view.retried ? " (retried)" : ""}${view.probe ? ` (probe ${view.probe.filter((c) => c.ok).length}/${view.probe.length})` : ""} | ${view.problems.map(cell).join("<br>")} |`),
1144
1159
  ""
1145
1160
  ].join("\n");
1146
1161
  }
@@ -1514,12 +1529,13 @@ async function crawlSite(options) {
1514
1529
  const start = server?.url ?? options.url;
1515
1530
  if (start === void 0) throw new Error("give out or url");
1516
1531
  const home = new URL(start.endsWith("/") ? start : `${start}/`);
1517
- const browser = await launch(executable, options.timeoutMs);
1532
+ let browser;
1518
1533
  const pages = [];
1519
1534
  const clicks = [];
1520
1535
  let sitemap = [];
1521
1536
  let left = [];
1522
1537
  try {
1538
+ browser = await launch(executable, options.timeoutMs);
1523
1539
  const { page } = browser;
1524
1540
  await Promise.all([page.send("Page.enable"), page.send("Runtime.enable")]);
1525
1541
  if (options.previewBar !== void 0) await previewBar(page, home, options.previewBar);
@@ -1592,7 +1608,7 @@ async function crawlSite(options) {
1592
1608
  await shot(page, join(options.shots, `${pageSlug(path)}-${width}.png`), width);
1593
1609
  }
1594
1610
  } finally {
1595
- await browser.close();
1611
+ await browser?.close();
1596
1612
  await server?.close();
1597
1613
  }
1598
1614
  const unread = navUnread(left, pages, sitemap);