Cakefile 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. fs = require 'fs'
  2. {exec} = require 'child_process'
  3. util = require 'util'
  4. binDir = "./node_modules/.bin/"
  5. task 'watch', 'Watch for changes in coffee files to build and test', ->
  6. util.log "Watching for changes in src and test"
  7. lastTest = 0
  8. watchDir 'src', ->
  9. invoke 'build:src'
  10. invoke 'build:min'
  11. invoke 'build:doc'
  12. invoke 'build:test'
  13. watchDir 'test', ->
  14. invoke 'build:test'
  15. watchDir 'dist/test', (file)->
  16. # We only want to run tests once (a second),
  17. # even if a bunch of test files change
  18. time = new Date().getTime()
  19. if (time-lastTest) > 1000
  20. lastTest = time
  21. invoke 'test'
  22. task 'test', 'Run the tests', ->
  23. util.log "Running tests..."
  24. exec binDir + "jasmine-node --nocolor dist/test", (err, stdout, stderr) ->
  25. if err
  26. handleError(parseTestResults(stdout), stderr)
  27. else
  28. displayNotification "Tests pass!"
  29. util.log lastLine(stdout)
  30. task 'build', 'Build source and tests', ->
  31. invoke 'build:src'
  32. invoke 'build:min'
  33. invoke 'build:test'
  34. task 'build:src', 'Build the src files into lib', ->
  35. util.log "Compiling src..."
  36. exec binDir + "coffee -o lib/ -c src/", (err, stdout, stderr) ->
  37. handleError(err) if err
  38. task 'build:min', 'Build the minified files into lib', ->
  39. util.log "Minify src..."
  40. exec binDir + "uglifyjs -m --comments all -o lib/stomp.min.js lib/stomp.js", (err, stdout, stderr) ->
  41. handleError(err) if err
  42. task 'build:doc', 'Build docco documentation', ->
  43. util.log "Building doc..."
  44. exec binDir + "docco -o doc/ src/*.coffee", (err, stdout, stderr) ->
  45. handleError(err) if err
  46. task 'build:test', 'Build the test files into lib/test', ->
  47. util.log "Compiling test..."
  48. exec binDir + "coffee -o dist/test/ -c test/", (err, stdout, stderr) ->
  49. handleError(err) if err
  50. watchDir = (dir, callback) ->
  51. fs.readdir dir, (err, files) ->
  52. handleError(err) if err
  53. for file in files then do (file) ->
  54. fs.watchFile "#{dir}/#{file}", (curr, prev) ->
  55. if +curr.mtime isnt +prev.mtime
  56. callback "#{dir}/#{file}"
  57. parseTestResults = (data) ->
  58. lines = (line for line in data.split('\n') when line.length > 5)
  59. results = lines.pop()
  60. details = lines[1...lines.length-2].join('\n')
  61. results + '\n\n' + details + '\n'
  62. lastLine = (data) ->
  63. (line for line in data.split('\n') when line.length > 5).pop()
  64. handleError = (error, stderr) ->
  65. if stderr? and !error
  66. util.log stderr
  67. displayNotification stderr.match(/\n(Error:[^\n]+)/)?[1]
  68. else
  69. util.log error
  70. displayNotification error
  71. displayNotification = (message = '') ->
  72. options = { title: 'CoffeeScript' }
  73. try require('growl').notify message, options