import fs from 'fs'
import path from 'path'
function listFiles(dir) {
const out = []
if (!fs.existsSync(dir)) return out
const stack = [dir]
while (stack.length) {
const d = stack.pop()
const ents = fs.readdirSync(d, { withFileTypes: true })
for (const e of ents) {
const p = path.join(d, e.name)
if (e.isDirectory()) stack.push(p)
else if (e.isFile() && p.endsWith('.ets')) out.push(p)
}
}
return out
}
function scanFile(fp, patterns) {
const s = fs.readFileSync(fp, 'utf-8')
const lines = s.split(/\r?\n/)
const issues = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
for (const p of patterns) {
if (p.re.test(line)) issues.push({ file: fp, line: i + 1, rule: p.name, text: line.trim() })
}
}
return issues
}
const roots = []
const arg = process.argv[2]
if (arg) {
roots.push(path.resolve(arg))
} else {
for (const r of ['packages-server/pb', 'packages-server/protobuf-core', 'packages/pb', 'packages/protobuf-core']) {
const abs = path.resolve(r)
if (fs.existsSync(abs)) roots.push(abs)
}
}
const patterns = [
{ name: 'import-path-has-ext', re: /\b(from|export\s+\*)\s+['"][^'"]*\.ets['"]/ },
{ name: 'disallowed-global', re: /\bTextEncoder\b|\bTextDecoder\b/ },
{ name: 'index-signature', re: /\{\s*\[key:\s*string\s*\]/ },
{ name: 'any-unknown', re: /(:\s*any\b)|(as\s+any\b)|(:\s*unknown\b)/ },
{ name: 'reserved-var-export', re: /\b(let|const)\s+export\b/ }
]
let files = []
for (const r of roots) files = files.concat(listFiles(r))
let all = []
for (const f of files) all = all.concat(scanFile(f, patterns))
if (all.length === 0) {
console.log('ArkTSCheck OK')
process.exit(0)
} else {
console.log('ArkTSCheck Issues: ' + all.length)
for (const it of all) console.log(it.file + ':' + it.line + ' ' + it.rule + ' ' + it.text)
process.exit(1)
}