This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Cakefile
174 lines (154 loc) · 6.81 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
{exec, spawn} = require 'child_process'
fs = require 'fs'
path = require 'path'
fixtures = require 'cozy-fixtures'
logger = require('printit')
date: false
prefix: 'cake'
option '-f', '--file [FILE*]' , 'List of test files to run'
option '-d', '--dir [DIR*]' , 'Directory of test files to run'
option '-e' , '--env [ENV]', 'Run tests with NODE_ENV=ENV. Default is test'
option '-j' , '--use-js', 'If enabled, tests will run with the built files'
option '-s' , '--use-server', 'If enabled, starts a server'
options = # defaults, will be overwritten by command line options
file: no
dir: no
# Grab test files of a directory recursively
walk = (dir, excludeElements = []) ->
fileList = []
list = fs.readdirSync dir
if list
for file in list
if file and file not in excludeElements
filename = "#{dir}/#{file}"
stat = fs.statSync filename
if stat and stat.isDirectory()
fileList2 = walk filename, excludeElements
fileList = fileList.concat fileList2
else if filename.substr(-6) is "coffee"
fileList.push filename
return fileList
taskDetails = '(default: ./tests, use -f or -d to specify files and directory)'
task 'tests:server', "Run tests #{taskDetails}", testsServer = (opts, callback) ->
logger.options.prefix = 'cake:tests'
files = []
options = opts
if options.dir
dirList = options.dir
files = walk(dir, files) for dir in dirList
if options.file
files = files.concat options.file
unless options.dir or options.file
files = walk "test"
env = if options['env'] then "NODE_ENV=#{options.env}" else "NODE_ENV=test"
env += " USE_JS=true" if options['use-js']? and options['use-js']
logger.info "Running tests with #{env}..."
command = "#{env} mocha " + files.join(" ") + " --reporter spec --colors "
command += "--compilers coffee:coffee-script/register"
exec command, (err, stdout, stderr) ->
console.log stdout if stdout? and stdout.length > 0
#console.log stderr if stderr? and stderr.length > 0
if err?
err = err
console.log "Running mocha caught exception:\n" + err
process.exit 1
else
console.log "Tests succeeded!"
if callback?
callback()
else
process.exit 0
task 'tests:client', 'Run tests for the client', testsClient=(opts, callback) ->
logger.options.prefix = 'cake:tests:client'
logger.info "Running client's tests..."
if opts['use-server']? and opts['use-server']
initializeServer = require path.join __dirname, './build/server'
else
initializeServer = (callback) -> callback()
app = null
fixtures.load
dirPath: './test/fixtures',
callback: ->
initializeServer (app) ->
if opts.file then files = opts.file.join ' '
else files = 'client/tests/e2e'
cmd = spawn 'casperjs', ['test', files]
cmd.stdout.pipe process.stdout
cmd.stderr.pipe process.stderr
cmd.on 'exit', (code) ->
app.server.close() if app?
if code is 1
console.log "A least one test failed."
process.exit 1
else
console.log "Clients tests successfully runned!"
if callback?
callback()
else
process.exit 0
task 'tests', 'Run tests for client and server', (opts) ->
testsServer opts, -> process.exit 0
# convert JSON lang files to JS
buildJsInLocales = ->
path = require 'path'
for file in fs.readdirSync './client/app/locales/'
filename = './client/app/locales/' + file
template = fs.readFileSync filename, 'utf8'
exported = "module.exports = #{template};\n"
name = file.replace '.json', '.js'
fs.writeFileSync "./build/client/app/locales/#{name}", exported
# server files
for file in fs.readdirSync './server/locales/'
filename = './server/locales/' + file
template = fs.readFileSync filename, 'utf8'
exported = "module.exports = #{template};\n"
name = file.replace '.json', '.js'
fs.writeFileSync "./build/server/locales/#{name}", exported
# add locales at the end of app.js
exec "rm -rf build/client/app/locales/*.json"
buildJade = ->
jade = require 'jade'
srcPath = './build/server/views'
buildFolder = (folderPath) ->
for file in fs.readdirSync(folderPath)
elemPath = "#{folderPath}/#{file}"
unless fs.lstatSync(elemPath).isDirectory()
template = fs.readFileSync elemPath, 'utf8'
output = "var jade = require('jade/runtime');\n"
output += "module.exports = " + jade.compileClient template, filename: elemPath
name = file.replace '.jade', '.js'
fs.writeFileSync "#{folderPath}/#{name}", output
else
buildFolder elemPath
buildFolder srcPath
task 'build', 'Build CoffeeScript to Javascript', ->
logger.options.prefix = 'cake:build'
logger.info "Start compilation..."
binCoffee = "./node_modules/.bin/coffee"
command = "#{binCoffee} -cb --output build/server server && " + \
"#{binCoffee} -cb --output build/ server.coffee && " + \
"cp -r server/views build/server && " + \
"rm -rf build/client && mkdir build/client && " + \
# prepare the client build
"cp ./server/views/index_build.jade client/app/assets/index.jade && " + \
"cp ./server/views/404_build.jade client/app/assets/404.jade && " + \
"cd client/ && brunch build --production && cd .. && " + \
"rm client/app/assets/*.jade && " + \
"rm build/server/views/*.js && " + \
"rm build/server/views/*/*.js && " + \
"mv build/client/public/*.jade build/server/views/ && " + \
"rm -rf build/server/views/*_build.jade && " + \
"mkdir -p build/client/app/locales/ && " + \
"rm -rf build/client/app/locales/* && " + \
"cp -R client/public build/client/ && " + \
"rm -rf client/app/locales/*.coffee"
exec command, (err, stdout, stderr) ->
if err
logger.error "An error has occurred while compiling:\n" + err
process.exit 1
else
buildJsInLocales()
buildJade()
exec "rm -rf build/server/views/*.jade && rm -rf build/server/views/*/*.jade", ->
logger.info "Compilation succeeded."
process.exit 0