|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const { spawnSync } = require('child_process'); |
| 5 | + |
| 6 | +const root = path.resolve(__dirname, '..'); |
| 7 | +const nodeCmd = process.execPath; |
| 8 | +const sourceExtensions = new Set(['.js', '.mjs', '.cjs']); |
| 9 | +const jsonExtensions = new Set(['.json']); |
| 10 | +const ignoreDirs = new Set(['.git', 'node_modules', '.tmp']); |
| 11 | + |
| 12 | +function stripUtf8Bom(text) { |
| 13 | + return typeof text === 'string' && text.charCodeAt(0) === 0xfeff ? text.slice(1) : text; |
| 14 | +} |
| 15 | + |
| 16 | +function walk(dirPath, files) { |
| 17 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 18 | + for (const entry of entries) { |
| 19 | + if (ignoreDirs.has(entry.name)) continue; |
| 20 | + const fullPath = path.join(dirPath, entry.name); |
| 21 | + if (entry.isDirectory()) { |
| 22 | + walk(fullPath, files); |
| 23 | + continue; |
| 24 | + } |
| 25 | + const ext = path.extname(entry.name).toLowerCase(); |
| 26 | + if (sourceExtensions.has(ext) || jsonExtensions.has(ext)) { |
| 27 | + files.push(fullPath); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function lintJson(filePath) { |
| 33 | + try { |
| 34 | + JSON.parse(stripUtf8Bom(fs.readFileSync(filePath, 'utf8'))); |
| 35 | + } catch (err) { |
| 36 | + throw new Error(`${path.relative(root, filePath)}: invalid JSON (${err.message || err})`); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function lintSource(filePath) { |
| 41 | + const ext = path.extname(filePath).toLowerCase(); |
| 42 | + const sourceText = fs.readFileSync(filePath, 'utf8'); |
| 43 | + const normalized = stripUtf8Bom(sourceText); |
| 44 | + const treatsAsModule = ext === '.mjs' |
| 45 | + || (ext === '.js' && /\b(?:import|export)\b|import\.meta/.test(normalized)); |
| 46 | + const args = treatsAsModule |
| 47 | + ? ['--input-type=module', '--check'] |
| 48 | + : ['--check', filePath]; |
| 49 | + const result = spawnSync(nodeCmd, args, { |
| 50 | + cwd: root, |
| 51 | + input: treatsAsModule ? normalized : undefined, |
| 52 | + encoding: 'utf8', |
| 53 | + env: process.env |
| 54 | + }); |
| 55 | + if (result.error) { |
| 56 | + throw new Error(`${path.relative(root, filePath)}: ${result.error.message}`); |
| 57 | + } |
| 58 | + if (result.status !== 0) { |
| 59 | + const detail = String(result.stderr || result.stdout || '').trim(); |
| 60 | + throw new Error(`${path.relative(root, filePath)}: ${detail || 'syntax check failed'}`); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function main() { |
| 65 | + const files = []; |
| 66 | + walk(root, files); |
| 67 | + files.sort((a, b) => a.localeCompare(b)); |
| 68 | + |
| 69 | + let checked = 0; |
| 70 | + for (const filePath of files) { |
| 71 | + const ext = path.extname(filePath).toLowerCase(); |
| 72 | + if (jsonExtensions.has(ext)) { |
| 73 | + lintJson(filePath); |
| 74 | + } else if (sourceExtensions.has(ext)) { |
| 75 | + lintSource(filePath); |
| 76 | + } |
| 77 | + checked += 1; |
| 78 | + } |
| 79 | + |
| 80 | + console.log(`[codexmate] Lint passed for ${checked} file(s).`); |
| 81 | +} |
| 82 | + |
| 83 | +try { |
| 84 | + main(); |
| 85 | +} catch (err) { |
| 86 | + console.error(`[codexmate] Lint failed: ${err.message || err}`); |
| 87 | + process.exit(1); |
| 88 | +} |
0 commit comments