This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
64 lines (53 loc) · 1.73 KB
/
main.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
import * as glob from "glob";
import * as path from "path";
import chalk from "chalk";
import analyse from "./analyse";
import report from "./report";
import writer from "./console";
import { Dir, Options, Report } from "./types";
const getSnapshots = (cwd: Dir, snapPattern?: string) =>
new Promise<string[]>((res, rej) => {
glob(
snapPattern || "**/*.js.snap",
{
cwd,
ignore: ["**/node_modules/**"]
},
(err, matches) => {
if (err) {
return rej(err);
}
if (matches.length === 0) {
console.log(chalk.yellow("No snaps were found"));
}
return res(matches.map(p => path.join(cwd, p)));
}
);
});
const reportHasError = (r: Report) =>
r.errors.length > 0 ||
r.lints.some(l => (l.error && l.error.length > 0) || l.errors.length > 0);
export default async (cwd: Dir, opts: Options) => {
const snapshots = await getSnapshots(cwd, opts.snapPattern);
const results = await Promise.all(snapshots.map(analyse));
const criteria = {
genericAttrs: opts.genericAttributes || [],
genericValues: opts.genericValues || ["[Function]"],
maxAttr: opts.maxAttributes || 5,
maxAttrArrayLength: opts.maxAttributeArrayLength || 5,
maxAttrStringLength: opts.maxAttributeStringLength || 32,
maxDepth: opts.maxDepth || 10,
maxFileSize: opts.maxFileSize || 10000,
maxGenericElement: opts.maxGenericElements || 10,
maxLines: opts.maxLines || 300
};
const reporter = report(criteria);
const output = results.map(r => reporter(r));
writer(criteria, output, {
errorsOnly: opts.usingCI,
isVerbose: opts.isVerbose
});
if (output.some(reportHasError)) {
throw new Error("Report has at least one error");
}
};