mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
f16dc290b7
addresses #1789, #1364 - Moves ./core/server/loader -> ./core/bootstrap. The bootstrap file is only accessed once during startup, and it’s sole job is to ensure a config.js file exists (creating one if it doesn’t) and then validates the contents of the config file. Since this is directly related to the initializing the application is is appropriate to have it in the ./core folder, named bootstrap as that is what it does. This also improves the dependency graph, as now the bootstrap file require’s the ./core/server/config module and is responsible for passing in the validated config file. Whereas before we had ./core/server/config require’ing ./core/server/loader and running its init code and then passing that value back to itself, the flow is now more straight forward of ./core/bootstrap handling initialization and then instatiation of config module - Merges ./core/server/config/paths into ./core/server/config This flow was always confusing me to that some config options were on the config object, and some were on the paths object. This change now incorporates all of the variables previously defined in config/paths directly into the config module, and in extension, the config.js file. This means that you now have the option of deciding at startup where the content directory for ghost should reside. - broke out loader tests in config_spec to bootstrap_spec - updated all relevant files to now use config().paths - moved urlFor and urlForPost function into ./server/config/url.js
191 lines
No EOL
6.6 KiB
JavaScript
191 lines
No EOL
6.6 KiB
JavaScript
var _ = require('lodash'),
|
|
dataProvider = require('../models'),
|
|
when = require('when'),
|
|
errors = require('../errorHandling'),
|
|
config = require('../config'),
|
|
settings,
|
|
settingsObject,
|
|
settingsCollection,
|
|
settingsFilter,
|
|
updateSettingsCache,
|
|
readSettingsResult,
|
|
// Holds cached settings
|
|
settingsCache = {};
|
|
|
|
// ### Helpers
|
|
// Turn a settings collection into a single object/hashmap
|
|
settingsObject = function (settings) {
|
|
if (_.isObject(settings)) {
|
|
return _.reduce(settings, function (res, item, key) {
|
|
if (_.isArray(item)) {
|
|
res[key] = item;
|
|
} else {
|
|
res[key] = item.value;
|
|
}
|
|
return res;
|
|
}, {});
|
|
}
|
|
return (settings.toJSON ? settings.toJSON() : settings).reduce(function (res, item) {
|
|
if (item.toJSON) { item = item.toJSON(); }
|
|
if (item.key) { res[item.key] = item.value; }
|
|
return res;
|
|
}, {});
|
|
};
|
|
// Turn an object into a collection
|
|
settingsCollection = function (settings) {
|
|
return _.map(settings, function (value, key) {
|
|
return { key: key, value: value };
|
|
});
|
|
};
|
|
|
|
// Filters an object based on a given filter object
|
|
settingsFilter = function (settings, filter) {
|
|
return _.object(_.filter(_.pairs(settings), function (setting) {
|
|
if (filter) {
|
|
return _.some(filter.split(','), function (f) {
|
|
return setting[1].type === f;
|
|
});
|
|
}
|
|
return true;
|
|
}));
|
|
};
|
|
|
|
// Maintain the internal cache of the settings object
|
|
updateSettingsCache = function (settings) {
|
|
settings = settings || {};
|
|
|
|
if (!_.isEmpty(settings)) {
|
|
_.map(settings, function (setting, key) {
|
|
settingsCache[key].value = setting.value;
|
|
});
|
|
} else {
|
|
return when(dataProvider.Settings.findAll()).then(function (result) {
|
|
return when(readSettingsResult(result)).then(function (s) {
|
|
settingsCache = s;
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
readSettingsResult = function (result) {
|
|
var settings = {};
|
|
return when(_.map(result.models, function (member) {
|
|
if (!settings.hasOwnProperty(member.attributes.key)) {
|
|
var val = {};
|
|
val.value = member.attributes.value;
|
|
val.type = member.attributes.type;
|
|
settings[member.attributes.key] = val;
|
|
}
|
|
})).then(function () {
|
|
return when(config().paths.availableThemes).then(function (themes) {
|
|
var themeKeys = Object.keys(themes),
|
|
res = [],
|
|
i,
|
|
item;
|
|
for (i = 0; i < themeKeys.length; i += 1) {
|
|
//do not include hidden files
|
|
if (themeKeys[i].indexOf('.') !== 0) {
|
|
item = {};
|
|
item.name = themeKeys[i];
|
|
//data about files currently not used
|
|
//item.details = themes[themeKeys[i]];
|
|
if (themeKeys[i] === settings.activeTheme.value) {
|
|
item.active = true;
|
|
}
|
|
res.push(item);
|
|
}
|
|
}
|
|
settings.availableThemes = {};
|
|
settings.availableThemes.value = res;
|
|
settings.availableThemes.type = 'theme';
|
|
return settings;
|
|
});
|
|
});
|
|
};
|
|
|
|
settings = {
|
|
// #### Browse
|
|
|
|
// **takes:** options object
|
|
browse: function browse(options) {
|
|
// **returns:** a promise for a settings json object
|
|
if (settingsCache) {
|
|
return when(settingsCache).then(function (settings) {
|
|
//TODO: omit where type==core
|
|
return settingsObject(settingsFilter(settings, options.type));
|
|
}, errors.logAndThrowError);
|
|
}
|
|
},
|
|
|
|
// #### Read
|
|
|
|
// **takes:** either a json object containing a key, or a single key string
|
|
read: function read(options) {
|
|
if (_.isString(options)) {
|
|
options = { key: options };
|
|
}
|
|
|
|
if (settingsCache) {
|
|
return when(settingsCache[options.key]).then(function (setting) {
|
|
if (!setting) {
|
|
return when.reject({errorCode: 404, message: 'Unable to find setting: ' + options.key});
|
|
}
|
|
var res = {};
|
|
res.key = options.key;
|
|
res.value = setting.value;
|
|
return res;
|
|
}, errors.logAndThrowError);
|
|
}
|
|
},
|
|
|
|
// #### Edit
|
|
|
|
// **takes:** either a json object representing a collection of settings, or a key and value pair
|
|
edit: function edit(key, value) {
|
|
// Check for passing a collection of settings first
|
|
if (_.isObject(key)) {
|
|
//clean data
|
|
var type = key.type;
|
|
delete key.type;
|
|
delete key.availableThemes;
|
|
|
|
key = settingsCollection(key);
|
|
return dataProvider.Settings.edit(key).then(function (result) {
|
|
result.models = result;
|
|
return when(readSettingsResult(result)).then(function (settings) {
|
|
updateSettingsCache(settings);
|
|
}).then(function () {
|
|
return config.theme.update(settings, config().url).then(function () {
|
|
return settingsObject(settingsFilter(settingsCache, type));
|
|
});
|
|
});
|
|
}).otherwise(function (error) {
|
|
return dataProvider.Settings.read(key.key).then(function (result) {
|
|
if (!result) {
|
|
return when.reject({errorCode: 404, message: 'Unable to find setting: ' + key});
|
|
}
|
|
return when.reject({message: error.message});
|
|
});
|
|
});
|
|
}
|
|
return dataProvider.Settings.read(key).then(function (setting) {
|
|
if (!setting) {
|
|
return when.reject({errorCode: 404, message: 'Unable to find setting: ' + key});
|
|
}
|
|
if (!_.isString(value)) {
|
|
value = JSON.stringify(value);
|
|
}
|
|
setting.set('value', value);
|
|
return dataProvider.Settings.edit(setting).then(function (result) {
|
|
settingsCache[_.first(result).attributes.key].value = _.first(result).attributes.value;
|
|
}).then(function () {
|
|
return config.theme.update(settings, config().url).then(function () {
|
|
return settingsObject(settingsCache);
|
|
});
|
|
}).otherwise(errors.logAndThrowError);
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = settings;
|
|
module.exports.updateSettingsCache = updateSettingsCache; |