Skip to content

Commit

Permalink
Add TypeScript support and sortObjectKeysByValue to processReport
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed Jul 25, 2020
1 parent 6810246 commit 6482651
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 75 deletions.
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ module.exports = {
return ["utils", "tests"].includes(dir);
},

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

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

// count instances
forEachComponent(({ componentName, component }) => {
Expand All @@ -185,18 +186,11 @@ module.exports = {
}
});

// sort by instances count
const entries = Object.entries(output);

entries.sort(([name1, count1], [name2, count2]) =>
count1 > count2 || (count1 === count2 && name1 <= name2) ? -1 : 1
);

const result = Object.fromEntries(entries);
output = sortObjectKeysByValue(output);

writeFile(
"./reports/oscar.json", // absolute or relative to the config file location
result // must be an object, will be JSON.stringified
output // must be an object, will be JSON.stringified
);
},
};
Expand Down
113 changes: 68 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
}
},
"dependencies": {
"@typescript-eslint/typescript-estree": "3.7.0",
"astray": "1.0.0",
"dlv": "1.1.3",
"dset": "2.0.1",
"fdir": "3.4.3",
"meriyah": "2.1.1",
"picomatch": "2.2.2",
"sade": "1.7.3"
"sade": "1.7.3",
"typescript": "3.9.7"
},
"devDependencies": {
"c8": "7.2.1",
Expand Down
29 changes: 18 additions & 11 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ const fs = require("fs");
const path = require("path");
const fdir = require("fdir");
const scan = require("./scan");
const { pluralize, forEachComponent } = require("./utils");
const {
pluralize,
forEachComponent,
sortObjectKeysByValue,
} = require("./utils");

const DEFAULT_GLOBS = ["**/!(*.test|*.spec).@(js|ts)?(x)"];

function run({ config, configDir, crawlFrom, startTime }) {
const globs = config.globs || ["**/*.js"];
const globs = config.globs || DEFAULT_GLOBS;
const files = new fdir()
.glob(...globs)
.exclude(config.exclude)
Expand All @@ -29,19 +35,11 @@ function run({ config, configDir, crawlFrom, startTime }) {
});
}

const endTime = process.hrtime.bigint();

// eslint-disable-next-line no-console
console.log(
`Scanned ${pluralize(files.length, "file")} in ${
Number(endTime - startTime) / 1e9
} seconds`
);

if (typeof config.processReport === "function") {
config.processReport({
report,
forEachComponent: forEachComponent(report),
sortObjectKeysByValue,
writeFile: (outputPath, object) => {
const data = JSON.stringify(object, null, 2);
const filePath = path.resolve(configDir, outputPath);
Expand All @@ -57,6 +55,15 @@ function run({ config, configDir, crawlFrom, startTime }) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(report, null, 2));
}

const endTime = process.hrtime.bigint();

// eslint-disable-next-line no-console
console.log(
`Scanned ${pluralize(files.length, "file")} in ${
Number(endTime - startTime) / 1e9
} seconds`
);
}

module.exports = run;
Loading

0 comments on commit 6482651

Please sign in to comment.