-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCakefile
314 lines (251 loc) · 6.85 KB
/
Cakefile
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
304
305
306
307
308
309
310
311
312
313
314
# 3rd party
fs = require 'fs-extra'
{dirname} = require 'path'
which = require 'which'
childProcess = require 'child_process'
coffee = require 'coffeescript'
glob = require 'glob'
#
MODULES = ['lib', 'css', 'node', 'cli']
QUEUE = []
ERRORS = 0
ESC = '\u001b'
RESET = "#{ESC}[0m"
BOLD = "#{ESC}[1m"
RED = "#{ESC}[31m"
GREEN = "#{ESC}[32m"
YELLOW = "#{ESC}[33m"
CHECK = "#{GREEN}√#{RESET}"
CROSS = "#{BOLD}#{RED}×#{RESET}"
BG_RED = "#{ESC}[41m"
NPM = which.sync 'npm'
NPM_BIN = childProcess.execSync("#{NPM} bin").toString().trim()
VERBOSE = no
WATCH = no
# Cheating a bit to get "global" options in this Cakefile
_task = global.task
global.task = (name, description, action) ->
_task name, description, (options) ->
if options.verbose
VERBOSE = yes
if options.watch
WATCH = yes
action options
log = (type = '', text = '') ->
if type is 'task'
text = "#{YELLOW}#{text} "
else
if type is 'ok'
mark = CHECK
else if type is 'error'
mark = CROSS
else if not VERBOSE
return
else
mark = "#{BOLD}#{type}#{RESET}"
if VERBOSE
text = "#{mark} #{text}"
else
text = "#{mark}\n"
text += RESET
process.stderr.write text
if VERBOSE
process.stderr.write "\n"
next = ->
if QUEUE.length
QUEUE[0]()
else
exit()
queue = (func) ->
QUEUE.push func
if QUEUE.length is 1
next()
exit = ->
if ERRORS and VERBOSE
s = if ERRORS > 1 then 's' else ''
log 'error', "#{BG_RED}#{BOLD}#{ERRORS} task#{s} failed"
process.exit ERRORS
done = ->
log 'ok', 'Done'
log()
QUEUE.shift()
next()
fail = (text = 'Error') ->
ERRORS++
log 'error', text
log()
QUEUE.shift()
next()
exec = (cmd, callback = done) ->
log 'exec', cmd
args = cmd.split /\s+/
cmd = args.shift()
cmd = "#{NPM_BIN}/#{cmd}"
stdio = if VERBOSE then 'inherit' else 'ignore'
child = childProcess.spawn cmd, args, stdio: stdio
child.on 'exit', (status) ->
if status is 0
callback()
else
fail "Command #{BOLD}#{cmd}#{RESET} did not exited nicely (#{status})"
mkdir = (path, callback = done) ->
fs.stat path, (err, stat) ->
if err
log 'mkdir', path
fs.mkdirsSync path
else unless stat.isDirectory()
throw new Error "Cannot make directory #{path}: file exists"
callback()
remove = (paths, callback = done) ->
for path in [].concat paths
try
fs.statSync path
log 'rm', path
fs.removeSync path
callback()
copy = (src, dest, callback = done) ->
mkdir (dirname dest), ->
log 'cp', "#{src} -> #{dest}"
fs.copySync src, dest
callback()
read = (path, callback = done) ->
fs.readFile path, 'utf-8', (err, data) ->
throw err if err
callback data
write = (path, contents, callback = done) ->
log 'write', path
fs.writeFile path, contents, (err) ->
throw err if err
callback()
chmod = (path, mode, callback = done) ->
log 'chmod', "#{mode} #{path}"
mode = parseInt mode, 8
fs.chmod path, mode, (err) ->
throw err if err
callback()
uncoffee = (source) ->
coffee.compile source, {
bare: yes,
header: no
transpile: {
presets: [
'env'
]
plugins: [
'transform-object-rest-spread'
]
}
}
test = (path, source = no, callback = done) ->
path = "test/#{path}"
args = [
'--slow 500'
'--timeout 10000'
'--reporter ' + (if VERBOSE then 'spec' else 'base')
]
args.push '--watch' if WATCH
if source
path = "src/#{path}/**/index.coffee"
args.push '--require coffeescript/register'
exec "mocha #{args.join ' '} #{path}", callback
option '-v', '--verbose', 'Enable verbose output'
option '-w', '--watch', 'Whatch sources for changes and re-run tasks'
task 'clean', 'Remove all built files and directories', ->
queue ->
log 'task', 'Cleaning up'
remove MODULES.concat ['test']
task 'build:test', 'Build tests', ->
queue ->
log 'task', 'Building tests'
remove 'test', ->
exec 'coffee --compile --transpile --output test/ src/test'
queue ->
log 'task', 'Copying test fixtures'
fixtures = require './src/test/fixtures.json'
files = []
for pattern in fixtures
files.push (glob.sync "src/test/#{pattern}", nodir: yes)...
do cp = ->
if files.length
file = files.shift()
dest = file.replace /^src\//, ''
copy file, dest, -> cp()
else
done()
MODULES.forEach (module) ->
task "build:#{module}", 'Build #{module} module', ->
queue ->
log 'task', "Building #{module} module"
args = ['--compile', ' --transpile', '--no-header']
args.push '--watch' if WATCH
mkdir module, ->
exec "coffee #{args.join ' '} --output #{module}/ src/#{module}", ->
if module is 'cli'
read "src/cli/layla", (source) ->
js = """
#!/usr/bin/env node
#{uncoffee source}
"""
mkdir 'cli', ->
write 'cli/layla', js, ->
chmod 'cli/layla', '0755'
else
done()
task 'build:license', 'Build license file', ->
queue ->
log 'task', 'Building license'
write './License.md', require './src/license'
task 'build:index', 'Build root `index.js`', ->
queue ->
log 'task', 'Building index.js'
read 'src/index.coffee', (source) ->
write 'index.js', uncoffee source
task 'build:module', 'Build NPM module', ->
invoke 'build:index'
task 'build:all', 'Build everything', ->
for module in MODULES
invoke "build:#{module}"
invoke 'build:license'
invoke 'build:module'
invoke 'build:test'
task 'build', 'Alias of build:all', ->
invoke 'build:all'
[no, yes].forEach (source) ->
prefix = 'test'
if source
prefix += ':source'
expl = ' (source)'
else
expl = ''
task "#{prefix}:unit", "Run unit tests#{expl}", ->
queue ->
log 'task', "Running unit tests#{expl}"
test 'unit', source
task "#{prefix}:cases", "Run test cases#{expl}", ->
queue ->
log 'task', "Running test cases#{expl}"
test 'cases', source
task "#{prefix}:style", "Run style tests#{expl}", ->
queue ->
log 'task', "Running style tests#{expl}"
test 'style', yes
task "#{prefix}:docs", "Run docs tests#{expl}", ->
queue ->
log 'task', "Running docs tests#{expl}"
test 'docs', source
task "#{prefix}:cli", "Run CLI tests#{expl}", ->
queue ->
log 'task', "Running CLI tests#{expl}"
test 'cli', source
task "#{prefix}:all", "Run all tests#{expl}", ->
invoke "#{prefix}:unit"
invoke "#{prefix}:cases"
invoke "#{prefix}:cli"
invoke "#{prefix}:style"
invoke "#{prefix}:docs"
task "#{prefix}", 'Alias of test:all', ->
invoke "#{prefix}:all"
task 'all', 'Build, test and then clean everything', ->
invoke 'build:all'
invoke 'test:all'
invoke 'clean'