forked from mdn/yari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-options.ts
118 lines (105 loc) · 3.42 KB
/
build-options.ts
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
import dotenv from "dotenv";
dotenv.config();
import { FLAW_LEVELS, VALID_FLAW_CHECKS } from "../libs/constants";
import {
DEFAULT_FLAW_LEVELS,
FILES,
FOLDERSEARCH,
NO_PROGRESSBAR,
FIX_FLAWS,
FIX_FLAWS_DRY_RUN,
FIX_FLAWS_TYPES,
FIX_FLAWS_VERBOSE,
} from "../libs/env";
const options = Object.freeze({
flawLevels: parseFlawLevels(DEFAULT_FLAW_LEVELS),
files: parseFiles(FILES),
folderSearch: parseFolderSearch(FOLDERSEARCH),
// Obviously false if the env var for NO_PROGRESSBAR was set but also,
// if you're going to display flaws, it's going to use stdout anyway.
noProgressbar: NO_PROGRESSBAR || FIX_FLAWS_DRY_RUN || FIX_FLAWS_VERBOSE,
fixFlaws: FIX_FLAWS,
fixFlawsDryRun: FIX_FLAWS_DRY_RUN,
fixFlawsTypes: FIX_FLAWS_TYPES,
fixFlawsVerbose: FIX_FLAWS_VERBOSE,
});
function parseFiles(filesStringList: string): Set<string> {
// The get-diff-action, which we use in the "PR Builds" CI,
// will make this a massive string that looks like
// this: `'content/files/en-us/a/index.html','content/files/en-us/a/index.html'`
// so we need to turn that into an array:
// ["content/files/en-us/a/index.html", "content/files/en-us/b/index.html"]`
// Note, when you use get-diff-action in GitHub Actions, it's a comma
// but if you use the manually `git diff --name-only ...` on your command
// line it's a newline.
return new Set(
filesStringList
.split(/[,\n]/)
.map((item) => {
// Remove any single or double-quote bookends.
return item.replace(/^(['"])(.*)\1$/, "$2");
})
.filter((s) => s)
);
}
function parseFolderSearch(searchpattern) {
if (searchpattern) {
// TODO: Consider turning it into a regex if there are * or $ or ^ in it
return searchpattern.toLowerCase();
}
return null;
}
// Override based on env vars but only for options that are *not*
// exclusive to building everything.
function parseFlawLevels(flawChecks) {
const checks = flawChecks
.split(",")
.map((x) => x.trim())
.filter((x) => x)
.map((x) => x.split(":").map((s) => s.trim()));
// Check that it doesn't contain more than 1 wildcard,
// because that'd make no sense.
const wildcards = checks.filter((tuple) => tuple[0] === "*");
if (wildcards.length > 1) {
throw new Error(`Can only be 1 wild card (not: ${wildcards})`);
}
// Put any wildcards (e.g. '*:warn') first
checks.sort((a, b) => {
if (a[0] === "*" && b[0] !== "*") {
return -1;
} else if (a[0] !== "*" && b[0] === "*") {
return 1;
}
return 0;
});
const checked = new Map();
// Unless specified, set 'ignore' on all of them first.
for (const check of VALID_FLAW_CHECKS) {
checked.set(check, FLAW_LEVELS.IGNORE);
}
const levelValues = Object.values(FLAW_LEVELS);
for (const tuple of checks) {
if (tuple.length !== 2) {
throw new Error(`Not a tuple pair of 2 (${tuple})`);
}
const [identifier, level] = tuple;
if (!levelValues.includes(level)) {
throw new Error(`Invalid level: '${level}' (only ${levelValues})`);
}
if (identifier === "*") {
for (const check of VALID_FLAW_CHECKS) {
checked.set(check, level);
}
} else if (!VALID_FLAW_CHECKS.has(identifier)) {
throw new Error(
`Unrecognized flaw identifier: '${identifier}' (only ${[
...VALID_FLAW_CHECKS,
]})`
);
} else {
checked.set(identifier, level);
}
}
return checked;
}
export default options;