From e9cf79841131c7efe9cb1090aca8c4731eab93df Mon Sep 17 00:00:00 2001 From: plamen5kov Date: Mon, 5 Dec 2016 12:15:14 +0200 Subject: [PATCH 1/2] moved caching logic to npminstallation manager the logic for dealing with npm is in the wrong place (ios-debug-service) so it's moved to npminstallation manager where it should be --- lib/declarations.ts | 1 + lib/npm-installation-manager.ts | 35 +++++++++++++++++++++++++++++++ lib/services/ios-debug-service.ts | 28 +------------------------ 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/lib/declarations.ts b/lib/declarations.ts index b8e3c02119..d9c811ce62 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -10,6 +10,7 @@ interface INpmInstallationManager { getLatestVersion(packageName: string): IFuture; getNextVersion(packageName: string): IFuture; getLatestCompatibleVersion(packageName: string): IFuture; + getInspectorFromCache(inspectorNpmPackageName: string): IFuture; } interface INpmInstallOptions { diff --git a/lib/npm-installation-manager.ts b/lib/npm-installation-manager.ts index b8adbd240b..594cb746ca 100644 --- a/lib/npm-installation-manager.ts +++ b/lib/npm-installation-manager.ts @@ -6,6 +6,8 @@ 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 $projectData: IProjectData, + private $childProcess: IChildProcess, private $logger: ILogger, private $errors: IErrors, private $options: IOptions, @@ -57,6 +59,39 @@ export class NpmInstallationManager implements INpmInstallationManager { }).future()(); } + public getInspectorFromCache(inspectorNpmPackageName: string) : IFuture { + 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.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()(); + } + + private inspectorAlreadyInstalled(pathToInspector: string): IFuture { + return (() => { + if(this.$fs.exists(pathToInspector).wait()) { + return true; + } + return false; + }).future()(); + } + private installCore(packageName: string, pathToSave: string, version: string, dependencyType: string): IFuture { return (() => { const possiblePackageName= path.resolve(packageName); diff --git a/lib/services/ios-debug-service.ts b/lib/services/ios-debug-service.ts index 04d652e0c1..48a3baf759 100644 --- a/lib/services/ios-debug-service.ts +++ b/lib/services/ios-debug-service.ts @@ -214,24 +214,7 @@ class IOSDebugService implements IDebugService { private openAppInspector(fileDescriptor: string): IFuture { 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).wait(); let inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html"); let inspectorApplicationPath = path.join(inspectorPath, inspectorAppName); @@ -245,14 +228,5 @@ class IOSDebugService implements IDebugService { }).future()(); } } - - private inspectorAlreadyInstalled(pathToInspector: string): IFuture { - return (() => { - if(this.$fs.exists(pathToInspector).wait()) { - return true; - } - return false; - }).future()(); - } } $injector.register("iOSDebugService", IOSDebugService); From c647723ec80f02748c5ee151727dd08abec805bf Mon Sep 17 00:00:00 2001 From: plamen5kov Date: Mon, 5 Dec 2016 15:24:01 +0200 Subject: [PATCH 2/2] fixed tests --- lib/declarations.ts | 2 +- lib/npm-installation-manager.ts | 5 ++--- lib/services/ios-debug-service.ts | 2 +- test/npm-installation-manager.ts | 2 ++ test/stubs.ts | 4 ++++ 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/declarations.ts b/lib/declarations.ts index d9c811ce62..4f0dafc3bc 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -10,7 +10,7 @@ interface INpmInstallationManager { getLatestVersion(packageName: string): IFuture; getNextVersion(packageName: string): IFuture; getLatestCompatibleVersion(packageName: string): IFuture; - getInspectorFromCache(inspectorNpmPackageName: string): IFuture; + getInspectorFromCache(inspectorNpmPackageName: string, projectDir: string): IFuture; } interface INpmInstallOptions { diff --git a/lib/npm-installation-manager.ts b/lib/npm-installation-manager.ts index 594cb746ca..b8b9c2f343 100644 --- a/lib/npm-installation-manager.ts +++ b/lib/npm-installation-manager.ts @@ -6,7 +6,6 @@ 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 $projectData: IProjectData, private $childProcess: IChildProcess, private $logger: ILogger, private $errors: IErrors, @@ -59,9 +58,9 @@ export class NpmInstallationManager implements INpmInstallationManager { }).future()(); } - public getInspectorFromCache(inspectorNpmPackageName: string) : IFuture { + public getInspectorFromCache(inspectorNpmPackageName: string, projectDir: string) : IFuture { return (() => { - let inspectorPath = path.join(this.$projectData.projectDir, "node_modules", inspectorNpmPackageName); + let inspectorPath = path.join(projectDir, "node_modules", inspectorNpmPackageName); // local installation takes precedence over cache if(!this.inspectorAlreadyInstalled(inspectorPath).wait()) { diff --git a/lib/services/ios-debug-service.ts b/lib/services/ios-debug-service.ts index 48a3baf759..1698d51ba4 100644 --- a/lib/services/ios-debug-service.ts +++ b/lib/services/ios-debug-service.ts @@ -214,7 +214,7 @@ class IOSDebugService implements IDebugService { private openAppInspector(fileDescriptor: string): IFuture { if (this.$options.client) { return (() => { - let inspectorPath = this.$npmInstallationManager.getInspectorFromCache(inspectorNpmPackageName).wait(); + let inspectorPath = this.$npmInstallationManager.getInspectorFromCache(inspectorNpmPackageName, this.$projectData.projectDir).wait(); let inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html"); let inspectorApplicationPath = path.join(inspectorPath, inspectorAppName); diff --git a/test/npm-installation-manager.ts b/test/npm-installation-manager.ts index 7c48fdb7b7..de732fa64f 100644 --- a/test/npm-installation-manager.ts +++ b/test/npm-installation-manager.ts @@ -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(); @@ -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); diff --git a/test/stubs.ts b/test/stubs.ts index 0cc089ba4a..4ca0208433 100644 --- a/test/stubs.ts +++ b/test/stubs.ts @@ -230,6 +230,10 @@ export class NpmInstallationManagerStub implements INpmInstallationManager { getLatestCompatibleVersion(packageName: string): IFuture { return Future.fromResult(""); } + + getInspectorFromCache(name: string, projectDir: string): IFuture { + return Future.fromResult(""); + } } export class ProjectDataStub implements IProjectData {