|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Run ATTW checks on all packages and output a properly formatted JSON file |
| 9 | + */ |
| 10 | + |
| 11 | +function getPackages() { |
| 12 | + try { |
| 13 | + const output = execSync('npx lerna list --all --json', { |
| 14 | + encoding: 'utf8', |
| 15 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 16 | + }); |
| 17 | + return JSON.parse(output); |
| 18 | + } catch (error) { |
| 19 | + console.error('Failed to get package list from lerna:', error.message); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function runAttwForPackage(packagePath) { |
| 25 | + try { |
| 26 | + const output = execSync('npx attw --pack . -f json', { |
| 27 | + cwd: packagePath, |
| 28 | + encoding: 'utf8', |
| 29 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 30 | + }); |
| 31 | + |
| 32 | + // Parse the JSON output (handle pretty-printed multi-line JSON) |
| 33 | + return JSON.parse(output); |
| 34 | + } catch (error) { |
| 35 | + // attw may exit with non-zero code if problems are found |
| 36 | + // but the output should still be valid JSON |
| 37 | + if (error.stdout) { |
| 38 | + try { |
| 39 | + return JSON.parse(error.stdout); |
| 40 | + } catch { |
| 41 | + console.error(`Failed to parse ATTW output for ${packagePath}:`, error.message); |
| 42 | + return null; |
| 43 | + } |
| 44 | + } |
| 45 | + console.error(`Failed to run ATTW for ${packagePath}:`, error.message); |
| 46 | + return null; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function main() { |
| 51 | + console.log('Getting package list...'); |
| 52 | + const packages = getPackages(); |
| 53 | + console.log(`Found ${packages.length} packages`); |
| 54 | + |
| 55 | + const results = []; |
| 56 | + let checked = 0; |
| 57 | + let skipped = 0; |
| 58 | + |
| 59 | + for (const pkg of packages) { |
| 60 | + // Skip private packages |
| 61 | + if (pkg.private) { |
| 62 | + console.log(`Skipping private package: ${pkg.name}`); |
| 63 | + skipped++; |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + console.log(`Checking ${pkg.name}...`); |
| 68 | + const result = runAttwForPackage(pkg.location); |
| 69 | + |
| 70 | + if (result) { |
| 71 | + results.push(result); |
| 72 | + checked++; |
| 73 | + } else { |
| 74 | + skipped++; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Write the results as a proper JSON array |
| 79 | + const outputPath = path.join(process.cwd(), 'attw-results.json'); |
| 80 | + fs.writeFileSync(outputPath, JSON.stringify(results, null, 2), 'utf8'); |
| 81 | + |
| 82 | + console.log(`\n✅ ATTW check complete!`); |
| 83 | + console.log(` Packages checked: ${checked}`); |
| 84 | + console.log(` Packages skipped: ${skipped}`); |
| 85 | + console.log(` Results written to: ${outputPath}`); |
| 86 | + |
| 87 | + // Exit with error code if any package has problems |
| 88 | + const packagesWithProblems = results.filter(r => |
| 89 | + r.problems && Object.keys(r.problems).length > 0 |
| 90 | + ).length; |
| 91 | + |
| 92 | + if (packagesWithProblems > 0) { |
| 93 | + console.log(`\n⚠️ ${packagesWithProblems} package(s) have type issues`); |
| 94 | + process.exit(1); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +main(); |
0 commit comments