@a.incantalupo/web-tools 0.1.0 → 0.3.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/dist/report.js ADDED
@@ -0,0 +1,148 @@
1
+ // One reporter for the page checks (text, images, glyphs). A check measures and records
2
+ // `fail(kind, note)` and `warn(kind, note)` per page; the reporter prints them in one of
3
+ // three forms. The measuring and the exit code are the same in all three.
4
+ //
5
+ // full every finding with its detail, as the check always printed it. The default.
6
+ // brief one line per page (ok | warn | FAIL, the page, its key numbers), every failure
7
+ // in full under it, warnings as a count with their kinds, the totals last.
8
+ // For agents and scripts that pay for every character they read.
9
+ // json the same counts and findings as JSON.
10
+ //
11
+ // Chosen by a flag (--full, --json, --brief) or by STACK_TOOLS_OUTPUT=brief|json in the
12
+ // environment; a flag wins over the variable, and --full wins over everything.
13
+ /** The three flags, for a check's `parseArgs` options. */
14
+ export const outputOptions = {
15
+ brief: { type: "boolean", default: false },
16
+ full: { type: "boolean", default: false },
17
+ json: { type: "boolean", default: false },
18
+ };
19
+ export const outputUsage = "[--brief | --full | --json]";
20
+ export function outputMode(flags) {
21
+ if (flags.full)
22
+ return "full";
23
+ if (flags.json)
24
+ return "json";
25
+ if (flags.brief)
26
+ return "brief";
27
+ const env = (process.env.STACK_TOOLS_OUTPUT ?? "").toLowerCase();
28
+ return env === "brief" || env === "json" ? env : "full";
29
+ }
30
+ export class PageReport {
31
+ result;
32
+ ended = false;
33
+ report;
34
+ constructor(report, url) {
35
+ this.report = report;
36
+ this.result = { url, status: "ok", keys: [], failures: [], warnings: [] };
37
+ }
38
+ /** A key number for this page's line in the brief form (`reading ease 68`). */
39
+ key(text) {
40
+ this.result.keys.push(text);
41
+ }
42
+ /** Records a failure; returns its line for the full form (`FAIL <note>`). */
43
+ fail(kind, note, where) {
44
+ this.result.failures.push({ kind, note, ...(where ? { where } : {}) });
45
+ this.result.status = "FAIL";
46
+ return `FAIL ${note}`;
47
+ }
48
+ /** Records a warning; returns its line for the full form (`warn <note>`). */
49
+ warn(kind, note, where) {
50
+ this.result.warnings.push({ kind, note, ...(where ? { where } : {}) });
51
+ if (this.result.status === "ok")
52
+ this.result.status = "warn";
53
+ return `warn ${note}`;
54
+ }
55
+ /** The page is measured: the brief form prints its line now. */
56
+ end() {
57
+ if (this.ended)
58
+ return;
59
+ this.ended = true;
60
+ if (this.report.mode !== "brief")
61
+ return;
62
+ const { status, keys, failures, warnings } = this.result;
63
+ const kinds = new Map();
64
+ for (const w of warnings)
65
+ kinds.set(w.kind, (kinds.get(w.kind) ?? 0) + 1);
66
+ const parts = [
67
+ ...keys,
68
+ ...(failures.length ? [`${failures.length} fail`] : []),
69
+ ...(warnings.length
70
+ ? [
71
+ `${warnings.length} warn (${[...kinds].map(([k, n]) => (n > 1 ? `${k} ${n}` : k)).join(", ")})`,
72
+ ]
73
+ : []),
74
+ ];
75
+ console.log(`${status.padEnd(5)} ${this.report.label(this.result.url)}${parts.length ? ` ${parts.join(" · ")}` : ""}`);
76
+ for (const f of failures)
77
+ console.log(` ${f.where ? `${f.where}: ` : ""}${f.note}`);
78
+ }
79
+ }
80
+ export class Report {
81
+ pages = [];
82
+ check;
83
+ mode;
84
+ oneSite;
85
+ constructor(check, urls, mode) {
86
+ this.check = check;
87
+ this.mode = mode;
88
+ const origins = new Set(urls.map((u) => {
89
+ try {
90
+ return new URL(u).origin;
91
+ }
92
+ catch {
93
+ return u;
94
+ }
95
+ }));
96
+ this.oneSite = origins.size <= 1;
97
+ }
98
+ /** A line of the full form: printed only there. */
99
+ say(line = "") {
100
+ if (this.mode === "full")
101
+ console.log(line);
102
+ }
103
+ /** How a page is named on its brief line: its path when every page is on one site. */
104
+ label(url) {
105
+ if (!this.oneSite)
106
+ return url;
107
+ try {
108
+ const { pathname, search } = new URL(url);
109
+ return pathname + search;
110
+ }
111
+ catch {
112
+ return url;
113
+ }
114
+ }
115
+ page(url) {
116
+ this.pages.at(-1)?.end();
117
+ const page = new PageReport(this, url);
118
+ this.pages.push(page);
119
+ return page;
120
+ }
121
+ get failures() {
122
+ return this.pages.reduce((a, p) => a + p.result.failures.length, 0);
123
+ }
124
+ get warnings() {
125
+ return this.pages.reduce((a, p) => a + p.result.warnings.length, 0);
126
+ }
127
+ /**
128
+ * The totals, in the chosen form, and the exit code: 1 when anything failed.
129
+ * `fullLine` replaces the full form's usual last line ("" for none).
130
+ */
131
+ done(fullLine) {
132
+ this.pages.at(-1)?.end();
133
+ const [f, w, n] = [this.failures, this.warnings, this.pages.length];
134
+ if (this.mode === "json")
135
+ console.log(JSON.stringify({
136
+ check: this.check,
137
+ failures: f,
138
+ warnings: w,
139
+ pages: this.pages.map((p) => p.result),
140
+ }, null, 2));
141
+ else if (this.mode === "brief")
142
+ console.log(`${n} page${n === 1 ? "" : "s"} · ${f} fail · ${w} warn${w ? " · --full lists the warnings" : ""}`);
143
+ else if (fullLine !== "")
144
+ console.log(fullLine ?? `\n${f} failure(s), ${w} warning(s) on ${n} page(s).`);
145
+ if (f)
146
+ process.exitCode = 1;
147
+ }
148
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@a.incantalupo/web-tools",
3
- "version": "0.1.0",
4
- "description": "Checks and captures for any website: readability and contrast, screenshots, style snapshots and diffs, glyph and image checks, a site audit with niche profiles, your own signed-in browser (web-stack-kb).",
3
+ "version": "0.3.0",
4
+ "description": "Checks and captures for any website: readability and contrast, screenshots, style snapshots and diffs, glyph and image checks, a site audit with niche profiles, your own signed-in browser.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "stack-tools": "dist/cli.js"
@@ -12,11 +12,6 @@
12
12
  },
13
13
  "files": ["dist", "README.md"],
14
14
  "license": "MIT",
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/Alessandro-Incantalupo/web-stack-kb.git",
18
- "directory": "tools/web-tools"
19
- },
20
15
  "scripts": {
21
16
  "build": "rm -rf dist && tsc -p tsconfig.json && chmod +x dist/cli.js",
22
17
  "check": "tsc -p tsconfig.json --noEmit",