-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filesystem.js
executable file
·188 lines (163 loc) · 6.61 KB
/
Filesystem.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
hwc.define([
'fs',
'path',
'hwc!{PATH_JS_LIB}nodejs/filesystem/include.js'
], function Filesystem (fs, path) {
var $ = this;
return $.NodeJs.Filesystem = $.class(
$.public.static({
/**
* This function allow to use existsSync in new nodejs versions
* @param {type} filePath
* @returns {Boolean}
*/
existsSync: fs.existsSync || function (filePath) {
try {
fs.statSync(filePath);
} catch (err) {
if (err.code == 'ENOENT')
return false;
}
return true;
},
remove: function (target, callback) {
var deferred = $.Async.defer(callback);
fs.stat(target, function (err, stats) {
if (err) {
console.log(JSON.stringify(err));
deferred.cbReject();
} else {
if (stats.isFile()) {
fs.unlink(target, function (err) {
if (err) {
console.log(JSON.stringify(err));
deferred.cbReject();
} else {
deferred.cbResolve();
}
});
return;
}
if (stats.isDirectory()) {
fs.rmdir(target, function (err) {
if (err) {
console.log(JSON.stringify(err));
deferred.cbReject();
} else {
deferred.cbResolve();
}
});
return;
}
}
});
return deferred.promise;
},
/**
*
* @param {type} source
* @param {type} target
* @param {type} options
* keepMTime: true/false
* @param {type} cb
* ( you can use Promise return value to avoid callback )
* @returns {Promise}
*/
copy: function (source, target, options, cb) {
var cbCalled = false;
console.log(cb.name);
var deferred = $.Async.defer(cb);
var rd = fs.createReadStream(source);
rd.on("error", function (err) {
done(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function (err) {
done(err);
});
wr.on("close", function (ex) {
var promises = [];
if (options.keepMTime == true) {
var deferred = $.Async.defer();
promises.push(deferred);
var stat = fs.stat(source, function () {
fs.utimes(target, stat.atime, stat.mtime, deferred.resolve);
});
}
$.Async.all(promises).then(function () {
done();
});
});
rd.pipe(wr);
function done (err) {
if (!cbCalled) {
deferred.cbResolve(err);
cbCalled = true;
}
}
return deferred.promise;
},
/**
*
* @param {type} dir
* @param {type} regExcludes, array of regexp
* @param {type} done callback function with results as first parameter
* ( you can use Promise return value to avoid callback )
* @returns {Promise}
*/
walk: function (dir, regExcludes, callback) {
var deferred = $.Async.defer();
function cb (err, result) {
deferred.cbResolve(err, result);
}
_walk(dir, regExcludes, cb);
function _walk (dir, regExcludes, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err)
return done(err);
var pending = list.length;
if (!pending)
return done(null, results);
list.forEach(function (file) {
file = path.join(dir, file);
var excluded = false;
var len = regExcludes.length;
var i = 0;
for (; i < len; i++) {
if (file.match(regExcludes[i])) {
excluded = true;
}
}
// Add if not in regExcludes
if (excluded === false) {
results.push(file);
// Check if its a folder
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
// If it is, walk again
_walk(file, regExcludes, function (err, res) {
results = results.concat(res);
if (!--pending) {
done(null, results);
}
});
} else {
if (!--pending) {
done(null, results);
}
}
});
} else {
if (!--pending) {
done(null, results);
}
}
});
});
}
return deferred.promise;
}
})
);
});