Skip to content

Commit e8c9f4f

Browse files
author
Simon Renoult
committed
feat: add 'excludes' flag
1 parent ec4fe81 commit e8c9f4f

File tree

4 files changed

+84
-28
lines changed

4 files changed

+84
-28
lines changed

src/cli.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const examples = [
99
"$ code-complexity <dir> --details",
1010
"$ code-complexity <dir> --min 10 --max 50",
1111
"$ code-complexity <dir> --sort complexity",
12-
"$ code-complexity <dir> --details --limit 10 --sort complexity"
12+
"$ code-complexity <dir> --excludes lib,test",
13+
"$ code-complexity <dir> --details --limit 10 --sort complexity --excludes test"
1314
];
1415

1516
export default commander
@@ -35,6 +36,11 @@ export default commander
3536
)
3637
.option("--min [min]", "Exclude results below <min>", parseInt)
3738
.option("--max [max]", "Exclude results above <max>", parseInt)
39+
.option(
40+
"--excludes <strings>",
41+
"List of strings (comma separated) used in filenames to exclude",
42+
commaSeparatedList
43+
)
3844
.on("--help", () => {
3945
console.log();
4046
console.log("Examples:");
@@ -49,3 +55,7 @@ function getPackageJson(): { description: string } {
4955
const rawPkg = readFileSync(path, "utf8");
5056
return JSON.parse(rawPkg);
5157
}
58+
59+
function commaSeparatedList(value: string): Array<string> {
60+
return value.split(",");
61+
}

src/services/compute-complexity.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export default async function computeComplexity(): Promise<void> {
1818
}
1919

2020
const [directory] = cli.args;
21-
const options = { firstParent: cli.firstParent, since: cli.since };
21+
const options = {
22+
firstParent: cli.firstParent,
23+
since: cli.since,
24+
excludes: cli.excludes
25+
};
2226

2327
const commitCountPerFiles: CommitCountPerFile[] = await countCommitsPerFile(
2428
directory,

src/services/prepare-stdout.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export function prepareStdout(
77
): string[] {
88
return filesComplexity
99
.sort(sortResult(cli.sort))
10+
.filter(exclude(cli.excludes))
1011
.filter(limitResult(cli.limit, cli.min, cli.max))
1112
.map(prepareLine(cli));
1213
}
@@ -35,6 +36,15 @@ function sortResult(sort) {
3536
};
3637
}
3738

39+
function exclude(exclusions = []) {
40+
return (fileComplexity): boolean => {
41+
const atLeastOneExclusionMatches = exclusions.some(exclusion => {
42+
return fileComplexity.relativePathToFile.includes(exclusion);
43+
});
44+
return atLeastOneExclusionMatches === false;
45+
};
46+
}
47+
3848
function limitResult(limit, min, max) {
3949
return (complexityPerFile: ComplexityPerFile, i: number): boolean => {
4050
if (limit && i >= limit) {

test/code-complexity.test.ts

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,66 @@ const codeComplexity = resolve(__dirname, "../src/index.ts");
77
const fixture = resolve(__dirname, "code-complexity-fixture");
88

99
describe("code-complexity", () => {
10-
let output;
10+
context("With details, limit and sort", () => {
11+
it("outputs the appropriate values", () => {
12+
// Given
13+
const command = [
14+
`ts-node ${codeComplexity}`,
15+
fixture,
16+
`--details`,
17+
`--limit 10`,
18+
`--sort complexity`
19+
].join(" ");
1120

12-
before(() => {
13-
const command = [
14-
`ts-node ${codeComplexity}`,
15-
fixture,
16-
`--details`,
17-
`--limit 10`,
18-
`--sort complexity`
19-
].join(" ");
21+
// When
22+
const output = execSync(command, { encoding: "utf8" });
2023

21-
output = execSync(command, { encoding: "utf8" });
22-
});
24+
// Then
25+
expect(output.trim()).to.deep.equal(
26+
[
27+
"lib/response.js 142416 (commits: 276, sloc: 516)",
28+
"test/app.router.js 54714 (commits: 66, sloc: 829)",
29+
"lib/router/index.js 40005 (commits: 105, sloc: 381)",
30+
"lib/application.js 32818 (commits: 122, sloc: 269)",
31+
"lib/request.js 21746 (commits: 131, sloc: 166)",
32+
"test/res.send.js 17822 (commits: 38, sloc: 469)",
33+
"test/res.sendFile.js 14674 (commits: 22, sloc: 667)",
34+
"test/Router.js 11100 (commits: 25, sloc: 444)",
35+
"lib/express.js 7875 (commits: 125, sloc: 63)",
36+
"lib/utils.js 7095 (commits: 55, sloc: 129)"
37+
].join("\n")
38+
);
39+
});
40+
41+
context("With exclude", () => {
42+
it("outputs the appropriate values", () => {
43+
// Given
44+
const command = [
45+
`ts-node ${codeComplexity}`,
46+
fixture,
47+
`--details`,
48+
`--limit 8`,
49+
`--sort complexity`,
50+
`--excludes response,request`
51+
].join(" ");
52+
53+
// When
54+
const output = execSync(command, { encoding: "utf8" });
2355

24-
it("outputs the appropriate values", () => {
25-
expect(output.trim()).to.deep.equal(
26-
[
27-
"lib/response.js 142416 (commits: 276, sloc: 516)",
28-
"test/app.router.js 54714 (commits: 66, sloc: 829)",
29-
"lib/router/index.js 40005 (commits: 105, sloc: 381)",
30-
"lib/application.js 32818 (commits: 122, sloc: 269)",
31-
"lib/request.js 21746 (commits: 131, sloc: 166)",
32-
"test/res.send.js 17822 (commits: 38, sloc: 469)",
33-
"test/res.sendFile.js 14674 (commits: 22, sloc: 667)",
34-
"test/Router.js 11100 (commits: 25, sloc: 444)",
35-
"lib/express.js 7875 (commits: 125, sloc: 63)",
36-
"lib/utils.js 7095 (commits: 55, sloc: 129)"
37-
].join("\n")
38-
);
56+
// Then
57+
expect(output.trim()).to.deep.equal(
58+
[
59+
"test/app.router.js 54714 (commits: 66, sloc: 829)",
60+
"lib/router/index.js 40005 (commits: 105, sloc: 381)",
61+
"lib/application.js 32818 (commits: 122, sloc: 269)",
62+
"test/res.send.js 17822 (commits: 38, sloc: 469)",
63+
"test/res.sendFile.js 14674 (commits: 22, sloc: 667)",
64+
"test/Router.js 11100 (commits: 25, sloc: 444)",
65+
"lib/express.js 7875 (commits: 125, sloc: 63)",
66+
"lib/utils.js 7095 (commits: 55, sloc: 129)"
67+
].join("\n")
68+
);
69+
});
70+
});
3971
});
4072
});

0 commit comments

Comments
 (0)