copyFiles.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _fs() {
  7. const data = _interopRequireDefault(require("fs"));
  8. _fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _path() {
  14. const data = _interopRequireDefault(require("path"));
  15. _path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _walk = _interopRequireDefault(require("./walk"));
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. /**
  23. * Copyright (c) Facebook, Inc. and its affiliates.
  24. *
  25. * This source code is licensed under the MIT license found in the
  26. * LICENSE file in the root directory of this source tree.
  27. */
  28. /**
  29. * Copy files (binary included) recursively.
  30. */
  31. async function copyFiles(srcPath, destPath, options = {}) {
  32. return Promise.all((0, _walk.default)(srcPath).map(async absoluteSrcFilePath => {
  33. const exclude = options.exclude;
  34. if (exclude && exclude.some(p => p.test(absoluteSrcFilePath))) {
  35. return;
  36. }
  37. const relativeFilePath = _path().default.relative(srcPath, absoluteSrcFilePath);
  38. await copyFile(absoluteSrcFilePath, _path().default.resolve(destPath, relativeFilePath));
  39. }));
  40. }
  41. /**
  42. * Copy a file to given destination.
  43. */
  44. function copyFile(srcPath, destPath) {
  45. if (_fs().default.lstatSync(srcPath).isDirectory()) {
  46. if (!_fs().default.existsSync(destPath)) {
  47. _fs().default.mkdirSync(destPath);
  48. } // Not recursive
  49. return;
  50. }
  51. return new Promise((resolve, reject) => {
  52. copyBinaryFile(srcPath, destPath, err => {
  53. if (err) {
  54. reject(err);
  55. }
  56. resolve(destPath);
  57. });
  58. });
  59. }
  60. /**
  61. * Same as 'cp' on Unix. Don't do any replacements.
  62. */
  63. function copyBinaryFile(srcPath, destPath, cb) {
  64. let cbCalled = false;
  65. const {
  66. mode
  67. } = _fs().default.statSync(srcPath);
  68. const readStream = _fs().default.createReadStream(srcPath);
  69. const writeStream = _fs().default.createWriteStream(destPath);
  70. readStream.on('error', err => {
  71. done(err);
  72. });
  73. writeStream.on('error', err => {
  74. done(err);
  75. });
  76. readStream.on('close', () => {
  77. done();
  78. _fs().default.chmodSync(destPath, mode);
  79. });
  80. readStream.pipe(writeStream);
  81. function done(err) {
  82. if (!cbCalled) {
  83. cb(err);
  84. cbCalled = true;
  85. }
  86. }
  87. }
  88. var _default = copyFiles;
  89. exports.default = _default;
  90. //# sourceMappingURL=copyFiles.js.map