|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const yaml = require('yaml'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Get the required Node.js version from package.json |
| 9 | + * @returns {string} The Node.js version number |
| 10 | + */ |
| 11 | +function getRequiredNodeVersion() { |
| 12 | + const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); |
| 13 | + return packageJson.engines.node.replace('>=', ''); |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Check Node.js version in .lando.yml |
| 18 | + * @param {string} requiredVersion - The required Node.js version |
| 19 | + * @returns {string|null} Mismatch message if version differs, null if matches |
| 20 | + */ |
| 21 | +function checkLandoFile(requiredVersion) { |
| 22 | + const landoPath = '.lando.yml'; |
| 23 | + const landoContent = fs.readFileSync(landoPath, 'utf8'); |
| 24 | + const landoConfig = yaml.parse(landoContent); |
| 25 | + |
| 26 | + const currentVersion = landoConfig.services.node.image.split(':')[1]; |
| 27 | + if (currentVersion !== requiredVersion) { |
| 28 | + return `- .lando.yml has node:${currentVersion}, expected node:${requiredVersion}`; |
| 29 | + } |
| 30 | + return null; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Check Node.js version in workflow files |
| 35 | + * @param {string} requiredVersion - The required Node.js version |
| 36 | + * @returns {string[]} Array of mismatch messages |
| 37 | + */ |
| 38 | +function checkWorkflowFiles(requiredVersion) { |
| 39 | + const mismatches = []; |
| 40 | + const workflowsDir = '.github/workflows'; |
| 41 | + |
| 42 | + const files = fs.readdirSync(workflowsDir) |
| 43 | + .filter(file => file.endsWith('.yml')); |
| 44 | + |
| 45 | + files.forEach(file => { |
| 46 | + const filePath = path.join(workflowsDir, file); |
| 47 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 48 | + const workflow = yaml.parse(content); |
| 49 | + |
| 50 | + const checkObject = obj => { |
| 51 | + for (const key in obj) { |
| 52 | + if (key === 'node-version' && typeof obj[key] === 'string') { |
| 53 | + const version = obj[key].replace(/['"]/g, ''); |
| 54 | + if (version !== requiredVersion) { |
| 55 | + mismatches.push(`- ${filePath} has Node.js ${version}, expected ${requiredVersion}`); |
| 56 | + } |
| 57 | + } else if (typeof obj[key] === 'object' && obj[key] !== null) { |
| 58 | + checkObject(obj[key]); |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + checkObject(workflow); |
| 64 | + }); |
| 65 | + |
| 66 | + return mismatches; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Main function to check Node.js versions |
| 71 | + */ |
| 72 | +function main() { |
| 73 | + try { |
| 74 | + const requiredVersion = getRequiredNodeVersion(); |
| 75 | + console.log(`Checking for Node.js version ${requiredVersion} across configuration files...`); |
| 76 | + |
| 77 | + const mismatches = []; |
| 78 | + |
| 79 | + const landoMismatch = checkLandoFile(requiredVersion); |
| 80 | + if (landoMismatch) mismatches.push(landoMismatch); |
| 81 | + |
| 82 | + const workflowMismatches = checkWorkflowFiles(requiredVersion); |
| 83 | + mismatches.push(...workflowMismatches); |
| 84 | + |
| 85 | + if (mismatches.length > 0) { |
| 86 | + console.error('\nNode.js version mismatches found:'); |
| 87 | + console.error(mismatches.join('\n')); |
| 88 | + console.error('\nPlease update all Node.js versions to match package.json'); |
| 89 | + process.exit(1); |
| 90 | + } else { |
| 91 | + console.log('All Node.js versions match package.json ✓'); |
| 92 | + process.exit(0); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + console.error('Error:', error); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +main(); |
0 commit comments