@@ -29,6 +29,16 @@ import { Spinner } from '../utilities/spinner';
29
29
import { isTTY } from '../utilities/tty' ;
30
30
import { Schema as AddCommandSchema } from './add' ;
31
31
32
+ /**
33
+ * The set of packages that should have certain versions excluded from consideration
34
+ * when attempting to find a compatible version for a package.
35
+ * The key is a package name and the value is a SemVer range of versions to exclude.
36
+ */
37
+ const packageVersionExclusions : Record < string , string | undefined > = {
38
+ // @angular /localize@9.x versions do not have peer dependencies setup
39
+ '@angular/localize' : '9.x' ,
40
+ } ;
41
+
32
42
export class AddCommand extends SchematicCommand < AddCommandSchema > {
33
43
override readonly allowPrivateSchematics = true ;
34
44
@@ -40,6 +50,7 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
40
50
}
41
51
}
42
52
53
+ // eslint-disable-next-line max-lines-per-function
43
54
async run ( options : AddCommandSchema & Arguments ) {
44
55
await ensureCompatibleNpm ( this . context . root ) ;
45
56
@@ -100,7 +111,13 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
100
111
return 1 ;
101
112
}
102
113
114
+ // Start with the version tagged as `latest` if it exists
103
115
const latestManifest = packageMetadata . tags [ 'latest' ] ;
116
+ if ( latestManifest ) {
117
+ packageIdentifier = npa . resolve ( latestManifest . name , latestManifest . version ) ;
118
+ }
119
+
120
+ // Adjust the version based on name and peer dependencies
104
121
if ( latestManifest && Object . keys ( latestManifest . peerDependencies ) . length === 0 ) {
105
122
if ( latestManifest . name === '@angular/pwa' ) {
106
123
const version = await this . findProjectVersion ( '@angular/cli' ) ;
@@ -113,38 +130,52 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
113
130
) {
114
131
packageIdentifier = npa . resolve ( '@angular/pwa' , '0.12' ) ;
115
132
}
116
- } else {
117
- packageIdentifier = npa . resolve ( latestManifest . name , latestManifest . version ) ;
118
133
}
134
+
119
135
spinner . succeed (
120
136
`Found compatible package version: ${ colors . grey ( packageIdentifier . toString ( ) ) } .` ,
121
137
) ;
122
138
} else if ( ! latestManifest || ( await this . hasMismatchedPeer ( latestManifest ) ) ) {
123
139
// 'latest' is invalid so search for most recent matching package
140
+ const versionExclusions = packageVersionExclusions [ packageMetadata . name ] ;
124
141
const versionManifests = Object . values ( packageMetadata . versions ) . filter (
125
- ( value : PackageManifest ) => ! prerelease ( value . version ) && ! value . deprecated ,
142
+ ( value : PackageManifest ) => {
143
+ // Prerelease versions are not stable and should not be considered by default
144
+ if ( prerelease ( value . version ) ) {
145
+ return false ;
146
+ }
147
+ // Deprecated versions should not be used or considered
148
+ if ( value . deprecated ) {
149
+ return false ;
150
+ }
151
+ // Excluded package versions should not be considered
152
+ if ( versionExclusions && satisfies ( value . version , versionExclusions ) ) {
153
+ return false ;
154
+ }
155
+
156
+ return true ;
157
+ } ,
126
158
) ;
127
159
128
160
versionManifests . sort ( ( a , b ) => rcompare ( a . version , b . version , true ) ) ;
129
161
130
162
let newIdentifier ;
131
163
for ( const versionManifest of versionManifests ) {
132
164
if ( ! ( await this . hasMismatchedPeer ( versionManifest ) ) ) {
133
- newIdentifier = npa . resolve ( packageIdentifier . name , versionManifest . version ) ;
165
+ newIdentifier = npa . resolve ( versionManifest . name , versionManifest . version ) ;
134
166
break ;
135
167
}
136
168
}
137
169
138
170
if ( ! newIdentifier ) {
139
- spinner . warn ( "Unable to find compatible package. Using 'latest'." ) ;
171
+ spinner . warn ( "Unable to find compatible package. Using 'latest' tag ." ) ;
140
172
} else {
141
173
packageIdentifier = newIdentifier ;
142
174
spinner . succeed (
143
175
`Found compatible package version: ${ colors . grey ( packageIdentifier . toString ( ) ) } .` ,
144
176
) ;
145
177
}
146
178
} else {
147
- packageIdentifier = npa . resolve ( latestManifest . name , latestManifest . version ) ;
148
179
spinner . succeed (
149
180
`Found compatible package version: ${ colors . grey ( packageIdentifier . toString ( ) ) } .` ,
150
181
) ;
0 commit comments