@@ -6,7 +6,7 @@ import semver = require("semver");
6
6
import npm = require( "npm" ) ;
7
7
import constants = require( "./constants" ) ;
8
8
9
- export class NpmInstallationManager {
9
+ export class NpmInstallationManager implements INpmInstallationManager {
10
10
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later." ;
11
11
private versionsCache : IDictionary < string [ ] > ;
12
12
@@ -27,13 +27,22 @@ export class NpmInstallationManager {
27
27
public getCachedPackagePath ( packageName : string , version : string ) : string {
28
28
return path . join ( this . getCacheRootPath ( ) , packageName , version , "package" ) ;
29
29
}
30
-
31
- public addToCache ( packageName : string , version : string ) : IFuture < void > {
30
+
31
+ public addToCache ( packageName : string , version : string ) : IFuture < void > {
32
32
return ( ( ) => {
33
- this . $npm . cache ( packageName , version ) . wait ( ) ;
34
- let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , version , "package" ) ;
35
- if ( ! this . isPackageUnpacked ( packagePath ) . wait ( ) ) {
36
- this . cacheUnpack ( packageName , version ) . wait ( ) ;
33
+ let shasumProperty = "dist.shasum" ;
34
+ let cachedPackagePath = this . getCachedPackagePath ( packageName , version ) ;
35
+ if ( ! this . $fs . exists ( cachedPackagePath ) . wait ( ) ) {
36
+ this . addToCacheCore ( packageName , version ) . wait ( ) ;
37
+ }
38
+
39
+ let realShasum = this . $npm . view ( `${ packageName } @${ version } ` , shasumProperty ) . wait ( ) [ version ] [ shasumProperty ] ;
40
+ let ourShasum = this . $fs . getFileShasum ( cachedPackagePath + ".tgz" ) . wait ( ) ;
41
+ this . $logger . trace ( `Checking shasum of package: ${ packageName } @${ version } : expected ${ realShasum } , actual ${ ourShasum } .` ) ;
42
+ if ( realShasum !== ourShasum ) {
43
+ // In some cases the package is not fully downloaded and the framework directory is missing
44
+ // Try removing the old package and add the real one to cache again
45
+ this . addCleanCopyToCache ( packageName , version ) . wait ( ) ;
37
46
}
38
47
} ) . future < void > ( ) ( ) ;
39
48
}
@@ -73,6 +82,25 @@ export class NpmInstallationManager {
73
82
} ) . future < string > ( ) ( ) ;
74
83
}
75
84
85
+ public addCleanCopyToCache ( packageName : string , version : string ) : IFuture < void > {
86
+ return ( ( ) => {
87
+ let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , version ) ;
88
+ this . $logger . trace ( `Deleting: ${ packagePath } .` ) ;
89
+ this . $fs . deleteDirectory ( packagePath ) . wait ( ) ;
90
+ this . addToCacheCore ( packageName , version ) . wait ( ) ;
91
+ } ) . future < void > ( ) ( ) ;
92
+ }
93
+
94
+ private addToCacheCore ( packageName : string , version : string ) : IFuture < void > {
95
+ return ( ( ) => {
96
+ this . $npm . cache ( packageName , version ) . wait ( ) ;
97
+ let packagePath = path . join ( this . getCacheRootPath ( ) , packageName , version , "package" ) ;
98
+ if ( ! this . isPackageUnpacked ( packagePath ) . wait ( ) ) {
99
+ this . cacheUnpack ( packageName , version ) . wait ( ) ;
100
+ }
101
+ } ) . future < void > ( ) ( ) ;
102
+ }
103
+
76
104
private installCore ( packageName : string , pathToSave : string , version : string ) : IFuture < string > {
77
105
return ( ( ) => {
78
106
if ( this . $options . frameworkPath ) {
0 commit comments