-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
293 lines (259 loc) · 6.86 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*!
* plasma <https://github.com/jonschlinkert/plasma>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var utils = require('./utils');
/**
* Create an instance of `Plasma`, optionally passing
* an object of `data` to initialize with.
*
* ```js
* var Plasma = require('plasma');
* var plasma = new Plasma();
*
* // load some data
* plasma.load(['*.json', 'data/*.yml']);
* plasma.load({a: 'b', c: 'd'});
* ```
*
* @param {Object} `data`
* @api public
*/
function Plasma(options) {
this.options = options || {};
this.cache = this.options.cache || {};
this.dataLoaders = [];
this.initPlasma();
}
/**
* Initialize defaults
*/
Plasma.prototype.initPlasma = function() {
if (typeof this.options.namespace === 'undefined') {
this.options.namespace = true;
}
this.dataLoader('json', function(fp) {
return JSON.parse(tryRead(fp));
});
};
/**
* Register a data loader for reading data. _(Note that as of
* 0.9.0, plasma no longer reads YAML files by default)_.
*
* ```js
* var fs = require('fs');
* var yaml = require('js-yaml');
*
* plasma.dataLoader('yml', function(fp) {
* var str = fs.readFileSync(fp, 'utf8');
* return yaml.safeLoad(str);
* });
*
* plasma.load('foo.yml');
* ```
*
* @param {String} `ext` The file extension to match to the loader.
* @param {Function} `fn` The loader function.
* @api public
*/
Plasma.prototype.dataLoader = function(name, fn) {
this.dataLoaders.push({name: name, fn: fn});
return this;
};
Plasma.prototype.matchLoader = function(fp) {
var len = this.dataLoaders.length, i = -1;
var loaders = this.dataLoaders;
var ext = path.extname(fp);
var fns = [];
while (++i < len) {
var loader = loaders[i];
var name = loader.name;
if (typeof name === 'string' && ext === utils.formatExt(name)) {
fns.push(loader.fn);
} else if (utils.typeOf(name) === 'regexp' && name.test(ext)) {
fns.push(loader.fn);
}
}
return fns;
};
/**
* Load data from the given `value`.
*
* @param {String|Array|Object} `value` String or array of glob patterns or file paths, or an object or array of objects.
* @param {Object} `options`
* @return {Object}
* @api private
*/
Plasma.prototype.load = function(key, val, options) {
if (utils.typeOf(options) === 'function') {
return options.call(this, val);
}
var opts = utils.extend({}, this.options, options);
if (utils.typeOf(val) === 'object') {
return this.merge(val);
}
if (typeof val === 'string') {
// if it's not a glob, don't use globby
if (!utils.isGlob(val)) {
return this.mergeFile(val, options);
}
// if it is, arrayify and fall through
val = [val];
}
if (Array.isArray(val)) {
return this.mergeArray(val, opts);
}
};
/**
* Merge an array of objects onto the `cache` object. We
* keep this method separate for performance reasons.
*
* @param {String} `patterns` Glob patterns to pass to [globby]
* @param {Object} `opts` Options for globby, or pass a custom `parse` or `name`.
* @return {Object}
* @api private
*/
Plasma.prototype.merge = function(obj) {
return utils.merge(this.cache, obj);
};
/**
* Merge an array of objects onto the `cache` object. We
* keep this method separate for performance reasons.
*
* @param {String} `patterns` Glob patterns to pass to [globby]
* @param {Object} `opts` Options for globby, or pass a custom `parse` or `name`.
* @return {Object}
* @api private
*/
Plasma.prototype.mergeArray = function(val, opts) {
var len = val.length, i = 0;
while (len--) {
var ele = val[i++];
if (utils.typeOf(ele) !== 'object') {
ele = this.glob(val, opts);
if (utils.typeOf(ele) !== 'object') {
return ele;
}
}
utils.merge(this.cache, ele);
}
return this.cache;
};
/**
* Merge an array of objects onto the `cache` object. We
* keep this method separate for performance reasons.
*
* @param {String} `patterns` Glob patterns to pass to [globby]
* @param {Object} `opts` Options for globby, or pass a custom `parse` or `name`.
* @return {Object}
* @api private
*/
Plasma.prototype.mergeFile = function(fp, options) {
var opts = utils.extend({cwd: process.cwd()}, this.options, options);
fp = utils.relative(path.resolve(opts.cwd, fp));
var key = name(fp, opts);
var obj = read.call(this, fp, opts);
var res = {};
// if the filename is `data`, or if `namespace` is
// turned off, merge data onto the root
if (key === 'data' || opts.namespace === false) {
res = this.merge(obj);
} else {
res[key] = obj;
}
return res;
};
/**
* Load an object from all files matching the given `patterns`
* and `options`.
*
* @param {String} `patterns` Glob patterns to pass to [globby]
* @param {Object} `opts` Options for globby, or pass a custom `parse` or `name`.
* @return {Object}
* @api private
*/
Plasma.prototype.glob = function(patterns, options) {
var opts = utils.extend({cwd: process.cwd()}, this.options, options);
var files = utils.glob.sync(patterns, opts);
var self = this;
// if this happens, it's probably an array of non-filepath
// strings, or invalid path/glob patterns
if (!files || !files.length) return patterns;
var len = files.length, i = 0;
while (len--) {
utils.merge(this.cache, self.mergeFile(files[i++], opts));
}
return this.cache;
};
/**
* Default `namespace` function. Pass a function on `options.namespace`
* to customize.
*
* @param {String} `fp`
* @param {Object} `opts`
* @return {String}
* @api private
*/
function name(fp, options) {
var opts = options || {};
if (typeof opts.namespace === 'function') {
return opts.namespace(fp, opts);
}
if (typeof opts.namespace === false) {
return fp;
}
var ext = path.extname(fp);
return path.basename(fp, ext);
}
/**
* Default `read` function. Pass a function on `options.read`
* to customize.
*
* @param {String} `fp`
* @param {Object} `opts`
* @return {String}
* @api private
*/
function read(fp, opts) {
if (opts && opts.read) {
return opts.read(fp, opts);
}
return readData.call(this, fp, opts);
}
/**
* Utility for reading data files.
*
* @param {String} `fp` Filepath to read.
* @param {Object} `options` Options to pass to [js-yaml]
* @api private
*/
function readData(fp, options) {
// shallow clone options
var opts = utils.extend({}, options);
// get the loader for this file.
var ext = opts.lang || path.extname(fp);
if (ext && ext.charAt(0) !== '.') {
ext = '.' + ext;
}
if (!this.dataLoaders.hasOwnProperty(ext)) {
return this.dataLoader('read')(fp, opts);
}
return this.dataLoader(ext)(fp, opts);
}
function tryRead(fp) {
try {
return fs.readFileSync(fp, 'utf8');
} catch(err) {
var ext = path.extname(ext);
throw new Error('plasma could not read ' + fp + ' ' + err);
}
}
/**
* Expose `Plasma`
*/
module.exports = Plasma;