Skip to content

Commit

Permalink
fix(spaces-delete): include filter to remove non string to sort
Browse files Browse the repository at this point in the history
Avoid TypeError for invalid version null when trying to compare with semver.lt or semver.gt
  • Loading branch information
marceloavf committed Aug 7, 2018
1 parent 3556fa2 commit 0d1b65d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Tasks/DigitalOceanSpacesDelete/utils/Delete.ts
Expand Up @@ -5,6 +5,7 @@ import * as semver from 'semver'
import * as tl from 'vsts-task-lib'
import { Spaces } from '../common/Spaces'
import { Parameters } from './Parameters'
import { Sort } from './Enums'

export class Delete extends Spaces<Parameters> {
constructor(params: Parameters) {
Expand Down Expand Up @@ -140,12 +141,16 @@ export class Delete extends Spaces<Parameters> {
const versionList: string[] = sortedUniq(
listedObjects.Contents.map(obj => {
return semver.valid(semver.coerce(obj.Key))
}).sort((a, b) => {
// Sort all version from oldest to newest one
if (semver.lt(a, b)) return -1
if (semver.gt(a, b)) return 1
return 0
})
.filter(item => {
return typeof item === 'string'
})
.sort((a, b) => {
// Sort all version from oldest to newest one
if (semver.lt(a, b)) return Sort.aBiggerThanB
if (semver.gt(a, b)) return Sort.bBiggerThanA
return Sort.aEqualToB
})
)

// Will remove from right to left the number of versions to keep intact
Expand Down
5 changes: 5 additions & 0 deletions Tasks/DigitalOceanSpacesDelete/utils/Enums.ts
@@ -0,0 +1,5 @@
export enum Sort {
aBiggerThanB = -1,
aEqualToB,
bBiggerThanA,
}

0 comments on commit 0d1b65d

Please sign in to comment.