Files
atom/src/app/directory.coffee
Corey Johnson & Kevin Sawicki 6c357aaca7 Make paths absolute before calling $native.watchPath
Also return a subscription that can be unwatched instead
of returning an id that is later passed to $native.unwatchPath
along with the watched path.

This allows specs to pass when run from a symlink'ed folder.
2013-03-20 13:27:49 -07:00

46 lines
1.1 KiB
CoffeeScript

_ = require 'underscore'
fs = require 'fs-utils'
File = require 'file'
EventEmitter = require 'event-emitter'
module.exports =
class Directory
path: null
constructor: (@path) ->
getBaseName: ->
fs.base(@path)
getPath: -> @path
getEntries: ->
directories = []
files = []
for path in fs.list(@path)
if fs.isDirectory(path)
directories.push(new Directory(path))
else if fs.isFile(path)
files.push(new File(path))
else
console.error "#{path} is neither a file nor a directory."
directories.concat(files)
afterSubscribe: ->
@subscribeToNativeChangeEvents() if @subscriptionCount() == 1
afterUnsubscribe: ->
@unsubscribeFromNativeChangeEvents() if @subscriptionCount() == 0
subscribeToNativeChangeEvents: ->
@watchSubscription = fs.watchPath @path, (eventType) =>
@trigger "contents-changed" if eventType is "contents-change"
unsubscribeFromNativeChangeEvents: ->
if @watchSubscription?
@watchSubscription.unwatch()
@watchSubscription = null
_.extend Directory.prototype, EventEmitter