Skip to content

Commit

Permalink
Added regex support
Browse files Browse the repository at this point in the history
  • Loading branch information
craigspaeth committed Sep 20, 2011
1 parent 4f9d58d commit ade1af1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
1 change: 1 addition & 0 deletions spec/fixtures/regex/bar.js
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions spec/fixtures/regex/deep/foo.txt
@@ -0,0 +1 @@
Hello World
46 changes: 15 additions & 31 deletions spec/lib/sentry_spec.coffee
Expand Up @@ -16,31 +16,31 @@ describe 'sentry.watch', ->
catch e
expect(e.message).toEqual "SENTRY: File 'garbage' does not exist!"

xit 'runs a function when the file is changed', ->
it 'runs a function when the file is changed', ->
done = false; waitsFor -> done
fs.writeFileSync __rootdir + '/spec/fixtures/string/foo.js', 'Blank'
sentry.watch __rootdir + '/spec/fixtures/string/foo.js', ->
expect(true).toBeTruthy()
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/string/foo.js', 'Hello World'

xit 'runs a task when the file is changed', ->
it 'runs a task when the file is changed', ->
done = false; waitsFor (-> done), null, 10000
fs.writeFileSync __rootdir + '/spec/fixtures/string/bar.js', 'Blank'
sentry.watch __rootdir + '/spec/fixtures/string/bar.js', 'cake stub', (err, stdout, stderr) ->
expect(stdout.indexOf 'foo').toNotEqual -1
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/string/bar.js', 'Hello World'

xit 'passes the filename to the callback' , ->
it 'passes the filename to the callback' , ->
done = false; waitsFor -> done
fs.writeFileSync __rootdir + '/spec/fixtures/string/baz.js', 'Blank'
sentry.watch __rootdir + '/spec/fixtures/string/baz.js', (filename) ->
expect(filename).toEqual __rootdir + '/spec/fixtures/string/baz.js'
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/string/baz.js', 'Hello World'

xdescribe 'given a single wild card', ->
describe 'given a single wild card', ->

it 'runs a function when a file is changed', ->
done = false; waitsFor -> done
Expand Down Expand Up @@ -86,7 +86,7 @@ describe 'sentry.watch', ->
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/wildcard/qux.js', 'Hello World'
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/wildcard/baz.coffee', 'Hello World'

xdescribe 'given a recursive wild card', ->
describe 'given a recursive wild card', ->

it 'runs a function when a deeply nested file is changed', ->
done = false; waitsFor -> done
Expand Down Expand Up @@ -133,29 +133,13 @@ describe 'sentry.watch', ->
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/deepwildcard/deep/baz.coffee', 'Hello World'


xdescribe 'given a regex', ->

xit 'runs a function when a deeply nested file that matches the regex changes', ->
done = false; waitsFor -> done
fs.writeFileSync __rootdir + '/spec/fixtures/regex/deep/foo.js', 'Blank'
regex = new RegExp "^#{__rootdir},js$"
sentry.watch regex, ->
expect(true).toBeTruthy()
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/regex/deep/foo.js', 'Hello World'

xit 'runs a function when a not so deeply nested file is changed', ->
done = false; waitsFor (-> done), null, 10000
fs.writeFileSync __rootdir + '/spec/fixtures/deepwildcard/bar.js', 'Blank'
sentry.watch __rootdir + '/spec/fixtures/deepwildcard/**/*.js', 'cake stub', (err, stdout, stderr) ->
expect(stdout.indexOf 'foo').toNotEqual -1
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/deepwildcard/bar.js', 'Hello World'

xit 'it passes the filename to the callback', ->
done = false; waitsFor -> done
fs.writeFileSync __rootdir + '/spec/fixtures/deepwildcard/deep/foo.js', 'Blank'
sentry.watch __rootdir + '/spec/fixtures/deepwildcard/**/*', (filename) ->
expect(filename.match(/foo|baz|qux/)).toBeTruthy()
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/deepwildcard/deep/foo.js', 'Hello World'
describe 'sentry.watchRegExp', ->

it 'runs a function when a deeply nested file that matches the regex changes', ->
done = false; waitsFor -> done
fs.writeFileSync __rootdir + '/spec/fixtures/regex/deep/foo.txt', 'Blank'
sentry.watchRegExp '../fixtures/regex/', /txt$/, ->
expect(true).toBeTruthy()
done = true
_.defer -> fs.writeFileSync __rootdir + '/spec/fixtures/regex/deep/foo.txt', 'Hello World'

31 changes: 21 additions & 10 deletions src/sentry.coffee
Expand Up @@ -12,24 +12,35 @@ path = require 'path'

callback = task if _.isFunction task

# Recursively find anything that matches the regex
if _.isRegExp file
# fileUtil.walkSync root, (root, flds, fls) ->
# root = (if root.charAt(root.length - 1) is '/' then root else root + '/')
# for file in fls
# if file.match(new RegExp ext + '$')? and _.indexOf(files, root + file) is -1
# files.push(root + file)

# If the file is a string without wildcards, watch just that file
else if file.indexOf('/*') isnt -1
if file.indexOf('/*') isnt -1
files = findWildcardFiles file
watchFile(file, task, callback) for file in files

# Get the files we want to catch with the wildcards
else
throw new Error("SENTRY: File '#{file}' does not exist!") unless path.existsSync file
watchFile(file, task, callback)


# sentry.watchRegExp(root, regex, [task], callback)
# If passed a task callback is passed (err, stdout, stderr)
# If task is ommitted then callback is passed (filename)
@watchRegExp = (root, regex, task, callback) ->

callback = task if _.isFunction task

# Recursively find anything that matches the regex
root = path.resolve(path.dirname(module.parent.filename), root)
files = []
fileUtil.walkSync root, (rt, flds, fls) ->
for fl in fls
flPath = rt + '/' + fl
files.push(flPath) if flPath.match regex
files

# Watch the matches files
watchFile(file, task, callback) for file in files

# Watch a file for changes and execute a callback or child process
watchFile = (file, task, callback) ->
fs.watchFile file, (curr, prev) ->
Expand Down

0 comments on commit ade1af1

Please sign in to comment.