forked from oclif/plugin-update
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cf204a
commit 9ac3c71
Showing
5 changed files
with
177 additions
and
19 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as semver from 'semver'; | ||
|
||
const EXTENDED_SEMVER_REGEX = | ||
/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?/; | ||
|
||
const SEMVER_REGEX = /^(\d+)\.(\d+)\.(\d+)$/; | ||
|
||
const FUZZY_SEMVER_REGEX = /^(\d+)\.?(\d+)?\.?(\d+)?$/; | ||
|
||
const getMatchingVersions = async ( | ||
versionList: string[], | ||
targetVersion: string, | ||
channel: string, | ||
prereleaseChannels: string[], | ||
): Promise<string[]> => { | ||
const isTargetExplicitSemver = FUZZY_SEMVER_REGEX.test(targetVersion); | ||
|
||
return versionList | ||
.filter((version) => { | ||
// - If the version contains 'partial', ignore it | ||
if (version.includes('partial')) { | ||
return false; | ||
} | ||
|
||
if (isTargetExplicitSemver && SEMVER_REGEX.test(version)) { | ||
// Ex: 2.1 should capture 2.1.3, 2.1.4, 2.1.5, but not 2.2.0 | ||
return version.startsWith(targetVersion); | ||
} | ||
// - If we request stable, only provide standard versions... | ||
if (channel === 'stable') { | ||
return !prereleaseChannels.some((c) => version.includes(c)); | ||
} | ||
// - ... otherwise check if the version is contained | ||
return version.includes(targetVersion); | ||
}) | ||
.sort((a, b) => semver.compare(b, a)); | ||
}; | ||
|
||
export { | ||
getMatchingVersions, | ||
SEMVER_REGEX, | ||
EXTENDED_SEMVER_REGEX, | ||
FUZZY_SEMVER_REGEX, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { getMatchingVersions } from '../../src/utils/version-calculation'; | ||
|
||
const PRERELEASE_CHANNELS = ['alpha', 'next']; | ||
describe('Version Calculation', () => { | ||
it('fetches a channel-specific set of non-partial channel versions', async () => { | ||
const versionMatch = await getMatchingVersions( | ||
[ | ||
'3.0.0-next.0', | ||
'3.0.0-next.1', | ||
'2.4.0', | ||
'3.0.0-next.3', | ||
'3.0.0-next.4', | ||
'3.0.0-alpha.4', | ||
], | ||
'next', | ||
'next', | ||
PRERELEASE_CHANNELS, | ||
); | ||
expect(versionMatch).toStrictEqual([ | ||
'3.0.0-next.4', | ||
'3.0.0-next.3', | ||
'3.0.0-next.1', | ||
'3.0.0-next.0', | ||
]); | ||
}); | ||
|
||
it('only fetches semvers when stable channel is provided', async () => { | ||
const versionMatch = await getMatchingVersions( | ||
['2.4.0', '2.6.0', '2.0.0-next.3', '3.0.0-alpha.5'], | ||
'2.6.0', | ||
'stable', | ||
PRERELEASE_CHANNELS, | ||
); | ||
|
||
expect(versionMatch).toStrictEqual(['2.6.0']); | ||
}); | ||
|
||
it('only fetches semvers when stable channel is targeted', async () => { | ||
const versionMatch = await getMatchingVersions( | ||
['2.4.0', '2.6.0', '2.0.0-next.3', '3.0.0-alpha.5'], | ||
'stable', | ||
'stable', | ||
PRERELEASE_CHANNELS, | ||
); | ||
|
||
expect(versionMatch).toStrictEqual(['2.6.0', '2.4.0']); | ||
}); | ||
|
||
it('Will search for up to the latest minor', async () => { | ||
const versionMatch = await getMatchingVersions( | ||
[ | ||
'2.4.0', | ||
'2.4.1', | ||
'2.6.1', | ||
'2.4.2', | ||
'2.6.0', | ||
'2.0.0-next.3', | ||
'3.0.0-alpha.5', | ||
], | ||
'2.4', | ||
'stable', | ||
PRERELEASE_CHANNELS, | ||
); | ||
|
||
expect(versionMatch).toStrictEqual(['2.4.2', '2.4.1', '2.4.0']); | ||
}); | ||
}); |