-
Notifications
You must be signed in to change notification settings - Fork 10
/
Cakefile
125 lines (100 loc) · 3.32 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
fs = require('fs-extra')
url = require('url')
exec = require('child_process').exec
http = require('http')
path = require('path')
project =
package: ->
JSON.parse(fs.readFileSync('package.json'))
name: ->
@package().name
version: ->
@package().version
tests: ->
fs.readdirSync('test/')
.filter (i) -> i.match /\.coffee$/
.map (i) -> "test/#{i}"
libs: ->
fs.readdirSync('lib/').sort().reverse()
.filter (i) -> i.indexOf('.js') != -1
.map (i) -> "lib/#{i}"
title: ->
capitalize = (s) -> s[0].toUpperCase() + s[1..-1]
@name().split('-').map( (i) -> capitalize(i) ).join(' ')
mocha =
template: """
<html>
<head>
<meta charset="UTF-8">
<title>#title# Tests</title>
<link rel="stylesheet" href="/style.css">
#system#
<script>
mocha.setup({ ui: 'bdd', ignoreLeaks: true });
window.onload = function() {
mocha.run();
};
</script>
#libs#
#tests#
<body>
<div id="mocha"></div>
<div id="fixtures"></div>
</body>
</html>
"""
html: ->
@render @template,
system: @system()
libs: @scripts project.libs()
tests: @scripts project.tests()
title: project.title()
render: (template, params) ->
html = template
for name, value of params
html = html.replace("##{name}#", value.replace(/\$/g, '$$$$'))
html
scripts: (files) ->
files.map( (i) -> "<script src=\"/#{i}\"></script>" ).join("\n ")
system: ->
@scripts ['node_modules/jquery/dist/jquery.js',
'node_modules/should/should.js',
'node_modules/mocha/mocha.js']
task 'server', 'Run test server', ->
coffee = require('coffee-script')
server = http.createServer (req, res) ->
pathname = url.parse(req.url).pathname
if pathname == '/'
res.writeHead 200, 'Content-Type': 'text/html'
res.write mocha.html()
else if pathname == '/style.css'
res.writeHead 200, 'Content-Type': 'text/css'
res.write fs.readFileSync('node_modules/mocha/mocha.css')
else if fs.existsSync('.' + pathname)
file = fs.readFileSync('.' + pathname).toString()
if pathname.match(/\.coffee$/)
file = coffee.compile(file)
if pathname.match(/\.(js|coffee)$/)
res.writeHead 200, 'Content-Type': 'application/javascript'
res.write file
else
res.writeHead 404, 'Content-Type': 'text/plain'
res.write 'Not Found'
res.end()
server.listen 8000
process.stdout.write("Open http://localhost:8000/\n")
task 'clean', 'Remove all generated files', ->
fs.removeSync('pkg/') if fs.existsSync('pkg/')
for file in fs.readdirSync('./')
fs.removeSync(file) if file.match(/\.gem$/)
task 'min', 'Create minimized version of library', ->
uglify = require('uglify-js')
invoke('clean')
fs.mkdirsSync('pkg/')
for file in project.libs()
name = file.replace(/^lib\//, '').replace(/\.js$/, '')
fs.copySync(file, "pkg/#{name}-#{project.version()}.min.js")
packages = fs.readdirSync('pkg/').filter( (i) -> i.match(/\.js$/) )
for file in packages
min = uglify.minify('pkg/' + file)
fs.writeFileSync('pkg/' + file, min.code)