|
| 1 | +import { existsSync } from "fs"; |
| 2 | + |
| 3 | +const IGNORED_PATTERNS = [ |
| 4 | + "node_modules", |
| 5 | + "/dist/", |
| 6 | + "/target/", |
| 7 | + "/pkg/", |
| 8 | + "/js/wasm/", |
| 9 | + "/bundler-test/", |
| 10 | +]; |
| 11 | + |
| 12 | +function filterIgnored(filenames) { |
| 13 | + return filenames.filter((f) => !IGNORED_PATTERNS.some((p) => f.includes(p))); |
| 14 | +} |
| 15 | + |
| 16 | +/** Extract package name from a file path like packages/wasm-utxo/src/foo.rs */ |
| 17 | +function getPackageName(filepath) { |
| 18 | + const match = filepath.match(/packages\/([^/]+)\//); |
| 19 | + return match?.[1]; |
| 20 | +} |
| 21 | + |
| 22 | +/** Group files by their package name */ |
| 23 | +function groupByPackage(filenames) { |
| 24 | + const groups = new Map(); |
| 25 | + for (const f of filenames) { |
| 26 | + const pkg = getPackageName(f); |
| 27 | + if (!pkg) continue; |
| 28 | + if (!groups.has(pkg)) groups.set(pkg, []); |
| 29 | + groups.get(pkg).push(f); |
| 30 | + } |
| 31 | + return groups; |
| 32 | +} |
| 33 | + |
| 34 | +export default { |
| 35 | + "*.{js,ts,tsx,mjs,cjs}": (filenames) => { |
| 36 | + const filtered = filterIgnored(filenames); |
| 37 | + if (filtered.length === 0) return []; |
| 38 | + |
| 39 | + const commands = [`prettier --write ${filtered.join(" ")}`]; |
| 40 | + |
| 41 | + for (const [pkg, files] of groupByPackage(filtered)) { |
| 42 | + const configPath = `packages/${pkg}/eslint.config.js`; |
| 43 | + if (existsSync(configPath)) { |
| 44 | + commands.push(`npx eslint -c ${configPath} --fix ${files.join(" ")}`); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return commands; |
| 49 | + }, |
| 50 | + |
| 51 | + "*.{json,yaml,yml,md}": (filenames) => { |
| 52 | + const filtered = filterIgnored(filenames); |
| 53 | + if (filtered.length === 0) return []; |
| 54 | + return [`prettier --write ${filtered.join(" ")}`]; |
| 55 | + }, |
| 56 | + |
| 57 | + "*.rs": (filenames) => { |
| 58 | + const filtered = filterIgnored(filenames); |
| 59 | + if (filtered.length === 0) return []; |
| 60 | + |
| 61 | + const commands = []; |
| 62 | + for (const pkg of new Set(filtered.map(getPackageName).filter(Boolean))) { |
| 63 | + const manifest = `packages/${pkg}/Cargo.toml`; |
| 64 | + if (existsSync(manifest)) { |
| 65 | + commands.push(`cargo fmt --manifest-path ${manifest}`); |
| 66 | + commands.push( |
| 67 | + `cargo clippy --manifest-path ${manifest} --all-targets --all-features -- -D warnings`, |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + return commands; |
| 72 | + }, |
| 73 | +}; |
0 commit comments