Cakefile 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. async = require 'async'
  2. {spawn, exec} = require 'child_process'
  3. fs = require 'fs'
  4. glob = require 'glob'
  5. log = console.log
  6. path = require 'path'
  7. remove = require 'remove'
  8. # Node 0.6 compatibility hack.
  9. unless fs.existsSync
  10. fs.existsSync = (filePath) -> path.existsSync filePath
  11. task 'build', ->
  12. build()
  13. task 'test', ->
  14. vendor ->
  15. build ->
  16. ssl_cert ->
  17. test_cases = glob.sync 'test/js/**/*_test.js'
  18. test_cases.sort() # Consistent test case order.
  19. run 'node_modules/.bin/mocha --colors --slow 200 --timeout 1000 ' +
  20. "--require test/js/helpers/setup.js #{test_cases.join(' ')}"
  21. task 'webtest', ->
  22. vendor ->
  23. build ->
  24. ssl_cert ->
  25. webtest()
  26. task 'cert', ->
  27. remove.removeSync 'test/ssl', ignoreMissing: true
  28. ssl_cert()
  29. task 'vendor', ->
  30. remove.removeSync './test/vendor', ignoreMissing: true
  31. vendor()
  32. task 'doc', ->
  33. run 'node_modules/.bin/codo --title "node-xhr API Documentation" src'
  34. build = (callback) ->
  35. commands = []
  36. # Ignoring ".coffee" when sorting.
  37. # We want "driver.coffee" to sort before "driver-browser.coffee"
  38. source_files = glob.sync 'src/**/*.coffee'
  39. source_files.sort (a, b) ->
  40. a.replace(/\.coffee$/, '').localeCompare b.replace(/\.coffee$/, '')
  41. # Compile without --join for decent error messages.
  42. commands.push 'node_modules/.bin/coffee --output tmp --compile ' +
  43. source_files.join(' ')
  44. commands.push 'node_modules/.bin/coffee --output lib --compile ' +
  45. "--join xhr2.js #{source_files.join(' ')}"
  46. # Tests are supposed to be independent, so the build order doesn't matter.
  47. test_dirs = glob.sync 'test/src/**/'
  48. for test_dir in test_dirs
  49. out_dir = test_dir.replace(/^test\/src\//, 'test/js/')
  50. test_files = glob.sync path.join(test_dir, '*.coffee')
  51. commands.push "node_modules/.bin/coffee --output #{out_dir} " +
  52. "--compile #{test_files.join(' ')}"
  53. async.forEachSeries commands, run, ->
  54. # Build the binary test image.
  55. buffer = fs.readFileSync 'test/fixtures/xhr2.png'
  56. bytes = (buffer.readUInt8(i) for i in [0...buffer.length])
  57. globalJs = '((function(){ return this.global || this; })())'
  58. js = "#{globalJs}.xhr2PngBytes = #{JSON.stringify(bytes)};"
  59. fs.writeFileSync 'test/js/helpers/xhr2.png.js', js
  60. callback() if callback
  61. webtest = (callback) ->
  62. xhrServer = require './test/js/helpers/xhr_server.js'
  63. if 'BROWSER' of process.env
  64. if process.env['BROWSER'] is 'false'
  65. url = xhrServer.https.testUrl()
  66. console.log "Please open the URL below in your browser:\n #{url}"
  67. else
  68. xhrServer.https.openBrowser process.env['BROWSER']
  69. else
  70. xhrServer.https.openBrowser()
  71. callback() if callback?
  72. ssl_cert = (callback) ->
  73. if fs.existsSync 'test/ssl/cert.pem'
  74. callback() if callback?
  75. return
  76. fs.mkdirSync 'test/ssl' unless fs.existsSync 'test/ssl'
  77. run 'openssl req -new -x509 -days 365 -nodes -sha256 -newkey rsa:2048 ' +
  78. '-batch -out test/ssl/cert.pem -keyout test/ssl/cert.pem ' +
  79. '-subj /O=xhr2.js/OU=Testing/CN=localhost ', callback
  80. vendor = (callback) ->
  81. # All the files will be dumped here.
  82. fs.mkdirSync 'test/vendor' unless fs.existsSync 'test/vendor'
  83. downloads = [
  84. # chai.js ships different builds for browsers vs node.js
  85. ['http://chaijs.com/chai.js', 'test/vendor/chai.js'],
  86. # sinon.js also ships special builds for browsers
  87. ['http://sinonjs.org/releases/sinon.js', 'test/vendor/sinon.js'],
  88. ]
  89. async.forEachSeries downloads, download, ->
  90. callback() if callback
  91. run = (args...) ->
  92. for a in args
  93. switch typeof a
  94. when 'string' then command = a
  95. when 'object'
  96. if a instanceof Array then params = a
  97. else options = a
  98. when 'function' then callback = a
  99. command += ' ' + params.join ' ' if params?
  100. cmd = spawn '/bin/sh', ['-c', command], options
  101. cmd.stdout.on 'data', (data) -> process.stdout.write data
  102. cmd.stderr.on 'data', (data) -> process.stderr.write data
  103. process.on 'SIGHUP', -> cmd.kill()
  104. cmd.on 'exit', (code) -> callback() if callback? and code is 0
  105. download = ([url, file], callback) ->
  106. if fs.existsSync file
  107. callback() if callback?
  108. return
  109. run "curl -o #{file} #{url}", callback