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

Commit b1ebb31

Browse files
committed
use npm to download extension dependencies
1 parent 27a6b79 commit b1ebb31

File tree

7 files changed

+82
-7
lines changed

7 files changed

+82
-7
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.1",
1720
"jasmine-node": "1.11.0",

src/extensibility/node/ExtensionManagerDomain.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var semver = require("semver"),
3232
request = require("request"),
3333
fs = require("fs-extra"),
3434
temp = require("temp"),
35+
npm = require("npm"),
3536
validate = require("./package-validator").validate;
3637

3738
// Automatically clean up temp files on exit
@@ -99,17 +100,61 @@ function _performInstall(packagePath, installDirectory, validationResult, callba
99100
var sourceDir = path.join(validationResult.extractDir, validationResult.commonPrefix);
100101

101102
fs.copy(sourceDir, installDirectory, function (err) {
102-
if (err) {
103+
104+
var fail = function (err) {
103105
_removeFailedInstallation(installDirectory);
104106
callback(err, null);
105-
} else {
107+
};
108+
109+
var finish = function () {
106110
// The status may have already been set previously (as in the
107111
// DISABLED case.
108112
if (!validationResult.installationStatus) {
109113
validationResult.installationStatus = Statuses.INSTALLED;
110114
}
111115
callback(null, validationResult);
116+
};
117+
118+
if (err) {
119+
fail(err);
120+
return;
121+
}
122+
123+
var packageJson,
124+
npmDependencies;
125+
126+
try {
127+
packageJson = JSON.parse(fs.readFileSync(path.join(installDirectory, "package.json"), {
128+
encoding: "utf8"
129+
}));
130+
} catch (e) {
131+
packageJson = null;
132+
}
133+
134+
if (packageJson && packageJson.dependencies) {
135+
npmDependencies = Object.keys(packageJson.dependencies).map(function (key) {
136+
return key + "@" + packageJson.dependencies[key];
137+
});
112138
}
139+
140+
if (npmDependencies && npmDependencies.length > 0) {
141+
npm.load(function (err, npm) {
142+
if (err) {
143+
fail(err);
144+
return;
145+
}
146+
npm.commands.install(installDirectory, npmDependencies, function (err, result) {
147+
if (err) {
148+
fail(err);
149+
return;
150+
}
151+
finish();
152+
});
153+
});
154+
} else {
155+
finish();
156+
}
157+
113158
});
114159
});
115160
}
@@ -264,7 +309,7 @@ function _cmdInstall(packagePath, destinationDirectory, options, callback, _doUp
264309
return;
265310
}
266311
}
267-
312+
268313
// The "legacy" stuff should go away after all of the commonly used extensions
269314
// have been upgraded with package.json files.
270315
var hasLegacyPackage = validationResult.metadata && legacyPackageCheck(legacyDirectory);

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

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

6162

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

0 commit comments

Comments
 (0)