loading plugins from config

This commit is contained in:
antelle 2017-05-16 21:26:25 +02:00
parent d01e06349f
commit 7d86e8323c
4 changed files with 56 additions and 41 deletions

View File

@ -33,7 +33,10 @@ ready(() => {
.then(loadConfigs) .then(loadConfigs)
.then(initModules) .then(initModules)
.then(loadRemoteConfig) .then(loadRemoteConfig)
.then(showApp); .then(showApp)
.catch(e => {
appModel.appLogger.error('Error starting app', e);
});
function loadMixins() { function loadMixins() {
require('./mixins/view'); require('./mixins/view');
@ -68,21 +71,18 @@ ready(() => {
} }
function loadRemoteConfig() { function loadRemoteConfig() {
return new Promise((resolve, reject) => { return Promise.resolve().then(() => {
SettingsManager.setBySettings(appModel.settings); SettingsManager.setBySettings(appModel.settings);
const configParam = getConfigParam(); const configParam = getConfigParam();
if (configParam) { if (configParam) {
appModel.loadConfig(configParam, err => { return appModel.loadConfig(configParam).then(() => {
SettingsManager.setBySettings(appModel.settings); SettingsManager.setBySettings(appModel.settings);
if (err && !appModel.settings.get('cacheConfigSettings')) { }).catch(e => {
if (!appModel.settings.get('cacheConfigSettings')) {
showSettingsLoadError(); showSettingsLoadError();
reject(err); throw e;
} else {
resolve();
} }
}); });
} else {
resolve();
} }
}); });
} }

View File

@ -17,6 +17,7 @@ const Format = require('../util/format');
const UrlUtil = require('../util/url-util'); const UrlUtil = require('../util/url-util');
const AutoType = require('../auto-type'); const AutoType = require('../auto-type');
const Launcher = require('../comp/launcher'); const Launcher = require('../comp/launcher');
const PluginManager = require('../plugins/plugin-manager');
require('../mixins/protected-value-ex'); require('../mixins/protected-value-ex');
@ -49,39 +50,42 @@ const AppModel = Backbone.Model.extend({
_.forEach(Storage, prv => prv.init()); _.forEach(Storage, prv => prv.init());
}, },
loadConfig: function(configLocation, callback) { loadConfig: function(configLocation) {
this.appLogger.debug('Loading config from', configLocation); return new Promise((resolve, reject) => {
const ts = this.appLogger.ts(); this.appLogger.debug('Loading config from', configLocation);
const xhr = new XMLHttpRequest(); const ts = this.appLogger.ts();
xhr.open('GET', configLocation); const xhr = new XMLHttpRequest();
xhr.responseType = 'json'; xhr.open('GET', configLocation);
xhr.send(); xhr.responseType = 'json';
xhr.addEventListener('load', () => { xhr.send();
let response = xhr.response; xhr.addEventListener('load', () => {
if (!response) { let response = xhr.response;
const errorDesc = xhr.statusText === 'OK' ? 'Malformed JSON' : xhr.statusText; if (!response) {
this.appLogger.error('Error loading app config', errorDesc); const errorDesc = xhr.statusText === 'OK' ? 'Malformed JSON' : xhr.statusText;
return callback(true); this.appLogger.error('Error loading app config', errorDesc);
} return reject('Error loading app config');
if (typeof response === 'string') {
try {
response = JSON.parse(response);
} catch (e) {
this.appLogger.error('Error parsing response', e, response);
return callback(true);
} }
} if (typeof response === 'string') {
if (!response.settings) { try {
this.appLogger.error('Invalid app config, no settings section', response); response = JSON.parse(response);
return callback(true); } catch (e) {
} this.appLogger.error('Error parsing response', e, response);
this.appLogger.info('Loaded app config from', configLocation, this.appLogger.ts(ts)); return reject('Error parsing response');
this.applyUserConfig(response); }
callback(); }
}); if (!response.settings) {
xhr.addEventListener('error', () => { this.appLogger.error('Invalid app config, no settings section', response);
this.appLogger.error('Error loading app config', xhr.statusText, xhr.status); return reject('Invalid app config, no settings section');
callback(true); }
this.appLogger.info('Loaded app config from', configLocation, this.appLogger.ts(ts));
resolve(response);
});
xhr.addEventListener('error', () => {
this.appLogger.error('Error loading app config', xhr.statusText, xhr.status);
reject('Error loading app config');
});
}).then(config => {
return this.applyUserConfig(config);
}); });
}, },
@ -104,6 +108,9 @@ const AppModel = Backbone.Model.extend({
.reverse() .reverse()
.forEach(fi => this.fileInfos.unshift(fi)); .forEach(fi => this.fileInfos.unshift(fi));
} }
if (config.plugins) {
return Promise.all(config.plugins.map(plugin => PluginManager.installIfNew(plugin.url, plugin.manifest)));
}
}, },
addFile: function(file) { addFile: function(file) {

View File

@ -45,6 +45,11 @@ const PluginManager = Backbone.Model.extend({
}); });
}, },
installIfNew(url, expectedManifest) {
const plugin = this.get('plugins').find({ url });
return plugin ? Promise.resolve() : this.install(url, expectedManifest);
},
uninstall(id) { uninstall(id) {
const plugins = this.get('plugins'); const plugins = this.get('plugins');
const plugin = plugins.get(id); const plugin = plugins.get(id);

View File

@ -49,5 +49,8 @@
"path": "webdav-url", "path": "webdav-url",
"options": { "user": "", "password": "" } "options": { "user": "", "password": "" }
}], }],
"plugins": [{
"url": ""
}],
"showOnlyFilesFromConfig": false "showOnlyFilesFromConfig": false
} }