Skip to content

Commit

Permalink
feat: added Octopus CLI version fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed May 3, 2022
1 parent 393cafc commit 767b69f
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/octopusCLIVersionFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {maxSatisfying, valid} from 'semver'

export class OctopusCLIVersionFetcher {
constructor(readonly versions: string[]) {}

getVersion(versionSpec: string): string | null {
if (versionSpec === '*') {
return maxSatisfying(this.versions, versionSpec)
}

if (valid(versionSpec) === null) {
const parts = versionSpec.split('.')
if (parts.length > 3) {
throw new Error(
`The '${versionSpec}' is an invalid version, a version needs to be a maximum of three parts.`
)
}

if (parts.length === 1) {
const majorVersion = parts[0]
if (Number.isNaN(Number.parseInt(majorVersion))) {
throw new Error(
`The '${versionSpec}' version needs to specify a number or '*' for its major part.`
)
}
}

if (parts.length === 2) {
const majorVersion = parts[0]
const minorVersion = parts[1]

// the major version number must be a number
if (Number.isNaN(Number.parseInt(majorVersion))) {
throw new Error(
`The '${versionSpec}' version needs to specify a number for its major part.`
)
}

if (
minorVersion !== '*' &&
Number.isNaN(Number.parseInt(minorVersion))
) {
throw new Error(
`The '${versionSpec}' version needs to specify a number or '*' for its minor part.`
)
}
}

if (parts.length === 3) {
const majorVersion = parts[0]
const minorVersion = parts[1]
const patchVersion = parts[2]

// the major version number must be a number
if (Number.isNaN(Number.parseInt(majorVersion))) {
throw new Error(
`The '${versionSpec}' version needs to specify a number for its major part.`
)
}

// the minor version number must be a number
if (Number.isNaN(Number.parseInt(minorVersion))) {
throw new Error(
`The '${versionSpec}' version needs to specify a number for its minor part.`
)
}

if (
patchVersion !== '*' &&
Number.isNaN(Number.parseInt(patchVersion))
) {
throw new Error(
`The '${versionSpec}' version needs to specify a number or '*' for its patch part.`
)
}
}
}

const version = maxSatisfying(this.versions, versionSpec)

if (!version) {
throw new Error(
`A version satisfying '${versionSpec}' could not be found.`
)
}

return version
}
}

0 comments on commit 767b69f

Please sign in to comment.