|
| 1 | +/** |
| 2 | + * Breaking changes data for migration paths |
| 3 | + * |
| 4 | + * Data structure: |
| 5 | + * - VERSIONS: Array of version strings derived from valid_migrations.yml |
| 6 | + * - VERSION_ORDER: Array of version strings in strict order (derived from VERSIONS) |
| 7 | + * - breakingChanges: Array of breaking change objects with: |
| 8 | + * - title: Display name of the breaking change |
| 9 | + * - url: Link to documentation |
| 10 | + * - introducedIn: Version where the breaking change was introduced |
| 11 | + * - affects (optional): Object with minSource and maxTarget versions |
| 12 | + * - minSource: Minimum source version affected |
| 13 | + * - maxTarget: Maximum target version affected |
| 14 | + * - comp: Array of components affected |
| 15 | + * - transformation (optional): Optional object with transformation information |
| 16 | + */ |
| 17 | + |
| 18 | +// Variables to store version ordering |
| 19 | +let VERSION_ORDER = []; |
| 20 | + |
| 21 | +// Version utility functions that use the dynamically determined version order |
| 22 | +export function getVersionIndex(version) { |
| 23 | + const index = VERSION_ORDER.indexOf(version); |
| 24 | + if (index === -1) throw new Error(`Unknown version: ${version}`); |
| 25 | + return index; |
| 26 | +} |
| 27 | + |
| 28 | +export function isVersionBetween(target, min, max) { |
| 29 | + const targetIdx = getVersionIndex(target); |
| 30 | + const minIdx = getVersionIndex(min); |
| 31 | + const maxIdx = getVersionIndex(max); |
| 32 | + return targetIdx >= minIdx && targetIdx <= maxIdx; |
| 33 | +} |
| 34 | + |
| 35 | +// Helper function to determine version ordering based on migration paths |
| 36 | +function determineVersionOrder(versions, migrationMap) { |
| 37 | + // Start with a copy of all versions |
| 38 | + const remainingVersions = [...versions]; |
| 39 | + const orderedVersions = []; |
| 40 | + |
| 41 | + // First, find versions that are only sources (not targets) |
| 42 | + // These are the oldest versions |
| 43 | + const onlySources = versions.filter(v => |
| 44 | + !Object.values(migrationMap.targetToSources).flat().includes(v) && |
| 45 | + migrationMap.sourceToTargets[v] |
| 46 | + ); |
| 47 | + |
| 48 | + // Add oldest versions first |
| 49 | + onlySources.forEach(v => { |
| 50 | + orderedVersions.push(v); |
| 51 | + const index = remainingVersions.indexOf(v); |
| 52 | + if (index !== -1) remainingVersions.splice(index, 1); |
| 53 | + }); |
| 54 | + |
| 55 | + // Then add the rest based on migration paths |
| 56 | + while (remainingVersions.length > 0) { |
| 57 | + let added = false; |
| 58 | + |
| 59 | + for (let i = 0; i < remainingVersions.length; i++) { |
| 60 | + const version = remainingVersions[i]; |
| 61 | + const sources = migrationMap.targetToSources[version] || []; |
| 62 | + |
| 63 | + // If all sources of this version are already in orderedVersions, |
| 64 | + // we can add this version next |
| 65 | + if (sources.every(s => orderedVersions.includes(s))) { |
| 66 | + orderedVersions.push(version); |
| 67 | + remainingVersions.splice(i, 1); |
| 68 | + added = true; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // If we couldn't add any version in this pass, there might be a cycle |
| 74 | + // Just add the first remaining version to break the cycle |
| 75 | + if (!added && remainingVersions.length > 0) { |
| 76 | + orderedVersions.push(remainingVersions[0]); |
| 77 | + remainingVersions.splice(0, 1); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return orderedVersions; |
| 82 | +} |
| 83 | + |
| 84 | +// Variables to store migration data |
| 85 | +let VALID_MIGRATIONS = {}; |
| 86 | +let VERSIONS = []; |
| 87 | +let MIGRATION_MAP = { |
| 88 | + sourceToTargets: {}, |
| 89 | + targetToSources: {} |
| 90 | +}; |
| 91 | + |
| 92 | +// Variables to store breaking changes data |
| 93 | +let breakingChanges = []; |
| 94 | + |
| 95 | +// Extract all unique versions for dropdowns |
| 96 | +function extractUniqueVersions(migrationsMap) { |
| 97 | + const versions = new Set(); |
| 98 | + |
| 99 | + // Add all sources |
| 100 | + Object.keys(migrationsMap).forEach(source => { |
| 101 | + versions.add(source); |
| 102 | + }); |
| 103 | + |
| 104 | + // Add all targets |
| 105 | + Object.values(migrationsMap).forEach(targets => { |
| 106 | + targets.forEach(target => { |
| 107 | + versions.add(target); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + return Array.from(versions).sort(); |
| 112 | +} |
| 113 | + |
| 114 | +// Function to generate the reverse mapping (target -> sources) |
| 115 | +function generateReverseMigrationMap(forwardMap) { |
| 116 | + const reverseMap = {}; |
| 117 | + |
| 118 | + Object.entries(forwardMap).forEach(([source, targets]) => { |
| 119 | + targets.forEach(target => { |
| 120 | + if (!reverseMap[target]) { |
| 121 | + reverseMap[target] = []; |
| 122 | + } |
| 123 | + reverseMap[target].push(source); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + return reverseMap; |
| 128 | +} |
| 129 | + |
| 130 | +// Function to initialize the migration data from the data attribute |
| 131 | +function initializeMigrationData() { |
| 132 | + const migrationDataElement = document.getElementById('migration-data'); |
| 133 | + |
| 134 | + if (migrationDataElement && migrationDataElement.dataset.migrationPaths) { |
| 135 | + try { |
| 136 | + // Parse the JSON data from the data attribute |
| 137 | + const migrationPaths = JSON.parse(migrationDataElement.dataset.migrationPaths); |
| 138 | + |
| 139 | + // Transform the data structure from YAML format to the expected JavaScript object format |
| 140 | + VALID_MIGRATIONS = migrationPaths.reduce((acc, path) => { |
| 141 | + acc[path.source] = path.targets; |
| 142 | + return acc; |
| 143 | + }, {}); |
| 144 | + |
| 145 | + // Now that we have the migration data, create the derived data structures |
| 146 | + VERSIONS = extractUniqueVersions(VALID_MIGRATIONS); |
| 147 | + MIGRATION_MAP = { |
| 148 | + sourceToTargets: VALID_MIGRATIONS, |
| 149 | + targetToSources: generateReverseMigrationMap(VALID_MIGRATIONS) |
| 150 | + }; |
| 151 | + |
| 152 | + // Determine version ordering based on migration paths |
| 153 | + VERSION_ORDER = determineVersionOrder(VERSIONS, MIGRATION_MAP); |
| 154 | + console.log('Determined version order:', VERSION_ORDER); |
| 155 | + } catch (error) { |
| 156 | + console.error('Failed to parse migration data:', error); |
| 157 | + // Fallback to empty object if parsing fails |
| 158 | + VALID_MIGRATIONS = {}; |
| 159 | + VERSIONS = []; |
| 160 | + MIGRATION_MAP = { sourceToTargets: {}, targetToSources: {} }; |
| 161 | + VERSION_ORDER = []; |
| 162 | + } |
| 163 | + } else { |
| 164 | + console.error('Migration data element not found or empty'); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// Function to initialize the breaking changes data from the data attribute |
| 169 | +function initializeBreakingChangesData() { |
| 170 | + const migrationDataElement = document.getElementById('migration-data'); |
| 171 | + |
| 172 | + if (!migrationDataElement || !migrationDataElement.dataset.breakingChanges) { |
| 173 | + console.error('Breaking changes data not found in migration-data element. Make sure to add data-breaking-changes attribute.'); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + try { |
| 178 | + // Parse the JSON data from the data attribute |
| 179 | + breakingChanges = JSON.parse(migrationDataElement.dataset.breakingChanges); |
| 180 | + console.log('Loaded breaking changes data:', breakingChanges.length); |
| 181 | + } catch (error) { |
| 182 | + console.error('Failed to parse breaking changes data:', error); |
| 183 | + // Fallback to empty array if parsing fails |
| 184 | + breakingChanges = []; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +// Initialize the data when the DOM is loaded |
| 189 | +document.addEventListener('DOMContentLoaded', () => { |
| 190 | + initializeMigrationData(); |
| 191 | + initializeBreakingChangesData(); |
| 192 | +}); |
| 193 | + |
| 194 | +// Export the breaking changes array for use in other modules |
| 195 | +export { breakingChanges }; |
0 commit comments