@trackunit/repo-checks 0.0.21 → 0.0.22

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.
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadStorybookIndexes = exports.StorybookIndexError = exports.STORYBOOK_ENTRY_TYPES = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs = tslib_1.__importStar(require("fs"));
6
+ const zod_1 = require("zod");
7
+ const fs_errors_1 = require("../doc-references/fs-errors");
8
+ const settings_1 = require("../doc-references/settings");
9
+ exports.STORYBOOK_ENTRY_TYPES = ["docs", "story"];
10
+ /**
11
+ * The part of a `storybook index` output (`{ v, entries: { [id]: { id, type, ... } } }`) the check
12
+ * needs. Other keys are allowed so a newer Storybook that adds fields still loads.
13
+ */
14
+ const storybookIndexSchema = zod_1.z.object({
15
+ entries: zod_1.z.record(zod_1.z.string(), zod_1.z.object({ type: zod_1.z.enum(exports.STORYBOOK_ENTRY_TYPES) })),
16
+ });
17
+ /**
18
+ * A Storybook index that isn't valid JSON or isn't shaped like a `storybook index` output, or no
19
+ * index at all: the check can't tell a valid link from a broken one, so it must not run.
20
+ */
21
+ class StorybookIndexError extends Error {
22
+ /** @param message What is wrong, naming the index file. */
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = "StorybookIndexError";
26
+ }
27
+ }
28
+ exports.StorybookIndexError = StorybookIndexError;
29
+ const readIndexFile = (file) => {
30
+ try {
31
+ return fs.readFileSync(file, "utf8");
32
+ }
33
+ catch (error) {
34
+ throw new fs_errors_1.ReadError(`Cannot read Storybook index ${file}: ${(0, fs_errors_1.errorMessage)(error)}`);
35
+ }
36
+ };
37
+ const loadStorybookIndex = (file) => {
38
+ const source = readIndexFile(file);
39
+ let json;
40
+ try {
41
+ json = JSON.parse(source.replace(/^\uFEFF/u, ""));
42
+ }
43
+ catch (error) {
44
+ throw new StorybookIndexError(`Invalid JSON in ${file}: ${(0, fs_errors_1.errorMessage)(error)}`);
45
+ }
46
+ const parsed = storybookIndexSchema.safeParse(json);
47
+ if (!parsed.success) {
48
+ throw new StorybookIndexError(`Invalid Storybook index ${file}: ${(0, settings_1.formatConfigIssues)(parsed.error)}`);
49
+ }
50
+ return Object.entries(parsed.data.entries).map(([id, entry]) => [id, entry.type]);
51
+ };
52
+ /**
53
+ * Read `storybook index` output files and combine their entries into one map of entry ID to the
54
+ * types it has in any of them. Several files are combined because no single Storybook (public or
55
+ * internal) covers every ID, and an ID may be a docs entry in one and a story in another.
56
+ *
57
+ * @throws {ReadError} when a file can't be read
58
+ * @throws {StorybookIndexError} when a file isn't valid JSON or not an index, or `files` is empty
59
+ */
60
+ const loadStorybookIndexes = (files) => {
61
+ if (files.length === 0) {
62
+ throw new StorybookIndexError("No Storybook index given; every link would pass unchecked");
63
+ }
64
+ const entries = new Map();
65
+ for (const [id, type] of files.flatMap(loadStorybookIndex)) {
66
+ entries.set(id, (entries.get(id) ?? new Set()).add(type));
67
+ }
68
+ return entries;
69
+ };
70
+ exports.loadStorybookIndexes = loadStorybookIndexes;
71
+ //# sourceMappingURL=storybook-index.js.map
@@ -0,0 +1,16 @@
1
+ import { type StorybookEntryType } from "./storybook-index";
2
+ /** The Storybook entry a `?path=/<type>/<id>` link addresses. */
3
+ export interface StorybookPath {
4
+ readonly type: StorybookEntryType;
5
+ readonly id: string;
6
+ }
7
+ /** Hosts serving our own Storybooks, whose absolute `?path=` links can be checked. */
8
+ export declare const DEFAULT_STORYBOOK_HOSTS: ReadonlyArray<string>;
9
+ /**
10
+ * The Storybook entry a link target addresses, or `undefined` for any other target. Relative
11
+ * targets look like `?path=/docs/<id>` or `../?foo=1&path=/story/<id>`. Absolute targets look like
12
+ * `https://<host>[/<base>][/][index.html]?path=/docs/<id>`, where `<host>` is one of `hosts`
13
+ * (case-insensitive) on the scheme's default port; absolute URLs on other hosts, non-http(s) and
14
+ * protocol-relative URLs are not parsed. Targets with a `#fragment` are never parsed.
15
+ */
16
+ export declare const parseStorybookPath: (target: string, hosts: ReadonlyArray<string>) => StorybookPath | undefined;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseStorybookPath = exports.DEFAULT_STORYBOOK_HOSTS = void 0;
4
+ const storybook_index_1 = require("./storybook-index");
5
+ /** Hosts serving our own Storybooks, whose absolute `?path=` links can be checked. */
6
+ exports.DEFAULT_STORYBOOK_HOSTS = ["design.iris.trackunit.com"];
7
+ /** What may precede the `?` of a relative Storybook link: nothing, `/`, `./`, or one or more `../`. */
8
+ const RELATIVE_PREFIX_RE = /^(?:\/|\.\/|(?:\.\.\/)+)?$/u;
9
+ const URL_SCHEME_RE = /^[a-z][a-z0-9+.-]*:/iu;
10
+ const PATH_VALUE_RE = /^\/([a-z]+)\/([^/]+)$/u;
11
+ /** A last path segment naming a file, e.g. `iframe.html`, rather than a base directory. */
12
+ const FILE_SEGMENT_RE = /\/[^/]*\.[a-z][a-z0-9]*$/iu;
13
+ const parsePathQuery = (query) => {
14
+ const match = PATH_VALUE_RE.exec(new URLSearchParams(query).get("path") ?? "");
15
+ const type = storybook_index_1.STORYBOOK_ENTRY_TYPES.find(entryType => entryType === match?.[1]);
16
+ const id = match?.[2];
17
+ return type && id ? { type, id } : undefined;
18
+ };
19
+ const parseUrl = (target) => {
20
+ try {
21
+ return new URL(target);
22
+ }
23
+ catch {
24
+ return undefined;
25
+ }
26
+ };
27
+ const isStorybookManagerUrl = (url, hosts) => (url.protocol === "https:" || url.protocol === "http:") &&
28
+ url.port === "" &&
29
+ url.username === "" &&
30
+ url.password === "" &&
31
+ hosts.some(host => host.toLowerCase() === url.hostname) &&
32
+ (!FILE_SEGMENT_RE.test(url.pathname) || url.pathname.endsWith("/index.html"));
33
+ /**
34
+ * The Storybook entry a link target addresses, or `undefined` for any other target. Relative
35
+ * targets look like `?path=/docs/<id>` or `../?foo=1&path=/story/<id>`. Absolute targets look like
36
+ * `https://<host>[/<base>][/][index.html]?path=/docs/<id>`, where `<host>` is one of `hosts`
37
+ * (case-insensitive) on the scheme's default port; absolute URLs on other hosts, non-http(s) and
38
+ * protocol-relative URLs are not parsed. Targets with a `#fragment` are never parsed.
39
+ */
40
+ const parseStorybookPath = (target, hosts) => {
41
+ if (target.includes("#"))
42
+ return undefined;
43
+ if (URL_SCHEME_RE.test(target)) {
44
+ const url = parseUrl(target);
45
+ return url !== undefined && isStorybookManagerUrl(url, hosts) ? parsePathQuery(url.search) : undefined;
46
+ }
47
+ const queryStart = target.indexOf("?");
48
+ if (queryStart === -1 || !RELATIVE_PREFIX_RE.test(target.slice(0, queryStart)))
49
+ return undefined;
50
+ return parsePathQuery(target.slice(queryStart));
51
+ };
52
+ exports.parseStorybookPath = parseStorybookPath;
53
+ //# sourceMappingURL=storybook-path.js.map