flatter is a command-line tool for determining a source file's "indentedness", which provides a rough indication of how well factored the code is.
$ flatter src
#!/usr/bin/env coffee | |
fs = require 'fs' | |
{resolve} = require 'path' | |
identity = (x) -> x | |
analyze = (path) -> | |
lines = fs.readFileSync(path, 'utf8').split(/\r\n|\r|\n/) | |
significantLines = lines.filter identity | |
indents = significantLines.map (line) -> /^[ \t]*/.exec(line)[0].length | |
indents = indents.reduce (sum = 0, n) -> sum + n | |
path: path | |
lines: lines.length | |
indents: | |
total: indents | |
average: indents / significantLines.length | |
log = (stats) -> | |
console.log """ | |
#{stats.path} | |
lines: #{stats.lines} | |
average indent: #{stats.indents.average.toFixed 1} | |
""" | |
walk = (path) -> | |
if fs.statSync(path).isDirectory() | |
walk resolve path, p for p in fs.readdirSync path | |
else | |
log analyze path | |
walk resolve process.cwd(), p for p in process.argv[2..] |