|
| 1 | +/* eslint-disable */ |
| 2 | +var changeTemplate = |
| 3 | + '<div class="change-container"><div class="change-header" id="change_rel_@@id-version@@">@@version@@</div><div class="change-content">@@changes@@</div></div>'; |
| 4 | +var commandAddedTemplate = |
| 5 | + '<div class="change-item-header">Commands Added</div><ul class="change-list">@@changes@@</ul>'; |
| 6 | +var commandRemovedTemplate = |
| 7 | + '<div class="change-item-header">Commands Removed</div><ul class="change-list">@@changes@@</ul>'; |
| 8 | +var commandValueChangedTemplate = |
| 9 | + '<div class="change-option-header">Command Values Changed</div><ul class="change-list">@@changes@@</ul>'; |
| 10 | +var optionAddedTemplate = |
| 11 | + '<div class="change-option-header">Options Added</div><ul class="change-list">@@changes@@</ul>'; |
| 12 | +var optionRemovedTemplate = |
| 13 | + '<div class="change-option-header">Options Removed</div><ul class="change-list">@@changes@@</ul>'; |
| 14 | +var optionChangedTemplate = |
| 15 | + '<div class="change-option-header">Options Changed</div><ul class="change-list">@@changes@@</ul>'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Returns variable name associate with an argument. |
| 19 | + * e.g. --service-principal-id -> servicePrincipalId |
| 20 | + * @param arg arugment |
| 21 | + */ |
| 22 | +function argToVariableName(arg) { |
| 23 | + var match = arg.match(/\s?--([-\w]+)\s?/); |
| 24 | + if (match) { |
| 25 | + return match[1] |
| 26 | + .replace(/\.?(-[a-z])/g, (_, y) => { |
| 27 | + return y.toUpperCase(); |
| 28 | + }) |
| 29 | + .replace(/-/g, ""); |
| 30 | + } |
| 31 | + return null; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Compare two versions to figure out what new commands are added |
| 36 | + * |
| 37 | + * @param prev Previous version |
| 38 | + * @param cur Current version |
| 39 | + */ |
| 40 | +function compareVersionDiff(prev, cur) { |
| 41 | + var prevKeys = Object.keys(prev); |
| 42 | + var results = Object.keys(cur).filter(function (k) { |
| 43 | + return prevKeys.indexOf(k) === -1; |
| 44 | + }); |
| 45 | + return results.length > 0 ? results : undefined; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Compare two set of options in the same command to figure out what new options are added. |
| 50 | + * |
| 51 | + * @param prev set of options in the same command in previous release |
| 52 | + * @param cur set of options in the same command in current release |
| 53 | + */ |
| 54 | +function compareArgsDiff(prev, cur) { |
| 55 | + var prevKeys = prev.map(function (opt) { |
| 56 | + return opt.arg; |
| 57 | + }); |
| 58 | + |
| 59 | + return cur |
| 60 | + .filter(function (opt) { |
| 61 | + return prevKeys.indexOf(opt.arg) === -1; |
| 62 | + }) |
| 63 | + .map(function (opt) { |
| 64 | + return opt.arg; |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Compare two set of options in the same command to figure out the changes. |
| 70 | + * New options added, Option removed, Option args changed. |
| 71 | + * |
| 72 | + * @param prev set of options in the same command in previous release |
| 73 | + * @param cur set of options in the same command in current release |
| 74 | + */ |
| 75 | +function compareArgsChanged(prev, cur) { |
| 76 | + var optionsPrev = prev.options || []; |
| 77 | + var optionsCur = cur.options || []; |
| 78 | + var changes = {}; |
| 79 | + var aliases = {}; |
| 80 | + var aliasChanged = []; |
| 81 | + var aliasesRm = {}; |
| 82 | + |
| 83 | + var removed = compareArgsDiff(optionsCur, optionsPrev); |
| 84 | + if (removed.length > 0) { |
| 85 | + removed.forEach(function (r) { |
| 86 | + var m = r.match(/^-([a-zA-Z]),\s/); |
| 87 | + if (m) { |
| 88 | + var varName = argToVariableName(r); |
| 89 | + aliases[varName] = m[1]; |
| 90 | + aliasesRm[varName] = r; |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + // to figure out the change in options by comparing |
| 96 | + // old vs new |
| 97 | + var added = (compareArgsDiff(optionsPrev, optionsCur) || []).filter(function ( |
| 98 | + add |
| 99 | + ) { |
| 100 | + var m = add.match(/^-([a-zA-Z]),\s/); |
| 101 | + if (m) { |
| 102 | + var varName = argToVariableName(add); |
| 103 | + if (varName in aliases) { |
| 104 | + var idx = removed.indexOf(aliasesRm[varName]); |
| 105 | + removed.splice(idx, 1); |
| 106 | + aliasChanged.push( |
| 107 | + 'change "' + aliasesRm[varName] + '" to "' + add + '"' |
| 108 | + ); |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + }); |
| 114 | + |
| 115 | + if (removed.length > 0) { |
| 116 | + changes.removed = removed; |
| 117 | + } |
| 118 | + |
| 119 | + if (added.length > 0) { |
| 120 | + changes.added = added; |
| 121 | + } |
| 122 | + |
| 123 | + if (aliasChanged.length > 0) { |
| 124 | + changes.changed = aliasChanged; |
| 125 | + } |
| 126 | + |
| 127 | + return Object.keys(changes).length > 0 ? changes : null; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Compare changes in two set of commands. one from previous |
| 132 | + * release and one from current release |
| 133 | + * |
| 134 | + * @param prev set of commands from previous release |
| 135 | + * @param cur set of commands from current release |
| 136 | + */ |
| 137 | +function compareVersionChanged(prev, cur) { |
| 138 | + var changes = {}; |
| 139 | + var curKeys = Object.keys(cur); |
| 140 | + var commonKeys = Object.keys(prev).filter(function (k) { |
| 141 | + return curKeys.indexOf(k) !== -1; |
| 142 | + }); |
| 143 | + |
| 144 | + commonKeys.forEach(function (k) { |
| 145 | + var newCmd = cur[k]; |
| 146 | + var prevCmd = prev[k]; |
| 147 | + var modified = {}; |
| 148 | + |
| 149 | + if (newCmd.command !== prevCmd.command) { |
| 150 | + modified["command"] = prevCmd.command + " to " + newCmd.command; |
| 151 | + } |
| 152 | + |
| 153 | + if (newCmd.alias !== prevCmd.alias) { |
| 154 | + modified["alias"] = { |
| 155 | + prev: prevCmd.alias, |
| 156 | + newCmd: newCmd.alias, |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + var optChanges = compareArgsChanged(prevCmd, newCmd); |
| 161 | + |
| 162 | + if (optChanges) { |
| 163 | + modified.options = optChanges; |
| 164 | + } |
| 165 | + |
| 166 | + if (Object.keys(modified).length > 0) { |
| 167 | + changes[k] = modified; |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + return Object.keys(changes).length > 0 ? changes : undefined; |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * Compare two releases/versions e.g. master against 0.6.0 |
| 176 | + * |
| 177 | + * @param prev previous version number e.g. 0.5.8 |
| 178 | + * @param cur current version number e.g. 0.5.9 |
| 179 | + */ |
| 180 | +function compareVersion(prev, cur) { |
| 181 | + var prevData = dataCache[prev]; |
| 182 | + var curData = dataCache[cur]; |
| 183 | + var data = {}; |
| 184 | + |
| 185 | + var added = compareVersionDiff(prevData, curData); |
| 186 | + if (added) { |
| 187 | + data.added = added; |
| 188 | + } |
| 189 | + |
| 190 | + var removed = compareVersionDiff(curData, prevData); |
| 191 | + if (removed) { |
| 192 | + data.removed = removed; |
| 193 | + } |
| 194 | + |
| 195 | + var changed = compareVersionChanged(prevData, curData); |
| 196 | + if (changed) { |
| 197 | + data.changed = changed; |
| 198 | + } |
| 199 | + |
| 200 | + return Object.keys(data).length > 0 ? data : undefined; |
| 201 | +} |
| 202 | + |
| 203 | +/** |
| 204 | + * Returns HTML for each command change |
| 205 | + * |
| 206 | + * @param oChanges changes information |
| 207 | + */ |
| 208 | +function getChangesHTML(oChanges) { |
| 209 | + changes = ""; |
| 210 | + if (oChanges.added) { |
| 211 | + changes = commandAddedTemplate.replace( |
| 212 | + "@@changes@@", |
| 213 | + oChanges.added |
| 214 | + .map(function (add) { |
| 215 | + return "<li>spk " + sanitize(add) + "</li>"; |
| 216 | + }) |
| 217 | + .join("") |
| 218 | + ); |
| 219 | + } |
| 220 | + if (oChanges.removed) { |
| 221 | + changes += commandRemovedTemplate.replace( |
| 222 | + "@@changes@@", |
| 223 | + oChanges.removed |
| 224 | + .map(function (rm) { |
| 225 | + return "<li>spk " + sanitize(rm) + "</li>"; |
| 226 | + }) |
| 227 | + .join("") |
| 228 | + ); |
| 229 | + } |
| 230 | + if (oChanges.changed) { |
| 231 | + var optionChanges = ""; |
| 232 | + Object.keys(oChanges.changed).forEach(function (k) { |
| 233 | + optionChanges += |
| 234 | + '<div class="option-change"><div class="option-change-title">' + |
| 235 | + k + |
| 236 | + "</div>"; |
| 237 | + |
| 238 | + if (oChanges.changed[k].command) { |
| 239 | + optionChanges += commandValueChangedTemplate.replace( |
| 240 | + "@@changes@@", |
| 241 | + sanitize(oChanges.changed[k].command) |
| 242 | + ); |
| 243 | + } |
| 244 | + |
| 245 | + if (oChanges.changed[k].options) { |
| 246 | + var options = oChanges.changed[k].options; |
| 247 | + |
| 248 | + if (options.added) { |
| 249 | + optionChanges += optionAddedTemplate.replace( |
| 250 | + "@@changes@@", |
| 251 | + options.added |
| 252 | + .map(function (add) { |
| 253 | + return "<li>" + sanitize(add) + "</li>"; |
| 254 | + }) |
| 255 | + .join("") |
| 256 | + ); |
| 257 | + } |
| 258 | + if (options.removed) { |
| 259 | + optionChanges += optionRemovedTemplate.replace( |
| 260 | + "@@changes@@", |
| 261 | + options.removed |
| 262 | + .map(function (rm) { |
| 263 | + return "<li>" + sanitize(rm) + "</li>"; |
| 264 | + }) |
| 265 | + .join("") |
| 266 | + ); |
| 267 | + } |
| 268 | + if (options.changed) { |
| 269 | + optionChanges += optionChangedTemplate.replace( |
| 270 | + "@@changes@@", |
| 271 | + options.changed |
| 272 | + .map(function (chg) { |
| 273 | + return "<li>" + sanitize(chg) + "</li>"; |
| 274 | + }) |
| 275 | + .join("") |
| 276 | + ); |
| 277 | + } |
| 278 | + } |
| 279 | + optionChanges += "</div>"; |
| 280 | + }); |
| 281 | + |
| 282 | + return ( |
| 283 | + changes + |
| 284 | + '<div class="change-item-header">Commands Changed</div>' + |
| 285 | + optionChanges |
| 286 | + ); |
| 287 | + } |
| 288 | +} |
| 289 | + |
| 290 | +/** |
| 291 | + * Insert HTML element for command changes DIV. |
| 292 | + */ |
| 293 | +function compareVersions() { |
| 294 | + var versions = Object.keys(dataCache); |
| 295 | + versions.sort(); |
| 296 | + var cur = versions.shift(); |
| 297 | + var dataChanges = {}; |
| 298 | + |
| 299 | + versions.forEach(function (ver) { |
| 300 | + dataChanges[ver] = compareVersion(cur, ver); |
| 301 | + cur = ver; |
| 302 | + }); |
| 303 | + versions.reverse(); |
| 304 | + |
| 305 | + $("#changes").append( |
| 306 | + versions |
| 307 | + .map(function (v) { |
| 308 | + var oChanges = dataChanges[v]; |
| 309 | + var changes = oChanges ? getChangesHTML(oChanges) : "no changes"; |
| 310 | + return changeTemplate |
| 311 | + .replace(/@@id-version@@/g, v.replace(/\./g, "_")) |
| 312 | + .replace(/@@version@@/g, v) |
| 313 | + .replace("@@changes@@", changes); |
| 314 | + }) |
| 315 | + .join("") |
| 316 | + ); |
| 317 | + if (window.location.hash && window.location.hash.startsWith("#change_rel_")) { |
| 318 | + try { |
| 319 | + var oDiv = $(window.location.hash); |
| 320 | + if (oDiv && oDiv[0]) { |
| 321 | + oDiv[0].scrollIntoView(); |
| 322 | + } |
| 323 | + } catch (e) { |
| 324 | + console.log(e); |
| 325 | + } |
| 326 | + } |
| 327 | + $(".change-header").click(function () { |
| 328 | + window.location.hash = "#" + $(this).prop("id"); |
| 329 | + }); |
| 330 | +} |
0 commit comments