-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
339 lines (280 loc) · 6.11 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'use strict';
var typeOf = require('kind-of');
var extend = require('extend-shallow');
var utils = require('./lib/utils');
function normalize(config, dest, options) {
if (arguments.length > 1) {
config = toObject(config, dest, options);
}
var res = null;
switch(typeOf(config)) {
case 'string':
res = normalizeString(config);
break;
case 'object':
res = normalizeObject(config);
break;
case 'array':
res = normalizeArray(config);
break;
default: {
throw new TypeError('unsupported argument type: ' + config);
}
}
var thisArg = {};
if (utils.isObject(this) && this.options) {
thisArg = this.options;
}
// copy `thisArg` options onto config root
copyOptions(res, thisArg);
return formatObject(res);
}
/**
* Move reserved options properties from `obj` to `obj.options`
*/
function normalizeOptions(obj) {
for (var key in obj) {
if (utils.optsKeys.indexOf(key) > -1) {
obj.options = obj.options || {};
obj.options[key] = obj[key];
delete obj[key];
}
}
return obj;
}
/**
* Copy options
*/
function copyOptions(config, thisArg) {
var ctx = thisArg.options || thisArg;
config.options = extend({}, ctx, config.options);
return config;
}
/**
* Convert args list to a config object.
*/
function toObject(src, dest, options) {
if (!utils.isObject(options)) {
options = {};
}
var config = {};
if (utils.isObject(dest)) {
options = extend({}, options, dest);
dest = null;
}
if (utils.isObject(src)) {
config = src;
}
if (isValidSrc(src)) {
config.src = src;
} else if (Array.isArray(src)) {
config = normalize(src);
}
if (typeof dest === 'string') {
config.dest = dest;
}
if (options.hasOwnProperty('options')) {
options = extend({}, options, options.options);
delete options.options;
}
config.options = extend({}, config.options, options);
return config;
}
/**
* Object
*/
function normalizeObject(val) {
if (val.hasOwnProperty('path')) {
val.src = val.path;
}
val = normalizeOptions(val);
if (val.options && val.options.files) {
var config = {};
config.files = val.options.files;
delete val.options.files;
return normalize(config, val);
}
// if src/dest are on options, move them to root
val = utils.move(val, 'src');
val = utils.move(val, 'dest');
// allow src to be a getter
if ('src' in val || val.hasOwnProperty('dest')) {
return toFiles(val);
}
if (!val.hasOwnProperty('files')) {
return filesObjects(val);
}
if (Array.isArray(val.files)) {
return reduceFiles(val.files, val);
}
if (val.files && !utils.isObject(val.files)) {
throw new TypeError('expected "files" to be an array or object');
}
val.files = normalizeFiles(val);
return val;
}
/**
* Ensure that `src` on the given val is an array
*
* @param {Object} `val` Object with a `src` property
* @return {Object}
*/
function normalizeSrc(val) {
if (!val.src) return val;
val.src = utils.arrayify(val.src);
return val;
}
/**
* String
*/
function normalizeString(val) {
return toFiles({src: [val]});
}
/**
* Array
*/
function normalizeArray(arr) {
if (isValidSrc(arr[0])) {
return toFiles({src: arr});
}
return reduceFiles(arr);
}
/**
* Files property
*/
function normalizeFiles(val) {
var res = normalize(val.files);
return res.files;
}
/**
* Normalize all objects in a `files` array.
*
* @param {Array} `files`
* @return {Array}
*/
function reduceFiles(files, orig) {
var config = {files: []};
var len = files.length;
var idx = -1;
while (++idx < len) {
var val = normalize(files[idx]);
config.files = config.files.concat(val.files);
}
return copyNonfiles(config, orig);
}
/**
* Create a `files` array from a src-dest object.
*
* ```js
* // converts from:
* { src: '*.js', dest: 'foo/' }
*
* // to:
* { files: [{ src: ['*.js'], dest: 'foo/' }] }
* ```
*/
function copyNonfiles(config, provider) {
if (!provider) return config;
for (var key in provider) {
if (!isFilesKey(key)) {
config[key] = provider[key];
}
}
return config;
}
/**
* Create a `files` array from a src-dest object.
*
* ```js
* // converts from:
* { src: '*.js', dest: 'foo/' }
*
* // to:
* { files: [{ src: ['*.js'], dest: 'foo/' }] }
* ```
*/
function toFiles(val) {
var config = {files: [normalizeSrc(val)]};
return copyNonfiles(config, val);
}
/**
* When `src`, `dest` and `files` are absent from the
* object, we check to see if file objects were defined.
*
* ```js
* // converts from:
* { 'foo/': '*.js' }
*
* // to
* { files: [{ src: ['*.js'], dest: 'foo/' }] }
* ```
*/
function filesObjects(val) {
var res = {};
if (val.options) res.options = val.options;
res.files = [];
for (var key in val) {
if (key !== 'options') {
var file = {};
if (val.options) file.options = val.options;
file.src = utils.arrayify(val[key]);
file.dest = key;
res.files.push(file);
}
}
return res;
}
/**
* Optionally sort the keys in all of the files objects.
* Helps with debugging.
*
* @param {Object} `val` Pass `{sort: true}` on `val.options` to enable sorting.
* @return {Object}
*/
function formatObject(val) {
if (val.options && val.options.format === false) {
return val;
}
var res = { options: val.options };
res.files = val.files;
for (var key in val) {
if (key === 'files' || key === 'options') {
continue;
}
res[key] = val[key];
}
var len = res.files.length;
var idx = -1;
while (++idx < len) {
var ele = res.files[idx];
var obj = {};
obj.options = {};
obj.src = ele.src || [];
obj.dest = ele.dest || '';
copyNonfiles(obj, ele);
if (!ele.options) {
obj.options = res.options;
}
res.files[idx] = obj;
}
return res;
}
/**
* Boolean checks
*/
function isFilesKey(key) {
return ['src', 'dest', 'files'].indexOf(key) > -1;
}
function isValidSrc(val) {
if (typeof val === 'string') {
return true;
}
if (Array.isArray(val)) {
return val[0].src || val[0].dest;
}
return false;
}
/**
* Expose `normalize`
*/
module.exports = normalize;