Perform directory check with single stat call

This commit is contained in:
Kevin Sawicki 2013-03-25 16:54:39 -04:00
parent 513ed3b4b1
commit 67adce6a99

View File

@ -1,6 +1,7 @@
EventEmitter = require 'event-emitter'
fs = require 'fs-utils'
fs = require 'fs'
fsUtils = require 'fs-utils'
_ = require 'underscore'
module.exports =
@ -9,32 +10,33 @@ class File
cachedContents: null
constructor: (@path, @symlink=false) ->
if @exists() and not fs.isFile(@path)
throw new Error("#{@path} is a directory")
try
if fs.statSync(@path).isDirectory()
throw new Error("#{@path} is a directory")
setPath: (@path) ->
getPath: -> @path
getBaseName: ->
fs.base(@path)
fsUtils.base(@path)
write: (text) ->
previouslyExisted = @exists()
@cachedContents = text
fs.write(@getPath(), text)
fsUtils.write(@getPath(), text)
@subscribeToNativeChangeEvents() if not previouslyExisted and @subscriptionCount() > 0
read: (flushCache)->
if not @exists()
@cachedContents = null
else if not @cachedContents? or flushCache
@cachedContents = fs.read(@getPath())
@cachedContents = fsUtils.read(@getPath())
else
@cachedContents
exists: ->
fs.exists(@getPath())
fsUtils.exists(@getPath())
afterSubscribe: ->
@subscribeToNativeChangeEvents() if @exists() and @subscriptionCount() == 1
@ -67,7 +69,7 @@ class File
@trigger "removed"
subscribeToNativeChangeEvents: ->
@watchSubscription = fs.watchPath @path, (eventType, path) =>
@watchSubscription = fsUtils.watchPath @path, (eventType, path) =>
@handleNativeChangeEvent(eventType, path)
unsubscribeFromNativeChangeEvents: ->