-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (91 loc) · 2.06 KB
/
index.js
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
/*!
* base-config <https://github.com/jonschlinkert/base-config>
*
* Copyright (c) 2015-2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var utils = require('./utils');
/**
* Expose `config`
*/
module.exports = function() {
return create('config');
};
/**
* Create a function for mapping `app` properties onto the
* given `prop` namespace.
*
* @param {String} `prop` The namespace to use
* @param {Object} `argv`
* @return {Object}
* @api public
*/
function create(prop) {
if (typeof prop !== 'string') {
throw new Error('expected the first argument to be a string.');
}
return function(app) {
if (this.isRegistered('base-' + prop)) return;
// map config
var config = utils.mapper(app)
// store/data
.map('store', store(app.store))
.map('data')
// options
.map('enable')
.map('disable')
.map('option')
.alias('options', 'option')
// get/set
.map('set')
.map('del')
// Expose `prop` (config) on the instance
app.define(prop, proxy(config));
// Expose `process` on app[prop]
app[prop].process = config.process;
};
function store(app) {
if (!app) return {};
var mapper = utils.mapper(app);
app.define(prop, proxy(mapper));
return mapper;
}
}
/**
* Proxy to support `app.config` as a function or object
* with methods, allowing the user to do either of
* the following:
*
* ```js
* base.config({foo: 'bar'});
*
* // or
* base.config.map('foo', 'bar');
* ```
*/
function proxy(config) {
function fn(key, val) {
if (typeof val === 'string') {
config.alias.apply(config, arguments);
return config;
}
if (typeof key === 'string') {
config.map.apply(config, arguments);
return config;
}
if (!utils.isObject(key)) {
throw new TypeError('expected key to be a string or object');
}
for (var prop in key) {
fn(prop, key[prop]);
}
return config;
}
fn.__proto__ = config;
return fn;
}
/**
* Expose `create`
*/
module.exports.create = create;