Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (61 sloc) 2.17 KB
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
task 'watch', 'Watch for changes in coffee files to build and test', ->
util.log "Watching for changes in src and test"
lastTest = 0
watchDir 'src', ->
invoke 'build:src'
invoke 'build:test'
watchDir 'test', ->
invoke 'build:test'
watchDir 'dist/test', (file)->
# We only want to run tests once (a second),
# even if a bunch of test files change
time = new Date().getTime()
if (time-lastTest) > 1000
lastTest = time
invoke 'test'
task 'test', 'Run the tests', ->
util.log "Running tests..."
exec "jasmine-node --nocolor dist/test", (err, stdout, stderr) ->
if err
handleError(parseTestResults(stdout), stderr)
displayNotification "Tests pass!"
util.log lastLine(stdout)
task 'build', 'Build source and tests', ->
invoke 'build:src'
invoke 'build:test'
task 'build:src', 'Build the src files into lib', ->
util.log "Compiling src..."
exec "coffee -o dist/ -c src/", (err, stdout, stderr) ->
handleError(err) if err
task 'build:test', 'Build the test files into lib/test', ->
util.log "Compiling test..."
exec "coffee -o dist/test/ -c test/", (err, stdout, stderr) ->
handleError(err) if err
watchDir = (dir, callback) ->
fs.readdir dir, (err, files) ->
handleError(err) if err
for file in files then do (file) ->
fs.watchFile "#{dir}/#{file}", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
callback "#{dir}/#{file}"
parseTestResults = (data) ->
lines = (line for line in data.split('\n') when line.length > 5)
results = lines.pop()
details = lines[1...lines.length-2].join('\n')
results + '\n\n' + details + '\n'
lastLine = (data) ->
(line for line in data.split('\n') when line.length > 5).pop()
handleError = (error, stderr) ->
if stderr? and !error
util.log stderr
displayNotification stderr.match(/\n(Error:[^\n]+)/)?[1]
util.log error
displayNotification error
displayNotification = (message = '') ->
options = { title: 'CoffeeScript' }
try require('growl').notify message, options