Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 16f4bc8

Browse files
committed
use npm to download extension dependencies
1 parent ca8b283 commit 16f4bc8

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"branch": "",
1313
"SHA": ""
1414
},
15+
"dependencies": {
16+
"npm": "2.15.10"
17+
},
1518
"devDependencies": {
1619
"grunt": "0.4.5",
1720
"jasmine-node": "1.11.0",

src/extensibility/node/ExtensionManagerDomain.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var semver = require("semver"),
3030
request = require("request"),
3131
fs = require("fs-extra"),
3232
temp = require("temp"),
33+
npm = require("npm"),
3334
validate = require("./package-validator").validate;
3435

3536
// Automatically clean up temp files on exit
@@ -97,17 +98,61 @@ function _performInstall(packagePath, installDirectory, validationResult, callba
9798
var sourceDir = path.join(validationResult.extractDir, validationResult.commonPrefix);
9899

99100
fs.copy(sourceDir, installDirectory, function (err) {
100-
if (err) {
101+
102+
var fail = function (err) {
101103
_removeFailedInstallation(installDirectory);
102104
callback(err, null);
103-
} else {
105+
};
106+
107+
var finish = function () {
104108
// The status may have already been set previously (as in the
105109
// DISABLED case.
106110
if (!validationResult.installationStatus) {
107111
validationResult.installationStatus = Statuses.INSTALLED;
108112
}
109113
callback(null, validationResult);
114+
};
115+
116+
if (err) {
117+
fail(err);
118+
return;
119+
}
120+
121+
var packageJson,
122+
npmDependencies;
123+
124+
try {
125+
packageJson = JSON.parse(fs.readFileSync(path.join(installDirectory, "package.json"), {
126+
encoding: "utf8"
127+
}));
128+
} catch (e) {
129+
packageJson = null;
110130
}
131+
132+
if (packageJson && packageJson.dependencies) {
133+
npmDependencies = Object.keys(packageJson.dependencies).map(function (key) {
134+
return key + "@" + packageJson.dependencies[key];
135+
});
136+
}
137+
138+
if (npmDependencies && npmDependencies.length > 0) {
139+
npm.load(function (err, npm) {
140+
if (err) {
141+
fail(err);
142+
return;
143+
}
144+
npm.commands.install(installDirectory, npmDependencies, function (err, result) {
145+
if (err) {
146+
fail(err);
147+
return;
148+
}
149+
finish();
150+
});
151+
});
152+
} else {
153+
finish();
154+
}
155+
111156
});
112157
});
113158
}

src/extensibility/node/spec/Installation.spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ var basicValidExtension = path.join(testFilesDirectory, "basic-valid-exten
5454
missingPackageJSON = path.join(testFilesDirectory, "missing-package-json.zip"),
5555
missingPackageJSONUpdate = path.join(testFilesDirectory, "missing-package-json-update.zip"),
5656
missingPackageJSONRenamed = path.join(testFilesDirectory, "added-package-json-test", "missing-package-json.zip"),
57-
withSymlink = path.join(testFilesDirectory, "with-symlink.zip");
57+
withSymlink = path.join(testFilesDirectory, "with-symlink.zip"),
58+
withNpmDependencies = path.join(testFilesDirectory, "with-npm-dependencies.zip");
5859

5960

6061
describe("Package Installation", function () {
@@ -323,4 +324,30 @@ describe("Package Installation", function () {
323324
done();
324325
});
325326
});
327+
328+
it("should download npm dependencies when present", function (done) {
329+
ExtensionsDomain._cmdInstall(withNpmDependencies, installDirectory, standardOptions, function (err, result) {
330+
expect(err).toBeNull();
331+
expect(result.errors.length).toEqual(0);
332+
expect(fs.existsSync(result.installedTo)).toBe(true);
333+
expect(fs.existsSync(path.join(result.installedTo, "node_modules"))).toBe(true);
334+
335+
expect(fs.existsSync(path.join(result.installedTo, "node_modules", "lodash"))).toBe(true);
336+
expect(fs.existsSync(path.join(result.installedTo, "node_modules", "lodash", "package.json"))).toBe(true);
337+
var packageInfo = JSON.parse(fs.readFileSync(path.join(result.installedTo, "node_modules", "lodash", "package.json")));
338+
expect(packageInfo.version.slice(0,2)).toBe("3.");
339+
340+
expect(fs.existsSync(path.join(result.installedTo, "node_modules", "moment"))).toBe(true);
341+
expect(fs.existsSync(path.join(result.installedTo, "node_modules", "moment", "package.json"))).toBe(true);
342+
packageInfo = JSON.parse(fs.readFileSync(path.join(result.installedTo, "node_modules", "moment", "package.json")));
343+
expect(packageInfo.version.slice(0,4)).toBe("2.5.");
344+
345+
expect(fs.existsSync(path.join(result.installedTo, "node_modules", "underscore"))).toBe(true);
346+
expect(fs.existsSync(path.join(result.installedTo, "node_modules", "underscore", "package.json"))).toBe(true);
347+
packageInfo = JSON.parse(fs.readFileSync(path.join(result.installedTo, "node_modules", "underscore", "package.json")));
348+
expect(packageInfo.version).toBe("1.0.4");
349+
350+
done();
351+
});
352+
});
326353
});
Binary file not shown.

0 commit comments

Comments
 (0)