Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ncp.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. var fs = require('fs'),
  2. path = require('path');
  3. module.exports = ncp;
  4. ncp.ncp = ncp;
  5. function ncp (source, dest, options, callback) {
  6. var cback = callback;
  7. if (!callback) {
  8. cback = options;
  9. options = {};
  10. }
  11. var basePath = process.cwd(),
  12. currentPath = path.resolve(basePath, source),
  13. targetPath = path.resolve(basePath, dest),
  14. filter = options.filter,
  15. rename = options.rename,
  16. transform = options.transform,
  17. clobber = options.clobber !== false,
  18. modified = options.modified,
  19. dereference = options.dereference,
  20. errs = null,
  21. started = 0,
  22. finished = 0,
  23. running = 0,
  24. limit = options.limit || ncp.limit || 16;
  25. limit = (limit < 1) ? 1 : (limit > 512) ? 512 : limit;
  26. startCopy(currentPath);
  27. function startCopy(source) {
  28. started++;
  29. if (filter) {
  30. if (filter instanceof RegExp) {
  31. if (!filter.test(source)) {
  32. return cb(true);
  33. }
  34. }
  35. else if (typeof filter === 'function') {
  36. if (!filter(source)) {
  37. return cb(true);
  38. }
  39. }
  40. }
  41. return getStats(source);
  42. }
  43. function getStats(source) {
  44. var stat = dereference ? fs.stat : fs.lstat;
  45. if (running >= limit) {
  46. return setImmediate(function () {
  47. getStats(source);
  48. });
  49. }
  50. running++;
  51. stat(source, function (err, stats) {
  52. var item = {};
  53. if (err) {
  54. return onError(err);
  55. }
  56. // We need to get the mode from the stats object and preserve it.
  57. item.name = source;
  58. item.mode = stats.mode;
  59. item.mtime = stats.mtime; //modified time
  60. item.atime = stats.atime; //access time
  61. if (stats.isDirectory()) {
  62. return onDir(item);
  63. }
  64. else if (stats.isFile()) {
  65. return onFile(item);
  66. }
  67. else if (stats.isSymbolicLink()) {
  68. // Symlinks don't really need to know about the mode.
  69. return onLink(source);
  70. }
  71. });
  72. }
  73. function onFile(file) {
  74. var target = file.name.replace(currentPath, targetPath);
  75. if(rename) {
  76. target = rename(target);
  77. }
  78. isWritable(target, function (writable) {
  79. if (writable) {
  80. return copyFile(file, target);
  81. }
  82. if(clobber) {
  83. rmFile(target, function () {
  84. copyFile(file, target);
  85. });
  86. }
  87. if (modified) {
  88. var stat = dereference ? fs.stat : fs.lstat;
  89. stat(target, function(err, stats) {
  90. //if souce modified time greater to target modified time copy file
  91. if (file.mtime.getTime()>stats.mtime.getTime())
  92. copyFile(file, target);
  93. else return cb();
  94. });
  95. }
  96. else {
  97. return cb();
  98. }
  99. });
  100. }
  101. function copyFile(file, target) {
  102. var readStream = fs.createReadStream(file.name),
  103. writeStream = fs.createWriteStream(target, { mode: file.mode });
  104. readStream.on('error', onError);
  105. writeStream.on('error', onError);
  106. if(transform) {
  107. transform(readStream, writeStream, file);
  108. } else {
  109. writeStream.on('open', function() {
  110. readStream.pipe(writeStream);
  111. });
  112. }
  113. writeStream.once('finish', function() {
  114. if (modified) {
  115. //target file modified date sync.
  116. fs.utimesSync(target, file.atime, file.mtime);
  117. cb();
  118. }
  119. else cb();
  120. });
  121. }
  122. function rmFile(file, done) {
  123. fs.unlink(file, function (err) {
  124. if (err) {
  125. return onError(err);
  126. }
  127. return done();
  128. });
  129. }
  130. function onDir(dir) {
  131. var target = dir.name.replace(currentPath, targetPath);
  132. isWritable(target, function (writable) {
  133. if (writable) {
  134. return mkDir(dir, target);
  135. }
  136. copyDir(dir.name);
  137. });
  138. }
  139. function mkDir(dir, target) {
  140. fs.mkdir(target, dir.mode, function (err) {
  141. if (err) {
  142. return onError(err);
  143. }
  144. copyDir(dir.name);
  145. });
  146. }
  147. function copyDir(dir) {
  148. fs.readdir(dir, function (err, items) {
  149. if (err) {
  150. return onError(err);
  151. }
  152. items.forEach(function (item) {
  153. startCopy(path.join(dir, item));
  154. });
  155. return cb();
  156. });
  157. }
  158. function onLink(link) {
  159. var target = link.replace(currentPath, targetPath);
  160. fs.readlink(link, function (err, resolvedPath) {
  161. if (err) {
  162. return onError(err);
  163. }
  164. checkLink(resolvedPath, target);
  165. });
  166. }
  167. function checkLink(resolvedPath, target) {
  168. if (dereference) {
  169. resolvedPath = path.resolve(basePath, resolvedPath);
  170. }
  171. isWritable(target, function (writable) {
  172. if (writable) {
  173. return makeLink(resolvedPath, target);
  174. }
  175. fs.readlink(target, function (err, targetDest) {
  176. if (err) {
  177. return onError(err);
  178. }
  179. if (dereference) {
  180. targetDest = path.resolve(basePath, targetDest);
  181. }
  182. if (targetDest === resolvedPath) {
  183. return cb();
  184. }
  185. return rmFile(target, function () {
  186. makeLink(resolvedPath, target);
  187. });
  188. });
  189. });
  190. }
  191. function makeLink(linkPath, target) {
  192. fs.symlink(linkPath, target, function (err) {
  193. if (err) {
  194. return onError(err);
  195. }
  196. return cb();
  197. });
  198. }
  199. function isWritable(path, done) {
  200. fs.lstat(path, function (err) {
  201. if (err) {
  202. if (err.code === 'ENOENT') return done(true);
  203. return done(false);
  204. }
  205. return done(false);
  206. });
  207. }
  208. function onError(err) {
  209. if (options.stopOnError) {
  210. return cback(err);
  211. }
  212. else if (!errs && options.errs) {
  213. errs = fs.createWriteStream(options.errs);
  214. }
  215. else if (!errs) {
  216. errs = [];
  217. }
  218. if (typeof errs.write === 'undefined') {
  219. errs.push(err);
  220. }
  221. else {
  222. errs.write(err.stack + '\n\n');
  223. }
  224. return cb();
  225. }
  226. function cb(skipped) {
  227. if (!skipped) running--;
  228. finished++;
  229. if ((started === finished) && (running === 0)) {
  230. if (cback !== undefined ) {
  231. return errs ? cback(errs) : cback(null);
  232. }
  233. }
  234. }
  235. }