|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Generate E2E test matrix by creating cross-product of locales and shards |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * node generate-e2e-matrix.js '<locale-matrix-json>' <shard-total> |
| 8 | + * |
| 9 | + * Example: |
| 10 | + * node generate-e2e-matrix.js '[{"locale":"zh-hans","secret_project_id":"VERCEL_PROJECT_ID_ZH_HANS"},{"locale":"zh-hant","secret_project_id":"VERCEL_PROJECT_ID_ZH_HANT"}]' 5 |
| 11 | + */ |
| 12 | + |
| 13 | +function generateE2EMatrix(localesJson, shardTotal) { |
| 14 | + try { |
| 15 | + // Parse the locales input |
| 16 | + const locales = JSON.parse(localesJson); |
| 17 | + const shards = Array.from({ length: shardTotal }, (_, i) => i + 1); |
| 18 | + |
| 19 | + // Generate cross-product of locales and shards |
| 20 | + const matrix = []; |
| 21 | + |
| 22 | + for (const locale of locales) { |
| 23 | + for (const shard of shards) { |
| 24 | + matrix.push({ |
| 25 | + ...locale, |
| 26 | + shard: shard, |
| 27 | + }); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const result = { |
| 32 | + include: matrix, |
| 33 | + }; |
| 34 | + |
| 35 | + return result; |
| 36 | + } catch (error) { |
| 37 | + console.error('Error generating matrix:', error.message); |
| 38 | + throw error; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Main execution |
| 43 | +if (require.main === module) { |
| 44 | + const args = process.argv.slice(2); |
| 45 | + |
| 46 | + if (args.length !== 2) { |
| 47 | + console.error( |
| 48 | + 'Usage: node generate-e2e-matrix.js <locale-matrix-json> <shard-total>', |
| 49 | + ); |
| 50 | + console.error( |
| 51 | + 'Example: node generate-e2e-matrix.js \'[{"locale":"zh-hans"}]\' 3', |
| 52 | + ); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + const [localesJson, shardTotalStr] = args; |
| 57 | + const shardTotal = Number.parseInt(shardTotalStr, 10); |
| 58 | + |
| 59 | + if (Number.isNaN(shardTotal) || shardTotal < 1) { |
| 60 | + console.error('Error: shard-total must be a positive integer'); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + const matrix = generateE2EMatrix(localesJson, shardTotal); |
| 66 | + |
| 67 | + // Output results in GitHub Actions format (same as prerelease matrix script) |
| 68 | + console.log(`test-matrix=${JSON.stringify(matrix)}`); |
| 69 | + } catch (error) { |
| 70 | + console.error('Failed to generate matrix:', error.message); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +module.exports = { generateE2EMatrix }; |
0 commit comments