Skip to content

Commit

Permalink
Make sure framework dir is part of the cached framework
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rosen-vladimirov committed Jul 28, 2015
1 parent c480c6b commit 11e9a59
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 2 files
+1 −0 declarations.d.ts
+24 −0 file-system.ts
1 change: 1 addition & 0 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface INpmInstallationManager {
install(packageName: string, options?: INpmInstallOptions): IFuture<string>;
getLatestVersion(packageName: string): IFuture<string>;
getCachedPackagePath(packageName: string, version: string): string;
addCleanCopyToCache(packageName: string, version: string): IFuture<void>;
}

interface INpmInstallOptions {
Expand Down
54 changes: 48 additions & 6 deletions lib/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import semver = require("semver");
import npm = require("npm");
import constants = require("./constants");

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

Expand All @@ -27,13 +27,18 @@ export class NpmInstallationManager {
public getCachedPackagePath(packageName: string, version: string): string {
return path.join(this.getCacheRootPath(), packageName, version, "package");
}

public addToCache(packageName: string, version: string): IFuture<void> {
return (() => {
this.$npm.cache(packageName, version).wait();
let packagePath = path.join(this.getCacheRootPath(), packageName, version, "package");
if(!this.isPackageUnpacked(packagePath).wait()) {
this.cacheUnpack(packageName, version).wait();
let cachedPackagePath = this.getCachedPackagePath(packageName, version);
if(!this.$fs.exists(cachedPackagePath).wait()) {
this.addToCacheCore(packageName, version).wait();
}

if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
// In some cases the package is not fully downloaded and the framework directory is missing
// Try removing the old package and add the real one to cache again
this.addCleanCopyToCache(packageName, version).wait();
}
}).future<void>()();
}
Expand Down Expand Up @@ -73,6 +78,43 @@ export class NpmInstallationManager {
}).future<string>()();
}

public addCleanCopyToCache(packageName: string, version: string): IFuture<void> {
return (() => {
let packagePath = path.join(this.getCacheRootPath(), packageName, version);
this.$logger.trace(`Deleting: ${packagePath}.`);
this.$fs.deleteDirectory(packagePath).wait();
this.addToCacheCore(packageName, version).wait();
if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
this.$errors.failWithoutHelp(`Unable to add package ${packageName} with version ${version} to npm cache. Try cleaning your cache and execute the command again.`)
}
}).future<void>()();
}

private addToCacheCore(packageName: string, version: string): IFuture<void> {
return (() => {
this.$npm.cache(packageName, version).wait();
let packagePath = path.join(this.getCacheRootPath(), packageName, version, "package");
if(!this.isPackageUnpacked(packagePath).wait()) {
this.cacheUnpack(packageName, version).wait();
}
}).future<void>()();
}

private isShasumOfPackageCorrect(packageName: string, version: string): IFuture<boolean> {
return ((): boolean => {
let shasumProperty = "dist.shasum";
let cachedPackagePath = this.getCachedPackagePath(packageName, version);
let realShasum = this.$npm.view(`${packageName}@${version}`, shasumProperty).wait()[version][shasumProperty];
let packageTgz = cachedPackagePath + ".tgz";
let currentShasum = "";
if(this.$fs.exists(packageTgz).wait()) {
currentShasum = this.$fs.getFileShasum(packageTgz).wait();
}
this.$logger.trace(`Checking shasum of package: ${packageName}@${version}: expected ${realShasum}, actual ${currentShasum}.`);
return realShasum === currentShasum;
}).future<boolean>()();
}

private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
return (() => {
if (this.$options.frameworkPath) {
Expand Down
8 changes: 6 additions & 2 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import util = require("util");
import constants = require("./../constants");
import helpers = require("./../common/helpers");
import semver = require("semver");
import Future = require("fibers/future");

export class PlatformService implements IPlatformService {
private static TNS_MODULES_FOLDER_NAME = "tns_modules";
Expand Down Expand Up @@ -498,8 +499,11 @@ export class PlatformService implements IPlatformService {

private ensurePackageIsCached(cachedPackagePath: string, packageName: string, version: string): IFuture<void> {
return (() => {
if(!this.$fs.exists(cachedPackagePath).wait()) {
this.$npmInstallationManager.addToCache(packageName, version).wait();
this.$npmInstallationManager.addToCache(packageName, version).wait();
if(!this.$fs.exists(path.join(cachedPackagePath, constants.PROJECT_FRAMEWORK_FOLDER_NAME)).wait()) {
// In some cases the package is not fully downloaded and the framework directory is missing
// Try removing the old package and add the real one to cache again
this.$npmInstallationManager.addCleanCopyToCache(packageName, version).wait();
}
}).future<void>()();
}
Expand Down
8 changes: 8 additions & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export class FileSystemStub implements IFileSystem {
isRelativePath(path: string): boolean {
return false;
}

getFileShasum(fileName: string): IFuture<string> {
return undefined;
}
}

export class ErrorsStub implements IErrors {
Expand Down Expand Up @@ -195,6 +199,10 @@ export class NpmInstallationManagerStub implements INpmInstallationManager {
return undefined;
}

addCleanCopyToCache(packageName: string, version: string): IFuture<void> {
return undefined;
}

cacheUnpack(packageName: string, version: string): IFuture<void> {
return undefined;
}
Expand Down

0 comments on commit 11e9a59

Please sign in to comment.