|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { padLeft } from 'lodash'; |
| 8 | + |
| 9 | +/* |
| 10 | + * The logic for ID is: XXYYZZAA, where XX is major version, YY is minor |
| 11 | + * version, ZZ is revision, and AA is alpha/beta/rc indicator. |
| 12 | + * |
| 13 | + * AA values below 25 are for alpha builder (since 5.0), and above 25 and below |
| 14 | + * 50 are beta builds, and below 99 are RC builds, with 99 indicating a release |
| 15 | + * the (internal) format of the id is there so we can easily do after/before |
| 16 | + * checks on the id |
| 17 | + * |
| 18 | + * Note: the conversion method is carried over from Elasticsearch: |
| 19 | + * https://github.com/elastic/elasticsearch/blob/de962b2/server/src/main/java/org/elasticsearch/Version.java |
| 20 | + */ |
| 21 | +export function getTemplateVersion(versionStr: string): number { |
| 22 | + // break up the string parts |
| 23 | + const splitted = versionStr.split('.'); |
| 24 | + const minorStr = splitted[2] || ''; |
| 25 | + |
| 26 | + // pad each part with leading 0 to make 2 characters |
| 27 | + const padded = splitted.map((v: string) => { |
| 28 | + const vMatches = v.match(/\d+/); |
| 29 | + if (vMatches) { |
| 30 | + return padLeft(vMatches[0], 2, '0'); |
| 31 | + } |
| 32 | + return '00'; |
| 33 | + }); |
| 34 | + const [majorV, minorV, patchV] = padded; |
| 35 | + |
| 36 | + // append the alpha/beta/rc indicator |
| 37 | + let buildV; |
| 38 | + if (minorStr.match('alpha')) { |
| 39 | + const matches = minorStr.match(/alpha(?<alpha>\d+)/); |
| 40 | + if (matches != null && matches.groups != null) { |
| 41 | + const alphaVerInt = parseInt(matches.groups.alpha, 10); // alpha build indicator |
| 42 | + buildV = padLeft(`${alphaVerInt}`, 2, '0'); |
| 43 | + } |
| 44 | + } else if (minorStr.match('beta')) { |
| 45 | + const matches = minorStr.match(/beta(?<beta>\d+)/); |
| 46 | + if (matches != null && matches.groups != null) { |
| 47 | + const alphaVerInt = parseInt(matches.groups.beta, 10) + 25; // beta build indicator |
| 48 | + buildV = padLeft(`${alphaVerInt}`, 2, '0'); |
| 49 | + } |
| 50 | + } else { |
| 51 | + buildV = '99'; // release build indicator |
| 52 | + } |
| 53 | + |
| 54 | + const joinedParts = [majorV, minorV, patchV, buildV].join(''); |
| 55 | + return parseInt(joinedParts, 10); |
| 56 | +} |
0 commit comments