forked from posm/OpenMapKitServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.js
68 lines (63 loc) · 1.76 KB
/
file.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
'use strict';
var fs = require('fs');
var Q = require('q');
var recursive = require('recursive-readdir');
/**
*
* @param fd - full path to a file or directory
* @returns Returns a deferred promise. Resolve with "stat" object for the passed in file descriptor.
*/
module.exports.statDeferred = function(fd){
var deferred = Q.defer();
fs.stat(fd, function(err, stats){
if(err) {
deferred.reject(err);
}
deferred.resolve(stats);
});
return deferred.promise;
};
/**
*
* @param dirPath - full path to a directory
* @returns Returns a deferred promise. Resolved with an array of names of items in a directory.
*/
module.exports.readDirDeferred = function(dirPath){
var deferred = Q.defer();
fs.readdir(dirPath, function(err, contents){
if(err) {
deferred.reject(err);
}
deferred.resolve(contents);
});
return deferred.promise;
};
module.exports.readDirRecursiveDeferred = function (dirPath) {
var deferred = Q.defer();
recursive(dirPath, function(err, contents){
if(err) {
deferred.reject(err);
}
var contentsRelativePath = contents.map(function (absPath) {
return absPath.replace(dirPath+'/', '');
});
deferred.resolve(contentsRelativePath);
});
return deferred.promise;
};
/**
*
* @param filePath
* @returns Returns a deferred promise. Resolved with a string of file contents.
*/
module.exports.readFileDeferred = function(filePath, opts){
var options = opts || {};
var deferred = Q.defer();
fs.readFile(filePath, options, function(err, filestream){
if(err) {
deferred.reject(err);
}
deferred.resolve(filestream);
});
return deferred.promise;
};