forked from ai/visibilityjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cakefile
148 lines (120 loc) · 4.06 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
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()
.filter (i) -> i.indexOf('.js') != -1
.map (i) -> "lib/#{i}"
title: ->
@name()[0].toUpperCase() + @name()[1..-1]
mocha =
template: """
<html>
<head>
<meta charset="UTF-8">
<title>#title# Tests</title>
<link rel="stylesheet" href="/style.css">
<style>
#integration {
position: absolute;
top: 22px;
left: 66px;
}
</style>
#system#
<script>
chai.should();
mocha.setup({ ui: 'bdd', ignoreLeaks: true });
window.onload = function() {
mocha.run();
};
</script>
#libs#
#tests#
<body>
<a href="/integration" id="integration" target="_blank">
see also integration test →
</a>
<div id="mocha"></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 ")
style: ->
fs.readFileSync('node_modules/mocha/mocha.css')
system: ->
@scripts ['node_modules/mocha/mocha.js',
'node_modules/chai/chai.js',
'node_modules/sinon/pkg/sinon.js',
'node_modules/sinon-chai/lib/sinon-chai.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 mocha.style()
else if pathname == '/integration'
res.writeHead 200, 'Content-Type': 'text/html'
res.write fs.readFileSync('test/integration.html')
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
console.log('Open http://localhost:8000/')
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()
continue if file == 'visibility.js'
name = file.replace(/^lib\//, '').replace(/\.js$/, '')
fs.copySync(file, "pkg/#{name}-#{project.version()}.min.js")
core = fs.readFileSync('lib/visibility.core.js').toString()
timers = fs.readFileSync('lib/visibility.timers.js').toString()
fs.writeFileSync("pkg/visibility-#{project.version()}.min.js", core + timers)
packages = fs.readdirSync('pkg/').filter( (i) -> i.match(/\.js$/) )
for file in packages
continue unless file.match(/\.js$/)
min = uglify.minify('pkg/' + file)
fs.writeFileSync('pkg/' + file, min.code)