Ghost/core/server/apps/index.js

98 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-01-21 12:45:27 +04:00
2014-02-05 12:40:30 +04:00
var _ = require('lodash'),
Promise = require('bluebird'),
errors = require('../errors'),
2014-01-21 12:45:27 +04:00
api = require('../api'),
loader = require('./loader'),
i18n = require('../i18n'),
2014-01-21 12:45:27 +04:00
// Holds the available apps
availableApps = {};
function getInstalledApps() {
return api.settings.read({context: {internal: true}, key: 'installedApps'}).then(function (response) {
var installed = response.settings[0];
2014-01-21 12:45:27 +04:00
installed.value = installed.value || '[]';
try {
installed = JSON.parse(installed.value);
} catch (e) {
return Promise.reject(e);
2014-01-21 12:45:27 +04:00
}
return installed;
});
}
function saveInstalledApps(installedApps) {
return getInstalledApps().then(function (currentInstalledApps) {
var updatedAppsInstalled = _.uniq(installedApps.concat(currentInstalledApps));
return api.settings.edit({settings: [{key: 'installedApps', value: updatedAppsInstalled}]}, {context: {internal: true}});
2014-01-21 12:45:27 +04:00
});
}
module.exports = {
init: function () {
var appsToLoad;
try {
// We have to parse the value because it's a string
api.settings.read({context: {internal: true}, key: 'activeApps'}).then(function (response) {
var aApps = response.settings[0];
2014-01-21 12:45:27 +04:00
appsToLoad = JSON.parse(aApps.value) || [];
});
} catch (e) {
errors.logError(
i18n.t('errors.apps.failedToParseActiveAppsSettings.error', {message: e.message}),
i18n.t('errors.apps.failedToParseActiveAppsSettings.context'),
i18n.t('errors.apps.failedToParseActiveAppsSettings.help')
2014-01-21 12:45:27 +04:00
);
return Promise.resolve();
2014-01-21 12:45:27 +04:00
}
// Grab all installed apps, install any not already installed that are in appsToLoad.
return getInstalledApps().then(function (installedApps) {
var loadedApps = {},
recordLoadedApp = function (name, loadedApp) {
// After loading the app, add it to our hash of loaded apps
loadedApps[name] = loadedApp;
return Promise.resolve(loadedApp);
2014-01-21 12:45:27 +04:00
},
loadPromises = _.map(appsToLoad, function (app) {
// If already installed, just activate the app
if (_.contains(installedApps, app)) {
return loader.activateAppByName(app).then(function (loadedApp) {
return recordLoadedApp(app, loadedApp);
});
}
// Install, then activate the app
return loader.installAppByName(app).then(function () {
return loader.activateAppByName(app);
}).then(function (loadedApp) {
return recordLoadedApp(app, loadedApp);
});
});
return Promise.all(loadPromises).then(function () {
2014-01-21 12:45:27 +04:00
// Save our installed apps to settings
return saveInstalledApps(_.keys(loadedApps));
}).then(function () {
// Extend the loadedApps onto the available apps
_.extend(availableApps, loadedApps);
}).catch(function (err) {
2014-01-21 12:45:27 +04:00
errors.logError(
err.message || err,
i18n.t('errors.apps.appWillNotBeLoaded.error'),
i18n.t('errors.apps.appWillNotBeLoaded.help')
2014-01-21 12:45:27 +04:00
);
});
});
},
availableApps: availableApps
};