-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (56 loc) · 1.7 KB
/
index.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
var through = require('through2')
, Freemarker = require('./lib/freemarker.js')
, PluginError = require('gulp-util').PluginError
module.exports = function(options) {
if (!arguments.length)
throw new PluginError('gulp-freemarker', 'invoked with no arguments!')
if (!options.viewRoot)
throw new PluginError('gulp-freemarker', 'viewRoot option is mandatory!')
var engine = new Freemarker(options)
return through.obj(function(file, encoding, callback) {
if (file.isNull()){
this.push(file)
return callback()
}
if (file.isBuffer()) {
try{
var config = JSON.parse(file.contents)
}catch(err){
callback(err)
}
engine.render(config.file || config.tpl, config.data, function(err, html, output) {
if (err) return callback(err)
file.contents = new Buffer(html || output)
file.path = file.path.replace('.json', '.html') // fixme: feels a bit hacky
this.push(file)
return callback(null)
}.bind(this))
}
if (file.isStream()){
var data = []
file.contents.on('data', function(chunk) {
data.push(chunk)
})
file.contents.on('end', function() {
try{
var config = JSON.parse(data.join(String()))
}catch(err){
callback(err)
}
engine.render(config.file || config.tpl, config.data, function(err, html, output) {
if (err) return callback(err)
var stream = through()
stream.on('error', this.emit.bind(this, 'error'))
stream.write(html || output)
file.contents = stream
stream.end()
this.push(file)
return callback(null)
}.bind(this))
}.bind(this))
file.contents.on('error', function(err) {
this.emit('error', new PluginError('gulp-freemarker', 'Read stream error!'))
}.bind(this))
}
})
}