Skip to content

Commit 02e76a7

Browse files
author
Simon Renoult
committed
feat: allow 'includes' and 'excludes' to be used together
1 parent 10fb02a commit 02e76a7

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ export default commander
4040
.option(
4141
"--excludes <strings>",
4242
"List of strings (comma separated) used in filenames to exclude " +
43-
"(mutually exclusive with '--includes')",
43+
"(executed after '--includes')",
4444
commaSeparatedList,
4545
[]
4646
)
4747
.option(
4848
"--includes <strings>",
4949
"List of strings (comma separated) used in filenames to include " +
50-
"(mutually exclusive with '--excludes')",
50+
"(executed before '--excludes')",
5151
commaSeparatedList,
5252
[]
5353
)

src/services/compute-complexity.ts

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

20-
if (cli.includes.length !== 0 && cli.excludes.length !== 0) {
21-
throw new Error("Options 'includes' and 'excludes' are mutually exclusive");
22-
}
23-
2420
const [directory] = cli.args;
2521
const options = { firstParent: cli.firstParent, since: cli.since };
2622
const commitCountPerFiles: CommitCountPerFile[] = await countCommitsPerFile(

src/services/prepare-stdout.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ export function prepareStdout(
66
cli
77
): string[] {
88
return filesComplexity
9-
.sort(sortResult(cli.sort))
10-
.filter(selectFiles(cli.includes, cli.excludes))
11-
.filter(limitResult(cli.limit, cli.min, cli.max))
9+
.sort(sort(cli.sort))
10+
.filter(include(cli.includes))
11+
.filter(exclude(cli.excludes))
12+
.filter(limit(cli.limit, cli.min, cli.max))
1213
.map(prepareLine(cli));
1314
}
1415

15-
function sortResult(sort) {
16+
function sort(sort) {
1617
return (fileA: ComplexityPerFile, fileB: ComplexityPerFile): number => {
1718
if (sort === "complexity") {
1819
return fileB.complexity - fileA.complexity;
@@ -36,28 +37,31 @@ function sortResult(sort) {
3637
};
3738
}
3839

39-
function selectFiles(
40-
inclusions = [],
41-
exclusions = []
42-
): (ComplexityPerFile) => boolean {
40+
function include(inclusions): (ComplexityPerFile) => boolean {
4341
return (fileComplexity: ComplexityPerFile): boolean => {
4442
if (inclusions.length) {
4543
return inclusions.some(pathContains(fileComplexity));
4644
}
4745

46+
return true;
47+
};
48+
}
49+
50+
function exclude(exclusions): (ComplexityPerFile) => boolean {
51+
return (fileComplexity: ComplexityPerFile): boolean => {
4852
if (exclusions.length) {
4953
return !exclusions.some(pathContains(fileComplexity));
5054
}
5155

5256
return true;
5357
};
58+
}
5459

55-
function pathContains(fileComplexity: ComplexityPerFile) {
56-
return (s): boolean => fileComplexity.relativePathToFile.includes(s);
57-
}
60+
function pathContains(fileComplexity: ComplexityPerFile) {
61+
return (s): boolean => fileComplexity.relativePathToFile.includes(s);
5862
}
5963

60-
function limitResult(limit, min, max) {
64+
function limit(limit, min, max) {
6165
return (complexityPerFile: ComplexityPerFile, i: number): boolean => {
6266
if (limit && i >= limit) {
6367
return false;

test/code-complexity.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("code-complexity", () => {
3838
);
3939
});
4040

41-
context("With exclude", () => {
41+
context("With excludes", () => {
4242
it("outputs the appropriate values", () => {
4343
// Given
4444
const command = [
@@ -101,5 +101,32 @@ describe("code-complexity", () => {
101101
);
102102
});
103103
});
104+
105+
context("With includes and excludes", () => {
106+
it("outputs the appropriate values", () => {
107+
// Given
108+
const command = [
109+
`ts-node ${codeComplexity}`,
110+
fixture,
111+
`--details`,
112+
`--limit 10`,
113+
`--sort complexity`,
114+
`--includes router`,
115+
`--excludes test,examples`
116+
].join(" ");
117+
118+
// When
119+
const output = execSync(command, { encoding: "utf8" });
120+
121+
// Then
122+
expect(output.trim()).to.deep.equal(
123+
[
124+
"lib/router/index.js 40005 (commits: 105, sloc: 381)",
125+
"lib/router/route.js 2856 (commits: 28, sloc: 102)",
126+
"lib/router/layer.js 1602 (commits: 18, sloc: 89)"
127+
].join("\n")
128+
);
129+
});
130+
});
104131
});
105132
});

0 commit comments

Comments
 (0)