This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem-node.coffee
147 lines (111 loc) · 3.28 KB
/
filesystem-node.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
Stat = require './stat'
fs = require 'graceful-fs'
_path = require 'path'
crypto = require 'crypto'
convert = require './convert'
module.exports =
class FilesystemNode
constructor: ({@name, @path, @digest, tree, stat}, @parent) ->
@stats = new Stat(stat)
@setTree(tree)
get: (path) ->
return this if @pathEquals(path)
if @mayContain(path)
match = null
@tree.find (node) ->
result = node.get(path)
if result?
match = result
true
match
pathEquals: (path) ->
path is @path or path is @localPath()
mayContain: (path) ->
return false if not @stats.isDirectory()
path.startsWith("#{@path}/") or path.startsWith(@localPath() + _path.sep)
has: (path) ->
@get(path)?
localPath: ->
return unless @path?
convert.remoteToLocal(@path)
update: (serializedNode) ->
node = @get(serializedNode.path)
if node?
node.constructor(serializedNode, node.parent)
node
else
@add(serializedNode)
remove: (path) ->
node = @get(path)
if not node?
return
parent = node.parent
if parent?
i = parent.tree.indexOf(node)
parent.tree.splice(i, 1)[0]
add: (serializedNode) ->
parentPath = _path.dirname(serializedNode.path)
parent = @get(parentPath) or @add({path: parentPath})
if parent.has(serializedNode.path)
return @update(serializedNode)
else
node = new FilesystemNode(serializedNode, parent)
parent.tree.push(node)
node
setTree: (tree) ->
if not tree?
@tree = []
else
entries = tree.filter (entry) -> entry?
@tree = entries.map (entry) => new FilesystemNode(entry, this)
setDigest: (digest) ->
@digest = digest
entries: ->
entries = []
@tree.forEach (node) ->
unless node.name.endsWith('.swp')
entries.push(node.name)
entries
siblings: ->
if not @parent?
[]
else
@parent.tree.filter (node) =>
node isnt this
list: (extension) ->
if extension?
entries = @entries().filter (entry) -> entry.endsWith(".#{extension}")
(entries or @entries()).map (entry) => "#{@path}/#{entry}"
traverse: (callback) ->
callback(this)
@tree.forEach (node) -> node.traverse(callback)
map: (callback, excluded) ->
initialValue = [callback(this)]
@tree.reduce (mapped, node) ->
if excluded? and node.path.match(excluded)
return mapped
mapped.concat(node.map(callback, excluded))
, initialValue
findPathsToSync: ->
pathsToSync = []
syncPromises = @map (node) ->
node.determineSync().then (shouldSync) ->
pathsToSync.push(node.path) if shouldSync
, /node_modules$|.git$|tmp$|vendor$|\.db$|\.swp/
Promise.all(syncPromises).then ->
pathsToSync
determineSync: ->
new Promise (resolve) =>
fs.stat @localPath(), (err, stats) =>
if err? or not @digest?
return resolve(true)
if stats.isDirectory()
return resolve(false)
hash = crypto.createHash('md5')
stream = fs.createReadStream(@localPath())
stream.on 'data', (data) ->
hash.update(data, 'utf8')
stream.on 'end', =>
stream.close()
localDigest = hash.digest('hex')
return resolve(@digest isnt localDigest)