kloc-cli
Version:
A simple CLI tool to count lines of code in current working directory
34 lines (27 loc) • 1.08 kB
JavaScript
import { validateFile, validateDir } from "../../utils/validateItem.js"
import { readFileSync, readdirSync, statSync } from "fs"
import { basename, join } from "path"
const printKloc = (path, level, prefix = "") => {
const root = readdirSync(path)
root.forEach((item, index) => {
const itemPath = join(path, item)
const isLastItem = index == root.length - 1
const branch = isLastItem ? "└── " : "├── "
const newPrefix = prefix + (isLastItem ? " " : "│ ")
if (statSync(itemPath).isDirectory() && validateDir(item)) {
console.log(prefix + branch + item);
printKloc(itemPath, level + 1, newPrefix)
}
else {
if (validateFile(item)) {
const file = readFileSync(itemPath, "utf8").toString()
const loc = file.split("\n").length
console.log(prefix + branch + item, loc)
}
}
})
}
export const klocFile = () => {
console.log("\n" + basename(process.cwd()))
printKloc(process.cwd(), 0)
}