Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Created March 20, 2012 04:26
Show Gist options
  • Save fukayatsu/2131318 to your computer and use it in GitHub Desktop.
Save fukayatsu/2131318 to your computer and use it in GitHub Desktop.
テストケースやテスト対象に変更があったら自動的にビルドとテストが走るようにする。
###
前提:
必要なパッケージ
npm install -g coffee-script
npm install -g jasmine-node
ディレクトリ構成
/src #*.coffee
/lib #*.js
/spec #*.spec.coffee
###
fs = require 'fs'
{print} = require 'util'
{spawn} = require 'child_process'
build = (callback) ->
coffee = spawn 'coffee', ['-cb', '-o', 'lib', 'src']
coffee.stderr.on 'data', (data) ->
print 'build OK\n'
process.stderr.write data.toString()
coffee.stdout.on 'data', (data) ->
print 'build ERROR\n'
print data.toString()
coffee.on 'exit', (code) ->
callback?() if code is 0
doTest = (callback) ->
jasmine = spawn 'jasmine-node', ['--coffee', 'spec']
jasmine.stderr.on 'data', (data) ->
process.stderr.write data.toString()
jasmine.stdout.on 'data', (data) ->
print data.toString()
callback?()
task 'build', 'Build lib/ from src/', ->
build()
task 'test', 'Build lib/ from src/, and test by spec/', ->
build(doTest)
task 'watch', 'watch src/ and spec/, and build and test', ->
watchFiles(dir, /.coffee$/, 'test') for dir in ['spec','src']
print "\n*** #{new Date()}\nStart\n"
invoke 'test'
#フォルダ内のファイルを再帰的にwatchする
watchFiles = (dir, regex, task) ->
files = fs.readdirSync dir
for file in files then do(file) ->
path = [dir, file].join('/')
#ディレクトリの場合は再帰的に関数を適応する
if (fs.statSync path).isDirectory()
watchFiles(path, regex, task)
return
#ファイル名が${regex}以外の場合は終了
return unless file.match regex
print "watch: #{path}\n"
fs.watchFile "#{path}",
{persistent: true, interval: 1000 },
(curr, prev) ->
#ファイルの更新チェック
if +curr.mtime isnt +prev.mtime
print "*** #{new Date()}\nModifed: #{path}\n"
invoke task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment