@@ -42,19 +42,26 @@ export class NpmInstallationManager implements INpmInstallationManager {
4242 return path . join ( this . getCacheRootPath ( ) , packageName , version , "package" ) ;
4343 }
4444
45- public addToCache ( packageName : string , version : string ) : IFuture < void > {
45+ public addToCache ( packageName : string , version : string ) : IFuture < any > {
4646 return ( ( ) => {
4747 let cachedPackagePath = this . getCachedPackagePath ( packageName , version ) ;
48+ let cachedPackageData : any ;
4849 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 ( ) ;
5051 }
5152
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 ( ) ) {
5358 // In some cases the package is not fully downloaded and there are missing directories
5459 // 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 ( ) ;
5661 }
57- } ) . future < void > ( ) ( ) ;
62+
63+ return cachedPackageData ;
64+ } ) . future < any > ( ) ( ) ;
5865 }
5966
6067 public cacheUnpack ( packageName : string , version : string , unpackTarget ?: string ) : IFuture < void > {
@@ -125,26 +132,29 @@ export class NpmInstallationManager implements INpmInstallationManager {
125132 } ) . future < string > ( ) ( ) ;
126133 }
127134
128- public addCleanCopyToCache ( packageName : string , version : string ) : IFuture < void > {
135+ private addCleanCopyToCache ( packageName : string , version : string ) : IFuture < any > {
129136 return ( ( ) => {
130137 let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , version ) ;
131138 this . $logger . trace ( `Deleting: ${ packagePath } .` ) ;
132139 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.` ) ;
136143 }
137- } ) . future < void > ( ) ( ) ;
144+
145+ return cachedPackageData ;
146+ } ) . future < any > ( ) ( ) ;
138147 }
139148
140- private addToCacheCore ( packageName : string , version : string ) : IFuture < void > {
149+ private addToCacheCore ( packageName : string , version : string ) : IFuture < any > {
141150 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" ) ;
144153 if ( ! this . isPackageUnpacked ( packagePath , packageName ) . wait ( ) ) {
145- this . cacheUnpack ( packageName , version ) . wait ( ) ;
154+ this . cacheUnpack ( packageName , cachedPackageData . version ) . wait ( ) ;
146155 }
147- } ) . future < void > ( ) ( ) ;
156+ return cachedPackageData ;
157+ } ) . future < any > ( ) ( ) ;
148158 }
149159
150160 private isShasumOfPackageCorrect ( packageName : string , version : string ) : IFuture < boolean > {
@@ -185,9 +195,9 @@ export class NpmInstallationManager implements INpmInstallationManager {
185195 return path . join ( pathToNodeModules , folders [ 0 ] ) ;
186196 } else {
187197 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 ) ;
191201 }
192202 } ) . future < string > ( ) ( ) ;
193203 }
0 commit comments