改进行数输出

This commit is contained in:
unanmed 2023-05-27 22:58:47 +08:00
parent e9821ef1e5
commit 6404c6f4d0

View File

@ -2,22 +2,37 @@ import fs from 'fs-extra';
import { extname, resolve } from 'path'; import { extname, resolve } from 'path';
(async function () { (async function () {
const dir = './src'; const dir = process.argv[2] || './src';
let totalLines = 0; let totalLines = 0;
let totalFiles = 0; let totalFiles = 0;
const list: Record<string, [number, number]> = {}; const list: Record<string, [number, number]> = {};
const ignoreDir = [
'node_modules',
'floors',
'thirdparty',
'dist',
'_bundle'
];
const ignoreFile = ['.d.ts', '.min.'];
const exts = [
'.ts',
'.tsx',
'.js',
'.jsx',
'.vue',
'.less',
'.css',
'.html'
];
const check = async (dir: string) => { const check = async (dir: string) => {
if (ignoreDir.some(v => dir.includes(v))) return;
const d = await fs.readdir(dir); const d = await fs.readdir(dir);
for await (const one of d) { for await (const one of d) {
if (ignoreFile.some(v => one.includes(v))) continue;
const stat = await fs.stat(resolve(dir, one)); const stat = await fs.stat(resolve(dir, one));
if (stat.isFile()) { if (stat.isFile()) {
if ( if (exts.some(v => one.endsWith(v))) {
['.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 file = await fs.readFile(resolve(dir, one), 'utf-8');
const lines = file.split('\n').length; const lines = file.split('\n').length;
const ext = extname(one); const ext = extname(one);
@ -34,7 +49,10 @@ import { extname, resolve } from 'path';
}; };
await check(dir); await check(dir);
for (const [ext, [file, lines]] of Object.entries(list)) { const sorted = Object.entries(list).sort((a, b) => {
return a[1][1] - b[1][1];
});
for (const [ext, [file, lines]] of sorted) {
console.log( console.log(
`${ext.slice(1).padEnd(7, ' ')}files: ${file `${ext.slice(1).padEnd(7, ' ')}files: ${file
.toString() .toString()