UNPKG

1.05 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var utils = require('../utils');
5
6module.exports = function(app) {
7 return {
8 type: ['array', 'boolean', 'string', 'object'],
9 normalize: function(val, key, config, schema) {
10 if (typeof val === 'undefined') {
11 return;
12 }
13
14 schema.update('cwd', config);
15 var cwd = path.resolve(config.cwd || app.cwd);
16
17 if (utils.isObject(val)) {
18 return val;
19 }
20
21 val = utils.arrayify(val);
22 val = normalizeArray(cwd, val);
23 config[key] = val;
24 return val;
25 }
26 };
27};
28
29function normalizePath(cwd, name) {
30 var fp = utils.stripQuotes(name);
31 if (!fp || utils.hasGlob(fp)) {
32 return fp;
33 }
34 if (fp.charAt(0) === '.' || /[\\\/]/.test(fp)) {
35 fp = path.resolve(fp);
36 }
37 if (!utils.exists(fp)) {
38 return name;
39 }
40 return path.resolve(cwd, fp);
41}
42
43function normalizeArray(cwd, arr) {
44 var len = arr.length;
45 var idx = -1;
46 var res = [];
47
48 while (++idx < len) {
49 res = res.concat(normalizePath(cwd, arr[idx]));
50 }
51 return res;
52}