-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompile.coffee
109 lines (104 loc) · 3.19 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
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
fs = require 'fs'
path = require 'path'
child = require 'child_process'
http = require 'http'
flow = require 'flow-coffee'
module.exports = (data, finalcb) ->
doT = data.doT ? require './'
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
readItem path.join(item, file), @multi() for file in files
@multi() null
(err, results) ->
item_cb err
)
(err) ->
callback err
)
readFile = (file, callback) ->
# start = new Date
flow.exec(
->
#TODO: prefilters
if file.match /.(haml|slim)$/
compile_with_ruby file, @
file = file.replace new RegExp("#{path.extname file}$"), ''
else
fs.readFile file, @
(err, text) ->
# console.log text
# console.log +(new Date) - start
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
try
f = doT.compile text
doT.addCached id, f
catch e
return @ e
@ null, f
(err, f) ->
callback err, f
)
ruby_server = null
do new flow
error: ->
ruby_server.kill('SIGINT') if ruby_server
finalcb? arguments...
blocks: [
->
return @() unless data.ruby_server == true
data.ruby_server =
host: 'localhost'
port: 4000
ruby_env = process.env
ruby_env.RACK_ENV = 'production'
ruby_server = child.spawn path.join(__dirname, 'bin', 'dot-compile-server'),
["-p#{data.ruby_server.port}", data.base], env: ruby_env
started = false
ruby_server.on 'error', (err) => @ err
ruby_server.stderr.on 'data', (data) =>
# console.log data.toString()
return if started
if /has taken the stage/.test data.toString()
started = true
console.error 'ruby-server started'
@()
->
readItem file, @multi() for file in data.files
@multi() null
(err, results) ->
ruby_server.kill('SIGINT') if ruby_server
finalcb? err, doT.exportCached()
]
# private helpers
compile_with_ruby = (file, callback) ->
return child.exec "haml '#{file}'", callback unless data.ruby_server
filename = file.replace new RegExp("#{path.extname file}$"), '.html'
request = '/' + path.relative(data.base, filename)
http.get(
host: data.ruby_server.host
port: data.ruby_server.port
path: request
(res) ->
res_data = ''
res.on 'data', (chunk) -> res_data += chunk
res.on 'error', (err) -> callback? err
res.on 'end', ->
unless 300 > @statusCode
return callback? new Error "#{@statusCode} @ #{file}"
callback? null, res_data
).on 'error', (err) -> callback? err