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

89 lines
2.7 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-19 18:51:52 +01:00
const SettingsStore = require('../comp/settings-store');
const Logger = require('../util/logger');
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
2017-02-19 18:51:52 +01:00
logger: new Logger('plugin-mgr'),
init() {
const ts = this.logger.ts();
return Promise.resolve().then(() => {
const state = this.loadState();
2017-02-19 20:09:09 +01:00
if (!state || !state.plugins || !state.plugins.length) {
2017-02-19 18:51:52 +01:00
return;
}
const promises = state.plugins.map(plugin => this.loadPlugin(plugin));
return Promise.all(promises).then(loadedPlugins => {
const plugins = this.get('plugins');
plugins.add(loadedPlugins.filter(plugin => plugin));
this.logger.info(`Loaded ${plugins.length} plugins`, this.logger.ts(ts));
});
});
},
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 });
2017-02-19 18:51:52 +01:00
return Plugin.loadFromUrl(url).then(plugin =>
2017-02-19 12:40:21 +01:00
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 18:51:52 +01:00
this.saveState();
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-19 19:14:09 +01:00
this.saveState();
2017-02-19 12:40:21 +01:00
});
2017-02-19 18:51:52 +01:00
},
loadPlugin(desc) {
const plugin = new Plugin(desc.manifest, desc.url, true);
return plugin.install()
.then(() => plugin)
.catch(() => undefined);
},
saveState() {
SettingsStore.save('plugins', {
plugins: this.get('plugins').map(plugin => ({
manifest: plugin.get('manifest'),
url: plugin.get('url')
}))
});
},
loadState() {
return SettingsStore.load('plugins');
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();