#!/usr/bin/env node const fs = require('fs') const path = require('path') const ROOT = path.join(__dirname, '..', 'miniprogram') const TARGET_EXTENSIONS = new Set(['.js', '.json', '.wxml', '.wxss']) function walk(dir, files = []) { const entries = fs.readdirSync(dir, { withFileTypes: true }) for (const entry of entries) { const fullPath = path.join(dir, entry.name) if (fullPath.includes(`${path.sep}workers${path.sep}svg-generator${path.sep}vendor${path.sep}`)) { continue } if (fullPath.includes(`${path.sep}i18n${path.sep}`)) { continue } if (fullPath.endsWith('.miniapp.json')) { continue } if (entry.isDirectory()) { walk(fullPath, files) continue } const ext = path.extname(entry.name) if (TARGET_EXTENSIONS.has(ext)) { files.push(fullPath) } } return files } function main() { console.log('开始执行小程序代码规范检查...') const files = walk(ROOT) const violations = [] for (const file of files) { const content = fs.readFileSync(file, 'utf8') const lines = content.split(/\r?\n/) for (let i = 0; i < lines.length; i += 1) { const line = lines[i] if (/\t/.test(line)) { violations.push(`${file}:${i + 1} 使用了 Tab 缩进`) } if (/\s+$/.test(line)) { violations.push(`${file}:${i + 1} 存在行尾空格`) } } } if (violations.length > 0) { console.error('发现代码规范问题:') for (const violation of violations) { console.error(`- ${violation}`) } process.exit(1) } console.log('小程序代码规范检查通过。') } main()