Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

copy_dir renamed to copy and now fully async

  • Loading branch information...
commit d0198a9c1a9f8b139147d9f3080299a61708c15d 1 parent f0e465e
@Shogun147 authored
Showing with 58 additions and 24 deletions.
  1. +58 −24 lib/utils.js
View
82 lib/utils.js
@@ -322,34 +322,68 @@ module.exports = {
});
},
- copy_dir: function copy(src, dest, cb) {
- var load = module.exports.load;
-
- src = Path.normalize(src);
- dest = Path.normalize(dest);
-
- load(src, function(error) {
- cb(error);
- },
- function(file, callback) {
- if (!Fs.existsSync(dest + file.dirname)) {
- mkdir.sync(dest + file.dirname, 0777);
- }
+ copy: function copy(source, destination, replace, callback) {
+ var copy_file = module.exports.copy_file;
- if (file.isdir && !Fs.existsSync(dest + file.dirname + file.fname)) {
- mkdir.sync(dest + file.dirname + file.fname, 0777);
- }
+ if (typeof(replace) === 'function') {
+ callback = replace;
+ replace = true;
+ }
+
+ Fs.stat(source, function(error, stat) {
+ if (error) { return callback(error); }
- if (!file.isdir) {
- var source = Fs.createReadStream(file.path + file.dirname + file.name);
- var destination = Fs.createWriteStream(dest + file.dirname + file.name);
+ if (!stat.isDirectory()) {
+ copy_file(source, destination, replace, function(error) {
+ callback(error);
+ });
+ } else {
+ var next = function() {
+ Fs.readdir(source, function(error, items) {
+ var pending = items.length;
+
+ if (!pending) {
+ return callback();
+ }
+
+ items.forEach(function(item) {
+ Fs.stat(source +'/'+ item, function(error, stat) {
+ if (error) { return callback(error); }
+
+ if (!stat.isDirectory()) {
+ copy_file(source +'/'+ item, destination +'/'+ item, replace, function(error) {
+ if (error) {
+ console.log(error);
+ }
+
+ if (!--pending) {
+ callback();
+ }
+ });
+ } else {
+ copy(source +'/'+ item, destination +'/'+ item, function(error) {
+ if (error) { console.log(error); }
+
+ if (!--pending) {
+ callback();
+ }
+ });
+ }
+ });
+ });
+ });
+ }
- pump(source, destination, function(error) {
- if (error) { console.log(error); }
+ Fs.mkdir(destination, function(error) {
+ Fs.stat(destination, function(error, stat) {
+ if (error || !stat.isDirectory()) {
+ return callback(new Error(error || 'could not create directory: '+ destination));
+ }
- callback();
- });
- } else { callback(); }
+ next();
+ });
+ });
+ }
});
},
Please sign in to comment.
Something went wrong with that request. Please try again.