-
Notifications
You must be signed in to change notification settings - Fork 3
/
fs2.js
351 lines (319 loc) · 9.87 KB
/
fs2.js
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Tiny wrapper over node fs module that makes its API handy, consistent and fixes legacy bugs.
//
// By handy I mean that it automatically creates parent directories, deletes not empty directories
// and other frequently needed things.
//
// Fetures:
//
// - createDirectory should create directory and parents if needed
// - deleteDirectory should delete directory with its content
// - delete should delete file or directory with its content
// - writeFile should write or overwrite file and create parents if needed
// - readFileSplittedBy should read file splitted by expression
// - appendFile should append file and create parents if needed
// - writeFileAtomically should atomically write or overwrite file and create parents if needed
// - readDirectory should list directory content recursively
// - copy should copy or overwrite file and create parents if needed
// - createTemporaryDirectory should create empty temporarry directory
var fs = require('fs')
var fsPath = require('path')
var util = require('util')
var os = require('os')
// Async helpers.
var fork = function(errCallback, cb){
if(!errCallback) throw new Error('no cb for error for fork')
return function(){
var err = arguments[0]
var argsWithoutError = [].slice.call(arguments, 1) || []
if(err) return errCallback(err)
if(cb) cb.apply(null, argsWithoutError)
}
}
var asyncEach = function(list, onEach, cb){
var processNext = function(i){
if(i == list.length) return cb()
try{
var next = function(err){
if(err) cb(err)
else processNext(i + 1)
}
if(onEach.length == 2) onEach(list[i], next)
else onEach(list[i], i, next)
}catch(e){cb(e)}
}
processNext(0)
}
// fs2.
var fs2 = {}
// Copying functions from standard `fs` module.
for(var attr in fs){fs2[attr] = fs[attr]}
// Create directory and all parent directories if they not exists.
fs2.createDirectory = function(path, cb){
fs.mkdir(path, function(err){
if(!err || err.code == 'EEXIST') return cb()
if(err.code == 'ENOENT')
fs2.createDirectory(fsPath.dirname(path), fork(cb, function(){
fs.mkdir(path, cb)
}))
else cb(err)
})
}
// Delete file or directory (empty or not).
fs2.delete = function(path, cb){
fs.unlink(path, function(err){
if(!err) return cb()
if(err.code == 'ENOENT') cb()
else if(err.code == 'EPERM') fs2.deleteDirectory(path, cb)
else cb(err)
})
}
// Delete directory (empty or not)
fs2.deleteDirectory = function(path, cb){
fs.rmdir(path, function(err){
if(!err) return cb()
if(err.code == 'ENOENT') cb()
else if(err.code == 'ENOTEMPTY'){
fs.readdir(path, fork(cb, function(entries){
asyncEach(entries, function(entry, next){
var child = fsPath.join(path, entry)
fs2.delete(child, next)
}, fork(cb, function(){
fs.rmdir(path, cb)
}))
}))
}else cb(err)
})
}
// Write file and create parent dirs if not exists.
fs2.writeFile = function(){
var args = [].slice.call(arguments, 0)
var path = args[0]
var cb = args[args.length - 1]
var args = args.slice(0, arguments.length - 1)
args.push(function(err){
if(!err) return cb()
if(err.code == 'ENOENT')
fs2.createDirectory(fsPath.dirname(path), fork(cb, function(){
args.pop()
args.push(cb)
fs.writeFile.apply(null, args)
}))
else cb(err)
})
fs.writeFile.apply(null, args)
}
fs2.appendFile = function(){
var args = [].slice.call(arguments, 0)
var path = args[0]
var cb = args[args.length - 1]
var args = args.slice(0, arguments.length - 1)
args.push(function(err){
if(!err) return cb()
if(err.code == 'ENOENT')
fs2.createDirectory(fsPath.dirname(path), fork(cb, function(){
args.pop()
args.push(cb)
fs.appendFile.apply(null, args)
}))
else cb(err)
})
fs.appendFile.apply(null, args)
}
// Write file atomically, if file exists it will be overriden. It writes content to temporarry
// file and then atomically rename temporarry file to the real.
fs2.writeFileAtomically = function(){
var args = [].slice.call(arguments, 0)
var path = args[0]
var cb = args[args.length - 1]
var tmpPath = path + '.tmp'
args.shift()
args.unshift(tmpPath)
args.pop()
args.push(function(err){
if(err) return cb(err)
// Atomically renaming temporarry file to real file.
fs.rename(tmpPath, path, cb)
})
// Writing file.
fs2.writeFile.apply(null, args)
}
// File or directory existence.
fs2.exists = function(path, cb){
fs.stat(path, function(err, stat){
if(err && err.code == 'ENOENT') return cb(null, false)
if(err) return cb(err)
cb(null, true)
})
}
// File existence.
fs2.isFile = function(path, cb){
fs.stat(path, function(err, stat){
if(err && err.code == 'ENOENT') return cb(null, false)
if(err) return cb(err)
cb(null, stat.isFile())
})
}
// Directory existence.
fs2.isDirectory = function(path, cb){
fs.stat(path, function(err, stat){
if(err && err.code == 'ENOENT') return cb(null, false)
if(err) return cb(err)
cb(null, stat.isDirectory())
})
}
// List directory content, accept `recursive` and `relative` options.
fs2.readDirectory = function(path, options, cb){
cb = cb || options
var basePath = options.relative ? null : path
var readDirectory = function(path, base, cb){
var list = []
fs.readdir(path, fork(cb, function(entries){
asyncEach(entries, function(entry, next){
var entryPath = fsPath.join(path, entry)
var entryBasePath = base ? fsPath.join(base, entry) : entry
fs2.isDirectory(entryPath, fork(cb, function(isDirectory){
if(isDirectory){
list.push({type: 'directory', path: entryBasePath})
if(options.recursive)
readDirectory(entryPath, entryBasePath, fork(cb, function(entryList){
Array.prototype.push.apply(list, entryList)
next()
}))
else next()
}else{
list.push({type: 'file', path: entryBasePath})
next()
}
}))
}, fork(cb, function(){
cb(null, list)
}))
}))
}
readDirectory(path, basePath, cb)
}
// Copy or overwrite file and create parents if needed.
fs2.copy = function(from, to, cb){
var copy = function(from, to, createParents, cb){
var fromStream = fs.createReadStream(from)
var toStream = fs.createWriteStream(to)
fromStream.pipe(toStream)
// If errors are in both streams callback will be called twice,
// need this to report only the first error.
var callbackCalled = false
var callbackForReadStream = function(){
if(callbackCalled) return
callbackCalled = true
fromStream.destroy()
toStream.destroy()
cb.apply(null, arguments)
}
fromStream.on('error', callbackForReadStream)
fromStream.on('close', callbackForReadStream)
// If errors are in both streams callback will be called twice,
// need this to report only the first error.
// It also creates parents for destination if needed.
var callbackForWriteStream = function(err){
if(callbackCalled) return
callbackCalled = true
fromStream.destroy()
toStream.destroy()
if(createParents && err.code == 'ENOENT')
// Parents for detination not exist, creating it.
fs2.createDirectory(fsPath.dirname(to), fork(cb, function(){
copy(from, to, false, cb)
}))
else cb.apply(null, arguments)
}
toStream.on('error', callbackForWriteStream)
}
copy(from, to, true, cb)
}
// Get empty temporarry directory.
fs2.getTemporaryDirectory = function(cb){
var tmpDir = os.tmpDir()
tmpDir = tmpDir.slice(0, (tmpDir.length - 1))
fs2.deleteDirectory(tmpDir, fork(cb, function(){
fs2.createDirectory(tmpDir, fork(cb, function(){
fs2.readDirectory(tmpDir, fork(cb, function(list){
cb(null, tmpDir)
}))
}))
}))
}
fs2.readFileSplittedBy = function(filePath, splitExpression, lineCb, cb){
// Guard to call callback only once.
var callbackCalled = false
var originalCb = cb
cb = function(){
if(callbackCalled) return
callbackCalled = true
originalCb.apply(null, arguments)
}
var stream = fs2.createReadStream(filePath)
stream.setEncoding('utf8')
var buffer = []
var flushBuffer = function(chunk){
if(chunk) buffer.push(chunk)
if(buffer.length > 0){
lineCb(buffer.join())
buffer = []
}
}
stream.on('data', function(chunk){
var lineChunks = chunk.split(splitExpression)
// No newlines.
if(lineChunks.length === 1){
buffer.push(chunk)
// At least one newline.
}else{
// Adding first line chunk to buffer and processing it.
flushBuffer(lineChunks[0])
// Processing newlines in the middle.
for(var i = 1; i < lineChunks.length - 1; i++) flushBuffer(lineChunks[i])
// Checking if the last symbol is newline.
var lastChunk = lineChunks[lineChunks.length - 1]
if(lastChunk !== '') buffer.push(lastChunk)
}
})
stream.on('error', function(err){cb(err)})
stream.on('end', function(){
flushBuffer()
cb()
})
stream.resume()
}
// Validates strings doesn't contain dangerous characters.
fs2.ensureSafe = function(){
for(var name in arguments){
if(/\.\./.test(name)) throw new Error("unsafe path (" + name + ")!")
}
}
// Synchronizing.
// fs2/sync is optional module and its dependency - synchronize.js not included in
// fs2 dependency modules list, You should install it by itself.
var sync = require('synchronize')
sync(fs2,
'createDirectory',
'delete',
'deleteDirectory',
'writeFile',
'writeFileAtomically',
'exists',
'isFile',
'isDirectory',
'readDirectory',
'copy',
'readFile',
'writeFile',
'stat',
'getTemporaryDirectory',
'close',
'createReadStream',
'createWriteStream'
)
// Export.
module.exports = fs2
// Need this for fs2 tests.
module.exports.fork = fork
module.exports.asyncEach = asyncEach