-
Notifications
You must be signed in to change notification settings - Fork 2
/
compile.coffee
63 lines (59 loc) · 1.58 KB
/
compile.coffee
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
module.exports = (data, finalcb) ->
fs = require 'fs'
path = require 'path'
flow = require 'flow'
doT = require './doT.js'
child = require 'child_process'
any_error = (results) ->
for r in results
return r[0] if r[0]
null
readItem = (item, callback) ->
flow.exec(
-> fs.stat item, @
(err, stat) ->
return @ err if err
return readFile item, @ unless stat.isDirectory()
item_cb = @
flow.exec(
-> fs.readdir item, @
(err, files) ->
return @MULTI err if err
files.forEach (file) =>
readItem path.join(item, file), @MULTI()
(results) -> item_cb any_error results
)
(err) ->
callback err
)
readFile = (file, callback) ->
flow.exec(
->
if file.match /.haml$/
child.exec "haml '#{file}'", @
file = path.basename file, '.haml'
else
fs.readFile file, @
(err, text) ->
return @ err if err
id = path.basename file, path.extname file
if data.base
rel = path.relative(data.base, path.dirname file)
.replace /\//g, '.'
id = "#{rel}.#{id}" if rel
f = dot_err = null
try
f = doT.compile text
doT.addCached id, f
catch e
dot_err = e
@ dot_err, f
(err, f) ->
callback err, f
)
flow.exec(
-> data.files.forEach ( val, i ) => readItem val, @MULTI()
(results) ->
return unless finalcb
finalcb any_error(results), doT.exportCached()
)