Repository navigation
Expand file tree
/
Copy pathcheck-docs-site.ts
More file actions
138 lines (121 loc) · 3.63 KB
/
Copy pathcheck-docs-site.ts
File metadata and controls
138 lines (121 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
existsSync,
readFileSync,
readdirSync,
rmSync,
statSync,
} from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import { spawnSync } from "node:child_process";
import { catalogNames } from "./component-policy.ts";
const outputDirectory = join("/tmp", `angularcss-docs-check-${process.pid}`);
const basePath = "/angular.css/";
const listFiles = (directory: string): string[] =>
readdirSync(directory).flatMap((entry) => {
const path = join(directory, entry);
return statSync(path).isDirectory() ? listFiles(path) : path;
});
const build = spawnSync(
"hugo",
[
"--source",
"docs",
"--destination",
outputDirectory,
"--cleanDestinationDir",
],
{ encoding: "utf8" },
);
if (build.status !== 0) {
process.stderr.write(build.stdout ?? "");
process.stderr.write(build.stderr ?? "");
process.exit(build.status ?? 1);
}
const failures: string[] = [];
try {
const htmlFiles = listFiles(outputDirectory).filter((file) =>
file.endsWith(".html"),
);
for (const file of htmlFiles) {
const html = readFileSync(file, "utf8");
for (const match of html.matchAll(/(?:href|src)="([^"]+)"/g)) {
const reference = match[1];
if (
!reference ||
reference.startsWith("#") ||
/^(?:https?:|mailto:|tel:|data:|javascript:)/.test(reference)
) {
continue;
}
const path = reference.split(/[?#]/)[0];
let target: string | undefined;
if (path.startsWith(basePath)) {
target = join(outputDirectory, path.slice(basePath.length));
} else if (path === basePath.slice(0, -1)) {
target = outputDirectory;
} else if (!path.startsWith("/")) {
target = resolve(dirname(file), path);
}
if (
target &&
!existsSync(target) &&
!existsSync(join(target, "index.html"))
) {
failures.push(
`${relative(outputDirectory, file)}: missing local reference ${reference}`,
);
}
}
}
const componentIndex = readFileSync(
join(outputDirectory, "docs/components/index.html"),
"utf8",
);
if (!componentIndex.includes("component-catalog-group")) {
failures.push("component catalog did not render grouped component links");
}
const home = readFileSync(join(outputDirectory, "index.html"), "utf8");
const homeComponentLinks = [
...home.matchAll(/class="component-catalog-list"[\s\S]*?<\/ul>/g),
].flatMap((section) => [
...section[0].matchAll(
/href="[^"]*\/docs\/(?:foundations|elements|patterns|components|recipes)\/[^"/]+\/"/g,
),
]);
if (homeComponentLinks.length !== catalogNames.length) {
failures.push(
`homepage must link all ${catalogNames.length} catalog entries; found ${homeComponentLinks.length}`,
);
}
const accordionReference = readFileSync(
join(outputDirectory, "docs/patterns/accordion/index.html"),
"utf8",
);
for (const expected of [
"View source",
"Installation",
"Anatomy",
"API",
"Behavior",
"Accessibility",
"Customization",
]) {
if (!accordionReference.includes(expected)) {
failures.push(`component reference is missing ${expected}`);
}
}
const config = readFileSync("docs/hugo.yaml", "utf8");
if (/USERNAME|https:\/\/example\.com/.test(config)) {
failures.push("docs/hugo.yaml contains publication placeholders");
}
if (failures.length > 0) {
console.error(failures.join("\n"));
process.exitCode = 1;
} else {
console.log(
`Docs site check passed for ${htmlFiles.length} generated HTML files.`,
);
}
} finally {
rmSync(outputDirectory, { force: true, recursive: true });
}