Skip to content

Commit c394233

Browse files
Merge pull request #1102 from NativeScript/vladimirov/do-not-install-every-time
Do not call npm install if everything is installed
2 parents 10d8321 + aa1ab49 commit c394233

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

lib/npm-installation-manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class NpmInstallationManager implements INpmInstallationManager {
8484
.sortBy(verData => verData.patch)
8585
.value();
8686

87-
let result = _.last(compatibleVersions);
87+
let result = _.last(compatibleVersions) || this.getVersionData(latestVersion);
8888

8989
let latestCompatibleVersion = `${result.major}.${result.minor}.${result.patch}`;
9090
return latestCompatibleVersion;

lib/services/plugins-service.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,15 @@ export class PluginsService implements IPluginsService {
161161

162162
public ensureAllDependenciesAreInstalled(): IFuture<void> {
163163
return (() => {
164-
let command = "npm install ";
165-
if(this.$options.ignoreScripts) {
166-
command += "--ignore-scripts";
164+
let installedDependencies = this.$fs.exists(this.nodeModulesPath).wait() ? this.$fs.readDirectory(this.nodeModulesPath).wait() : [];
165+
let packageJsonContent = this.$fs.readJson(this.getPackageJsonFilePath()).wait();
166+
if(this.$options.force || (packageJsonContent.dependencies && _.difference(_.keys(packageJsonContent.dependencies), installedDependencies).length)) {
167+
let command = "npm install ";
168+
if(this.$options.ignoreScripts) {
169+
command += "--ignore-scripts";
170+
}
171+
this.$childProcess.exec(command, { cwd: this.$projectData.projectDir }).wait();
167172
}
168-
this.$childProcess.exec(command, { cwd: this.$projectData.projectDir }).wait();
169173
}).future<void>()();
170174
}
171175

test/npm-installation-manager.ts

+20
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe("Npm installation manager tests", () => {
7272
let expectedLatestCompatibleVersion = "1.4.0";
7373
assert.equal(actualLatestCompatibleVersion, expectedLatestCompatibleVersion);
7474
});
75+
7576
it("returns correct latest compatible version", () => {
7677
let testInjector = createTestInjector();
7778

@@ -92,4 +93,23 @@ describe("Npm installation manager tests", () => {
9293
let expectedLatestCompatibleVersion = "1.3.3";
9394
assert.equal(actualLatestCompatibleVersion, expectedLatestCompatibleVersion);
9495
});
96+
97+
it("returns correct latest compatible version", () => {
98+
let testInjector = createTestInjector();
99+
100+
let versions = ["1.2.0", "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.4.0"];
101+
let latestVersion = _.last(versions);
102+
mockNpm(testInjector, versions, latestVersion);
103+
104+
// Mock staticConfig.version
105+
let staticConfig = testInjector.resolve("staticConfig");
106+
staticConfig.version = "1.5.0";
107+
108+
// Mock npmInstallationManager.getLatestVersion
109+
let npmInstallationManager = testInjector.resolve("npmInstallationManager");
110+
npmInstallationManager.getLatestVersion = (packageName: string) => Future.fromResult(latestVersion);
111+
112+
let actualLatestCompatibleVersion = npmInstallationManager.getLatestCompatibleVersion("").wait();
113+
assert.equal(actualLatestCompatibleVersion, latestVersion);
114+
});
95115
});

0 commit comments

Comments
 (0)