Allow core:manage-palette action to auto install modules

This commit is contained in:
GogoVega
2024-10-27 16:30:33 +01:00
parent 44b4c7da24
commit 8d310c6c1c
3 changed files with 99 additions and 5 deletions

View File

@@ -1971,15 +1971,27 @@ RED.nodes = (function() {
// Provide option to install missing modules
notificationOptions.buttons = [
{
text: "Manage dependencies",
class:"primary",
text: RED._("palette.editor.manageModules"),
class: "primary",
click: function(e) {
unknownNotification.close();
RED.actions.invoke('core:manage-palette', {
view: 'install',
filter: '"' + Object.keys(options.modules).join('", "') + '"'
})
});
}
},
{
text: RED._("palette.editor.installEverything"),
class: "pull-left",
click: function(e) {
unknownNotification.close();
RED.actions.invoke('core:manage-palette', {
autoInstall: true,
modules: options.modules
});
}
}
]

View File

@@ -626,6 +626,9 @@ RED.palette.editor = (function() {
})
RED.actions.add("core:manage-palette", function(opts) {
if (opts && opts.autoInstall && opts.modules) {
autoInstallModules(opts.modules);
} else {
RED.userSettings.show('palette');
if (opts) {
if (opts.view) {
@@ -639,7 +642,8 @@ RED.palette.editor = (function() {
}
}
}
});
}
});
RED.events.on('registry:module-updated', function(ns) {
refreshNodeModule(ns.module);
@@ -1501,6 +1505,80 @@ RED.palette.editor = (function() {
})
}
function autoInstallModules(modules) {
if (RED.settings.get('externalModules.palette.allowInstall', true) === false) {
console.error(new Error('Palette not editable'));
return;
}
let notification;
const notificationOptions = {
fixed: true,
buttons: [
{
text: RED._("common.label.close"),
click: function () {
notification.close();
}
}, {
text: RED._("eventLog.view"),
click: function () {
notification.close();
RED.actions.invoke("core:show-event-log");
}
}
]
};
const moduleArray = Object.entries(modules);
const installModule = function (module) {
const [moduleName, moduleVersion] = module;
const msg = RED.notify(RED._("palette.editor.installing", { module: moduleName }));
if (!notification) {
notification = RED.notify(msg, notificationOptions);
} else {
notification.update(msg);
}
// TODO: add spinner to a div below the title
//const spinner = RED.utils.addSpinnerOverlay(container, true);
RED.eventLog.startEvent(RED._("palette.editor.confirm.button.install") + " : " + moduleName + " " + moduleVersion);
installNodeModule(moduleName, moduleVersion, undefined, function(xhr, textStatus, err) {
//spinner.close();
if (err && xhr.status === 504) {
notification.update(RED._("palette.editor.errors.installTimeout"), {
modal: true,
fixed: true,
buttons: notificationOptions.buttons
});
} else if (xhr) {
if (xhr.responseJSON) {
notification.update(RED._("palette.editor.errors.installFailed", { module: moduleName, message:xhr.responseJSON.message }), {
type: "error",
modal: true,
fixed: true,
buttons: notificationOptions.buttons
});
}
} else {
if (moduleArray.length) {
installModule(moduleArray.shift());
} else {
notification.update(RED._("palette.editor.successfulInstall"), { ...notificationOptions, type: "success", timeout: 1000 });
}
}
});
};
if (moduleArray.length) {
installModule(moduleArray.shift());
}
}
return {
init: init,
install: install