all files / keystone/lib/core/ options.js

73.13% Statements 49/67
54.9% Branches 28/51
100% Functions 5/5
73.13% Lines 49/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153                                 147× 82×     65×                                                                                     12×   12×   12×     12×     65× 65×                       18× 18×                                                      
var callerId = require('caller-id');
var cloudinary = require('cloudinary');
var path = require('path');
var url = require('url');
 
/**
 * This file contains methods specific to dealing with Keystone's options.
 * All exports are added to the Keystone.prototype
 */
 
// Determines if path is absolute or relative
function isAbsolutePath (value) {
	return path.resolve(value) === path.normalize(value).replace(new RegExp(path.sep + '$'), '');
}
 
/**
 * Sets keystone options
 *
 * Example:
 *     keystone.set('user model', 'User') // sets the 'user model' option to `User`
 */
exports.set = function (key, value) {
 
	if (arguments.length === 1) {
		return this._options[key];
	}
 
	switch (key) {
		// throw on unsupported options
		case 'email rules':
			throw new Error('The option "' + key + '" is no longer supported. See https://github.com/keystonejs/keystone/wiki/0.3.x-to-0.4.x-Changes');
		// handle special settings
		case 'cloudinary config':
			if (typeof value === 'string') {
				var parts = url.parse(value, true);
				var auth = parts.auth ? parts.auth.split(':') : [];
				value = {
					cloud_name: parts.host,
					api_key: auth[0],
					api_secret: auth[1],
					private_cdn: parts.pathname != null,
					secure_distribution: parts.pathname && parts.pathname.substring(1),
				};
			}
			cloudinary.config(value);
			value = cloudinary.config();
			break;
		case 'auth':
			Eif (value === true && !this.get('session')) {
				this.set('session', true);
			}
			break;
		case 'nav':
			this.nav = this.initNav(value);
			break;
		case 'mongo':
			if (typeof value !== 'string') {
				if (Array.isArray(value) && (value.length === 2 || value.length === 3)) {
					console.log('\nWarning: using an array for the `mongo` option has been deprecated.\nPlease use a mongodb connection string, e.g. mongodb://localhost/db_name instead.\n\n'
					+ 'Support for arrays as the `mongo` setting will be removed in a future version.');
					value = (value.length === 2) ? 'mongodb://' + value[0] + '/' + value[1] : 'mongodb://' + value[0] + ':' + value[2] + '/' + value[1];
				} else {
					console.error('\nInvalid Configuration:\nThe `mongo` option must be a mongodb connection string, e.g. mongodb://localhost/db_name\n');
					process.exit(1);
				}
			}
			break;
		case 'module root':
			// if relative path is used, resolve it based on the caller's path
			if (!isAbsolutePath(value)) {
				var caller = callerId.getData();
				value = path.resolve(path.dirname(caller.filePath), value);
			}
			break;
		case 'app':
			this.app = value;
			break;
		case 'mongoose':
			this.mongoose = value;
			break;
		case 'frame guard':
			var validFrameGuardOptions = ['deny', 'sameorigin'];
 
			if (value === true) {
				value = 'deny';
			}
			if (typeof value === 'string') {
				value = value.toLowerCase();
				if (validFrameGuardOptions.indexOf(value) < 0) {
					value = false;
				}
			} else if (typeof value !== 'boolean') {
				value = false;
			}
			break;
	}
 
	this._options[key] = value;
	return this;
};
 
 
/**
 * Sets multiple keystone options.
 *
 * Example:
 *     keystone.options({test: value}) // sets the 'test' option to `value`
 */
exports.options = function (options) {
	Iif (!arguments.length) {
		return this._options;
	}
	if (typeof options === 'object') {
		var keys = Object.keys(options);
		var i = keys.length;
		var k;
		while (i--) {
			k = keys[i];
			this.set(k, options[k]);
		}
	}
	return this._options;
};
 
 
/**
 * Gets keystone options
 *
 * Example:
 *     keystone.get('test') // returns the 'test' value
 */
exports.get = exports.set;
 
/**
 * Gets an expanded path option, expanded to include moduleRoot if it is relative
 *
 * Example:
 *     keystone.get('pathOption', 'defaultValue')
 */
exports.getPath = function (key, defaultValue) {
	return this.expandPath(this.get(key) || defaultValue);
};
 
/**
 * Expands a path to include moduleRoot if it is relative
 */
exports.expandPath = function (pathValue) {
	pathValue = (typeof pathValue === 'string' && pathValue.substr(0, 1) !== path.sep && pathValue.substr(1, 2) !== ':\\')
		? path.join(this.get('module root'), pathValue)
		: pathValue;
	return pathValue;
};