import { execFileSync, spawnSync } from 'node:child_process'; import { resolve } from 'node:path'; const root = resolve(import.meta.dirname, '../..'); const mode = process.argv[2]; if (!['lint', 'format'].includes(mode)) throw new Error('Usage: node run-changed-code-quality.mjs '); const base = process.env.QUALITY_BASE_REF ?? 'HEAD'; const tracked = execFileSync('git', ['diff', '--name-only', '--diff-filter=ACMR', base, '--'], { cwd: root, encoding: 'utf8', }); const untracked = execFileSync('git', ['ls-files', '--others', '--exclude-standard'], { cwd: root, encoding: 'utf8' }); const files = [...new Set(`${tracked}\n${untracked}`.split(/\r?\n/).filter(Boolean))].filter((file) => /^(?:src|api\/src|tools)\/.+\.(?:[cm]?[jt]sx?|css)$/.test(file), ); if (!files.length) { console.log(`No changed code files require ${mode}.`); process.exit(0); } const args = mode === 'lint' ? files.filter((file) => !file.endsWith('.css')) : ['--check', ...files]; if (mode === 'lint' && !args.length) { console.log('No changed code files require lint.'); process.exit(0); } const executable = process.execPath; const moduleEntry = resolve( root, mode === 'lint' ? 'node_modules/eslint/bin/eslint.js' : 'node_modules/prettier/bin/prettier.cjs', ); const result = spawnSync(executable, [moduleEntry, ...args], { cwd: root, stdio: 'inherit' }); process.exit(result.status ?? 1);