HumanBreak/script/lines.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-05-26 21:39:19 +08:00
import fs from 'fs-extra';
2023-05-27 22:29:02 +08:00
import { extname, resolve } from 'path';
2023-05-26 21:39:19 +08:00
(async function () {
const dir = './src';
2023-05-27 22:29:02 +08:00
let totalLines = 0;
let totalFiles = 0;
const list: Record<string, [number, number]> = {};
2023-05-26 21:39:19 +08:00
const check = async (dir: string) => {
const d = await fs.readdir(dir);
for await (const one of d) {
const stat = await fs.stat(resolve(dir, one));
if (stat.isFile()) {
if (
['.ts', '.tsx', '.js', '.jsx', '.vue', '.less'].some(v =>
one.endsWith(v)
) &&
!one.endsWith('.d.ts')
) {
const file = await fs.readFile(resolve(dir, one), 'utf-8');
const lines = file.split('\n').length;
2023-05-27 22:29:02 +08:00
const ext = extname(one);
list[ext] ??= [0, 0];
list[ext][0]++;
list[ext][1] += lines;
totalLines += lines;
totalFiles++;
2023-05-26 21:39:19 +08:00
}
} else {
await check(resolve(dir, one));
}
}
};
await check(dir);
2023-05-27 22:29:02 +08:00
for (const [ext, [file, lines]] of Object.entries(list)) {
console.log(
`${ext.slice(1).padEnd(7, ' ')}files: ${file
.toString()
.padEnd(6, ' ')}lines: ${lines}`
);
}
console.log(
`\x1b[33mtotal files: ${totalFiles
.toString()
.padEnd(6, ' ')}lines: ${totalLines}\x1b[0m`
);
2023-05-26 21:39:19 +08:00
})();