Permalink
Browse files

added empty

  • Loading branch information...
1 parent 4620214 commit b702d206f323753638125b875bc4af1db000e4ec @tommy351 committed Sep 3, 2012
Showing with 69 additions and 1 deletion.
  1. +69 −1 lib/qfs.js
View
@@ -246,6 +246,44 @@ qfs.fn.parentsUntil = function(name){
return qfs(utils.unique(arr));
};
+var emptyDirSync = function(){
+ this.children().each(function(){
+ var stat = this.stat();
+
+ if (stat.isFile()){
+ fs.unlink(this.path());
+ } else {
+ emptyDirSync.apply(this);
+ }
+ });
+};
+
+var emptyDir = function(callback){
+ this.children(function(err, files){
+ if (err) return callback(err);
+
+ var fn = [];
+
+ files.each(function(){
+ var _this = this;
+
+ fn.push(function(next){
+ _this.stat(function(err, stat){
+ if (err) return callback(err);
+
+ if (stat.isFile()){
+ fs.unlink(_this.path(), next);
+ } else {
+ emptyDir.call(_this, next);
+ }
+ });
+ });
+ });
+
+ async.parallel(fn, callback);
+ });
+};
+
utils.each({
children: [
function(selector, callback){
@@ -274,7 +312,7 @@ utils.each({
async.parallel(fn, function(err, results){
if (err) return callback(err);
var arr = qfs(utils.merge.apply(null, results));
- callback(_this, arr);
+ callback.call(_this, err, arr);
});
return this;
@@ -401,10 +439,40 @@ utils.each({
],
empty: [
function(callback){
+ var fn = [];
+ this.each(function(){
+ var _this = this;
+
+ fn.push(function(next){
+ _this.stat(function(err, stat){
+ if (err) return callback(err);
+
+ if (stat.isFile()){
+ _this.write('');
+ } else {
+ emptyDir.call(_this, next);
+ }
+ });
+ });
+ });
+
+ async.parallel(fn, callback);
+
+ return this;
},
function(){
+ this.each(function(){
+ var stat = this.stat();
+ if (stat.isFile()){
+ this.write('');
+ } else {
+ emptyDirSync.apply(this);
+ }
+ });
+
+ return this;
}
],
write: [

0 comments on commit b702d20

Please sign in to comment.