Skip to content

Commit ee70706

Browse files
author
Simon Renoult
committed
feat: add 'includes' flag (mutually exclusive with 'excludes')
1 parent cbe1e45 commit ee70706

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

src/cli.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const examples = [
1010
"$ code-complexity <dir> --min 10 --max 50",
1111
"$ code-complexity <dir> --sort complexity",
1212
"$ code-complexity <dir> --excludes lib,test",
13+
"$ code-complexity <dir> --includes users",
1314
"$ code-complexity <dir> --details --limit 10 --sort complexity --excludes test"
1415
];
1516

@@ -38,8 +39,17 @@ export default commander
3839
.option("--max [max]", "Exclude results above <max>", parseInt)
3940
.option(
4041
"--excludes <strings>",
41-
"List of strings (comma separated) used in filenames to exclude",
42-
commaSeparatedList
42+
"List of strings (comma separated) used in filenames to exclude " +
43+
"(mutually exclusive with '--includes')",
44+
commaSeparatedList,
45+
[]
46+
)
47+
.option(
48+
"--includes <strings>",
49+
"List of strings (comma separated) used in filenames to include " +
50+
"(mutually exclusive with '--excludes')",
51+
commaSeparatedList,
52+
[]
4353
)
4454
.on("--help", () => {
4555
console.log();

src/services/compute-complexity.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ export default async function computeComplexity(): Promise<void> {
1717
process.exit(0);
1818
}
1919

20-
const [directory] = cli.args;
21-
const options = {
22-
firstParent: cli.firstParent,
23-
since: cli.since,
24-
excludes: cli.excludes
25-
};
20+
if (cli.includes.length !== 0 && cli.excludes.length !== 0) {
21+
throw new Error("Options 'includes' and 'excludes' are mutually exclusive");
22+
}
2623

24+
const [directory] = cli.args;
25+
const options = { firstParent: cli.firstParent, since: cli.since };
2726
const commitCountPerFiles: CommitCountPerFile[] = await countCommitsPerFile(
2827
directory,
2928
options

src/services/prepare-stdout.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function prepareStdout(
77
): string[] {
88
return filesComplexity
99
.sort(sortResult(cli.sort))
10-
.filter(exclude(cli.excludes))
10+
.filter(selectFiles(cli.includes, cli.excludes))
1111
.filter(limitResult(cli.limit, cli.min, cli.max))
1212
.map(prepareLine(cli));
1313
}
@@ -36,13 +36,25 @@ function sortResult(sort) {
3636
};
3737
}
3838

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;
39+
function selectFiles(
40+
inclusions = [],
41+
exclusions = []
42+
): (ComplexityPerFile) => boolean {
43+
return (fileComplexity: ComplexityPerFile): boolean => {
44+
if (inclusions.length) {
45+
return inclusions.some(pathContains(fileComplexity));
46+
}
47+
48+
if (exclusions.length) {
49+
return !exclusions.some(pathContains(fileComplexity));
50+
}
51+
52+
return true;
4553
};
54+
55+
function pathContains(fileComplexity: ComplexityPerFile) {
56+
return (s): boolean => fileComplexity.relativePathToFile.includes(s);
57+
}
4658
}
4759

4860
function limitResult(limit, min, max) {

test/code-complexity.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("code-complexity", () => {
4545
`ts-node ${codeComplexity}`,
4646
fixture,
4747
`--details`,
48-
`--limit 8`,
48+
`--limit 10`,
4949
`--sort complexity`,
5050
`--excludes response,request`
5151
].join(" ");
@@ -63,7 +63,40 @@ describe("code-complexity", () => {
6363
"test/res.sendFile.js 14674 (commits: 22, sloc: 667)",
6464
"test/Router.js 11100 (commits: 25, sloc: 444)",
6565
"lib/express.js 7875 (commits: 125, sloc: 63)",
66-
"lib/utils.js 7095 (commits: 55, sloc: 129)"
66+
"lib/utils.js 7095 (commits: 55, sloc: 129)",
67+
"lib/view.js 6384 (commits: 84, sloc: 76)",
68+
"test/app.use.js 5936 (commits: 14, sloc: 424)"
69+
].join("\n")
70+
);
71+
});
72+
});
73+
74+
context("With includes", () => {
75+
it("outputs the appropriate values", () => {
76+
// Given
77+
const command = [
78+
`ts-node ${codeComplexity}`,
79+
fixture,
80+
`--details`,
81+
`--limit 10`,
82+
`--sort complexity`,
83+
`--includes router`
84+
].join(" ");
85+
86+
// When
87+
const output = execSync(command, { encoding: "utf8" });
88+
89+
// Then
90+
expect(output.trim()).to.deep.equal(
91+
[
92+
"test/app.router.js 54714 (commits: 66, sloc: 829)",
93+
"lib/router/index.js 40005 (commits: 105, sloc: 381)",
94+
"lib/router/route.js 2856 (commits: 28, sloc: 102)",
95+
"lib/router/layer.js 1602 (commits: 18, sloc: 89)",
96+
"test/acceptance/multi-router.js 78 (commits: 2, sloc: 39)",
97+
"examples/multi-router/index.js 22 (commits: 2, sloc: 11)",
98+
"examples/multi-router/controllers/api_v1.js 9 (commits: 1, sloc: 9)",
99+
"examples/multi-router/controllers/api_v2.js 9 (commits: 1, sloc: 9)"
67100
].join("\n")
68101
);
69102
});

0 commit comments

Comments
 (0)