1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge branch 'master' into runtime-api

This commit is contained in:
Nick O'Leary 2018-07-30 10:13:51 +01:00
commit 06abe63fb1
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -38,6 +38,8 @@ function init(runtime) {
log = runtime.log; log = runtime.log;
} }
var activePromise = Promise.resolve();
function checkModulePath(folder) { function checkModulePath(folder) {
var moduleName; var moduleName;
var err; var err;
@ -73,79 +75,86 @@ function checkExistingModule(module,version) {
return false; return false;
} }
function installModule(module,version) { function installModule(module,version) {
//TODO: ensure module is 'safe' activePromise = activePromise.then(() => {
return new Promise(function(resolve,reject) { //TODO: ensure module is 'safe'
var installName = module; return new Promise((resolve,reject) => {
var isUpgrade = false; var installName = module;
try { var isUpgrade = false;
if (moduleRe.test(module)) { try {
// Simple module name - assume it can be npm installed if (moduleRe.test(module)) {
if (version) { // Simple module name - assume it can be npm installed
installName += "@"+version; if (version) {
installName += "@"+version;
}
} else if (slashRe.test(module)) {
// A path - check if there's a valid package.json
installName = module;
module = checkModulePath(module);
} }
} else if (slashRe.test(module)) { isUpgrade = checkExistingModule(module,version);
// A path - check if there's a valid package.json } catch(err) {
installName = module; return reject(err);
module = checkModulePath(module);
} }
isUpgrade = checkExistingModule(module,version); if (!isUpgrade) {
} catch(err) { log.info(log._("server.install.installing",{name: module,version: version||"latest"}));
return reject(err);
}
if (!isUpgrade) {
log.info(log._("server.install.installing",{name: module,version: version||"latest"}));
} else {
log.info(log._("server.install.upgrading",{name: module,version: version||"latest"}));
}
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var args = ['install','--save','--save-prefix="~"','--production',installName];
log.trace(npmCommand + JSON.stringify(args));
var child = child_process.spawn(npmCommand,args,{
cwd: installDir,
shell: true
});
var output = "";
child.stdout.on('data', (data) => {
output += data;
});
child.stderr.on('data', (data) => {
output += data;
});
child.on('close', (code) => {
if (code !== 0) {
var e;
var lookFor404 = new RegExp(" 404 .*"+module,"m");
var lookForVersionNotFound = new RegExp("version not found: "+module+"@"+version,"m");
if (lookFor404.test(output)) {
log.warn(log._("server.install.install-failed-not-found",{name:module}));
e = new Error("Module not found");
e.code = 404;
reject(e);
} else if (isUpgrade && lookForVersionNotFound.test(output)) {
log.warn(log._("server.install.upgrade-failed-not-found",{name:module}));
e = new Error("Module not found");
e.code = 404;
reject(e);
} else {
log.warn(log._("server.install.install-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(output);
log.warn("------------------------------------------");
reject(new Error(log._("server.install.install-failed")));
}
} else { } else {
if (!isUpgrade) { log.info(log._("server.install.upgrading",{name: module,version: version||"latest"}));
log.info(log._("server.install.installed",{name:module}));
resolve(require("./index").addModule(module).then(reportAddedModules));
} else {
log.info(log._("server.install.upgraded",{name:module, version:version}));
events.emit("runtime-event",{id:"restart-required",payload:{type:"warning",text:"notification.warnings.restartRequired"},retain:true});
resolve(require("./registry").setModulePendingUpdated(module,version));
}
} }
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var args = ['install','--save','--save-prefix="~"','--production',installName];
log.trace(npmCommand + JSON.stringify(args));
var child = child_process.spawn(npmCommand,args,{
cwd: installDir,
shell: true
});
var output = "";
child.stdout.on('data', (data) => {
output += data;
});
child.stderr.on('data', (data) => {
output += data;
});
child.on('close', (code) => {
if (code !== 0) {
var e;
var lookFor404 = new RegExp(" 404 .*"+module,"m");
var lookForVersionNotFound = new RegExp("version not found: "+module+"@"+version,"m");
if (lookFor404.test(output)) {
log.warn(log._("server.install.install-failed-not-found",{name:module}));
e = new Error("Module not found");
e.code = 404;
reject(e);
} else if (isUpgrade && lookForVersionNotFound.test(output)) {
log.warn(log._("server.install.upgrade-failed-not-found",{name:module}));
e = new Error("Module not found");
e.code = 404;
reject(e);
} else {
log.warn(log._("server.install.install-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(output);
log.warn("------------------------------------------");
reject(new Error(log._("server.install.install-failed")));
}
} else {
if (!isUpgrade) {
log.info(log._("server.install.installed",{name:module}));
resolve(require("./index").addModule(module).then(reportAddedModules));
} else {
log.info(log._("server.install.upgraded",{name:module, version:version}));
events.emit("runtime-event",{id:"restart-required",payload:{type:"warning",text:"notification.warnings.restartRequired"},retain:true});
resolve(require("./registry").setModulePendingUpdated(module,version));
}
}
});
}); });
}).catch(err => {
// In case of error, reset activePromise to be resolvable
activePromise = Promise.resolve();
throw err;
}); });
return activePromise;
} }
@ -178,46 +187,53 @@ function reportRemovedModules(removedNodes) {
} }
function uninstallModule(module) { function uninstallModule(module) {
return new Promise(function(resolve,reject) { activePromise = activePromise.then(() => {
if (/[\s;]/.test(module)) { return new Promise((resolve,reject) => {
reject(new Error(log._("server.install.invalid"))); if (/[\s;]/.test(module)) {
return; reject(new Error(log._("server.install.invalid")));
} return;
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module);
try {
fs.statSync(moduleDir);
} catch(err) {
return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
}
var list = registry.removeModule(module);
log.info(log._("server.install.uninstalling",{name:module}));
var args = ['remove','--save',module];
log.trace(npmCommand + JSON.stringify(args));
var child = child_process.execFile(npmCommand,args,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
log.warn(log._("server.install.uninstall-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error(log._("server.install.uninstall-failed",{name:module})));
} else {
log.info(log._("server.install.uninstalled",{name:module}));
reportRemovedModules(list);
library.removeExamplesDir(module);
resolve(list);
}
} }
); var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module);
try {
fs.statSync(moduleDir);
} catch(err) {
return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
}
var list = registry.removeModule(module);
log.info(log._("server.install.uninstalling",{name:module}));
var args = ['remove','--save',module];
log.trace(npmCommand + JSON.stringify(args));
var child = child_process.execFile(npmCommand,args,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
log.warn(log._("server.install.uninstall-failed-long",{name:module}));
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error(log._("server.install.uninstall-failed",{name:module})));
} else {
log.info(log._("server.install.uninstalled",{name:module}));
reportRemovedModules(list);
library.removeExamplesDir(module);
resolve(list);
}
}
);
});
}).catch(err => {
// In case of error, reset activePromise to be resolvable
activePromise = Promise.resolve();
throw err;
}); });
return activePromise;
} }
function checkPrereq() { function checkPrereq() {
@ -230,7 +246,7 @@ function checkPrereq() {
paletteEditorEnabled = false; paletteEditorEnabled = false;
return Promise.resolve(); return Promise.resolve();
} else { } else {
return new Promise(function(resolve) { return new Promise(resolve => {
child_process.execFile(npmCommand,['-v'],function(err) { child_process.execFile(npmCommand,['-v'],function(err) {
if (err) { if (err) {
log.info(log._("server.palette-editor.npm-not-found")); log.info(log._("server.palette-editor.npm-not-found"));