-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathrun.js
More file actions
134 lines (117 loc) · 3.07 KB
/
run.js
File metadata and controls
134 lines (117 loc) · 3.07 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
const fs = require("fs");
const path = require("path");
const { fdir } = require("fdir");
const { isPlainObject } = require("is-plain-object");
const scan = require("./scan");
const {
pluralize,
forEachComponent,
sortObjectKeysByValue,
getExcludeFn,
} = require("./utils");
const DEFAULT_GLOBS = ["**/!(*.test|*.spec).@(js|ts)?(x)"];
const DEFAULT_PROCESSORS = ["count-components-and-props"];
async function run({
config,
configDir,
crawlFrom,
startTime,
method = "cli",
silent,
}) {
const rootDir = config.rootDir || configDir;
const globs = config.globs || DEFAULT_GLOBS;
const files = new fdir()
.glob(...globs)
.exclude(getExcludeFn(config.exclude))
.withFullPaths()
.crawl(crawlFrom)
.sync();
if (files.length === 0) {
if (!silent) {
console.error(`No files found to scan.`);
}
process.exit(1);
}
let report = {};
const {
components,
includeSubComponents,
importedFrom,
getComponentName,
getPropValue,
} = config;
for (let i = 0, len = files.length; i < len; i++) {
const filePath = files[i];
const code = fs.readFileSync(filePath, "utf8");
scan({
code,
filePath,
components,
includeSubComponents,
importedFrom,
getComponentName,
report,
getPropValue,
});
}
const endTime = process.hrtime.bigint();
if (!silent) {
// eslint-disable-next-line no-console
console.log(
`Scanned ${pluralize(files.length, "file")} in ${
Number(endTime - startTime) / 1e9
} seconds`
);
}
const processors =
config.processors && config.processors.length > 0
? config.processors
: DEFAULT_PROCESSORS;
const prevResults = [];
const output = (data, destination) => {
const defaultDestination = method === "cli" ? "stdout" : "return";
const dest = destination || defaultDestination;
const dataStr = isPlainObject(data)
? JSON.stringify(data, null, 2)
: String(data);
switch (dest) {
case "stdout": {
if (!silent) {
// eslint-disable-next-line no-console
console.log(dataStr);
}
break;
}
case "return": {
break;
}
default: {
const filePath = path.resolve(rootDir, destination);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, dataStr);
}
}
};
for (const processor of processors) {
let processorFn;
if (typeof processor === "string") {
processorFn = require(`./processors/${processor}`)();
} else if (Array.isArray(processor)) {
processorFn = require(`./processors/${processor[0]}`)(processor[1]);
} else if (typeof processor === "function") {
processorFn = processor;
}
const result = await processorFn({
report,
prevResults,
prevResult: prevResults[prevResults.length - 1],
forEachComponent: forEachComponent(report),
sortObjectKeysByValue,
output,
});
prevResults.push(result);
}
return prevResults[prevResults.length - 1];
}
module.exports = run;