copydir.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var async = require("async");
  2. var fs = require("fs");
  3. var path = require("path");
  4. // cursively make dir
  5. function mkdirs(p, mode, f, made) {
  6. if (typeof mode === 'function' || mode === undefined) {
  7. f = mode;
  8. mode = 0777 & (~process.umask());
  9. }
  10. if (!made)
  11. made = null;
  12. var cb = f || function () { };
  13. if (typeof mode === 'string')
  14. mode = parseInt(mode, 8);
  15. p = path.resolve(p);
  16. fs.mkdir(p, mode, function (er) {
  17. if (!er) {
  18. made = made || p;
  19. return cb(null, made);
  20. }
  21. switch (er.code) {
  22. case 'ENOENT':
  23. mkdirs(path.dirname(p), mode, function (er, made) {
  24. if (er) {
  25. cb(er, made);
  26. } else {
  27. mkdirs(p, mode, cb, made);
  28. }
  29. });
  30. break;
  31. // In the case of any other error, just see if there's a dir
  32. // there already. If so, then hooray! If not, then something
  33. // is borked.
  34. default:
  35. fs.stat(p, function (er2, stat) {
  36. // if the stat fails, then that's super weird.
  37. // let the original error be the failure reason.
  38. if (er2 || !stat.isDirectory()) {
  39. cb(er, made);
  40. } else {
  41. cb(null, made)
  42. };
  43. });
  44. break;
  45. }
  46. });
  47. }
  48. // single file copy
  49. function copyFile(file, toDir, cb) {
  50. async.waterfall([
  51. function (callback) {
  52. fs.exists(toDir, function (exists) {
  53. if (exists) {
  54. callback(null, false);
  55. } else {
  56. callback(null, true);
  57. }
  58. });
  59. }, function (need, callback) {
  60. if (need) {
  61. mkdirs(path.dirname(toDir), callback);
  62. } else {
  63. callback(null, true);
  64. }
  65. }, function (p, callback) {
  66. var reads = fs.createReadStream(file);
  67. var writes = fs.createWriteStream(path.join(path.dirname(toDir), path.basename(file)));
  68. reads.pipe(writes);
  69. //don't forget close the when all the data are read
  70. reads.on("end", function () {
  71. writes.end();
  72. callback(null);
  73. });
  74. reads.on("error", function (err) {
  75. console.log("error occur in reads");
  76. callback(true, err);
  77. });
  78. }
  79. ], cb);
  80. }
  81. // cursively count the files that need to be copied
  82. function _ccoutTask(from, to, cbw) {
  83. async.waterfall([
  84. function (callback) {
  85. fs.stat(from, callback);
  86. },
  87. function (stats, callback) {
  88. if (stats.isFile()) {
  89. cbw.addFile(from, to);
  90. callback(null, []);
  91. } else if (stats.isDirectory()) {
  92. fs.readdir(from, callback);
  93. }
  94. },
  95. function (files, callback) {
  96. if (files.length) {
  97. for (var i = 0; i < files.length; i++) {
  98. _ccoutTask(path.join(from, files[i]), path.join(to, files[i]), cbw.increase());
  99. }
  100. }
  101. callback(null);
  102. }
  103. ], cbw);
  104. }
  105. // wrap the callback before counting
  106. function ccoutTask(from, to, cb) {
  107. var files = [];
  108. var count = 1;
  109. function wrapper(err) {
  110. count--;
  111. if (err || count <= 0) {
  112. cb(err, files)
  113. }
  114. }
  115. wrapper.increase = function () {
  116. count++;
  117. return wrapper;
  118. }
  119. wrapper.addFile = function (file, dir) {
  120. files.push({
  121. file: file,
  122. dir: dir
  123. });
  124. }
  125. _ccoutTask(from, to, wrapper);
  126. }
  127. function copyDir(from, to, cb) {
  128. if (!cb) {
  129. cb = function () { };
  130. }
  131. async.waterfall([
  132. function (callback) {
  133. fs.exists(from, function (exists) {
  134. if (exists) {
  135. callback(null, true);
  136. } else {
  137. console.log(from + " not exists");
  138. callback(true);
  139. }
  140. });
  141. },
  142. function (exists, callback) {
  143. fs.stat(from, callback);
  144. },
  145. function (stats, callback) {
  146. if (stats.isFile()) {
  147. // one file copy
  148. copyFile(from, to, function (err) {
  149. if (err) {
  150. // break the waterfall
  151. callback(true);
  152. } else {
  153. callback(null, []);
  154. }
  155. });
  156. } else if (stats.isDirectory()) {
  157. ccoutTask(from, to, callback);
  158. }
  159. },
  160. function (files, callback) {
  161. // prevent reaching to max file open limit
  162. async.mapLimit(files, 10, function (f, cb) {
  163. copyFile(f.file, f.dir, cb);
  164. }, callback);
  165. }
  166. ], cb);
  167. }
  168. module.exports = copyDir