This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
/
baseRegistry.js
92 lines (75 loc) · 2.29 KB
/
baseRegistry.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
import extend from 'extend'
import Util from 'gulp-util'
import stringify from 'stringify-object'
import DefaultRegistry from 'undertaker-registry'
export const Default = {
debug: false,
// preset: -- mixed in at runtime in the constructor to avoid issues in non-rails projects
global: {debug: false} // mixed into every config i.e debug: true
}
const BaseRegistry = class extends DefaultRegistry {
/**
*
* @param gulp - gulp instance
* @param config - customized overrides
*/
constructor(...configs) {
super()
this.config = extend(true, {}, Default, ...configs)
this.debugDump(`[${this.constructor.name}] using resolved config:`, this.config)
}
// ----------------------------------------------
// protected
/**
* Class-based configuration overrides. Shortcut to #keyConfig with class name lookup.
*/
classConfig(clazz) {
const className = clazz.prototype.constructor.name
return this.keyConfig(className)
}
/**
* config key-based configuration overrides:
* - these may be a single config hash or array of config hashes (last hash overrides earlier hashes)
* - in some cases, passing false for the class name may be implemented as omitting the registration of the recipe (see implementation of #init for details)
*
* @return - array - one or more configs as an array, so usage below in init is a universal spread/splat
*/
keyConfig(key) {
this.debug(`Resolving config for: ${key}...`)
let config = this.config[key]
this.debugDump(`config`, config)
if (config === undefined) {
config = [{}]
}
if (!Array.isArray(config)) {
config = [config]
}
// add global at the begining of the array
config.unshift(this.config.global)
return config
}
requireValue(value, name) {
if (value === undefined || value == null) {
this.notifyError(`${name} must be defined, found: ${value}`)
}
}
log(msg) {
Util.log(msg)
}
debug(msg) {
if (this.config.debug) {
this.log(`[${Util.colors.cyan('debug')}][${Util.colors.cyan(this.constructor.name)}] ${msg}`)
}
}
debugDump(msg, obj) {
this.debug(`${msg}:\n${this.dump(obj)}`)
}
dump(obj) {
return stringify(obj)
}
notifyError(error, e) {
this.log(error)
throw e
}
}
export default BaseRegistry