Skip to content

moved caching logic to npminstallation manager #2309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface INpmInstallationManager {
getLatestVersion(packageName: string): IFuture<string>;
getNextVersion(packageName: string): IFuture<string>;
getLatestCompatibleVersion(packageName: string): IFuture<string>;
getInspectorFromCache(inspectorNpmPackageName: string, projectDir: string): IFuture<string>;
}

interface INpmInstallOptions {
Expand Down
34 changes: 34 additions & 0 deletions lib/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class NpmInstallationManager implements INpmInstallationManager {
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";

constructor(private $npm: INodePackageManager,
private $childProcess: IChildProcess,
private $logger: ILogger,
private $errors: IErrors,
private $options: IOptions,
Expand Down Expand Up @@ -57,6 +58,39 @@ export class NpmInstallationManager implements INpmInstallationManager {
}).future<string>()();
}

public getInspectorFromCache(inspectorNpmPackageName: string, projectDir: string) : IFuture<string> {
return (() => {
let inspectorPath = path.join(projectDir, "node_modules", inspectorNpmPackageName);

// local installation takes precedence over cache
if(!this.inspectorAlreadyInstalled(inspectorPath).wait()) {
let cachepath = this.$childProcess.exec("npm get cache").wait().trim();
let version = this.getLatestCompatibleVersion(inspectorNpmPackageName).wait();
let pathToPackageInCache = path.join(cachepath, inspectorNpmPackageName, version);
let pathToUnzippedInspector = path.join(pathToPackageInCache, "package");

if(!this.$fs.exists(pathToPackageInCache).wait()) {
this.$childProcess.exec(`npm cache add ${inspectorNpmPackageName}@${version}`).wait();
let inspectorTgzPathInCache = path.join(pathToPackageInCache, "package.tgz");
this.$childProcess.exec(`tar -xf ${inspectorTgzPathInCache} -C ${pathToPackageInCache}`).wait();
this.$childProcess.exec(`npm install --prefix ${pathToUnzippedInspector}`).wait();
}
this.$logger.out("Using inspector from cache.");
return pathToUnzippedInspector;
}
return inspectorPath;
}).future<string>()();
}

private inspectorAlreadyInstalled(pathToInspector: string): IFuture<Boolean> {
return (() => {
if(this.$fs.exists(pathToInspector).wait()) {
return true;
}
return false;
}).future<Boolean>()();
}

private installCore(packageName: string, pathToSave: string, version: string, dependencyType: string): IFuture<string> {
return (() => {
const possiblePackageName= path.resolve(packageName);
Expand Down
28 changes: 1 addition & 27 deletions lib/services/ios-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,7 @@ class IOSDebugService implements IDebugService {
private openAppInspector(fileDescriptor: string): IFuture<void> {
if (this.$options.client) {
return (() => {
let inspectorPath = path.join(this.$projectData.projectDir, "node_modules", inspectorNpmPackageName);

// local installation takes precedence over cache
if(!this.inspectorAlreadyInstalled(inspectorPath).wait()) {
let cachepath = this.$childProcess.exec("npm get cache").wait().trim();
let version = this.$npmInstallationManager.getLatestCompatibleVersion(inspectorNpmPackageName).wait();
let pathToPackageInCache = path.join(cachepath, inspectorNpmPackageName, version);
let pathToUnzippedInspector = path.join(pathToPackageInCache, "package");

if(!this.$fs.exists(pathToPackageInCache).wait()) {
this.$childProcess.exec(`npm cache add ${inspectorNpmPackageName}@${version}`).wait();
let inspectorTgzPathInCache = path.join(pathToPackageInCache, "package.tgz");
this.$childProcess.exec(`tar -xf ${inspectorTgzPathInCache} -C ${pathToPackageInCache}`).wait();
this.$childProcess.exec(`npm install --prefix ${pathToUnzippedInspector}`).wait();
}
this.$logger.out("Using inspector from cache.");
inspectorPath = pathToUnzippedInspector;
}
let inspectorPath = this.$npmInstallationManager.getInspectorFromCache(inspectorNpmPackageName, this.$projectData.projectDir).wait();

let inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html");
let inspectorApplicationPath = path.join(inspectorPath, inspectorAppName);
Expand All @@ -245,14 +228,5 @@ class IOSDebugService implements IDebugService {
}).future<void>()();
}
}

private inspectorAlreadyInstalled(pathToInspector: string): IFuture<Boolean> {
return (() => {
if(this.$fs.exists(pathToInspector).wait()) {
return true;
}
return false;
}).future<Boolean>()();
}
}
$injector.register("iOSDebugService", IOSDebugService);
2 changes: 2 additions & 0 deletions test/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as OptionsLib from "../lib/options";
import * as StaticConfigLib from "../lib/config";
import Future = require("fibers/future");
import * as yok from "../lib/common/yok";
import ChildProcessLib = require("../lib/common/child-process");

function createTestInjector(): IInjector {
let testInjector = new yok.Yok();
Expand All @@ -21,6 +22,7 @@ function createTestInjector(): IInjector {
testInjector.register("fs", FsLib.FileSystem);
testInjector.register("hostInfo", HostInfoLib.HostInfo);
testInjector.register("staticConfig", StaticConfigLib.StaticConfig);
testInjector.register("childProcess", ChildProcessLib.ChildProcess);

testInjector.register("npmInstallationManager", NpmInstallationManagerLib.NpmInstallationManager);

Expand Down
4 changes: 4 additions & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export class NpmInstallationManagerStub implements INpmInstallationManager {
getLatestCompatibleVersion(packageName: string): IFuture<string> {
return Future.fromResult("");
}

getInspectorFromCache(name: string, projectDir: string): IFuture<string> {
return Future.fromResult("");
}
}

export class ProjectDataStub implements IProjectData {
Expand Down