Skip to content

Instantly share code, notes, and snippets.

@lund0n
Last active October 17, 2019 15:26
Show Gist options
  • Save lund0n/c676f177fe60ca41d2456ed64ed9e931 to your computer and use it in GitHub Desktop.
Save lund0n/c676f177fe60ca41d2456ed64ed9e931 to your computer and use it in GitHub Desktop.
Reports file statistics for the specified directory tree.

Reports file statistics for the specified directory tree, including:

  • All source files (split by JavaScript and TypeScript)
  • All test files (split by JavaScript and TypeScript)

Quick Start

npx {url-of-this-gist} {root-path-to-files to analyze}

NOTE Make sure that you don't include non-source directories (like node-modules). Otherwise, the script will not finish anytime soon (and will waste a lot of time!)

#!/usr/bin/env node
const glob = require('glob')
const { bold, cyan, gray, italic, magenta } = require('kleur')
const cwd = process.argv[2] || process.cwd()
const toPercentage = (numerator, denominator) => denominator > 0 ? ((numerator / denominator) * 100) : 0
const stats = Object.entries({
ALL: '**/*.{js,tsx}',
JS: '**/*.js',
TS: '**/*.tsx',
TEST: '**/*.test.{js,tsx}',
TEST_JS: '**/*.test.js',
TEST_TS: '**/*.test.tsx',
}).reduce((result, [query, pattern]) => {
// eslint-disable-next-line immutable/no-mutation
result[query] = glob.sync(pattern, { cwd }).length
return result
}, {})
const FORMULAE = [
{ name: 'All Files', valueFn: ({ ALL }) => ALL, percentFn: () => 100 },
{
name: '- All Source Files',
valueFn: ({ ALL, TEST }) => ALL - TEST,
percentFn: (value, { ALL }) => toPercentage(value, ALL),
},
{
name: ' - JavaScript',
valueFn: ({ JS, TEST_JS }) => JS - TEST_JS,
percentFn: (value, { ALL, TEST }) => toPercentage(value, ALL - TEST),
},
{
name: ' - TypeScript',
valueFn: ({ TS, TEST_TS }) => TS - TEST_TS,
percentFn: (value, { ALL, TEST }) => toPercentage(value, ALL - TEST),
},
{
name: '- All Test Files',
valueFn: ({ TEST }) => TEST,
percentFn: (value, { ALL }) => toPercentage(value, ALL),
},
{
name: ' - JavaScript',
valueFn: ({ TEST_JS }) => TEST_JS,
percentFn: (value, { TEST }) => toPercentage(value, TEST),
},
{
name: ' - TypeScript',
valueFn: ({ TEST_TS }) => TEST_TS,
percentFn: (value, { TEST }) => toPercentage(value, TEST),
},
]
console.log(bold('File Statistics:'))
FORMULAE.forEach(({ name, valueFn, percentFn }) => {
const value = valueFn(stats)
const percent = percentFn(value, stats)
console.log(
[
italic(cyan(name.padEnd(25, ' '))),
magenta(String(value).padStart(10, ' ')),
magenta((percent.toFixed(2) + '%').padStart(10, ' ')),
].join(gray(' | '))
)
})
{
"name": "file-stats",
"version": "0.0.0",
"bin": "./index.js",
"dependencies": {
"glob": "^7.1.4",
"kleur": "^3.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment