From 67adce6a998afa5b70d7c01d74ff57c98453f91d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 25 Mar 2013 16:54:39 -0400 Subject: [PATCH] Perform directory check with single stat call --- src/app/file.coffee | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/app/file.coffee b/src/app/file.coffee index 72138ebb2..f982223b5 100644 --- a/src/app/file.coffee +++ b/src/app/file.coffee @@ -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: ->