@@ -42,19 +42,26 @@ export class NpmInstallationManager implements INpmInstallationManager {
42
42
return path . join ( this . getCacheRootPath ( ) , packageName , version , "package" ) ;
43
43
}
44
44
45
- public addToCache ( packageName : string , version : string ) : IFuture < void > {
45
+ public addToCache ( packageName : string , version : string ) : IFuture < any > {
46
46
return ( ( ) => {
47
47
let cachedPackagePath = this . getCachedPackagePath ( packageName , version ) ;
48
+ let cachedPackageData : any ;
48
49
if ( ! this . $fs . exists ( cachedPackagePath ) . wait ( ) || ! this . $fs . exists ( path . join ( cachedPackagePath , "framework" ) ) . wait ( ) ) {
49
- this . addToCacheCore ( packageName , version ) . wait ( ) ;
50
+ cachedPackageData = this . addToCacheCore ( packageName , version ) . wait ( ) ;
50
51
}
51
52
52
- if ( ! this . isShasumOfPackageCorrect ( packageName , version ) . wait ( ) ) {
53
+ // In case the version is tag (for example `next`), we need the real version number from the cache.
54
+ // In these cases the cachePackageData is populated when data is added to the cache.
55
+ // Also whenever the version is tag, we always get inside the above `if` and the cachedPackageData is populated.
56
+ let realVersion = ( cachedPackageData && cachedPackageData . version ) || version ;
57
+ if ( ! this . isShasumOfPackageCorrect ( packageName , realVersion ) . wait ( ) ) {
53
58
// In some cases the package is not fully downloaded and there are missing directories
54
59
// Try removing the old package and add the real one to cache again
55
- this . addCleanCopyToCache ( packageName , version ) . wait ( ) ;
60
+ cachedPackageData = this . addCleanCopyToCache ( packageName , version ) . wait ( ) ;
56
61
}
57
- } ) . future < void > ( ) ( ) ;
62
+
63
+ return cachedPackageData ;
64
+ } ) . future < any > ( ) ( ) ;
58
65
}
59
66
60
67
public cacheUnpack ( packageName : string , version : string , unpackTarget ?: string ) : IFuture < void > {
@@ -125,26 +132,29 @@ export class NpmInstallationManager implements INpmInstallationManager {
125
132
} ) . future < string > ( ) ( ) ;
126
133
}
127
134
128
- public addCleanCopyToCache ( packageName : string , version : string ) : IFuture < void > {
135
+ private addCleanCopyToCache ( packageName : string , version : string ) : IFuture < any > {
129
136
return ( ( ) => {
130
137
let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , version ) ;
131
138
this . $logger . trace ( `Deleting: ${ packagePath } .` ) ;
132
139
this . $fs . deleteDirectory ( packagePath ) . wait ( ) ;
133
- this . addToCacheCore ( packageName , version ) . wait ( ) ;
134
- if ( ! this . isShasumOfPackageCorrect ( packageName , version ) . wait ( ) ) {
135
- this . $errors . failWithoutHelp ( `Unable to add package ${ packageName } with version ${ version } to npm cache. Try cleaning your cache and execute the command again.` ) ;
140
+ let cachedPackageData = this . addToCacheCore ( packageName , version ) . wait ( ) ;
141
+ if ( ! this . isShasumOfPackageCorrect ( packageName , cachedPackageData . version ) . wait ( ) ) {
142
+ this . $errors . failWithoutHelp ( `Unable to add package ${ packageName } with version ${ cachedPackageData . version } to npm cache. Try cleaning your cache and execute the command again.` ) ;
136
143
}
137
- } ) . future < void > ( ) ( ) ;
144
+
145
+ return cachedPackageData ;
146
+ } ) . future < any > ( ) ( ) ;
138
147
}
139
148
140
- private addToCacheCore ( packageName : string , version : string ) : IFuture < void > {
149
+ private addToCacheCore ( packageName : string , version : string ) : IFuture < any > {
141
150
return ( ( ) => {
142
- this . $npm . cache ( packageName , version ) . wait ( ) ;
143
- let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , version , "package" ) ;
151
+ let cachedPackageData = this . $npm . cache ( packageName , version ) . wait ( ) ;
152
+ let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , cachedPackageData . version , "package" ) ;
144
153
if ( ! this . isPackageUnpacked ( packagePath , packageName ) . wait ( ) ) {
145
- this . cacheUnpack ( packageName , version ) . wait ( ) ;
154
+ this . cacheUnpack ( packageName , cachedPackageData . version ) . wait ( ) ;
146
155
}
147
- } ) . future < void > ( ) ( ) ;
156
+ return cachedPackageData ;
157
+ } ) . future < any > ( ) ( ) ;
148
158
}
149
159
150
160
private isShasumOfPackageCorrect ( packageName : string , version : string ) : IFuture < boolean > {
@@ -185,9 +195,9 @@ export class NpmInstallationManager implements INpmInstallationManager {
185
195
return path . join ( pathToNodeModules , folders [ 0 ] ) ;
186
196
} else {
187
197
version = version || this . getLatestCompatibleVersion ( packageName ) . wait ( ) ;
188
- let packagePath = this . getCachedPackagePath ( packageName , version ) ;
189
- this . addToCache ( packageName , version ) . wait ( ) ;
190
- return packagePath ;
198
+ let cachedData = this . addToCache ( packageName , version ) . wait ( ) ;
199
+ let packageVersion = ( cachedData && cachedData . version ) || version ;
200
+ return this . getCachedPackagePath ( packageName , packageVersion ) ;
191
201
}
192
202
} ) . future < string > ( ) ( ) ;
193
203
}
0 commit comments