Skip to content

Commit e9cf798

Browse files
committed
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
1 parent 1335ae7 commit e9cf798

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

lib/declarations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface INpmInstallationManager {
1010
getLatestVersion(packageName: string): IFuture<string>;
1111
getNextVersion(packageName: string): IFuture<string>;
1212
getLatestCompatibleVersion(packageName: string): IFuture<string>;
13+
getInspectorFromCache(inspectorNpmPackageName: string): IFuture<string>;
1314
}
1415

1516
interface INpmInstallOptions {

lib/npm-installation-manager.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export class NpmInstallationManager implements INpmInstallationManager {
66
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
77

88
constructor(private $npm: INodePackageManager,
9+
private $projectData: IProjectData,
10+
private $childProcess: IChildProcess,
911
private $logger: ILogger,
1012
private $errors: IErrors,
1113
private $options: IOptions,
@@ -57,6 +59,39 @@ export class NpmInstallationManager implements INpmInstallationManager {
5759
}).future<string>()();
5860
}
5961

62+
public getInspectorFromCache(inspectorNpmPackageName: string) : IFuture<string> {
63+
return (() => {
64+
let inspectorPath = path.join(this.$projectData.projectDir, "node_modules", inspectorNpmPackageName);
65+
66+
// local installation takes precedence over cache
67+
if(!this.inspectorAlreadyInstalled(inspectorPath).wait()) {
68+
let cachepath = this.$childProcess.exec("npm get cache").wait().trim();
69+
let version = this.getLatestCompatibleVersion(inspectorNpmPackageName).wait();
70+
let pathToPackageInCache = path.join(cachepath, inspectorNpmPackageName, version);
71+
let pathToUnzippedInspector = path.join(pathToPackageInCache, "package");
72+
73+
if(!this.$fs.exists(pathToPackageInCache).wait()) {
74+
this.$childProcess.exec(`npm cache add ${inspectorNpmPackageName}@${version}`).wait();
75+
let inspectorTgzPathInCache = path.join(pathToPackageInCache, "package.tgz");
76+
this.$childProcess.exec(`tar -xf ${inspectorTgzPathInCache} -C ${pathToPackageInCache}`).wait();
77+
this.$childProcess.exec(`npm install --prefix ${pathToUnzippedInspector}`).wait();
78+
}
79+
this.$logger.out("Using inspector from cache.");
80+
return pathToUnzippedInspector;
81+
}
82+
return inspectorPath;
83+
}).future<string>()();
84+
}
85+
86+
private inspectorAlreadyInstalled(pathToInspector: string): IFuture<Boolean> {
87+
return (() => {
88+
if(this.$fs.exists(pathToInspector).wait()) {
89+
return true;
90+
}
91+
return false;
92+
}).future<Boolean>()();
93+
}
94+
6095
private installCore(packageName: string, pathToSave: string, version: string, dependencyType: string): IFuture<string> {
6196
return (() => {
6297
const possiblePackageName= path.resolve(packageName);

lib/services/ios-debug-service.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,7 @@ class IOSDebugService implements IDebugService {
214214
private openAppInspector(fileDescriptor: string): IFuture<void> {
215215
if (this.$options.client) {
216216
return (() => {
217-
let inspectorPath = path.join(this.$projectData.projectDir, "node_modules", inspectorNpmPackageName);
218-
219-
// local installation takes precedence over cache
220-
if(!this.inspectorAlreadyInstalled(inspectorPath).wait()) {
221-
let cachepath = this.$childProcess.exec("npm get cache").wait().trim();
222-
let version = this.$npmInstallationManager.getLatestCompatibleVersion(inspectorNpmPackageName).wait();
223-
let pathToPackageInCache = path.join(cachepath, inspectorNpmPackageName, version);
224-
let pathToUnzippedInspector = path.join(pathToPackageInCache, "package");
225-
226-
if(!this.$fs.exists(pathToPackageInCache).wait()) {
227-
this.$childProcess.exec(`npm cache add ${inspectorNpmPackageName}@${version}`).wait();
228-
let inspectorTgzPathInCache = path.join(pathToPackageInCache, "package.tgz");
229-
this.$childProcess.exec(`tar -xf ${inspectorTgzPathInCache} -C ${pathToPackageInCache}`).wait();
230-
this.$childProcess.exec(`npm install --prefix ${pathToUnzippedInspector}`).wait();
231-
}
232-
this.$logger.out("Using inspector from cache.");
233-
inspectorPath = pathToUnzippedInspector;
234-
}
217+
let inspectorPath = this.$npmInstallationManager.getInspectorFromCache(inspectorNpmPackageName).wait();
235218

236219
let inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html");
237220
let inspectorApplicationPath = path.join(inspectorPath, inspectorAppName);
@@ -245,14 +228,5 @@ class IOSDebugService implements IDebugService {
245228
}).future<void>()();
246229
}
247230
}
248-
249-
private inspectorAlreadyInstalled(pathToInspector: string): IFuture<Boolean> {
250-
return (() => {
251-
if(this.$fs.exists(pathToInspector).wait()) {
252-
return true;
253-
}
254-
return false;
255-
}).future<Boolean>()();
256-
}
257231
}
258232
$injector.register("iOSDebugService", IOSDebugService);

0 commit comments

Comments
 (0)