copy.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var fstream = require('fstream');
  2. var fstreamIgnore = require('fstream-ignore');
  3. var fs = require('graceful-fs');
  4. var Q = require('q');
  5. function copy(reader, writer) {
  6. var deferred;
  7. var ignore;
  8. // Filter symlinks because they are not 100% portable, specially
  9. // when linking between different drives
  10. // Following can't be enabled either because symlinks that reference
  11. // another symlinks will get filtered
  12. // See: https://github.com/bower/bower/issues/699
  13. reader.filter = filterSymlinks;
  14. reader.follow = false;
  15. if (reader.type === 'Directory' && reader.ignore) {
  16. ignore = reader.ignore;
  17. reader = fstreamIgnore(reader);
  18. reader.addIgnoreRules(ignore);
  19. } else {
  20. reader = fstream.Reader(reader);
  21. }
  22. deferred = Q.defer();
  23. reader
  24. .on('error', deferred.reject)
  25. // Pipe to writer
  26. .pipe(fstream.Writer(writer))
  27. .on('error', deferred.reject)
  28. .on('close', deferred.resolve);
  29. return deferred.promise;
  30. }
  31. function copyMode(src, dst) {
  32. return Q.nfcall(fs.stat, src)
  33. .then(function (stat) {
  34. return Q.nfcall(fs.chmod, dst, stat.mode);
  35. });
  36. }
  37. function filterSymlinks(entry) {
  38. return entry.type !== 'SymbolicLink';
  39. }
  40. function parseOptions(opts) {
  41. opts = opts || {};
  42. if (opts.mode != null) {
  43. opts.copyMode = false;
  44. } else if (opts.copyMode == null) {
  45. opts.copyMode = true;
  46. }
  47. return opts;
  48. }
  49. // ---------------------
  50. // Available options:
  51. // - mode: force final mode of dst (defaults to null)
  52. // - copyMode: copy mode of src to dst, only if mode is not specified (defaults to true)
  53. function copyFile(src, dst, opts) {
  54. var promise;
  55. opts = parseOptions(opts);
  56. promise = copy({
  57. path: src,
  58. type: 'File'
  59. }, {
  60. path: dst,
  61. mode: opts.mode,
  62. type: 'File'
  63. });
  64. if (opts.copyMode) {
  65. promise = promise.then(copyMode.bind(copyMode, src, dst));
  66. }
  67. return promise;
  68. }
  69. // Available options:
  70. // - ignore: array of patterns to be ignored (defaults to null)
  71. // - mode: force final mode of dst (defaults to null)
  72. // - copyMode: copy mode of src to dst, only if mode is not specified (defaults to true)
  73. function copyDir(src, dst, opts) {
  74. var promise;
  75. opts = parseOptions(opts);
  76. promise = copy({
  77. path: src,
  78. type: 'Directory',
  79. ignore: opts.ignore
  80. }, {
  81. path: dst,
  82. mode: opts.mode,
  83. type: 'Directory'
  84. });
  85. if (opts.copyMode) {
  86. promise = promise.then(copyMode.bind(copyMode, src, dst));
  87. }
  88. return promise;
  89. }
  90. module.exports.copyDir = copyDir;
  91. module.exports.copyFile = copyFile;