Last active
April 23, 2021 15:53
-
-
Save subzey/53ad3920ab00fdaf689d3ef10a57e6b5 to your computer and use it in GitHub Desktop.
linestat
This file contains hidden or 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
import { createReadStream } from 'fs'; | |
async function * splitLines(inputStream) { | |
let carry = ''; | |
for await (const chunk of inputStream) { | |
const lines = (carry + chunk).split('\n'); | |
carry = lines.pop(); | |
yield * lines; | |
} | |
if (carry !== '') { | |
yield carry; | |
} | |
} | |
async function main() { | |
if (process.stdin.isTTY) { | |
console.error("Pipe filenames into stdin"); | |
console.error("Ex: git ls-files '*.js' | node linestat"); | |
return; | |
} | |
const stats = []; | |
let sloc = 0; | |
process.stdin.setEncoding('utf-8'); | |
for await (const stdinLine of splitLines(process.stdin)) { | |
const filename = stdinLine.trim(); | |
if (filename === '') { | |
continue; | |
} | |
for await (const line of splitLines(createReadStream(filename))) { | |
if (line.trim() === '') { | |
continue; | |
} | |
const lineLength = line.trimEnd().replace(/^\t+/g, s => ' '.repeat(s.length * 4)).length; | |
if (lineLength > 10000) { | |
console.error(`Skipping the line of length ${lineLength} in ${filename}`); | |
continue; | |
} | |
stats[lineLength] = (stats[lineLength] || 0) + 1; | |
sloc++; | |
} | |
} | |
for (let i = 0; i < stats.length; i++) { | |
console.log(`${i}\t${stats[i] || 0}`); | |
} | |
console.error(`(${sloc} SLOC total)`); | |
} | |
main().catch(reason => { | |
console.error(reason); | |
process.exit(1); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment