bin/nloc
#!/usr/bin/env node
// Requirements
var path = require('path');
var pkg = require('../package.json');
var cli = require('commander');
var nloc = require('..');
var clitable = require('cli-table');
// Helper Methods for options
function list(val) {
return val.split(',');
}
// CLI Config
cli
.version(pkg.version)
.usage('[options] <file|directory>')
.option('-d, --details', 'print loc per file')
.option('-e, --exclude <expressions>', 'comma-seperated list of expression to exclude', list)
.option('-f, --format <format>', /^(simple|clitable|json)$/i, 'simple')
.option('-t, --types <filetypes>', 'comma-seperated list of filetypes for matcher', list)
.option('--with-encoding', 'include encoding information for each file')
.option('--with-size', 'include size information for each file');
// Help Examples
cli.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ nloc --help');
console.log(' $ nloc -h');
console.log('');
});
// Parse
cli.parse(process.argv);
if (!process.argv.slice(2).length) {
cli.outputHelp();
} else {
var target = cli.args[0];
var options = {};
options.target = target;
if(cli.withEncoding) options.withEncoding = cli.withEncoding;
if(cli.exclude && cli.exclude.length > 0) options.exclude = cli.exclude;
if(cli.types && cli.types.length > 0) options.types = cli.types;
nloc(options, function(err, results) {
if(err) {
console.error(err);
} else {
if(!nloc.isAbsolute(target)) {
target = path.resolve(process.cwd(), target);
}
switch(cli.format) {
case 'clitable':
var head = ['File', 'Total', 'Empty', 'Comment'];
if(cli.withEncoding) {
head.push('Encoding');
}
if(cli.withSize) {
head.push('Size (Bytes)');
}
var table = new clitable({
head: head
});
if(cli.details) {
for(var key in results) {
if(key !== 'summary') {
var detail = [
target!=key?path.relative(target, key):results[key].name,
results[key].sloc.total,
results[key].sloc.empty,
results[key].sloc.comment
];
if(cli.withEncoding) {
detail.push(results[key].analyze.encoding);
}
if(cli.withSize) {
detail.push(results[key].stats.size);
}
table.push(detail);
}
}
}
// summary
var summary = [
'SUMMARY('+results.summary.files+')',
results.summary.loc.total,
results.summary.loc.empty,
results.summary.loc.comment,
];
if(cli.withEncoding) {
summary.push(' - ');
}
if(cli.withSize) {
summary.push(' - ');
}
table.push(summary);
console.log(table.toString());
break;
case 'json': break;
case 'simple':
default:
if(cli.details) {
for(var key in results) {
if(key !== 'summary') {
console.log(target!=key?path.relative(target, key):results[key].name);
console.log("Type | Value");
console.log("---------------------------------------------------");
console.log("Total LOC | " + results[key].sloc.total);
console.log("---------------------------------------------------");
console.log("Empty LOC | " + results[key].sloc.empty);
console.log("---------------------------------------------------");
console.log("Comment LOC | " + results[key].sloc.comment);
console.log("---------------------------------------------------");
if(cli.withEncoding) {
console.log("Encoding | " + results[key].analyze.encoding);
console.log("---------------------------------------------------");
}
if(cli.withSize) {
console.log("Size | " + results[key].stats.size + " Bytes");
console.log("---------------------------------------------------");
}
console.log();
}
}
}
console.log("SUMMARY");
console.log("Type | Value");
console.log("---------------------------------------------------");
console.log("Files | " + results.summary.files);
console.log("---------------------------------------------------");
console.log("Total LOC | " + results.summary.loc.total);
console.log("---------------------------------------------------");
console.log("Empty LOC | " + results.summary.loc.empty);
console.log("---------------------------------------------------");
console.log("Comment LOC | " + results.summary.loc.comment);
console.log("---------------------------------------------------");
break;
}
}
});
}