keeweb/app/scripts/plugins/plugin-manager.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-02-18 23:46:59 +01:00
'use strict';
2017-02-19 10:29:18 +01:00
const Backbone = require('backbone');
2017-02-18 23:46:59 +01:00
const Plugin = require('./plugin');
2017-02-19 10:29:18 +01:00
const PluginCollection = require('./plugin-collection');
2017-02-18 23:46:59 +01:00
2017-02-19 10:29:18 +01:00
const PluginManager = Backbone.Model.extend({
defaults: {
state: '',
2017-02-19 12:40:21 +01:00
installing: null,
uninstalling: null,
lastInstall: null,
2017-02-19 10:29:18 +01:00
plugins: new PluginCollection()
},
2017-02-18 23:46:59 +01:00
install(url) {
2017-02-19 12:40:21 +01:00
const lastInstall = { url, dt: new Date() };
this.set({ installing: url, lastInstall: lastInstall });
return Plugin.load(url).then(plugin =>
this.uninstall(plugin.id).then(() => {
2017-02-19 10:29:18 +01:00
return plugin.install().then(() => {
2017-02-19 15:57:41 +01:00
this.get('plugins').push(plugin);
2017-02-19 12:40:21 +01:00
this.set({ installing: null });
2017-02-19 10:29:18 +01:00
});
2017-02-19 12:40:21 +01:00
})
).catch(e => {
this.set({ installing: null, lastInstall: _.extend(lastInstall, { error: e.toString() }) });
2017-02-19 10:29:18 +01:00
throw e;
2017-02-18 23:46:59 +01:00
});
2017-02-19 12:40:21 +01:00
},
uninstall(id) {
const plugins = this.get('plugins');
const plugin = plugins.get(id);
if (!plugin) {
return Promise.resolve();
}
this.set('uninstalling', id);
return plugin.uninstall().then(() => {
plugins.remove(id);
this.set('uninstalling', null);
});
2017-02-18 23:46:59 +01:00
}
2017-02-19 10:29:18 +01:00
});
2017-02-18 23:46:59 +01:00
2017-02-19 10:29:18 +01:00
module.exports = new PluginManager();