-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Expand file tree
/
Copy pathvalidate-redirects.ts
More file actions
111 lines (91 loc) · 2.67 KB
/
Copy pathvalidate-redirects.ts
File metadata and controls
111 lines (91 loc) · 2.67 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
import { readFile } from "fs/promises";
async function main() {
const redirects = await readFile("public/__redirects", { encoding: "utf-8" });
let numInfiniteRedirects = 0;
let numUrlsWithFragment = 0;
let numDuplicateRedirects = 0;
let numNonSlashedRedirects = 0;
let seenDynamicRedirects = false;
let numStaticRedirectsAfterDynamicRedirect = 0;
const validEndings = [
"/",
"*",
".xml",
".md",
".json",
".html",
".pdf",
".zip",
".yaml",
];
const redirectSourceUrls: string[] = [];
for (const line of redirects.split("\n")) {
if (line.startsWith("#") || line.trim() === "") continue;
const [from, to] = line.split(" ");
if (from.includes("*")) {
seenDynamicRedirects = true;
}
if (seenDynamicRedirects && !from.includes("*")) {
console.log(
`✘ Found static redirect after dynamic redirect:\n ${from}`,
);
numStaticRedirectsAfterDynamicRedirect++;
}
if (from === to) {
console.log(`✘ Found infinite redirect:\n ${from} -> ${to}`);
numInfiniteRedirects++;
}
if (from.includes("#")) {
console.log(`✘ Found source URL with fragment:\n ${from}`);
numUrlsWithFragment++;
}
if (
// CED-76 - flag unslashed redirects b/c these don't behave as expected due to our routing / preference for slashed endpoints
!validEndings.some((ending) => from.endsWith(ending)) &&
// CED-99 - known exception for /api where this isn't natively handled by our app
from != "/api"
) {
console.log(`✘ Found unslashed source URLs:\n ${from}`);
numNonSlashedRedirects++;
}
if (redirectSourceUrls.includes(from)) {
console.log(`✘ Found repeated source URL:\n ${from}`);
numDuplicateRedirects++;
} else {
redirectSourceUrls.push(from);
}
}
if (
numInfiniteRedirects ||
numUrlsWithFragment ||
numDuplicateRedirects ||
numNonSlashedRedirects ||
numStaticRedirectsAfterDynamicRedirect
) {
console.log("\nDetected errors:");
if (numInfiniteRedirects > 0) {
console.log(`- ${numInfiniteRedirects} infinite redirect(s)`);
}
if (numUrlsWithFragment > 0) {
console.log(`- ${numUrlsWithFragment} source URL(s) with a fragment`);
}
if (numDuplicateRedirects > 0) {
console.log(`- ${numDuplicateRedirects} repeated source URL(s)`);
}
if (numNonSlashedRedirects > 0) {
console.log(
`- ${numNonSlashedRedirects} need slashes at the end of the source URL`,
);
}
if (numStaticRedirectsAfterDynamicRedirect > 0) {
console.log(
`- ${numStaticRedirectsAfterDynamicRedirect} static redirect(s) after dynamic redirect(s)`,
);
}
console.log("\nPlease fix the errors above before merging :)");
process.exit(1);
} else {
console.log("\nDone!");
}
}
main();