This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
directory.coffee
334 lines (276 loc) · 10.4 KB
/
directory.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
path = require 'path'
async = require 'async'
{Emitter, Disposable} = require 'event-kit'
fs = require 'fs-plus'
Grim = require 'grim'
File = require './file'
PathWatcher = require './main'
# Extended: Represents a directory on disk that can be watched for changes.
module.exports =
class Directory
realPath: null
subscriptionCount: 0
###
Section: Construction
###
# Public: Configures a new Directory instance, no files are accessed.
#
# * `directoryPath` A {String} containing the absolute path to the directory
# * `symlink` (optional) A {Boolean} indicating if the path is a symlink.
# (default: false)
constructor: (directoryPath, @symlink=false, includeDeprecatedAPIs=Grim.includeDeprecatedAPIs) ->
@emitter = new Emitter
if includeDeprecatedAPIs
@on 'contents-changed-subscription-will-be-added', @willAddSubscription
@on 'contents-changed-subscription-removed', @didRemoveSubscription
if directoryPath
directoryPath = path.normalize(directoryPath)
# Remove a trailing slash
if directoryPath.length > 1 and directoryPath[directoryPath.length - 1] is path.sep
directoryPath = directoryPath.substring(0, directoryPath.length - 1)
@path = directoryPath
@lowerCasePath = @path.toLowerCase() if fs.isCaseInsensitive()
@reportOnDeprecations = true if Grim.includeDeprecatedAPIs
# Public: Creates the directory on disk that corresponds to `::getPath()` if
# no such directory already exists.
#
# * `mode` (optional) {Number} that defaults to `0777`.
#
# Returns a {Promise} that resolves once the directory is created on disk. It
# resolves to a boolean value that is true if the directory was created or
# false if it already existed.
create: (mode = 0o0777) ->
@exists().then (isExistingDirectory) =>
return false if isExistingDirectory
throw Error("Root directory does not exist: #{@getPath()}") if @isRoot()
@getParent().create().then =>
new Promise (resolve, reject) =>
fs.mkdir @getPath(), mode, (error) ->
if error
reject error
else
resolve true
###
Section: Event Subscription
###
# Public: Invoke the given callback when the directory's contents change.
#
# * `callback` {Function} to be called when the directory's contents change.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChange: (callback) ->
@willAddSubscription()
@trackUnsubscription(@emitter.on('did-change', callback))
willAddSubscription: =>
@subscribeToNativeChangeEvents() if @subscriptionCount is 0
@subscriptionCount++
didRemoveSubscription: =>
@subscriptionCount--
@unsubscribeFromNativeChangeEvents() if @subscriptionCount is 0
trackUnsubscription: (subscription) ->
new Disposable =>
subscription.dispose()
@didRemoveSubscription()
###
Section: Directory Metadata
###
# Public: Returns a {Boolean}, always false.
isFile: -> false
# Public: Returns a {Boolean}, always true.
isDirectory: -> true
# Public: Returns a {Boolean} indicating whether or not this is a symbolic link
isSymbolicLink: ->
@symlink
# Public: Returns a promise that resolves to a {Boolean}, true if the
# directory exists, false otherwise.
exists: ->
new Promise (resolve) => fs.exists(@getPath(), resolve)
# Public: Returns a {Boolean}, true if the directory exists, false otherwise.
existsSync: ->
fs.existsSync(@getPath())
# Public: Return a {Boolean}, true if this {Directory} is the root directory
# of the filesystem, or false if it isn't.
isRoot: ->
@getParent().getRealPathSync() is @getRealPathSync()
###
Section: Managing Paths
###
# Public: Returns the directory's {String} path.
#
# This may include unfollowed symlinks or relative directory entries. Or it
# may be fully resolved, it depends on what you give it.
getPath: -> @path
# Public: Returns this directory's completely resolved {String} path.
#
# All relative directory entries are removed and symlinks are resolved to
# their final destination.
getRealPathSync: ->
unless @realPath?
try
@realPath = fs.realpathSync(@path)
@lowerCaseRealPath = @realPath.toLowerCase() if fs.isCaseInsensitive()
catch e
@realPath = @path
@lowerCaseRealPath = @lowerCasePath if fs.isCaseInsensitive()
@realPath
# Public: Returns the {String} basename of the directory.
getBaseName: ->
path.basename(@path)
# Public: Returns the relative {String} path to the given path from this
# directory.
relativize: (fullPath) ->
return fullPath unless fullPath
# Normalize forward slashes to back slashes on windows
fullPath = fullPath.replace(/\//g, '\\') if process.platform is 'win32'
if fs.isCaseInsensitive()
pathToCheck = fullPath.toLowerCase()
directoryPath = @lowerCasePath
else
pathToCheck = fullPath
directoryPath = @path
if pathToCheck is directoryPath
return ''
else if @isPathPrefixOf(directoryPath, pathToCheck)
return fullPath.substring(directoryPath.length + 1)
# Check real path
@getRealPathSync()
if fs.isCaseInsensitive()
directoryPath = @lowerCaseRealPath
else
directoryPath = @realPath
if pathToCheck is directoryPath
''
else if @isPathPrefixOf(directoryPath, pathToCheck)
fullPath.substring(directoryPath.length + 1)
else
fullPath
# Given a relative path, this resolves it to an absolute path relative to this
# directory. If the path is already absolute or prefixed with a URI scheme, it
# is returned unchanged.
#
# * `uri` A {String} containing the path to resolve.
#
# Returns a {String} containing an absolute path or `undefined` if the given
# URI is falsy.
resolve: (relativePath) ->
return unless relativePath
if relativePath?.match(/[A-Za-z0-9+-.]+:\/\//) # leave path alone if it has a scheme
relativePath
else if fs.isAbsolute(relativePath)
path.normalize(fs.resolveHome(relativePath))
else
path.normalize(fs.resolveHome(path.join(@getPath(), relativePath)))
###
Section: Traversing
###
# Public: Traverse to the parent directory.
#
# Returns a {Directory}.
getParent: ->
new Directory(path.join @path, '..')
# Public: Traverse within this Directory to a child File. This method doesn't
# actually check to see if the File exists, it just creates the File object.
#
# * `filename` The {String} name of a File within this Directory.
#
# Returns a {File}.
getFile: (filename...) ->
new File(path.join @getPath(), filename...)
# Public: Traverse within this a Directory to a child Directory. This method
# doesn't actually check to see if the Directory exists, it just creates the
# Directory object.
#
# * `dirname` The {String} name of the child Directory.
#
# Returns a {Directory}.
getSubdirectory: (dirname...) ->
new Directory(path.join @path, dirname...)
# Public: Reads file entries in this directory from disk synchronously.
#
# Returns an {Array} of {File} and {Directory} objects.
getEntriesSync: ->
directories = []
files = []
for entryPath in fs.listSync(@path)
try
stat = fs.lstatSync(entryPath)
symlink = stat.isSymbolicLink()
stat = fs.statSync(entryPath) if symlink
if stat?.isDirectory()
directories.push(new Directory(entryPath, symlink))
else if stat?.isFile()
files.push(new File(entryPath, symlink))
directories.concat(files)
# Public: Reads file entries in this directory from disk asynchronously.
#
# * `callback` A {Function} to call with the following arguments:
# * `error` An {Error}, may be null.
# * `entries` An {Array} of {File} and {Directory} objects.
getEntries: (callback) ->
fs.list @path, (error, entries) ->
return callback(error) if error?
directories = []
files = []
addEntry = (entryPath, stat, symlink, callback) ->
if stat?.isDirectory()
directories.push(new Directory(entryPath, symlink))
else if stat?.isFile()
files.push(new File(entryPath, symlink))
callback()
statEntry = (entryPath, callback) ->
fs.lstat entryPath, (error, stat) ->
if stat?.isSymbolicLink()
fs.stat entryPath, (error, stat) ->
addEntry(entryPath, stat, true, callback)
else
addEntry(entryPath, stat, false, callback)
async.eachLimit entries, 1, statEntry, ->
callback(null, directories.concat(files))
# Public: Determines if the given path (real or symbolic) is inside this
# directory. This method does not actually check if the path exists, it just
# checks if the path is under this directory.
#
# * `pathToCheck` The {String} path to check.
#
# Returns a {Boolean} whether the given path is inside this directory.
contains: (pathToCheck) ->
return false unless pathToCheck
# Normalize forward slashes to back slashes on windows
pathToCheck = pathToCheck.replace(/\//g, '\\') if process.platform is 'win32'
if fs.isCaseInsensitive()
directoryPath = @lowerCasePath
pathToCheck = pathToCheck.toLowerCase()
else
directoryPath = @path
return true if @isPathPrefixOf(directoryPath, pathToCheck)
# Check real path
@getRealPathSync()
if fs.isCaseInsensitive()
directoryPath = @lowerCaseRealPath
else
directoryPath = @realPath
@isPathPrefixOf(directoryPath, pathToCheck)
###
Section: Private
###
subscribeToNativeChangeEvents: ->
@watchSubscription ?= PathWatcher.watch @path, (eventType) =>
if eventType is 'change'
@emit 'contents-changed' if Grim.includeDeprecatedAPIs
@emitter.emit 'did-change'
unsubscribeFromNativeChangeEvents: ->
if @watchSubscription?
@watchSubscription.close()
@watchSubscription = null
# Does given full path start with the given prefix?
isPathPrefixOf: (prefix, fullPath) ->
fullPath.indexOf(prefix) is 0 and fullPath[prefix.length] is path.sep
if Grim.includeDeprecatedAPIs
EmitterMixin = require('emissary').Emitter
EmitterMixin.includeInto(Directory)
Directory::on = (eventName) ->
if eventName is 'contents-changed'
Grim.deprecate("Use Directory::onDidChange instead")
else if @reportOnDeprecations
Grim.deprecate("Subscribing via ::on is deprecated. Use documented event subscription methods instead.")
EmitterMixin::on.apply(this, arguments)