import fs from 'node:fs'
import path from 'node:path'
import readline from 'node:readline'
import type { ViteDevServer } from 'vite'
let count = 0
async function countLines(root: string): Promise<number> {
return new Promise((resolve, reject) => {
const strem = fs.createReadStream(root)
const rl = readline.createInterface({
input: strem,
crlfDelay: Infinity,
})
let lineCount = 0
rl.on('line', () => {
lineCount++
})
rl.on('error', () => reject(0))
rl.on('close', () => {
resolve(lineCount)
})
})
}
const cwdPath = process.cwd()
function readDir(srcPath: string, maxLine: number) {
if (srcPath.includes('src/apis')) return
const fullPath = path.join(cwdPath, srcPath)
console.log(fullPath)
const res = fs.readdirSync(fullPath, { withFileTypes: true })
console.log(res)
res.forEach((item) => {
const fPath = path.join(cwdPath, srcPath, item.name)
console.log(fPath)
console.log(item.isFile())
if (item.isFile()) {
const match = /\.(ts|vue|js)$/.test(item.name)
if (match) {
countLines(fPath)
.then((lineCount: number) => {
if (lineCount > maxLine) {
count += 1
console.log(`🚨 发现 ${lineCount} 行代码的文件: ${fPath}`)
process.exit(0)
}
})
.catch((err) => {
console.error(`读取文件 ${fPath} 时出错:`, err)
})
}
} else {
readDir(srcPath + '/' + item.name, maxLine)
}
})
}
export default function lineCounterPlugin(path: string, maxLine: number) {
return {
name: 'vite-plugin-line-counter',
configureServer(server: ViteDevServer) {
readDir(path, maxLine)
server.watcher.on('all', (event, filePath) => {
console.log(`文件 ${filePath} 发生了 ${event} 事件`)
if (
(event === 'change' || event === 'add') &&
filePath.includes(path)
) {
count = 0
readDir(path, maxLine)
}
})
},
}
}