@mizchi/vlmkit-core 0.8.1 → 0.9.1
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 +1 -1
- package/dist/a11y-semantic.d.mts +1 -6
- package/dist/a11y-semantic.mjs +1 -6
- package/dist/arg-reader.d.mts +54 -0
- package/dist/arg-reader.mjs +136 -0
- package/dist/auth-state.d.mts +30 -0
- package/dist/auth-state.mjs +82 -0
- package/dist/cli-args.d.mts +24 -5
- package/dist/cli-args.mjs +49 -12
- package/dist/cli-error.d.mts +15 -1
- package/dist/cli-error.mjs +52 -4
- package/dist/dom-equivalence.d.mts +2 -7
- package/dist/dom-equivalence.mjs +1 -6
- package/dist/element-compare.mjs +8 -8
- package/dist/gate-config.d.mts +116 -0
- package/dist/gate-config.mjs +169 -0
- package/dist/gate-exit.d.mts +45 -0
- package/dist/gate-exit.mjs +25 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +3 -3
- package/dist/navigation-redirect.d.mts +28 -0
- package/dist/navigation-redirect.mjs +47 -0
- package/dist/page-open.d.mts +102 -0
- package/dist/page-open.mjs +133 -0
- package/dist/project-config.d.mts +12 -0
- package/dist/project-config.mjs +28 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
VRT diff engine and shared types. Pure-TypeScript image / DOM / a11y / visual
|
|
4
4
|
comparison primitives — no Playwright required for the lightweight surface.
|
|
5
5
|
|
|
6
|
-
Part of the [`
|
|
6
|
+
Part of the [`vlmkit`](https://github.com/mizchi/vlmkit) monorepo.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
package/dist/a11y-semantic.d.mts
CHANGED
|
@@ -13,13 +13,8 @@ declare function diffA11yTrees(baseline: A11ySnapshot, current: A11ySnapshot): A
|
|
|
13
13
|
* Walks the tree and returns issues such as missing labels on
|
|
14
14
|
* interactive elements and missing alt text on images.
|
|
15
15
|
*
|
|
16
|
-
* @since 0.5.0 — replaces `checkA11yTree`.
|
|
17
16
|
*/
|
|
18
17
|
declare function verifyA11yTree(tree: A11yNode): A11yIssue[];
|
|
19
|
-
/**
|
|
20
|
-
* @deprecated since 0.5.0 — use `verifyA11yTree` instead. Removed in 1.0.0.
|
|
21
|
-
*/
|
|
22
|
-
declare const checkA11yTree: typeof verifyA11yTree;
|
|
23
18
|
interface A11yIssue {
|
|
24
19
|
path: string;
|
|
25
20
|
rule: string;
|
|
@@ -31,4 +26,4 @@ interface A11yIssue {
|
|
|
31
26
|
*/
|
|
32
27
|
declare function parsePlaywrightA11ySnapshot(testId: string, testTitle: string, raw: Record<string, unknown>): A11ySnapshot;
|
|
33
28
|
//#endregion
|
|
34
|
-
export { A11yIssue, INTERACTIVE_ROLES, LANDMARK_ROLES,
|
|
29
|
+
export { A11yIssue, INTERACTIVE_ROLES, LANDMARK_ROLES, diffA11yTrees, parsePlaywrightA11ySnapshot, verifyA11yTree };
|
package/dist/a11y-semantic.mjs
CHANGED
|
@@ -205,17 +205,12 @@ function isInteractive(node) {
|
|
|
205
205
|
* Walks the tree and returns issues such as missing labels on
|
|
206
206
|
* interactive elements and missing alt text on images.
|
|
207
207
|
*
|
|
208
|
-
* @since 0.5.0 — replaces `checkA11yTree`.
|
|
209
208
|
*/
|
|
210
209
|
function verifyA11yTree(tree) {
|
|
211
210
|
const issues = [];
|
|
212
211
|
walkTree(tree, "", issues);
|
|
213
212
|
return issues;
|
|
214
213
|
}
|
|
215
|
-
/**
|
|
216
|
-
* @deprecated since 0.5.0 — use `verifyA11yTree` instead. Removed in 1.0.0.
|
|
217
|
-
*/
|
|
218
|
-
const checkA11yTree = verifyA11yTree;
|
|
219
214
|
function walkTree(node, path, issues) {
|
|
220
215
|
const nodePath = path ? `${path} > ${nodeLabel(node)}` : nodeLabel(node);
|
|
221
216
|
if (isInteractive(node) && !node.name) issues.push({
|
|
@@ -258,4 +253,4 @@ function normalizeA11yNode(raw) {
|
|
|
258
253
|
};
|
|
259
254
|
}
|
|
260
255
|
//#endregion
|
|
261
|
-
export { INTERACTIVE_ROLES, LANDMARK_ROLES,
|
|
256
|
+
export { INTERACTIVE_ROLES, LANDMARK_ROLES, diffA11yTrees, parsePlaywrightA11ySnapshot, verifyA11yTree };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//#region src/arg-reader.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Argv readers that take the array explicitly, and refuse to guess.
|
|
4
|
+
*
|
|
5
|
+
* `cli-args.ts` binds to `process.argv` at import time, which makes it
|
|
6
|
+
* untestable and unusable from a dispatcher-driven leaf. The hand-rolled
|
|
7
|
+
* readers that grew in its place all shared two silent failure modes:
|
|
8
|
+
*
|
|
9
|
+
* - `--output --json` consumed `--json` as the output directory;
|
|
10
|
+
* - `--concurrency abc` produced `NaN`, and `NaN` does not fail loudly. It
|
|
11
|
+
* made `runPool` build zero lanes and "successfully" run nothing.
|
|
12
|
+
*
|
|
13
|
+
* So a missing value is an error, a value that looks like another flag is an
|
|
14
|
+
* error, and a numeric flag is validated where it is read rather than wherever
|
|
15
|
+
* the NaN eventually lands.
|
|
16
|
+
*/
|
|
17
|
+
declare function hasFlag(argv: readonly string[], name: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Last occurrence wins, matching how shells and most CLIs behave when a flag is
|
|
20
|
+
* repeated. Throws when the value is missing or is another flag.
|
|
21
|
+
*/
|
|
22
|
+
declare function readFlag(argv: readonly string[], name: string): string | undefined;
|
|
23
|
+
/** Read a string flag whose public contract is a closed set of values. */
|
|
24
|
+
declare function readChoice<const T extends readonly string[]>(argv: readonly string[], name: string, choices: T): T[number] | undefined;
|
|
25
|
+
/** Every occurrence, for repeatable flags (`--gate`, `--only`, `--pages`). */
|
|
26
|
+
declare function readAll(argv: readonly string[], name: string): string[];
|
|
27
|
+
interface NumberFlagOptions {
|
|
28
|
+
min?: number;
|
|
29
|
+
max?: number;
|
|
30
|
+
integer?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function readNumber(argv: readonly string[], name: string, options?: NumberFlagOptions): number | undefined;
|
|
33
|
+
declare function readInt(argv: readonly string[], name: string, options?: NumberFlagOptions): number | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Arguments that are neither a flag nor a flag's value.
|
|
36
|
+
*
|
|
37
|
+
* The caller has to name the flags that take values, because argv alone cannot
|
|
38
|
+
* tell `--concurrency 4` (a value) from `--quiet page.html` (a positional). The
|
|
39
|
+
* hand-rolled version of this was a for-loop per CLI that advanced its own
|
|
40
|
+
* index, i.e. the same knowledge encoded three times.
|
|
41
|
+
*/
|
|
42
|
+
declare function readPositionals(argv: readonly string[], valueFlags?: readonly string[]): string[];
|
|
43
|
+
/**
|
|
44
|
+
* Split a command string into argv, honouring quotes.
|
|
45
|
+
*
|
|
46
|
+
* Needed because gate commands are authored as single strings — in a config
|
|
47
|
+
* file (`"check copy --manifest copy/press kit.txt"`) or on the command line
|
|
48
|
+
* (`--gate 'check breakpoints --mask ".hero, .promo"'`). Splitting on
|
|
49
|
+
* whitespace would hand the gate `kit.txt` and `.promo"` as separate
|
|
50
|
+
* arguments, which fails in a way that looks like the gate's fault.
|
|
51
|
+
*/
|
|
52
|
+
declare function tokenizeCommand(command: string): string[];
|
|
53
|
+
//#endregion
|
|
54
|
+
export { NumberFlagOptions, hasFlag, readAll, readChoice, readFlag, readInt, readNumber, readPositionals, tokenizeCommand };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { UsageError } from "./cli-error.mjs";
|
|
2
|
+
//#region src/arg-reader.ts
|
|
3
|
+
/**
|
|
4
|
+
* Argv readers that take the array explicitly, and refuse to guess.
|
|
5
|
+
*
|
|
6
|
+
* `cli-args.ts` binds to `process.argv` at import time, which makes it
|
|
7
|
+
* untestable and unusable from a dispatcher-driven leaf. The hand-rolled
|
|
8
|
+
* readers that grew in its place all shared two silent failure modes:
|
|
9
|
+
*
|
|
10
|
+
* - `--output --json` consumed `--json` as the output directory;
|
|
11
|
+
* - `--concurrency abc` produced `NaN`, and `NaN` does not fail loudly. It
|
|
12
|
+
* made `runPool` build zero lanes and "successfully" run nothing.
|
|
13
|
+
*
|
|
14
|
+
* So a missing value is an error, a value that looks like another flag is an
|
|
15
|
+
* error, and a numeric flag is validated where it is read rather than wherever
|
|
16
|
+
* the NaN eventually lands.
|
|
17
|
+
*/
|
|
18
|
+
const flagName = (name) => name.startsWith("-") ? name : `--${name}`;
|
|
19
|
+
/** A value must not itself look like a flag — `-1` is a value, `--json` is not. */
|
|
20
|
+
const looksLikeFlag = (value) => /^--/.test(value) || /^-[a-zA-Z]/.test(value);
|
|
21
|
+
function hasFlag(argv, name) {
|
|
22
|
+
return argv.includes(flagName(name));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Last occurrence wins, matching how shells and most CLIs behave when a flag is
|
|
26
|
+
* repeated. Throws when the value is missing or is another flag.
|
|
27
|
+
*/
|
|
28
|
+
function readFlag(argv, name) {
|
|
29
|
+
const flag = flagName(name);
|
|
30
|
+
const index = argv.lastIndexOf(flag);
|
|
31
|
+
if (index < 0) return void 0;
|
|
32
|
+
const value = argv[index + 1];
|
|
33
|
+
if (value === void 0) throw new UsageError(`${flag} needs a value`);
|
|
34
|
+
if (looksLikeFlag(value)) throw new UsageError(`${flag} needs a value, got the next flag ${value}`);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
/** Read a string flag whose public contract is a closed set of values. */
|
|
38
|
+
function readChoice(argv, name, choices) {
|
|
39
|
+
const value = readFlag(argv, name);
|
|
40
|
+
if (value === void 0) return void 0;
|
|
41
|
+
if (choices.includes(value)) return value;
|
|
42
|
+
throw new UsageError(`${flagName(name)} must be one of ${choices.join(", ")}, got ${JSON.stringify(value)}`);
|
|
43
|
+
}
|
|
44
|
+
/** Every occurrence, for repeatable flags (`--gate`, `--only`, `--pages`). */
|
|
45
|
+
function readAll(argv, name) {
|
|
46
|
+
const flag = flagName(name);
|
|
47
|
+
const out = [];
|
|
48
|
+
for (let i = 0; i < argv.length; i++) {
|
|
49
|
+
if (argv[i] !== flag) continue;
|
|
50
|
+
const value = argv[i + 1];
|
|
51
|
+
if (value === void 0) throw new UsageError(`${flag} needs a value`);
|
|
52
|
+
if (looksLikeFlag(value)) throw new UsageError(`${flag} needs a value, got the next flag ${value}`);
|
|
53
|
+
out.push(value);
|
|
54
|
+
i++;
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
function readNumber(argv, name, options = {}) {
|
|
59
|
+
const raw = readFlag(argv, name);
|
|
60
|
+
if (raw === void 0) return void 0;
|
|
61
|
+
const flag = flagName(name);
|
|
62
|
+
const value = Number.parseFloat(raw);
|
|
63
|
+
if (!Number.isFinite(value) || !/^-?\d+(\.\d+)?$/.test(raw.trim())) throw new UsageError(`${flag} must be a number, got "${raw}"`);
|
|
64
|
+
if (options.integer && !Number.isInteger(value)) throw new UsageError(`${flag} must be a whole number, got "${raw}"`);
|
|
65
|
+
if (options.min !== void 0 && value < options.min) throw new UsageError(`${flag} must be >= ${options.min}, got ${value}`);
|
|
66
|
+
if (options.max !== void 0 && value > options.max) throw new UsageError(`${flag} must be <= ${options.max}, got ${value}`);
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
function readInt(argv, name, options = {}) {
|
|
70
|
+
return readNumber(argv, name, {
|
|
71
|
+
...options,
|
|
72
|
+
integer: true
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Arguments that are neither a flag nor a flag's value.
|
|
77
|
+
*
|
|
78
|
+
* The caller has to name the flags that take values, because argv alone cannot
|
|
79
|
+
* tell `--concurrency 4` (a value) from `--quiet page.html` (a positional). The
|
|
80
|
+
* hand-rolled version of this was a for-loop per CLI that advanced its own
|
|
81
|
+
* index, i.e. the same knowledge encoded three times.
|
|
82
|
+
*/
|
|
83
|
+
function readPositionals(argv, valueFlags = []) {
|
|
84
|
+
const takesValue = new Set(valueFlags.map(flagName));
|
|
85
|
+
const out = [];
|
|
86
|
+
for (let i = 0; i < argv.length; i++) {
|
|
87
|
+
const arg = argv[i];
|
|
88
|
+
if (takesValue.has(arg)) {
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (arg.startsWith("-")) continue;
|
|
93
|
+
out.push(arg);
|
|
94
|
+
}
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Split a command string into argv, honouring quotes.
|
|
99
|
+
*
|
|
100
|
+
* Needed because gate commands are authored as single strings — in a config
|
|
101
|
+
* file (`"check copy --manifest copy/press kit.txt"`) or on the command line
|
|
102
|
+
* (`--gate 'check breakpoints --mask ".hero, .promo"'`). Splitting on
|
|
103
|
+
* whitespace would hand the gate `kit.txt` and `.promo"` as separate
|
|
104
|
+
* arguments, which fails in a way that looks like the gate's fault.
|
|
105
|
+
*/
|
|
106
|
+
function tokenizeCommand(command) {
|
|
107
|
+
const out = [];
|
|
108
|
+
let current = "";
|
|
109
|
+
let quote = null;
|
|
110
|
+
let started = false;
|
|
111
|
+
for (const char of command) {
|
|
112
|
+
if (quote) {
|
|
113
|
+
if (char === quote) quote = null;
|
|
114
|
+
else current += char;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (char === "\"" || char === "'") {
|
|
118
|
+
quote = char;
|
|
119
|
+
started = true;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (/\s/.test(char)) {
|
|
123
|
+
if (started) out.push(current);
|
|
124
|
+
current = "";
|
|
125
|
+
started = false;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
current += char;
|
|
129
|
+
started = true;
|
|
130
|
+
}
|
|
131
|
+
if (quote) throw new UsageError(`Unterminated ${quote} quote in command: ${command}`);
|
|
132
|
+
if (started) out.push(current);
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
//#endregion
|
|
136
|
+
export { hasFlag, readAll, readChoice, readFlag, readInt, readNumber, readPositionals, tokenizeCommand };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/auth-state.d.ts
|
|
2
|
+
declare const STORAGE_STATE_ENV = "VLMKIT_STORAGE_STATE";
|
|
3
|
+
/**
|
|
4
|
+
* Explicit flag beats env; returns undefined when neither is set. A blank
|
|
5
|
+
* explicit value counts as "not passed" and falls through to the
|
|
6
|
+
* environment, so `--storage-state "$MAYBE_UNSET"` in a script does not
|
|
7
|
+
* silently disable an env-configured session.
|
|
8
|
+
*/
|
|
9
|
+
declare function storageStatePath(explicit?: string): string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Validate eagerly and loudly. A silently ignored auth file would send the
|
|
12
|
+
* caller straight back to the failure this feature exists to fix — an
|
|
13
|
+
* unauthenticated measurement that looks like a real result.
|
|
14
|
+
*/
|
|
15
|
+
declare function readStorageState(path: string): {
|
|
16
|
+
cookies: unknown[];
|
|
17
|
+
origins: unknown[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Merge `storageState` into Playwright page options when configured.
|
|
21
|
+
* `browser.newPage(options)` forwards context options, so every gate can
|
|
22
|
+
* adopt this by wrapping its existing options object.
|
|
23
|
+
*/
|
|
24
|
+
declare function withAuthState<T extends object>(base: T, explicit?: string): T & {
|
|
25
|
+
storageState?: string;
|
|
26
|
+
};
|
|
27
|
+
/** One-line notice so a run that used auth says so in its output. */
|
|
28
|
+
declare function authStateNotice(explicit?: string): string | null;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { STORAGE_STATE_ENV, authStateNotice, readStorageState, storageStatePath, withAuthState };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
//#region src/auth-state.ts
|
|
4
|
+
/**
|
|
5
|
+
* Authenticated-page support for the gates.
|
|
6
|
+
*
|
|
7
|
+
* The 2026-08-01 hard-target audit made the auth gap loud (a redirect to a
|
|
8
|
+
* login wall is now a reported defect instead of a silent CLEAN), but loud
|
|
9
|
+
* failure is not access: for most product apps the interesting pages —
|
|
10
|
+
* dashboard, checkout, settings — are all behind a session. This closes
|
|
11
|
+
* that by accepting a Playwright storage-state file, which is the format
|
|
12
|
+
* `playwright codegen --save-storage` and `context.storageState()` already
|
|
13
|
+
* produce, so teams reuse the artifact their e2e suite has.
|
|
14
|
+
*
|
|
15
|
+
* Two ways in, because gate invocations live in CI yaml / npm scripts and
|
|
16
|
+
* threading a flag through every one of them is the exact config sprawl
|
|
17
|
+
* this project is trying not to create:
|
|
18
|
+
* - `--storage-state <path>` on any URL-capable gate
|
|
19
|
+
* - `VLMKIT_STORAGE_STATE=<path>` for all of them at once
|
|
20
|
+
*
|
|
21
|
+
* An explicit flag always wins over the environment.
|
|
22
|
+
*/
|
|
23
|
+
const STORAGE_STATE_ENV = "VLMKIT_STORAGE_STATE";
|
|
24
|
+
/**
|
|
25
|
+
* Explicit flag beats env; returns undefined when neither is set. A blank
|
|
26
|
+
* explicit value counts as "not passed" and falls through to the
|
|
27
|
+
* environment, so `--storage-state "$MAYBE_UNSET"` in a script does not
|
|
28
|
+
* silently disable an env-configured session.
|
|
29
|
+
*/
|
|
30
|
+
function storageStatePath(explicit) {
|
|
31
|
+
for (const candidate of [explicit, process.env[STORAGE_STATE_ENV]]) if (candidate && candidate.trim() !== "") return candidate;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Validate eagerly and loudly. A silently ignored auth file would send the
|
|
35
|
+
* caller straight back to the failure this feature exists to fix — an
|
|
36
|
+
* unauthenticated measurement that looks like a real result.
|
|
37
|
+
*/
|
|
38
|
+
function readStorageState(path) {
|
|
39
|
+
const abs = resolve(path);
|
|
40
|
+
let raw;
|
|
41
|
+
try {
|
|
42
|
+
raw = readFileSync(abs, "utf8");
|
|
43
|
+
} catch {
|
|
44
|
+
throw new Error(`storage state file not found: ${abs}\nCreate one with: npx playwright codegen --save-storage=auth.json <your-login-url>`);
|
|
45
|
+
}
|
|
46
|
+
let parsed;
|
|
47
|
+
try {
|
|
48
|
+
parsed = JSON.parse(raw);
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Error(`storage state file is not valid JSON: ${abs}`);
|
|
51
|
+
}
|
|
52
|
+
const obj = parsed;
|
|
53
|
+
if (!obj || typeof obj !== "object" || !Array.isArray(obj.cookies) && !Array.isArray(obj.origins)) throw new Error(`storage state file has no "cookies" or "origins" array: ${abs}\nExpected a Playwright storage-state file (context.storageState() output).`);
|
|
54
|
+
const cookies = Array.isArray(obj.cookies) ? obj.cookies : [];
|
|
55
|
+
const origins = Array.isArray(obj.origins) ? obj.origins : [];
|
|
56
|
+
if (cookies.length === 0 && origins.length === 0) throw new Error(`storage state file carries no cookies and no origins: ${abs}\nIt would authenticate nothing — re-capture it after logging in.`);
|
|
57
|
+
return {
|
|
58
|
+
cookies,
|
|
59
|
+
origins
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Merge `storageState` into Playwright page options when configured.
|
|
64
|
+
* `browser.newPage(options)` forwards context options, so every gate can
|
|
65
|
+
* adopt this by wrapping its existing options object.
|
|
66
|
+
*/
|
|
67
|
+
function withAuthState(base, explicit) {
|
|
68
|
+
const path = storageStatePath(explicit);
|
|
69
|
+
if (!path) return base;
|
|
70
|
+
readStorageState(path);
|
|
71
|
+
return {
|
|
72
|
+
...base,
|
|
73
|
+
storageState: resolve(path)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** One-line notice so a run that used auth says so in its output. */
|
|
77
|
+
function authStateNotice(explicit) {
|
|
78
|
+
const path = storageStatePath(explicit);
|
|
79
|
+
return path ? `auth: storage state from ${resolve(path)}` : null;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { STORAGE_STATE_ENV, authStateNotice, readStorageState, storageStatePath, withAuthState };
|
package/dist/cli-args.d.mts
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
1
|
//#region src/cli-args.d.ts
|
|
2
|
-
/** Shared CLI argument parsing helpers */
|
|
3
|
-
declare const args: string[];
|
|
4
2
|
declare function getArg(name: string, fallback: string): string;
|
|
5
3
|
declare function getArg(name: string): string | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Numeric flags, validated. Callers used to write `parseInt(getArg(n, d), 10)`,
|
|
6
|
+
* which is where a bad value became `NaN` — far from the flag that caused it.
|
|
7
|
+
*/
|
|
8
|
+
declare function getIntArg(name: string, fallback: number, options?: {
|
|
9
|
+
min?: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
}): number;
|
|
12
|
+
declare function getFloatArg(name: string, fallback: number, options?: {
|
|
13
|
+
min?: number;
|
|
14
|
+
max?: number;
|
|
15
|
+
}): number;
|
|
6
16
|
declare function hasFlag(name: string): boolean;
|
|
7
17
|
declare function getArgValues(name: string): string[];
|
|
8
|
-
/**
|
|
9
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Positional arguments.
|
|
20
|
+
*
|
|
21
|
+
* `valueFlags` names the flags that consume the next argument — required,
|
|
22
|
+
* because argv alone cannot tell `--limit 30` from `--md model-name`. The old
|
|
23
|
+
* implementation assumed *every* flag takes a value, which silently dropped a
|
|
24
|
+
* positional that happened to follow a boolean flag.
|
|
25
|
+
*/
|
|
26
|
+
declare function getPositionalArgs(valueFlags?: readonly string[]): string[];
|
|
27
|
+
/** Raw argv tail, for callers doing their own scanning. */
|
|
28
|
+
declare function getRawArgs(): string[];
|
|
10
29
|
//#endregion
|
|
11
|
-
export {
|
|
30
|
+
export { getArg, getArgValues, getFloatArg, getIntArg, getPositionalArgs, getRawArgs, hasFlag };
|
package/dist/cli-args.mjs
CHANGED
|
@@ -1,21 +1,58 @@
|
|
|
1
|
+
import { hasFlag as hasFlag$1, readAll, readFlag, readNumber, readPositionals } from "./arg-reader.mjs";
|
|
1
2
|
//#region src/cli-args.ts
|
|
2
|
-
/**
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Shared CLI argument helpers, kept as the ergonomic `process.argv`-bound
|
|
5
|
+
* facade over `arg-reader.ts`.
|
|
6
|
+
*
|
|
7
|
+
* This used to be a standalone implementation, and it silently produced wrong
|
|
8
|
+
* values. `getArg("seed", fallback)` returned whatever followed the flag, so
|
|
9
|
+
* `fix-loop --seed --mode selector` yielded `"--mode"`, and the caller's
|
|
10
|
+
* `parseInt` turned that into a `NaN` seed with no complaint. Every reader here
|
|
11
|
+
* now delegates to `arg-reader`, which treats a flag-shaped value and a
|
|
12
|
+
* non-numeric number as errors.
|
|
13
|
+
*
|
|
14
|
+
* `argv()` is read per call rather than captured at import time, so a leaf
|
|
15
|
+
* reached through the CLI dispatcher — which rewrites `process.argv` before
|
|
16
|
+
* loading the leaf — sees the arguments it was actually given.
|
|
17
|
+
*/
|
|
18
|
+
const argv = () => process.argv.slice(2);
|
|
4
19
|
function getArg(name, fallback) {
|
|
5
|
-
const
|
|
6
|
-
return
|
|
20
|
+
const value = readFlag(argv(), name);
|
|
21
|
+
return value === void 0 || value === "" ? fallback : value;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Numeric flags, validated. Callers used to write `parseInt(getArg(n, d), 10)`,
|
|
25
|
+
* which is where a bad value became `NaN` — far from the flag that caused it.
|
|
26
|
+
*/
|
|
27
|
+
function getIntArg(name, fallback, options = {}) {
|
|
28
|
+
return readNumber(argv(), name, {
|
|
29
|
+
...options,
|
|
30
|
+
integer: true
|
|
31
|
+
}) ?? fallback;
|
|
32
|
+
}
|
|
33
|
+
function getFloatArg(name, fallback, options = {}) {
|
|
34
|
+
return readNumber(argv(), name, options) ?? fallback;
|
|
7
35
|
}
|
|
8
36
|
function hasFlag(name) {
|
|
9
|
-
return
|
|
37
|
+
return hasFlag$1(argv(), name);
|
|
10
38
|
}
|
|
11
39
|
function getArgValues(name) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
40
|
+
return readAll(argv(), name);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Positional arguments.
|
|
44
|
+
*
|
|
45
|
+
* `valueFlags` names the flags that consume the next argument — required,
|
|
46
|
+
* because argv alone cannot tell `--limit 30` from `--md model-name`. The old
|
|
47
|
+
* implementation assumed *every* flag takes a value, which silently dropped a
|
|
48
|
+
* positional that happened to follow a boolean flag.
|
|
49
|
+
*/
|
|
50
|
+
function getPositionalArgs(valueFlags = []) {
|
|
51
|
+
return readPositionals(argv(), valueFlags);
|
|
15
52
|
}
|
|
16
|
-
/**
|
|
17
|
-
function
|
|
18
|
-
return
|
|
53
|
+
/** Raw argv tail, for callers doing their own scanning. */
|
|
54
|
+
function getRawArgs() {
|
|
55
|
+
return argv();
|
|
19
56
|
}
|
|
20
57
|
//#endregion
|
|
21
|
-
export {
|
|
58
|
+
export { getArg, getArgValues, getFloatArg, getIntArg, getPositionalArgs, getRawArgs, hasFlag };
|
package/dist/cli-error.d.mts
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
//#region src/cli-error.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A bad flag or missing argument — the caller's typo, not a defect. Thrown by
|
|
4
|
+
* `arg-reader` and printed as one line, because a stack trace for
|
|
5
|
+
* `--concurrency abc` buries the one sentence that fixes it.
|
|
6
|
+
*/
|
|
7
|
+
declare class UsageError extends Error {
|
|
8
|
+
readonly name = "UsageError";
|
|
9
|
+
}
|
|
10
|
+
interface PlaywrightInstallTarget {
|
|
11
|
+
version: string;
|
|
12
|
+
cliPath: string;
|
|
13
|
+
nodePath: string;
|
|
14
|
+
}
|
|
15
|
+
declare function formatMissingPlaywrightBrowserError(error: unknown, target?: PlaywrightInstallTarget | null): string | null;
|
|
2
16
|
declare function handleCliError(e: unknown): never;
|
|
3
17
|
//#endregion
|
|
4
|
-
export { handleCliError };
|
|
18
|
+
export { PlaywrightInstallTarget, UsageError, formatMissingPlaywrightBrowserError, handleCliError };
|
package/dist/cli-error.mjs
CHANGED
|
@@ -1,17 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
2
4
|
//#region src/cli-error.ts
|
|
3
5
|
/**
|
|
4
6
|
* Shared CLI error-prettifier. Recognizes common error shapes that
|
|
5
|
-
* make `
|
|
7
|
+
* make `vlmkit` look user-hostile (ENOENT stack traces, Playwright
|
|
6
8
|
* navigation errors with absolute source paths) and rewrites them
|
|
7
9
|
* to a one-line message.
|
|
8
10
|
*
|
|
9
11
|
* Use:
|
|
10
12
|
* main().catch(handleCliError);
|
|
11
13
|
*/
|
|
14
|
+
/**
|
|
15
|
+
* A bad flag or missing argument — the caller's typo, not a defect. Thrown by
|
|
16
|
+
* `arg-reader` and printed as one line, because a stack trace for
|
|
17
|
+
* `--concurrency abc` buries the one sentence that fixes it.
|
|
18
|
+
*/
|
|
19
|
+
var UsageError = class extends Error {
|
|
20
|
+
name = "UsageError";
|
|
21
|
+
};
|
|
22
|
+
function shellArg(value) {
|
|
23
|
+
return /^[A-Za-z0-9_./:@+-]+$/.test(value) ? value : JSON.stringify(value);
|
|
24
|
+
}
|
|
25
|
+
function resolvePlaywrightInstallTarget() {
|
|
26
|
+
try {
|
|
27
|
+
const manifestPath = createRequire(import.meta.url).resolve("playwright/package.json");
|
|
28
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
29
|
+
if (!manifest.version) return null;
|
|
30
|
+
return {
|
|
31
|
+
version: manifest.version,
|
|
32
|
+
cliPath: join(dirname(manifestPath), "cli.js"),
|
|
33
|
+
nodePath: process.execPath
|
|
34
|
+
};
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function formatMissingPlaywrightBrowserError(error, target = resolvePlaywrightInstallTarget()) {
|
|
40
|
+
const message = String(error?.message ?? error);
|
|
41
|
+
if (!/browserType\.launch:[\s\S]*Executable doesn't exist at/i.test(message)) return null;
|
|
42
|
+
if (!target) return "error: Playwright browser executable is not installed; reinstall the browser for vlmkit's resolved Playwright.";
|
|
43
|
+
const command = [
|
|
44
|
+
target.nodePath,
|
|
45
|
+
target.cliPath,
|
|
46
|
+
"install",
|
|
47
|
+
"chromium"
|
|
48
|
+
].map(shellArg).join(" ");
|
|
49
|
+
return `error: Playwright ${target.version} browser executable is not installed.\n run: ${command}`;
|
|
50
|
+
}
|
|
12
51
|
function handleCliError(e) {
|
|
13
52
|
const err = e;
|
|
14
53
|
const msg = String(err?.message ?? e);
|
|
54
|
+
if (err?.name === "UsageError") {
|
|
55
|
+
process.stderr.write(`error: ${msg}\n`);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
15
58
|
if (err?.code === "ENOENT") {
|
|
16
59
|
const path = err.path ?? msg.match(/ENOENT: no such file or directory[^']*'([^']+)'/)?.[1] ?? "?";
|
|
17
60
|
process.stderr.write(`error: file not found: ${path}\n`);
|
|
@@ -24,6 +67,11 @@ function handleCliError(e) {
|
|
|
24
67
|
process.stderr.write(`error: expected an HTML file, got a directory: ${path}\n${hint}\n`);
|
|
25
68
|
process.exit(1);
|
|
26
69
|
}
|
|
70
|
+
const missingBrowser = formatMissingPlaywrightBrowserError(e);
|
|
71
|
+
if (missingBrowser) {
|
|
72
|
+
process.stderr.write(`${missingBrowser}\n`);
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
27
75
|
if (/net::ERR_NAME_NOT_RESOLVED/i.test(msg) || /net::ERR_CONNECTION_REFUSED/i.test(msg) || /Cannot navigate to invalid URL/i.test(msg)) {
|
|
28
76
|
const url = msg.match(/Navigating to ([^,]+)/i)?.[1] ?? msg.match(/(https?:\/\/[^\s"]+)/)?.[1] ?? "the URL";
|
|
29
77
|
let reason = "failed to load";
|
|
@@ -44,7 +92,7 @@ function handleCliError(e) {
|
|
|
44
92
|
* EISDIR fallback: Node's `fsPromises.readFile` doesn't attach `.path`
|
|
45
93
|
* to the error, and the message string doesn't include the path
|
|
46
94
|
* either. Scan argv for the first positional that exists as a
|
|
47
|
-
* directory — close enough since most
|
|
95
|
+
* directory — close enough since most vlmkit subcommands take exactly
|
|
48
96
|
* one path argument and the failure happens during the initial read.
|
|
49
97
|
*/
|
|
50
98
|
function findDirectoryArg() {
|
|
@@ -56,4 +104,4 @@ function findDirectoryArg() {
|
|
|
56
104
|
}
|
|
57
105
|
}
|
|
58
106
|
//#endregion
|
|
59
|
-
export { handleCliError };
|
|
107
|
+
export { UsageError, formatMissingPlaywrightBrowserError, handleCliError };
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
* Both pure helpers (sequence diff) and a browser-side capture script live
|
|
16
16
|
* here; migration-compare evaluates the script with `page.evaluate()`
|
|
17
|
-
* and feeds the results into `
|
|
17
|
+
* and feeds the results into `verifyDomEquivalence`.
|
|
18
18
|
*/
|
|
19
19
|
interface DomFingerprint {
|
|
20
20
|
headingTexts: string[];
|
|
@@ -35,7 +35,6 @@ interface DomEquivalenceResult {
|
|
|
35
35
|
/**
|
|
36
36
|
* Verify structural DOM equivalence between two captured fingerprints.
|
|
37
37
|
*
|
|
38
|
-
* @since 0.5.0 — replaces `evaluateDomEquivalence`.
|
|
39
38
|
*/
|
|
40
39
|
declare function verifyDomEquivalence(baseline: DomFingerprint, variant: DomFingerprint): DomEquivalenceResult;
|
|
41
40
|
/**
|
|
@@ -43,9 +42,5 @@ declare function verifyDomEquivalence(baseline: DomFingerprint, variant: DomFing
|
|
|
43
42
|
* `page.evaluate()`; produces a {@link DomFingerprint}.
|
|
44
43
|
*/
|
|
45
44
|
declare const DOM_FINGERPRINT_BROWSER_SCRIPT = "(() => {\n const norm = (s) => (s || \"\").replace(/\\s+/g, \" \").trim();\n const headingTexts = Array.from(document.querySelectorAll(\"h1, h2, h3, h4, h5, h6\")).map((el) => norm(el.textContent));\n const buttonTexts = Array.from(document.querySelectorAll(\"button\")).map((el) => norm(el.textContent));\n const inputValues = Array.from(document.querySelectorAll(\"input, textarea, select\")).map((el) => {\n const v = el.value != null ? el.value : el.getAttribute(\"value\") || \"\";\n return norm(v);\n });\n const elementCount = document.querySelectorAll(\"*\").length;\n return { headingTexts, buttonTexts, inputValues, elementCount };\n})()";
|
|
46
|
-
/**
|
|
47
|
-
* @deprecated since 0.5.0 — use `verifyDomEquivalence` instead. Removed in 1.0.0.
|
|
48
|
-
*/
|
|
49
|
-
declare const evaluateDomEquivalence: typeof verifyDomEquivalence;
|
|
50
45
|
//#endregion
|
|
51
|
-
export { DOM_FINGERPRINT_BROWSER_SCRIPT, DomEquivalenceResult, DomEquivalenceWarning, DomFingerprint,
|
|
46
|
+
export { DOM_FINGERPRINT_BROWSER_SCRIPT, DomEquivalenceResult, DomEquivalenceWarning, DomFingerprint, verifyDomEquivalence };
|
package/dist/dom-equivalence.mjs
CHANGED
|
@@ -31,7 +31,6 @@ const ELEMENT_COUNT_DRIFT_THRESHOLD = .05;
|
|
|
31
31
|
/**
|
|
32
32
|
* Verify structural DOM equivalence between two captured fingerprints.
|
|
33
33
|
*
|
|
34
|
-
* @since 0.5.0 — replaces `evaluateDomEquivalence`.
|
|
35
34
|
*/
|
|
36
35
|
function verifyDomEquivalence(baseline, variant) {
|
|
37
36
|
const warnings = [];
|
|
@@ -83,9 +82,5 @@ const DOM_FINGERPRINT_BROWSER_SCRIPT = `(() => {
|
|
|
83
82
|
const elementCount = document.querySelectorAll("*").length;
|
|
84
83
|
return { headingTexts, buttonTexts, inputValues, elementCount };
|
|
85
84
|
})()`;
|
|
86
|
-
/**
|
|
87
|
-
* @deprecated since 0.5.0 — use `verifyDomEquivalence` instead. Removed in 1.0.0.
|
|
88
|
-
*/
|
|
89
|
-
const evaluateDomEquivalence = verifyDomEquivalence;
|
|
90
85
|
//#endregion
|
|
91
|
-
export { DOM_FINGERPRINT_BROWSER_SCRIPT,
|
|
86
|
+
export { DOM_FINGERPRINT_BROWSER_SCRIPT, verifyDomEquivalence };
|