Fix npm install after adding entry to..

.. package.json. Without the proper prefix, npm will install the package
to the wrong location and will fail. pass prefix to ask it to install in
the current project's node_modules folder.
This commit is contained in:
Balaji Krishnan 2017-03-15 16:36:58 -07:00
Родитель a665bf94ac
Коммит 045a3e0c9f
3 изменённых файлов: 40 добавлений и 7 удалений

Просмотреть файл

@ -1,4 +1,5 @@
var fs = require('fs');
var path = require('path');
var vscode = require('vscode');
var npmUserPackages = require('npm-user-packages');
var jsonEditor = require('../codegen/jsoneditor');
@ -38,12 +39,16 @@ function updatePackageJsonAndNpmInstall(packageToAdd) {
jsonEditor.addDependenciesIfRequired(filePath, packages);
// TODO: run npm-install only if package.json was touched
var installTask = utils.npmInstall(packages, { global: false });
var npmOptions = {
prefix: filePath.slice(0, filePath.lastIndexOf(path.sep))
};
var installTask = utils.npmInstall(packages, npmOptions);
return installTask.then(
function onFulfilled(value) {
function onFulfilled() {
vscode.window.setStatusBarMessage(`npm install succeeded for ${packageToAdd}.`);
},
function onRejected(reason) {
function onRejected() {
vscode.window.setStatusBarMessage(`npm install failed for ${packageToAdd}.`);
}
);

Просмотреть файл

@ -1,4 +1,5 @@
var fs = require('fs');
var path = require('path');
var vscode = require('vscode');
var codegen = require('../codegen/codgen.template-deploy');
var jsonEditor = require('../codegen/jsoneditor');
@ -28,12 +29,16 @@ function updatePackageJsonAndNpmInstall() {
jsonEditor.addDependenciesIfRequired(filePath, packages);
// TODO: run npm-install only if package.json was touched
var installTask = utils.npmInstall(packages, { global: false });
var npmOptions = {
prefix: filePath.slice(0, filePath.lastIndexOf(path.sep))
};
var installTask = utils.npmInstall(packages, npmOptions);
return installTask.then(
function onFulfilled(value) {
function onFulfilled() {
vscode.window.setStatusBarMessage(`npm install succeeded.`);
},
function onRejected(reason) {
function onRejected() {
vscode.window.setStatusBarMessage(`npm install failed.`);
}
);

Просмотреть файл

@ -2,6 +2,7 @@ var fs = require('fs');
var path = require('path');
var vscode = require('vscode');
let exec = require('child_process').exec;
var npm = require('npm');
// checks if there exists a valid installation of NodeJs on this machine
exports.isNodeInstalled = function isNodeInstalled() {
@ -73,7 +74,8 @@ exports.npmInstall = function npmInstall(packages, opts) {
var cmdString = "npm install " + packages.join(" ") + " "
+ (opts.global ? " -g" : "")
+ (opts.save ? " --save" : "")
+ (opts.saveDev ? " --saveDev" : "");
+ (opts.saveDev ? " --saveDev" : "")
+ (opts.prefix ? " --prefix " + opts.prefix : "");
return new Promise(function (resolve, reject) {
exec(cmdString, { cwd: opts.cwd ? opts.cwd : "/" }, (error) => {
@ -113,3 +115,24 @@ function getSourceLocation() {
}
}
exports.install = function install(pkgName, options) {
var promise = new Promise(function (resolve, reject) {
if (!options) {
reject("options.prefix is required.");
}
npm.load(options, function (err) {
if (err) {
return reject(err);
}
npm.commands.install([pkgName + '@latest'], function (err, info) {
if (err) {
reject(err);
}
resolve(info);
});
});
});
return promise;
};