Last active
August 24, 2017 23:49
-
-
Save CarlosLanderas/e1c20e7a4245f7e43eaf83acdc9e907f to your computer and use it in GitHub Desktop.
Source code analytics with sloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chalk = require('chalk'); | |
const glob = require('glob'); | |
const sloc = require('sloc'); | |
const fs = require('fs'); | |
let pattern = "**"; | |
let jsExcludes = ['**/app.bundle.*', '**/node_modules/**', '**/lib/**']; | |
let slocKeys = ['total', 'comment', 'todo', 'source']; | |
let extension = process.argv.slice(2)[0]; | |
let targetExtension = `*.${extension}`; | |
globExtension(targetExtension, jsExcludes) | |
.then(slocFiles) | |
.catch( err => console.log(chalk.red(err))); | |
function slocFiles(files) { | |
let total = {}; | |
for (file of files.reverse()) { | |
let code = fs.readFileSync(file, "utf8"); | |
var stats = sloc(code, extension); | |
printLine("-"); | |
console.log(chalk.yellow(file)); | |
for (key of slocKeys) { | |
total[key] ? total[key] += stats[key] : total[key] = stats[key]; | |
console.log(chalk.green(key + " : " + stats[key])); | |
} | |
printLine("-"); | |
} | |
printTotals(total); | |
} | |
function globExtension(extension, ignore) { | |
return new Promise((res, rej) => { | |
let options = { | |
ignore | |
}; | |
glob(`${pattern}/${extension}`, options, function (err, files) { | |
if (err) { | |
rej(err); | |
} | |
res(files); | |
}) | |
}); | |
} | |
function printTotals(total) { | |
printLine("*"); | |
console.log(chalk.bgBlue(`\n\nAll files resume for ${targetExtension} extension`)); | |
for (key in total) { | |
console.log(chalk.cyan(`${key} : ${total[key]}`)); | |
} | |
printLine("*"); | |
} | |
function printLine(char) { | |
console.log(chalk.white(getLine(char))); | |
} | |
function getLine(char) { | |
return "".padStart(20, char); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment