Skip to content

Commit 6482651

Browse files
committed
Add TypeScript support and sortObjectKeysByValue to processReport
1 parent 6810246 commit 6482651

File tree

8 files changed

+267
-75
lines changed

8 files changed

+267
-75
lines changed

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ module.exports = {
144144
return ["utils", "tests"].includes(dir);
145145
},
146146

147-
// [optional] defaults to: ["**/*.js"]
147+
// [optional] defaults to: ["**/!(*.test|*.spec).@(js|ts)?(x)"]
148148
// Only files matching these globs will be scanned (see here for glob syntax: https://github.com/micromatch/picomatch#globbing-features).
149-
globs: ["**/*.js", "**/*.jsx"],
149+
globs: ["**/*.js"],
150150

151151
// [optional]
152152
// Components to report on (omit to report on all components).
@@ -171,10 +171,11 @@ module.exports = {
171171
// The processReport function gets an object with the following in it:
172172
// * report - the full JSON report
173173
// * forEachComponent - recursively visits every component in the report
174+
// * sortObjectKeysByValue - sorts object keys by some function of the value (this function is identity by default)
174175
// * writeFile - use it to store the result object in a file
175176
// When processReport is not specified, the report is logged out.
176-
processReport: ({ forEachComponent, writeFile }) => {
177-
const output = {};
177+
processReport: ({ forEachComponent, sortObjectKeysByValue, writeFile }) => {
178+
let output = {};
178179

179180
// count instances
180181
forEachComponent(({ componentName, component }) => {
@@ -185,18 +186,11 @@ module.exports = {
185186
}
186187
});
187188

188-
// sort by instances count
189-
const entries = Object.entries(output);
190-
191-
entries.sort(([name1, count1], [name2, count2]) =>
192-
count1 > count2 || (count1 === count2 && name1 <= name2) ? -1 : 1
193-
);
194-
195-
const result = Object.fromEntries(entries);
189+
output = sortObjectKeysByValue(output);
196190

197191
writeFile(
198192
"./reports/oscar.json", // absolute or relative to the config file location
199-
result // must be an object, will be JSON.stringified
193+
output // must be an object, will be JSON.stringified
200194
);
201195
},
202196
};

package-lock.json

Lines changed: 68 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
}
1717
},
1818
"dependencies": {
19+
"@typescript-eslint/typescript-estree": "3.7.0",
1920
"astray": "1.0.0",
2021
"dlv": "1.1.3",
2122
"dset": "2.0.1",
2223
"fdir": "3.4.3",
23-
"meriyah": "2.1.1",
2424
"picomatch": "2.2.2",
25-
"sade": "1.7.3"
25+
"sade": "1.7.3",
26+
"typescript": "3.9.7"
2627
},
2728
"devDependencies": {
2829
"c8": "7.2.1",

src/run.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ const fs = require("fs");
22
const path = require("path");
33
const fdir = require("fdir");
44
const scan = require("./scan");
5-
const { pluralize, forEachComponent } = require("./utils");
5+
const {
6+
pluralize,
7+
forEachComponent,
8+
sortObjectKeysByValue,
9+
} = require("./utils");
10+
11+
const DEFAULT_GLOBS = ["**/!(*.test|*.spec).@(js|ts)?(x)"];
612

713
function run({ config, configDir, crawlFrom, startTime }) {
8-
const globs = config.globs || ["**/*.js"];
14+
const globs = config.globs || DEFAULT_GLOBS;
915
const files = new fdir()
1016
.glob(...globs)
1117
.exclude(config.exclude)
@@ -29,19 +35,11 @@ function run({ config, configDir, crawlFrom, startTime }) {
2935
});
3036
}
3137

32-
const endTime = process.hrtime.bigint();
33-
34-
// eslint-disable-next-line no-console
35-
console.log(
36-
`Scanned ${pluralize(files.length, "file")} in ${
37-
Number(endTime - startTime) / 1e9
38-
} seconds`
39-
);
40-
4138
if (typeof config.processReport === "function") {
4239
config.processReport({
4340
report,
4441
forEachComponent: forEachComponent(report),
42+
sortObjectKeysByValue,
4543
writeFile: (outputPath, object) => {
4644
const data = JSON.stringify(object, null, 2);
4745
const filePath = path.resolve(configDir, outputPath);
@@ -57,6 +55,15 @@ function run({ config, configDir, crawlFrom, startTime }) {
5755
// eslint-disable-next-line no-console
5856
console.log(JSON.stringify(report, null, 2));
5957
}
58+
59+
const endTime = process.hrtime.bigint();
60+
61+
// eslint-disable-next-line no-console
62+
console.log(
63+
`Scanned ${pluralize(files.length, "file")} in ${
64+
Number(endTime - startTime) / 1e9
65+
} seconds`
66+
);
6067
}
6168

6269
module.exports = run;

0 commit comments

Comments
 (0)