2015-11-09 12:29:48 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-11-09 12:29:48 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
var path = require("path");
|
|
|
|
var fs = require("fs");
|
|
|
|
|
|
|
|
var registry = require("./registry");
|
2018-04-26 13:32:05 +02:00
|
|
|
var library = require("./library");
|
2018-04-23 15:24:51 +02:00
|
|
|
var log;
|
2018-10-19 00:49:47 +02:00
|
|
|
var exec;
|
2015-11-09 12:29:48 +01:00
|
|
|
|
2018-04-26 13:32:05 +02:00
|
|
|
var events;
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
var child_process = require('child_process');
|
2016-10-12 23:30:32 +02:00
|
|
|
var npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
|
|
var paletteEditorEnabled = false;
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
var settings;
|
2018-01-24 16:07:43 +01:00
|
|
|
var moduleRe = /^(@[^/]+?[/])?[^/]+?$/;
|
2015-11-09 17:52:14 +01:00
|
|
|
var slashRe = process.platform === "win32" ? /\\|[/]/ : /[/]/;
|
2019-11-11 10:25:36 +01:00
|
|
|
var pkgurlRe = /^(https?|git(|\+https?|\+ssh|\+file)):\/\//;
|
2015-11-09 17:52:14 +01:00
|
|
|
|
2018-04-23 15:24:51 +02:00
|
|
|
function init(runtime) {
|
2018-04-26 13:32:05 +02:00
|
|
|
events = runtime.events;
|
2018-04-23 15:24:51 +02:00
|
|
|
settings = runtime.settings;
|
|
|
|
log = runtime.log;
|
2018-10-19 00:49:47 +02:00
|
|
|
exec = runtime.exec;
|
2015-11-09 12:29:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 11:08:39 +02:00
|
|
|
var activePromise = Promise.resolve();
|
|
|
|
|
2015-11-09 17:52:14 +01:00
|
|
|
function checkModulePath(folder) {
|
|
|
|
var moduleName;
|
|
|
|
var err;
|
|
|
|
var fullPath = path.resolve(folder);
|
|
|
|
var packageFile = path.join(fullPath,'package.json');
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
2015-11-09 17:52:14 +01:00
|
|
|
var pkg = require(packageFile);
|
|
|
|
moduleName = pkg.name;
|
|
|
|
if (!pkg['node-red']) {
|
|
|
|
// TODO: nls
|
|
|
|
err = new Error("Invalid Node-RED module");
|
|
|
|
err.code = 'invalid_module';
|
|
|
|
throw err;
|
|
|
|
}
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err2) {
|
2015-11-09 17:52:14 +01:00
|
|
|
err = new Error("Module not found");
|
|
|
|
err.code = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return moduleName;
|
|
|
|
}
|
|
|
|
|
2017-01-22 00:46:44 +01:00
|
|
|
function checkExistingModule(module,version) {
|
|
|
|
var info = registry.getModuleInfo(module);
|
|
|
|
if (info) {
|
|
|
|
if (!version || info.version === version) {
|
|
|
|
var err = new Error("Module already loaded");
|
|
|
|
err.code = "module_already_loaded";
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return true;
|
2015-11-09 17:52:14 +01:00
|
|
|
}
|
2017-01-22 00:46:44 +01:00
|
|
|
return false;
|
2015-11-09 17:52:14 +01:00
|
|
|
}
|
2019-11-11 10:25:36 +01:00
|
|
|
function installModule(module,version,url) {
|
2018-07-30 11:08:39 +02:00
|
|
|
activePromise = activePromise.then(() => {
|
|
|
|
//TODO: ensure module is 'safe'
|
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
var installName = module;
|
|
|
|
var isUpgrade = false;
|
|
|
|
try {
|
2019-11-11 10:25:36 +01:00
|
|
|
if (url && pkgurlRe.test(url)) {
|
|
|
|
// Git remote url or Tarball url - check the valid package url
|
|
|
|
installName = url;
|
|
|
|
} else if (moduleRe.test(module)) {
|
2018-07-30 11:08:39 +02:00
|
|
|
// Simple module name - assume it can be npm installed
|
|
|
|
if (version) {
|
|
|
|
installName += "@"+version;
|
|
|
|
}
|
|
|
|
} else if (slashRe.test(module)) {
|
|
|
|
// A path - check if there's a valid package.json
|
|
|
|
installName = module;
|
|
|
|
module = checkModulePath(module);
|
2017-01-22 00:46:44 +01:00
|
|
|
}
|
2018-07-30 11:08:39 +02:00
|
|
|
isUpgrade = checkExistingModule(module,version);
|
|
|
|
} catch(err) {
|
|
|
|
return reject(err);
|
2015-11-09 17:52:14 +01:00
|
|
|
}
|
2018-07-30 11:08:39 +02:00
|
|
|
if (!isUpgrade) {
|
|
|
|
log.info(log._("server.install.installing",{name: module,version: version||"latest"}));
|
2018-01-24 23:40:42 +01:00
|
|
|
} else {
|
2018-07-30 11:08:39 +02:00
|
|
|
log.info(log._("server.install.upgrading",{name: module,version: version||"latest"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
|
2019-03-07 10:03:25 +01:00
|
|
|
var args = ['install','--no-audit','--no-update-notifier','--save','--save-prefix="~"','--production',installName];
|
2018-07-30 11:08:39 +02:00
|
|
|
log.trace(npmCommand + JSON.stringify(args));
|
2018-10-19 00:49:47 +02:00
|
|
|
exec.run(npmCommand,args,{
|
|
|
|
cwd: installDir
|
|
|
|
}, true).then(result => {
|
|
|
|
if (!isUpgrade) {
|
|
|
|
log.info(log._("server.install.installed",{name:module}));
|
|
|
|
resolve(require("./index").addModule(module).then(reportAddedModules));
|
2015-11-09 12:29:48 +01:00
|
|
|
} else {
|
2018-10-19 00:49:47 +02:00
|
|
|
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));
|
2015-11-09 12:29:48 +01:00
|
|
|
}
|
2018-10-19 00:49:47 +02:00
|
|
|
}).catch(result => {
|
|
|
|
var output = result.stderr;
|
|
|
|
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")));
|
|
|
|
}
|
|
|
|
})
|
2018-01-24 23:40:42 +01:00
|
|
|
});
|
2018-07-30 11:08:39 +02:00
|
|
|
}).catch(err => {
|
|
|
|
// In case of error, reset activePromise to be resolvable
|
|
|
|
activePromise = Promise.resolve();
|
|
|
|
throw err;
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
2018-07-30 11:08:39 +02:00
|
|
|
return activePromise;
|
2015-11-09 12:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function reportAddedModules(info) {
|
|
|
|
//comms.publish("node/added",info.nodes,false);
|
|
|
|
if (info.nodes.length > 0) {
|
|
|
|
log.info(log._("server.added-types"));
|
|
|
|
for (var i=0;i<info.nodes.length;i++) {
|
|
|
|
for (var j=0;j<info.nodes[i].types.length;j++) {
|
|
|
|
log.info(" - "+
|
|
|
|
(info.nodes[i].module?info.nodes[i].module+":":"")+
|
|
|
|
info.nodes[i].types[j]+
|
|
|
|
(info.nodes[i].err?" : "+info.nodes[i].err:"")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportRemovedModules(removedNodes) {
|
|
|
|
//comms.publish("node/removed",removedNodes,false);
|
|
|
|
log.info(log._("server.removed-types"));
|
|
|
|
for (var j=0;j<removedNodes.length;j++) {
|
|
|
|
for (var i=0;i<removedNodes[j].types.length;i++) {
|
|
|
|
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removedNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function uninstallModule(module) {
|
2018-07-30 11:08:39 +02:00
|
|
|
activePromise = activePromise.then(() => {
|
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
if (/[\s;]/.test(module)) {
|
|
|
|
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);
|
2015-11-16 12:31:55 +01:00
|
|
|
|
2018-07-30 11:08:39 +02:00
|
|
|
try {
|
|
|
|
fs.statSync(moduleDir);
|
|
|
|
} catch(err) {
|
|
|
|
return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
|
|
|
|
}
|
2015-11-09 12:29:48 +01:00
|
|
|
|
2018-07-30 11:08:39 +02:00
|
|
|
var list = registry.removeModule(module);
|
|
|
|
log.info(log._("server.install.uninstalling",{name:module}));
|
2018-01-22 14:46:11 +01:00
|
|
|
|
2019-03-07 10:03:25 +01:00
|
|
|
var args = ['remove','--no-audit','--no-update-notifier','--save',module];
|
2018-07-30 11:08:39 +02:00
|
|
|
log.trace(npmCommand + JSON.stringify(args));
|
2018-01-22 14:46:11 +01:00
|
|
|
|
2018-10-19 00:49:47 +02:00
|
|
|
exec.run(npmCommand,args,{
|
|
|
|
cwd: installDir,
|
|
|
|
},true).then(result => {
|
|
|
|
log.info(log._("server.install.uninstalled",{name:module}));
|
|
|
|
reportRemovedModules(list);
|
|
|
|
library.removeExamplesDir(module);
|
|
|
|
resolve(list);
|
|
|
|
}).catch(result => {
|
|
|
|
var output = result.stderr;
|
|
|
|
log.warn(log._("server.install.uninstall-failed-long",{name:module}));
|
|
|
|
log.warn("------------------------------------------");
|
|
|
|
log.warn(output.toString());
|
|
|
|
log.warn("------------------------------------------");
|
|
|
|
reject(new Error(log._("server.install.uninstall-failed",{name:module})));
|
|
|
|
});
|
2018-07-30 11:08:39 +02:00
|
|
|
});
|
|
|
|
}).catch(err => {
|
|
|
|
// In case of error, reset activePromise to be resolvable
|
|
|
|
activePromise = Promise.resolve();
|
|
|
|
throw err;
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
2018-07-30 11:08:39 +02:00
|
|
|
return activePromise;
|
2015-11-09 12:29:48 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 23:30:32 +02:00
|
|
|
function checkPrereq() {
|
|
|
|
if (settings.hasOwnProperty('editorTheme') &&
|
|
|
|
settings.editorTheme.hasOwnProperty('palette') &&
|
|
|
|
settings.editorTheme.palette.hasOwnProperty('editable') &&
|
|
|
|
settings.editorTheme.palette.editable === false
|
|
|
|
) {
|
|
|
|
log.info(log._("server.palette-editor.disabled"));
|
|
|
|
paletteEditorEnabled = false;
|
2018-04-25 00:49:57 +02:00
|
|
|
return Promise.resolve();
|
2016-10-12 23:30:32 +02:00
|
|
|
} else {
|
2018-07-30 11:08:39 +02:00
|
|
|
return new Promise(resolve => {
|
2018-11-14 19:29:27 +01:00
|
|
|
child_process.execFile(npmCommand,['-v'],function(err,stdout) {
|
2016-10-12 23:30:32 +02:00
|
|
|
if (err) {
|
|
|
|
log.info(log._("server.palette-editor.npm-not-found"));
|
|
|
|
paletteEditorEnabled = false;
|
|
|
|
} else {
|
2018-11-14 19:29:27 +01:00
|
|
|
if (parseInt(stdout.split(".")[0]) < 3) {
|
|
|
|
log.info(log._("server.palette-editor.npm-too-old"));
|
|
|
|
paletteEditorEnabled = false;
|
|
|
|
} else {
|
|
|
|
paletteEditorEnabled = true;
|
|
|
|
}
|
2016-10-12 23:30:32 +02:00
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-11-09 12:29:48 +01:00
|
|
|
module.exports = {
|
|
|
|
init: init,
|
2016-10-12 23:30:32 +02:00
|
|
|
checkPrereq: checkPrereq,
|
2015-11-09 12:29:48 +01:00
|
|
|
installModule: installModule,
|
2016-10-12 23:30:32 +02:00
|
|
|
uninstallModule: uninstallModule,
|
|
|
|
paletteEditorEnabled: function() {
|
|
|
|
return paletteEditorEnabled
|
|
|
|
}
|
2015-11-09 12:29:48 +01:00
|
|
|
}
|