Skip to content

Commit 933a748

Browse files
authored
Merge pull request #2309 from NativeScript/plamen5kov/move_logic
moved caching logic to npminstallation manager
2 parents 1335ae7 + c647723 commit 933a748

File tree

5 files changed

+42
-27
lines changed

5 files changed

+42
-27
lines changed

lib/declarations.ts

+1
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, projectDir: string): IFuture<string>;
1314
}
1415

1516
interface INpmInstallOptions {

lib/npm-installation-manager.ts

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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 $childProcess: IChildProcess,
910
private $logger: ILogger,
1011
private $errors: IErrors,
1112
private $options: IOptions,
@@ -57,6 +58,39 @@ export class NpmInstallationManager implements INpmInstallationManager {
5758
}).future<string>()();
5859
}
5960

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

lib/services/ios-debug-service.ts

+1-27
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, this.$projectData.projectDir).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);

test/npm-installation-manager.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as OptionsLib from "../lib/options";
99
import * as StaticConfigLib from "../lib/config";
1010
import Future = require("fibers/future");
1111
import * as yok from "../lib/common/yok";
12+
import ChildProcessLib = require("../lib/common/child-process");
1213

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

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

test/stubs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ export class NpmInstallationManagerStub implements INpmInstallationManager {
230230
getLatestCompatibleVersion(packageName: string): IFuture<string> {
231231
return Future.fromResult("");
232232
}
233+
234+
getInspectorFromCache(name: string, projectDir: string): IFuture<string> {
235+
return Future.fromResult("");
236+
}
233237
}
234238

235239
export class ProjectDataStub implements IProjectData {

0 commit comments

Comments
 (0)