-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (48 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env node
var measure = require('./src/metrics');
var sourceFiles = require('./src/sourceFiles');
var complexityInfo = require('./src/reporter').getComplexityInfo;
var program = require('commander');
var package = require('./package.json');
var check = require('check-types');
var path = require('path');
var repoRevision = require('ggit').repoRevision;
var repoRoot = require('ggit').repoRoot;
program.version(package.version);
program._name = package.name;
program.command('help')
.description('show help and exit')
.action(function () {
console.log('Complexity metrics:\n\n' + complexityInfo());
program.help();
});
program.command('changes [filename]')
.description('analyze file complexity against Git repo version')
.option('-f, --filename [filename]', 'filename to analyze')
.action(function (filename, options) {
filename = filename || options.filename;
check.verifyString(filename, 'missing filename');
var files = sourceFiles(filename);
check.verifyArray(files, 'should get full filenames');
if (!files.length) {
console.log('not files found matching', filename);
process.exit(0);
}
repoRoot(function (rootFolder) {
var relativePaths = files.map(path.relative.bind(this, rootFolder));
relativePaths.forEach(reportChange);
});
});
if (process.argv.length === 2) {
process.argv.push('help');
}
program.parse(process.argv);
function reportChange(filename) {
check.verifyString(filename, 'missing filename');
repoRevision(filename, function (contents) {
check.verifyString(contents, 'could not get repo for', filename);
var repoComplexity = measure.getSourceComplexity(contents, filename);
var diskComplexity = measure.getFileComplexity(filename);
diskComplexity.compare(repoComplexity, filename);
});
}