Skip to content

Commit 917c5ef

Browse files
author
Florens Verschelde
committed
feat(config): allow sharing options between build configs
1 parent da8b427 commit 917c5ef

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

gulp/helpers/normalize.js

+40-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
'use strict'
22

3+
var notify = require('./notify.js')
4+
35
/**
46
* Normalize a list of config objects with `src`, `dest`
57
* and optional `watch` properties.
6-
* @param {Array|Object} configs
8+
* @param {Object} baseConfig
79
* @returns {Array}
810
*/
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]
1720
})
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)
2247
})
48+
return ok
49+
})
2350
}

0 commit comments

Comments
 (0)