|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +const witPath = (proposal, version) => { |
| 7 | + if (version === '0.2') return `proposals/${proposal}/wit`; |
| 8 | + if (version === '0.3') return `proposals/${proposal}/wit-0.3.0-draft`; |
| 9 | + throw new Error(`Unknown version: ${version}`); |
| 10 | +}; |
| 11 | + |
| 12 | +const parseFiles = (filesJson) => { |
| 13 | + if (!filesJson || filesJson === 'null') return []; |
| 14 | + try { |
| 15 | + return JSON.parse(filesJson); |
| 16 | + } catch { |
| 17 | + return []; |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +const extractProposals = (files) => { |
| 22 | + const proposals = new Set(); |
| 23 | + for (const f of files) { |
| 24 | + const match = f.match(/^proposals\/([^/]+)\//); |
| 25 | + if (match) proposals.add(match[1]); |
| 26 | + } |
| 27 | + return [...proposals].sort(); |
| 28 | +}; |
| 29 | + |
| 30 | +const run = (cmd) => { |
| 31 | + console.log(` $ ${cmd}`); |
| 32 | + try { |
| 33 | + const output = execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }); |
| 34 | + if (output.trim()) { |
| 35 | + console.log(output); |
| 36 | + } |
| 37 | + return true; |
| 38 | + } catch (err) { |
| 39 | + if (err.stdout) console.log(err.stdout); |
| 40 | + if (err.stderr) console.error(err.stderr); |
| 41 | + console.error(` Exit code: ${err.status}`); |
| 42 | + return false; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +// Collect proposals to validate from changed files |
| 47 | +const toValidate = []; |
| 48 | +const filesByVersion = [ |
| 49 | + [process.env.WIT_02_FILES, '0.2'], |
| 50 | + [process.env.WIT_03_FILES, '0.3'], |
| 51 | +]; |
| 52 | + |
| 53 | +for (const [filesJson, version] of filesByVersion) { |
| 54 | + for (const proposal of extractProposals(parseFiles(filesJson))) { |
| 55 | + toValidate.push({ proposal, version }); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +if (toValidate.length === 0) { |
| 60 | + console.log('No proposals to validate'); |
| 61 | + process.exit(0); |
| 62 | +} |
| 63 | + |
| 64 | +let failed = false; |
| 65 | + |
| 66 | +for (const { proposal, version } of toValidate) { |
| 67 | + const witDir = witPath(proposal, version); |
| 68 | + console.log(`::group::Validating ${proposal} v${version}`); |
| 69 | + |
| 70 | + try { |
| 71 | + console.log(` Path: ${witDir}`); |
| 72 | + |
| 73 | + // Check wit-deps lock if deps.toml exists |
| 74 | + if (fs.existsSync(`${witDir}/deps.toml`)) { |
| 75 | + console.log(' Checking dependencies...'); |
| 76 | + if (!run(`wit-deps -m "${witDir}"/deps.toml -l "${witDir}"/deps.lock -d "${witDir}"/deps lock --check`)) { |
| 77 | + console.log(`::error::wit-deps lock check failed for ${proposal} v${version}`); |
| 78 | + failed = true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Validate WIT syntax |
| 83 | + console.log(' Validating WIT...'); |
| 84 | + if (!run(`wasm-tools component wit "${witDir}" -o /dev/null`)) { |
| 85 | + console.log(`::error::WIT validation failed for ${proposal} v${version}`); |
| 86 | + failed = true; |
| 87 | + } |
| 88 | + |
| 89 | + // Validate wasm encoding |
| 90 | + console.log(' Validating wasm encoding...'); |
| 91 | + if (!run(`wasm-tools component wit "${witDir}" --wasm -o /dev/null`)) { |
| 92 | + console.log(`::error::wasm encoding failed for ${proposal} v${version}`); |
| 93 | + failed = true; |
| 94 | + } |
| 95 | + } finally { |
| 96 | + console.log('::endgroup::'); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +if (failed) { |
| 101 | + console.log('\n❌ Validation failed'); |
| 102 | + process.exit(1); |
| 103 | +} else { |
| 104 | + console.log('\n✅ All proposals validated successfully'); |
| 105 | + process.exit(0); |
| 106 | +} |
0 commit comments