This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseRecipe.js
123 lines (100 loc) · 3.65 KB
/
baseRecipe.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
import BaseGulp from './baseGulp'
import Preset from './preset'
import extend from 'extend'
import Util from 'gulp-util'
import stringify from 'stringify-object'
export const Default = {
watch: true,
debug: false
}
const BaseRecipe = class extends BaseGulp {
/**
*
* @param gulp - gulp instance
* @param preset - base preset configuration - either one from preset.js or a custom hash
* @param configs - customized overrides for this recipe
*/
constructor(gulp, preset, ...configs) {
super(gulp,
extend(true, {}, // extend presets here since BaseGulp doesn't use preset.
Default,
{baseDirectories: preset.baseDirectories},
Preset.resolveConfig(preset, ...configs)
)
)
// in case someone needs to inspect it later i.e. buildControl
this.preset = preset
this.registerTask()
this.registerWatchTask()
}
registerWatchTask() {
if (this.config.watch) {
// generate watch task e.g. sass:watch
let name = this.watchTaskName()
this.debug(`Registering task: ${Util.colors.green(name)}`)
this.watchFn = (done) => {
this.log(`[${Util.colors.green(name)}] watching ${this.config.watch.glob} ${stringify(this.config.watch.options)}...`)
return this.gulp.watch(this.config.watch.glob, this.config.watch.options, () => {
this.log(`Watched file changed, running ${this.taskName()}...`);
return Promise
.resolve(this.run(done, true))
.then(() => this.logFinish())
})
}
this.watchFn.description = this.createWatchDescription()
this.gulp.task(name, this.watchFn)
}
}
createWatchDescription() {
return Util.colors.grey(`|___ watches ${this.config.watch.options.cwd}/${this.config.watch.glob}`)
}
registerTask() {
// generate primary task e.g. sass
// set a fn for use by the task, also used by aggregate/series/parallel
let taskFn = (done) => {
//this.log(`Running task: ${Util.colors.green(name)}`)
if (this.config.debug) {
this.debugDump(`Executing ${Util.colors.green(this.displayName())} with config`, this.config)
}
return this.run(done)
}
// metadata for convenience so that gulp tasks show up with this instead of 'anonymous'
taskFn.displayName = this.displayName()
// assign it last so that displayName() can resolve this first as others may set it externally like <clean>
this.taskFn = taskFn
if (this.shouldRegisterTask()) {
// set the description
if (this.createDescription !== undefined) {
this.config.task.description = this.createDescription()
}
// set metadata on fn for discovery by gulp
this.taskFn.description = this.config.task.description
// register
let name = this.taskName()
this.debug(`Registering task: ${Util.colors.green(name)}`)
this.gulp.task(name, this.taskFn)
}
}
shouldRegisterTask() {
return (this.config.task && this.config.task.name)
}
displayName() {
if (this.taskFn !== undefined && this.taskFn.displayName) {
return this.taskFn.displayName
}
else if (this.shouldRegisterTask()) {
return this.taskName()
}
else {
// metadata for convenience so that gulp tasks show up with this instead of 'anonymous'
return `<${this.constructor.name}>`
}
}
logFinish(message = 'finished.') {
this.log(`[${Util.colors.green(this.taskName())}] ${message}`)
}
debugOptions() { // this controls the gulp-debug log statement, created to mirror our #debug's log format
return {title: `[${Util.colors.cyan('debug')}][${Util.colors.cyan(this.constructor.name)}]`}
}
}
export default BaseRecipe