-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
59 lines (47 loc) · 1.31 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const path = require('path');
const fs = require('fs').promises;
const globby = require('globby');
const chalk = require('chalk');
const histogram = require('ascii-histogram');
const percent = (n, total) => (n && (n / total * 100).toFixed(1)) + '%';
const formatter = total => n => `${n} (${percent(n, total)})`;
const printOutput = data => {
const total = Object.values(data)
.reduce((acc, v) => acc + v, 0);
const output = histogram(data, { map: formatter(total) })
.split('\n')
.map((v, i) => {
switch (i) {
case 0:
return chalk.yellowBright(v);
case 1:
return chalk.blueBright(v);
default:
return v;
}
})
.join('\n');
console.log(total
? output
: 'No sources found ¯\\_(ツ)_/¯.'
);
};
/**
* @param {string} directory
*/
module.exports = async (directory = process.cwd()) => {
const extensions = {};
directory = path.resolve(directory);
await fs.access(directory);
for await (const p of globby.stream(`${directory}/**/*.{js,jsx,ts,tsx}`)) {
const extension = path.extname(p);
extensions[extension] = (extensions[extension] || 0) + 1;
}
const jsx = (extensions['.js'] || 0) + (extensions['.jsx'] || 0);
const tsx = (extensions['.ts'] || 0) + (extensions['.tsx'] || 0);
const data = {
'.js(x)': jsx || 0,
'.ts(x)': tsx || 0,
};
printOutput(data);
};