Skip to content

Commit 11e9a59

Browse files
Make sure framework dir is part of the cached framework
In some cases framework dir is missing in the npm cached package of the framework. Check if it exists and if not, remove the package from the cache and add it again. Fail when the shasum of the package is not correct Fixes #699
1 parent c480c6b commit 11e9a59

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

lib/common

lib/declarations.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface INpmInstallationManager {
1515
install(packageName: string, options?: INpmInstallOptions): IFuture<string>;
1616
getLatestVersion(packageName: string): IFuture<string>;
1717
getCachedPackagePath(packageName: string, version: string): string;
18+
addCleanCopyToCache(packageName: string, version: string): IFuture<void>;
1819
}
1920

2021
interface INpmInstallOptions {

lib/npm-installation-manager.ts

+48-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import semver = require("semver");
66
import npm = require("npm");
77
import constants = require("./constants");
88

9-
export class NpmInstallationManager {
9+
export class NpmInstallationManager implements INpmInstallationManager {
1010
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
1111
private versionsCache: IDictionary<string[]>;
1212

@@ -27,13 +27,18 @@ export class NpmInstallationManager {
2727
public getCachedPackagePath(packageName: string, version: string): string {
2828
return path.join(this.getCacheRootPath(), packageName, version, "package");
2929
}
30-
30+
3131
public addToCache(packageName: string, version: string): IFuture<void> {
3232
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 cachedPackagePath = this.getCachedPackagePath(packageName, version);
34+
if(!this.$fs.exists(cachedPackagePath).wait()) {
35+
this.addToCacheCore(packageName, version).wait();
36+
}
37+
38+
if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
39+
// In some cases the package is not fully downloaded and the framework directory is missing
40+
// Try removing the old package and add the real one to cache again
41+
this.addCleanCopyToCache(packageName, version).wait();
3742
}
3843
}).future<void>()();
3944
}
@@ -73,6 +78,43 @@ export class NpmInstallationManager {
7378
}).future<string>()();
7479
}
7580

81+
public addCleanCopyToCache(packageName: string, version: string): IFuture<void> {
82+
return (() => {
83+
let packagePath = path.join(this.getCacheRootPath(), packageName, version);
84+
this.$logger.trace(`Deleting: ${packagePath}.`);
85+
this.$fs.deleteDirectory(packagePath).wait();
86+
this.addToCacheCore(packageName, version).wait();
87+
if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
88+
this.$errors.failWithoutHelp(`Unable to add package ${packageName} with version ${version} to npm cache. Try cleaning your cache and execute the command again.`)
89+
}
90+
}).future<void>()();
91+
}
92+
93+
private addToCacheCore(packageName: string, version: string): IFuture<void> {
94+
return (() => {
95+
this.$npm.cache(packageName, version).wait();
96+
let packagePath = path.join(this.getCacheRootPath(), packageName, version, "package");
97+
if(!this.isPackageUnpacked(packagePath).wait()) {
98+
this.cacheUnpack(packageName, version).wait();
99+
}
100+
}).future<void>()();
101+
}
102+
103+
private isShasumOfPackageCorrect(packageName: string, version: string): IFuture<boolean> {
104+
return ((): boolean => {
105+
let shasumProperty = "dist.shasum";
106+
let cachedPackagePath = this.getCachedPackagePath(packageName, version);
107+
let realShasum = this.$npm.view(`${packageName}@${version}`, shasumProperty).wait()[version][shasumProperty];
108+
let packageTgz = cachedPackagePath + ".tgz";
109+
let currentShasum = "";
110+
if(this.$fs.exists(packageTgz).wait()) {
111+
currentShasum = this.$fs.getFileShasum(packageTgz).wait();
112+
}
113+
this.$logger.trace(`Checking shasum of package: ${packageName}@${version}: expected ${realShasum}, actual ${currentShasum}.`);
114+
return realShasum === currentShasum;
115+
}).future<boolean>()();
116+
}
117+
76118
private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
77119
return (() => {
78120
if (this.$options.frameworkPath) {

lib/services/platform-service.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import util = require("util");
77
import constants = require("./../constants");
88
import helpers = require("./../common/helpers");
99
import semver = require("semver");
10+
import Future = require("fibers/future");
1011

1112
export class PlatformService implements IPlatformService {
1213
private static TNS_MODULES_FOLDER_NAME = "tns_modules";
@@ -498,8 +499,11 @@ export class PlatformService implements IPlatformService {
498499

499500
private ensurePackageIsCached(cachedPackagePath: string, packageName: string, version: string): IFuture<void> {
500501
return (() => {
501-
if(!this.$fs.exists(cachedPackagePath).wait()) {
502-
this.$npmInstallationManager.addToCache(packageName, version).wait();
502+
this.$npmInstallationManager.addToCache(packageName, version).wait();
503+
if(!this.$fs.exists(path.join(cachedPackagePath, constants.PROJECT_FRAMEWORK_FOLDER_NAME)).wait()) {
504+
// In some cases the package is not fully downloaded and the framework directory is missing
505+
// Try removing the old package and add the real one to cache again
506+
this.$npmInstallationManager.addCleanCopyToCache(packageName, version).wait();
503507
}
504508
}).future<void>()();
505509
}

test/stubs.ts

+8
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export class FileSystemStub implements IFileSystem {
154154
isRelativePath(path: string): boolean {
155155
return false;
156156
}
157+
158+
getFileShasum(fileName: string): IFuture<string> {
159+
return undefined;
160+
}
157161
}
158162

159163
export class ErrorsStub implements IErrors {
@@ -195,6 +199,10 @@ export class NpmInstallationManagerStub implements INpmInstallationManager {
195199
return undefined;
196200
}
197201

202+
addCleanCopyToCache(packageName: string, version: string): IFuture<void> {
203+
return undefined;
204+
}
205+
198206
cacheUnpack(packageName: string, version: string): IFuture<void> {
199207
return undefined;
200208
}

0 commit comments

Comments
 (0)