@@ -2,65 +2,30 @@ import Route from '@ember/routing/route';
2
2
import { inject as service } from '@ember/service' ;
3
3
4
4
import * as Sentry from '@sentry/browser' ;
5
- import prerelease from 'semver/functions/prerelease' ;
6
5
7
6
import { AjaxError } from '../../utils/ajax' ;
8
7
9
- function isUnstableVersion ( version ) {
10
- return ! ! prerelease ( version ) ;
11
- }
12
-
13
8
export default class VersionRoute extends Route {
14
9
@service notifications ;
15
10
16
11
async model ( params ) {
17
- const requestedVersion = params . version_num ;
18
- const crate = this . modelFor ( 'crate' ) ;
19
- const maxVersion = crate . max_version ;
20
-
12
+ let crate = this . modelFor ( 'crate' ) ;
21
13
let versions = await crate . get ( 'versions' ) ;
22
14
23
- // Fallback to the crate's last stable version
24
- // If `max_version` is `0.0.0` then all versions have been yanked
25
- if ( ! params . version_num && maxVersion !== '0.0.0' ) {
26
- if ( isUnstableVersion ( maxVersion ) ) {
27
- // Find the latest version that is stable AND not-yanked.
28
- const latestStableVersion = versions . find ( version => ! isUnstableVersion ( version . num ) && ! version . yanked ) ;
29
-
30
- if ( latestStableVersion == null ) {
31
- // Cannot find any version that is stable AND not-yanked.
32
- // The fact that "maxVersion" itself cannot be found means that
33
- // we have to fall back to the latest one that is unstable....
34
-
35
- // Find the latest version that not yanked.
36
- const latestUnyankedVersion = versions . find ( version => ! version . yanked ) ;
37
-
38
- if ( latestUnyankedVersion == null ) {
39
- // There's not even any unyanked version...
40
- params . version_num = maxVersion ;
41
- } else {
42
- params . version_num = latestUnyankedVersion . num ;
43
- }
44
- } else {
45
- params . version_num = latestStableVersion . num ;
46
- }
47
- } else {
48
- params . version_num = maxVersion ;
15
+ let version ;
16
+ let requestedVersion = params . version_num ;
17
+ if ( requestedVersion ) {
18
+ version = versions . find ( version => version . num === requestedVersion ) ;
19
+ if ( ! version ) {
20
+ this . notifications . error ( `Version '${ requestedVersion } ' of crate '${ crate . name } ' does not exist` ) ;
21
+ this . replaceWith ( 'crate.index' ) ;
49
22
}
23
+ } else {
24
+ let { defaultVersion } = crate ;
25
+ version = versions . find ( version => version . num === defaultVersion ) ?? versions . lastObject ;
50
26
}
51
27
52
- const version = versions . find ( version => version . num === params . version_num ) ;
53
- if ( params . version_num && ! version ) {
54
- this . notifications . error ( `Version '${ params . version_num } ' of crate '${ crate . name } ' does not exist` ) ;
55
- this . replaceWith ( 'crate.index' ) ;
56
- return ;
57
- }
58
-
59
- return {
60
- crate,
61
- requestedVersion,
62
- version : version || versions . find ( version => version . num === maxVersion ) || versions . objectAt ( 0 ) ,
63
- } ;
28
+ return { crate, requestedVersion, version } ;
64
29
}
65
30
66
31
setupController ( controller , model ) {
0 commit comments