|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +var notify = require('./notify.js') |
| 4 | + |
3 | 5 | /**
|
4 | 6 | * Normalize a list of config objects with `src`, `dest`
|
5 | 7 | * and optional `watch` properties.
|
6 |
| - * @param {Array|Object} configs |
| 8 | + * @param {Object} baseConfig |
7 | 9 | * @returns {Array}
|
8 | 10 | */
|
9 |
| -module.exports = function(configs) { |
10 |
| - return [].concat(configs) |
11 |
| - .filter(function(config) { |
12 |
| - return typeof config === 'object' && config.src && config.dest |
13 |
| - }) |
14 |
| - .map(function(config) { |
15 |
| - config.src = [].concat(config.src).filter(function(x) { |
16 |
| - return typeof x === 'string' |
| 11 | +module.exports = function(baseConfig) { |
| 12 | + var list = [] |
| 13 | + // Make full config objects if we have several build entries |
| 14 | + if (typeof baseConfig === 'object' && Array.isArray(baseConfig.builds)) { |
| 15 | + var common = Object.keys(baseConfig).filter(function(x){return x!=='builds'}) |
| 16 | + baseConfig.builds.forEach(function(build) { |
| 17 | + var config = {} |
| 18 | + common.concat(Object.keys(build)).forEach(function(key) { |
| 19 | + config[key] = key in build ? build[key] : baseConfig[key] |
17 | 20 | })
|
18 |
| - if (config.watch === true) { |
19 |
| - config.watch = config.src |
20 |
| - } |
21 |
| - return config |
| 21 | + list.push(config) |
| 22 | + }) |
| 23 | + } |
| 24 | + else { |
| 25 | + list.push(baseConfig) |
| 26 | + } |
| 27 | + // Normalize the src and watch properties |
| 28 | + var normalized = list.map(function(config) { |
| 29 | + if (typeof config !== 'object') return config |
| 30 | + config.src = [].concat(config.src).filter(function(x) { |
| 31 | + return typeof x === 'string' && x.trim() !== '' |
| 32 | + }) |
| 33 | + if (config.watch === true) { |
| 34 | + config.watch = config.src |
| 35 | + } |
| 36 | + return config |
| 37 | + }) |
| 38 | + // And filter final configs objects |
| 39 | + return normalized.filter(function(config) { |
| 40 | + var ok = typeof config === 'object' && |
| 41 | + typeof config.dest === 'string' && |
| 42 | + Array.isArray(config.src) && |
| 43 | + config.src.length > 0 |
| 44 | + if (!ok) notify({ |
| 45 | + title: '[assets-builder] Invalid config object', |
| 46 | + message: JSON.stringify(config) |
22 | 47 | })
|
| 48 | + return ok |
| 49 | + }) |
23 | 50 | }
|
0 commit comments