'use strict' const { spawnSync } = require('child_process') const fs = require('fs') const path = require('path') // 跨平台的语法检查:原来 package.json 里的 `for f in $(find ...)` 只在 POSIX shell 下能用, // Windows 构建机上直接报错,这里改用 Node 递归遍历 + node --check。 const ROOT = path.resolve(__dirname, '..') const SKIP_DIRS = new Set(['node_modules', '.git']) function collect(dir, files = []) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { if (entry.isDirectory()) { if (SKIP_DIRS.has(entry.name)) continue collect(path.join(dir, entry.name), files) } else if (entry.isFile() && entry.name.endsWith('.js')) { files.push(path.join(dir, entry.name)) } } return files } const files = collect(ROOT).sort() const failed = [] for (const file of files) { const result = spawnSync(process.execPath, ['--check', file], { stdio: 'inherit' }) if (result.status !== 0) failed.push(file) } if (failed.length) { console.error(`\n语法检查失败 ${failed.length}/${files.length} 个文件:`) for (const file of failed) console.error(` ${path.relative(ROOT, file)}`) process.exit(1) } console.log(`语法检查通过:${files.length} 个文件`)