wcount-cli
Version:
34 lines (26 loc) • 855 B
JavaScript
import { readFile } from 'node:fs/promises'
const filePath = process.argv[2]
const specificWord = process.argv[3]
let wordCount = {}
async function readContent() {
try {
const fileData = await readFile(filePath, 'utf-8')
const words = fileData
.split(/[\W]/)
.filter((w) => w)
.map((word) => word.toLowerCase())
words.forEach((word) => {
wordCount[word] = (wordCount[word] || 0) + 1
})
console.log(wordCount)
if (specificWord) {
console.log(`Count of "${specificWord}": ${wordCount[specificWord.toLowerCase()] || 0}`)
}
} catch (err) {
if (err.code === 'ENOENT') {
console.error(`❌ File not found: ${filePath}`)
}
}
}
await readContent()