Skip to content

Commit 3ea9d49

Browse files
author
Simon Renoult
committed
feat: v4.0.0
With the addition of the `json` format, I chose to make the default output cleaner. It is way easier to read this way! \o/ Note that this format is also available using the `--format=table` flag. It looks like this: ```shell script $ npx code-complexity . --sort=ratio --limit=3 ┌──────────────────────────────┬────────────┬───────┬───────┐ │ file │ complexity │ churn │ ratio │ ├──────────────────────────────┼────────────┼───────┼───────┤ │ src/cli.ts │ 103 │ 8 │ 824 │ ├──────────────────────────────┼────────────┼───────┼───────┤ │ test/code-complexity.test.ts │ 107 │ 7 │ 749 │ ├──────────────────────────────┼────────────┼───────┼───────┤ │ .idea/workspace.xml │ 123 │ 6 │ 738 │ └──────────────────────────────┴────────────┴───────┴───────┘ ``` While handy, this solution was a rather naive approach to filtering. I've removed them in favor of a `--filter` flag supporting globs. The implementation is using [micromatch][1]. Example: ```shell script $ npx code-complexity . --filter='!test/**','src/*.ts' ``` While interesting, these flags were not adding much value since you usually want to list the highest ratio/churn/complexity values. A combination of `--sort=ratio` and `--limit=5` would usually do the trick. They'd be interesting if min/max could be targeted on a metric (eg: `ratio`, `churn` or `complexity`). But since they were only targeting `ratios`, I don't see them adding much value. And they add more code to maintain. I'll add an issue about adding them back and wait to see if it gets traction. The new `table` stdout is clearer to read and already show these information so we don't need these flags anymore. Yay, less code to maintain! Using `--first-parent` was causing problems where `git log` would not output all the files. Using `no-first-parent` is now the default. With the addition of the `json` support, I chose to make the `text` output a bit more opinionated. Metrics like `complexity`, `churn` and `ratio` will always be shown from now on. See `--format`. I've added this flag to support more output format. It currently allows `table` and `json`. The `table` format is the default one. The `json` format will ease integration with other softwares. Example with `--format=table`: ```shell script $ npx code-complexity . --sort=ratio --limit=3 ┌──────────────────────────────┬────────────┬───────┬───────┐ │ file │ complexity │ churn │ ratio │ ├──────────────────────────────┼────────────┼───────┼───────┤ │ src/cli.ts │ 103 │ 8 │ 824 │ ├──────────────────────────────┼────────────┼───────┼───────┤ │ test/code-complexity.test.ts │ 107 │ 7 │ 749 │ ├──────────────────────────────┼────────────┼───────┼───────┤ │ .idea/workspace.xml │ 123 │ 6 │ 738 │ └──────────────────────────────┴────────────┴───────┴───────┘ ``` Example with `--format=json`: ```sh $ npx code-complexity . --format=json --limit=3 [{"path":"bin/code-complexity.ts","churn":1,"complexity":6,"ratio":6},{"path":".eslintignore","churn":1,"complexity":1,"ratio":1},{"path":".eslintrc.js","churn":1,"complexity":19,"ratio":19}] ``` I'm also thinking about an `html` format in order to display a complexity/churn graph but it's a bit of work so I'm not sure when that will lend. As mentioned above, it replaces both `--excludes` and `--includes` whil adding glob support using [micromatch][1]. Good job me! If you're interested in metrics and what's going on without having to look at the source code, you can use the `DEBUG=*` environment variable. [1]: https://www.npmjs.com/package/micromatch
1 parent df98adc commit 3ea9d49

23 files changed

+1217
-822
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
rules: {
1515
"prettier/prettier": "error",
1616
"no-console": "off",
17-
"@typescript-eslint/no-use-before-define": "off"
17+
"@typescript-eslint/no-use-before-define": "off",
18+
"@typescript-eslint/no-explicit-any": "off"
1819
}
1920
};

README.md

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,43 @@ $ npx code-complexity <path-to-git-directory>
1616
## Help
1717

1818
```sh
19-
Usage: code-complexity <dir> [options]
20-
21-
Measure projects complexity based on files sloc and commit count.
22-
23-
Options:
24-
-l, --limit [limit] Limit the number of files to output
25-
-d, --details Show the number of commit and computed sloc
26-
-c, --commit Show the number of commits
27-
-s, --sloc Show the computed sloc
28-
-i, --since [since] Limit the age of the commit analyzed
29-
-n, --no-first-parent Do not use the git-log flag '--first-parent' when counting commits
30-
--sort [sort] Sort results by commit, complexity, file or sloc
31-
--min [min] Exclude results below <min>
32-
--max [max] Exclude results above <max>
33-
--excludes <strings> List of strings (comma separated) used in filenames to exclude
34-
(executed after '--includes') (default: [])
35-
--includes <strings> List of strings (comma separated) used in filenames to include
36-
(executed before '--excludes') (default: [])
37-
-h, --help display help for command
38-
39-
Examples:
40-
19+
Usage: code-complexity <dir> [options]
20+
21+
Measure the churn/complexity ratio. Higher values mean hotspots where refactorings should happen.
22+
23+
Options:
24+
-V, --version output the version number
25+
--filter <strings> list of globs (comma separated) to filter
26+
-f, --format [format] format results using table or json
27+
-l, --limit [limit] limit the number of files to output
28+
-i, --since [since] limit the age of the commit analyzed
29+
-s, --sort [sort] sort results (allowed valued: ratio,
30+
churn, complexity or file)
31+
-h, --help display help for command
32+
33+
Examples:
34+
4135
$ code-complexity <dir>
4236
$ code-complexity <dir> --limit 3
43-
$ code-complexity <dir> --details
44-
$ code-complexity <dir> --min 10 --max 50
45-
$ code-complexity <dir> --sort complexity
46-
$ code-complexity <dir> --excludes lib,test
47-
$ code-complexity <dir> --includes users
48-
$ code-complexity <dir> --details --limit 10 --sort complexity --excludes test
37+
$ code-complexity <dir> --sort ratio
38+
$ code-complexity <dir> --filter 'src/**','!src/front'
39+
$ code-complexity <dir> --limit 10 --sort ratio
4940
```
5041

5142
## Output
5243

5344
```sh
54-
$ npx code-complexity . --details --sort complexity --limit 10
45+
$ npx code-complexity . --sort=ratio --limit=3
5546

56-
test/code-complexity.test.ts 535 (commits: 5, sloc: 107)
57-
src/cli.ts 402 (commits: 6, sloc: 67)
58-
src/services/prepare-stdout.ts 348 (commits: 4, sloc: 87)
59-
src/services/compute-complexity.ts 108 (commits: 4, sloc: 27)
60-
src/services/count-commits-per-file.ts 83 (commits: 1, sloc: 83)
61-
src/services/compute-complexity-per-file.ts 36 (commits: 1, sloc: 36)
62-
.eslintrc.js 19 (commits: 1, sloc: 19)
47+
┌──────────────────────────────┬────────────┬───────┬───────┐
48+
│ file │ complexity │ churn │ ratio │
49+
├──────────────────────────────┼────────────┼───────┼───────┤
50+
│ src/cli.ts │ 103 │ 8 │ 824 │
51+
├──────────────────────────────┼────────────┼───────┼───────┤
52+
│ test/code-complexity.test.ts │ 107 │ 7 │ 749 │
53+
├──────────────────────────────┼────────────┼───────┼───────┤
54+
│ .idea/workspace.xml │ 123 │ 6 │ 738 │
55+
└──────────────────────────────┴────────────┴───────┴───────┘
6356
```
6457

6558
## Troubleshooting

bin/code-complexity.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
import computeCodeComplexity from "../src/io";
4+
5+
computeCodeComplexity().catch((error) => {
6+
console.error(error);
7+
process.exit(1);
8+
});

0 commit comments

Comments
 (0)