Skip to content

Commit 506fe60

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. Fixes #699
1 parent c480c6b commit 506fe60

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
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

+35-7
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,22 @@ export class NpmInstallationManager {
2727
public getCachedPackagePath(packageName: string, version: string): string {
2828
return path.join(this.getCacheRootPath(), packageName, version, "package");
2929
}
30-
31-
public addToCache(packageName: string, version: string): IFuture<void> {
30+
31+
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 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();
3746
}
3847
}).future<void>()();
3948
}
@@ -73,6 +82,25 @@ export class NpmInstallationManager {
7382
}).future<string>()();
7483
}
7584

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+
76104
private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
77105
return (() => {
78106
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)