-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcompile.ls
221 lines (207 loc) · 7.43 KB
/
compile.ls
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
require! {
\fs
\through
\reactify-ls : \reactify
\browserify-incremental : \browserifyInc
\livescript
\browserify
\xtend
\node-sass : \sassc
\node-watch : \watch
\fix-indents
\chalk : { red, yellow, gray, green }
\express
\vm
\javascrypt : { encrypt }
}
basedir = process.cwd!
compileddir = "#{basedir}/.compiled"
base-title = (colored, symbol, text)-->
text = "[#{colored symbol}] #{colored text}"
max = 40 - text.length
if max <= 0 then text
else text + [0 to max].map(-> " ").join('')
title = base-title green, "✓"
error = base-title red, "x"
warn = base-title yellow, "!"
fs.mkdir(compileddir) if not fs.exists-sync(compileddir)
save = (file, content)->
save-origin "#{compileddir}/#{file}" , content
save-origin = (file, content)->
console.log "#{title 'save'} #{file}"
fs.write-file-sync file , content
setup-watch = (commander)->
return if setup-watch.init
console.log warn "watcher started..."
setup-watch.init = yes
watcher = watch do
* basedir
* recursive: yes
filter: (name)->
!/(node_modules|\.git)/.test(name)
* (evt, name)->
return if setup-watch.disabled
console.log "#{warn 'changed'} #name"
setup-watch.disabled = yes
#watcher.close!
err <-! compile commander
<-! set-timeout _, 500
setup-watch.disabled = no
server-start = (commander)->
return if server-start.init
server-start.init = yes
app = express!
app.use(express.static(compileddir))
port = if commander.nodestart is yes then 8080 else commander.nodestart
start = ->
app.listen port, ->
console.log("#{warn 'node started'} port #{port}")
script = new vm.Script("(#{start.to-string!})()" )
context = new vm.create-context( { port, app, console, warn } )
script.run-in-context context
port
compile-file = (input, data)->
console.log "#{title 'compile'} #{input}"
code = reactify data
state =
js: null
try
state.js = livescript.compile code.ls
catch err
state.err = err.message
errorline = err.message.match(/line ([0-9]+)/).1 ? 0
lines = code.ls.split(\\n)
for index of lines
if index is errorline
lines[index] = lines[index] + " <<< #{red err.message}"
else
lines[index] = gray lines[index]
console.log ([] ++ lines).join(\\n)
#target = input.replace(/\[^\/]+.ls/,\.js)
#save target, state.js
{ code.ls, code.sass, state.js, state.err}
apply-variables = (text, variables)->
apply-variable = (text, name)->
text.replace "<#{name}/>", variables[name]
Object.keys(variables).reduce(apply-variable, text)
compile = (commander, cb)->
console.log "----------------------"
cb2 = (err, data)->
if err?
console.log "#{red 'Error'} err"
cb? err, data
file = commander.compile
sass-cache = do
path = "#{compileddir}/#{file}.sass.cache"
save: (obj)->
fs.write-file-sync(path, JSON.stringify(obj))
load: ->
return {} if not fs.exists-sync(path)
JSON.parse fs.read-file-sync(path).to-string(\utf8)
sass-c = sass-cache.load!
#return if file.index-of('.ls') is -1
filename = file.replace /\.ls/,''
return cb2 'File is required' if not file?
bundle = if commander.bundle is yes then \bundle else commander.bundle
bundle-js = "#{filename}-#{bundle}.js"
bundle-css = "#{filename}-#{bundle}.css"
html = if commander.html is yes then \index else commander.html
bundle-html = "#{filename}-#{html}.html"
sass = if commander.sass is yes then \style else commander.sass
compilesass = if commander.compilesass is yes then \style else commander.compilesass
sass-c[commander.compile] = sass-c[commander.compile] ? {}
make-bundle = (file, callback)->
console.log "#{title 'start main file'} #file"
options =
basedir: basedir
paths: ["#{basedir}/node_modules"]
debug: no
commondir: no
entries: [file]
b = browserify xtend(browserify-inc.args, options)
b.transform (file) ->
#json = file.match(/([a-z-0-9_]+)\.json$/)?1
#js = file.match(/([a-z-0-9_]+)\.js$/)?1
filename = file.match(/([a-z-0-9_]+)\.ls$/)?1
data = ''
write = (buf) -> data += buf
end = ->
t = @
send = (data)->
t.queue data
t.queue null
return send data if not filename?
code =
compile-file file, data
if sass?
save "#{filename}.sass", code.sass
if commander.fixindents
indented = fix-indents data
if data isnt indented
console.log "#{title 'fix indents'} #file"
save-origin file, indented
if compilesass?
console.log "#{title 'compile'} #{filename}.sass"
if code.sass.length > 0
sass-conf =
data: code.sass
indented-syntax: yes
try
sass-c[commander.compile][file] = sassc.render-sync(sass-conf).css.to-string(\utf8)
catch err
console.error "#{error 'err compile sass'} #{yellow err.message}"
else
sass-c[commander.compile][file] = ""
if commander.javascrypt
code.js = encrypt code.js
save "#{filename}.js", code.js
send code.js
through write, end
browserify-inc b, { cache-file: "#{compileddir}/#{file}.cache" }
bundle = b.bundle!
string = ""
bundle.on \data, (data)->
string += data.to-string!
bundle.on \error, (err)->
console.log "#{ error 'bundle err' } #{err.message ? err}"
_ <-! bundle.on \end
compiled-sass = sass-c[commander.compile]
result =
css: Object.keys(compiled-sass).map(-> compiled-sass[it]).join(\\n)
js: string
sass-cache.save sass-c
callback null, result
if commander.bundle?
err, bundlec <-! make-bundle file
return cb2 err if err?
if not commander.putinhtml?
save bundle-js, bundlec.js
if compilesass? and not commander.putinhtml?
save bundle-css, bundlec.css
dynamicCSS = | commander.putinhtml => """<style>#{bundlec.css}</style>"""
| _ => """ <link rel="stylesheet" type="text/css" href="./#{bundle-css}"> """
dynamicHTML = | commander.putinhtml => """<script>#{bundlec.js}</script>"""
| _ => """<script type="text/javascript" src="./#{bundle-js}"></script>"""
if commander.html?
default-template = '''
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>loading...</title>
<dynamicCSS/>
</head>
<dynamicHTML/>
</html>
'''
current-template =
| commander.template? => fs.read-file-sync commander.template, \utf8
| _ => default-template
html = apply-variables current-template, { dynamicCSS, dynamicHTML }
save bundle-html, html
if commander.nodestart?
server-start commander
if commander.watch
setup-watch commander
cb2 null, "success"
module.exports = compile