-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
executable file
·153 lines (128 loc) · 3.75 KB
/
core.js
File metadata and controls
executable file
·153 lines (128 loc) · 3.75 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { readFileSync } from "fs";
import { resolve } from "path";
import { globSync } from "glob";
import { parse } from "yaml";
import chalk from "chalk";
// if running in github actions debug mode, do extra logging
export const verbose = !!process.env.RUNNER_DEBUG;
// get full list of redirects
export function getList() {
// get yaml files that match glob pattern
const files = globSync("*.y?(a)ml", { cwd: __dirname });
info(`Found ${files.length} list file(s)`);
// list of redirects
const list = [];
// keep track of duplicate entries
const duplicates = {};
// go through each yaml file
for (const file of files) {
// load file contents
const contents = readFileSync(resolve(__dirname, file), "utf8");
// try to parse as yaml
let data;
try {
data = parse(contents);
} catch (e) {
addError(`Couldn't parse ${file}. Make sure it is valid YAML.`);
continue;
}
// check if top level is list
if (!Array.isArray(data)) {
addError(`${file} is not a list`);
continue;
}
// go through each entry
for (let index = 0; index < data.length; index++) {
// get entry
let entry = data[index];
// check if dict
if (typeof entry !== "object" || Array.isArray(entry) || entry === null) {
addError(["Entry is not a dict", trace({ file, index, entry })]);
continue;
}
// add metadata
entry.file = file;
entry.index = index;
const { from, to } = entry;
// check "from" field
if (!(typeof from === "string" && from.trim())) {
addError([
`"from" field ${from === undefined ? "missing" : "invalid"}`,
trace(entry),
]);
continue;
}
// normalize "from" field. lower case, remove leading slashes.
entry.from = from.toLowerCase().replace(/^(\/+)/, "");
// add to duplicate list
duplicates[from] ??= [];
duplicates[from].push(entry);
// check "to" field
if (!(typeof to === "string" && to.trim())) {
addError([
`"to" field ${to === undefined ? "missing" : "invalid"}`,
trace(entry),
]);
continue;
}
// add to list
list.push(entry);
}
}
// check that any redirects exist
(list.length ? info : addError)(`Found ${list.length} total entr(ies)`);
if (verbose) {
info("Combined list");
debug(JSON.stringify(list));
}
// go through duplicates
for (const [, entries] of Object.entries(duplicates)) {
if (entries.length <= 1) continue;
addError([`"from" appears ${entries.length} times`, ...entries.map(trace)]);
}
return list;
}
// when script finished, report all errors together
export function onExit() {
process.on("exit", () => {
if (errors.length) {
critical(`${errors.length} error(s) occurred:`);
for (const [firstLine, ...lines] of errors) {
error(firstLine);
for (const line of lines) debug(" " + line);
}
process.exit(1);
} else {
success("Done");
process.exitCode = 0;
}
});
}
// track errors
let errors = [];
export function addError(value) {
errors.push([value].flat());
}
// colored logs
export function info(message) {
console.log(chalk.cyan(message));
}
export function debug(message) {
console.log(chalk.gray(message));
}
export function warning(message) {
console.log(chalk.yellow(message));
}
export function error(message) {
console.log(chalk.red(message));
}
export function success(message) {
console.log(chalk.bgGreen(message));
}
export function critical(message) {
console.log(chalk.bgRed(message));
}
// log info for identifying bad entries
export function trace({ file, index, ...entry }) {
return `${file} entry #${index + 1} ${JSON.stringify(entry)}`;
}