Created
January 19, 2022 15:22
-
-
Save ephys/e8047cf132a5c35c18b73c439f2d4a50 to your computer and use it in GitHub Desktop.
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 {readdir, stat} from 'fs/promises'; | |
import path from 'path'; | |
const dir = process.argv[2]; | |
async function listFiles(dir) { | |
const subdirs = await readdir(dir); | |
const files = await Promise.all(subdirs.map(async (subdir) => { | |
const res = path.resolve(dir, subdir); | |
return (await stat(res)).isDirectory() ? listFiles(res) : res; | |
})); | |
return files.reduce((a, f) => a.concat(f), []); | |
} | |
const countByExt = new Map(); | |
for (const file of await listFiles(dir)) { | |
const ext = path.extname(file); | |
countByExt.set(ext, (countByExt.get(ext) ?? 0) + 1); | |
} | |
console.log('Count per file extension:'); | |
const entries = Array.from(countByExt.entries()) | |
.sort((a, b) => b[1] - a[1]); | |
for (const [ext, count] of entries) { | |
console.log((ext || 'no extension') + ': ' + count) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment