(ver: string)
| 31 | } |
| 32 | |
| 33 | export function parseMajorVersion(ver: string): number | undefined { |
| 34 | let version = ver; |
| 35 | // if it has a `v` prefix, remove it |
| 36 | if (version.startsWith('v')) { |
| 37 | version = version.slice(1); |
| 38 | } |
| 39 | |
| 40 | // First, try simple lookup of exact, ~ and ^ versions |
| 41 | const regex = /^[\^~]?(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 42 | |
| 43 | const match = version.match(regex); |
| 44 | if (match) { |
| 45 | return parseInt(match[1] as string, 10); |
| 46 | } |
| 47 | |
| 48 | // Try to parse e.g. 1.x |
| 49 | const coerced = parseInt(version, 10); |
| 50 | if (!Number.isNaN(coerced)) { |
| 51 | return coerced; |
| 52 | } |
| 53 | |
| 54 | // Match <= and >= ranges. |
| 55 | const gteLteRegex = /^[<>]=\s*(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 56 | const gteLteMatch = version.match(gteLteRegex); |
| 57 | if (gteLteMatch) { |
| 58 | return parseInt(gteLteMatch[1] as string, 10); |
| 59 | } |
| 60 | |
| 61 | // match < ranges |
| 62 | const ltRegex = /^<\s*(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 63 | const ltMatch = version.match(ltRegex); |
| 64 | if (ltMatch) { |
| 65 | // Two scenarios: |
| 66 | // a) < 2.0.0 --> return 1 |
| 67 | // b) < 2.1.0 --> return 2 |
| 68 | |
| 69 | const major = parseInt(ltMatch[1] as string, 10); |
| 70 | |
| 71 | if ( |
| 72 | // minor version > 0 |
| 73 | (typeof ltMatch[2] === 'string' && parseInt(ltMatch[2].slice(1), 10) > 0) || |
| 74 | // patch version > 0 |
| 75 | (typeof ltMatch[3] === 'string' && parseInt(ltMatch[3].slice(1), 10) > 0) |
| 76 | ) { |
| 77 | return major; |
| 78 | } |
| 79 | |
| 80 | return major - 1; |
| 81 | } |
| 82 | |
| 83 | // match > ranges |
| 84 | const gtRegex = /^>\s*(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 85 | const gtMatch = version.match(gtRegex); |
| 86 | if (gtMatch) { |
| 87 | // We always return the version here, even though it _may_ be incorrect |
| 88 | // E.g. if given > 2.0.0, it should be 2 if there exists any 2.x.x version, else 3 |
| 89 | // Since there is no way for us to know this, we're going to assume any kind of patch/feature release probably exists |
| 90 | return parseInt(gtMatch[1] as string, 10); |
no test coverage detected