| 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
170×
170×
170×
170×
170×
170×
4×
4×
6×
6×
4×
165×
174×
165×
41×
41×
13×
13×
13×
10×
25×
22×
20×
19×
26×
10×
11×
13×
14×
13×
13×
10×
99×
99×
1×
98×
98×
40×
40×
1×
39×
39×
40×
40×
1×
39×
39×
1×
1×
171×
1×
179×
181×
|
'use strict'
let Build = require('./build')
let compile = require('./compile')
let debug = require('debug')('mako:runner')
let flatten = require('array-flatten')
let Hooks = require('./hooks')
let parse = require('./parse')
let path = require('path')
let Promise = require('bluebird')
let Tree = require('mako-tree')
let utils = require('./utils')
/**
* The core class for the mako builder. This is the public API developers will
* use when using the JS API directly.
*
* @class
*/
class Runner {
/**
* Builds a new instance.
*
* Available `options`:
* - `concurrency` manages concurrency throughout some phases of the build
* - `root` the project root for the tree
* - `tree` a pre-existing tree
*
* @param {Object} [options] Runner configuration.
*/
constructor (options) {
debug('initialize')
if (!options) options = Object.create(null)
this.hooks = new Hooks()
this.concurrency = options.concurrency || 100
this.tree = options.tree || new Tree(options.root)
if (options.plugins) this.use(options.plugins)
}
/**
* Add plugins to the builder. All arguments will be flattened into a single
* array of plugin functions.
*
* @return {Runner} This method is chainable.
*/
use () {
let plugins = flatten.from(arguments)
plugins.forEach(plugin => {
debug('using plugin %s', plugin.name)
plugin(this)
})
return this
}
/**
* Add file hooks for the given types.
*
* @param {String} hook The hook name.
* @param {String} type The file type(s).
* @param {Function} handler The handler fn.
* @return {Runner} This method is chainable.
*/
addFileHooks (hook, type, handler) {
let types = flatten([ type ])
types.forEach(type => this.hooks.add([ hook, type ], handler))
return this
}
/**
* Add build hooks.
*
* @param {String} hook The hook name.
* @param {Function} handler The handler fn.
* @return {Runner} This method is chainable.
*/
addBuildHooks (hook, handler) {
this.hooks.add(hook, handler)
return this
}
/**
* Used by plugins to mark a file as needing to be parsed again.
*
* @param {File} file The file to mark dirty.
*/
dirty (file) {
debug('marking file %s as dirty', utils.relative(file.path))
file.reset()
file.parsed = false
}
/**
* Adds a preparse hook handler.
*
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
preparse (handler) {
return this.addBuildHooks('preparse', handler)
}
/**
* Adds a preread hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
preread (type, handler) {
return this.addFileHooks('preread', type, handler)
}
/**
* Adds a read hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
read (type, handler) {
return this.addFileHooks('read', type, handler)
}
/**
* Adds a postread hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
postread (type, handler) {
return this.addFileHooks('postread', type, handler)
}
/**
* Adds a predependencies hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
predependencies (type, handler) {
return this.addFileHooks('predependencies', type, handler)
}
/**
* Adds a dependencies hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
dependencies (type, handler) {
return this.addFileHooks('dependencies', type, handler)
}
/**
* Adds a postparse hook handler.
*
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
postparse (handler) {
return this.addBuildHooks('postparse', handler)
}
/**
* Adds a precompile hook handler.
*
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
precompile (handler) {
return this.addBuildHooks('precompile', handler)
}
/**
* Adds a postdependencies hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
postdependencies (type, handler) {
return this.addFileHooks('postdependencies', type, handler)
}
/**
* Adds a prewrite hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
prewrite (type, handler) {
return this.addFileHooks('prewrite', type, handler)
}
/**
* Adds a write hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
write (type, handler) {
return this.addFileHooks('write', type, handler)
}
/**
* Adds a postwrite hook handler.
*
* @param {String} type The file type(s) to add the handler for.
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
postwrite (type, handler) {
return this.addFileHooks('postwrite', type, handler)
}
/**
* Adds a postcompile hook handler.
*
* @param {Function} handler The handler to use.
* @return {Runner} This method is chainable.
*/
postcompile (handler) {
return this.addBuildHooks('postcompile', handler)
}
/**
* Runs an analysis on the given `entries` for the given `build`.
*
* @return {Promise}
*/
parse () {
let entries = normalizeEntries(this.tree.root, arguments)
if (!entries.length) {
return Promise.reject(new Error('an entry file is required'))
}
let build = new Build(this, entries, this.tree)
return parse(build).tap(timing)
}
/**
* Assembles the given `entries` for the given `build`.
*
* @return {Promise}
*/
compile () {
let entries = normalizeEntries(this.tree.root, arguments)
if (!entries.length) {
return Promise.reject(new Error('an entry file is required'))
}
let build = new Build(this, entries, this.tree)
return compile(build).tap(timing)
}
/**
* The primary public interface, runs a complete build. (ie: both analysis and
* compile) All arguments passed here are assumed to be entries.
*
* @return {Promise}
*/
build () {
let entries = normalizeEntries(this.tree.root, arguments)
if (!entries.length) {
return Promise.reject(new Error('an entry file is required'))
}
let build = new Build(this, entries, this.tree)
return parse(build).then(compile).tap(timing)
}
}
// single export
module.exports = Runner
/**
* Helper for running timing debug output and chaining the build back.
*
* @param {Build} build The current build object.
*/
function timing (build) {
utils.timing(build.timing)
}
/**
* Convert the input args into a list of entries. Any strings will be run
* through `path.resolve()` to ensure absolute paths are being used.
*
* @param {Arguments} args The input arguments object.
* @return {Array} entries
*/
function normalizeEntries (root, args) {
return flatten.from(args).map(arg => {
return typeof arg === 'string' ? path.resolve(root, arg) : arg
})
}
|