copyAndReplace.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. /**
  22. * Copyright (c) Facebook, Inc. and its affiliates.
  23. *
  24. * This source code is licensed under the MIT license found in the
  25. * LICENSE file in the root directory of this source tree.
  26. *
  27. * @format
  28. */
  29. // Binary files, don't process these (avoid decoding as utf8)
  30. const binaryExtensions = ['.png', '.jar', '.keystore'];
  31. /**
  32. * Copy a file to given destination, replacing parts of its contents.
  33. * @param srcPath Path to a file to be copied.
  34. * @param destPath Destination path.
  35. * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
  36. * @param contentChangedCallback
  37. * Used when upgrading projects. Based on if file contents would change
  38. * when being replaced, allows the caller to specify whether the file
  39. * should be replaced or not.
  40. * If null, files will be overwritten.
  41. * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
  42. */
  43. function copyAndReplace(srcPath, destPath, replacements, contentChangedCallback) {
  44. if (_fs().default.lstatSync(srcPath).isDirectory()) {
  45. if (!_fs().default.existsSync(destPath)) {
  46. _fs().default.mkdirSync(destPath);
  47. } // Not recursive
  48. return;
  49. }
  50. const extension = _path().default.extname(srcPath);
  51. if (binaryExtensions.indexOf(extension) !== -1) {
  52. // Binary file
  53. let shouldOverwrite = 'overwrite';
  54. if (contentChangedCallback) {
  55. const newContentBuffer = _fs().default.readFileSync(srcPath);
  56. let contentChanged = 'identical';
  57. try {
  58. const origContentBuffer = _fs().default.readFileSync(destPath);
  59. if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
  60. contentChanged = 'changed';
  61. }
  62. } catch (err) {
  63. if (err.code === 'ENOENT') {
  64. contentChanged = 'new';
  65. } else {
  66. throw err;
  67. }
  68. }
  69. shouldOverwrite = contentChangedCallback(destPath, contentChanged);
  70. }
  71. if (shouldOverwrite === 'overwrite') {
  72. copyBinaryFile(srcPath, destPath, err => {
  73. if (err) {
  74. throw err;
  75. }
  76. });
  77. }
  78. } else {
  79. // Text file
  80. const srcPermissions = _fs().default.statSync(srcPath).mode;
  81. let content = _fs().default.readFileSync(srcPath, 'utf8');
  82. Object.keys(replacements).forEach(regex => {
  83. content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
  84. });
  85. let shouldOverwrite = 'overwrite';
  86. if (contentChangedCallback) {
  87. // Check if contents changed and ask to overwrite
  88. let contentChanged = 'identical';
  89. try {
  90. const origContent = _fs().default.readFileSync(destPath, 'utf8');
  91. if (content !== origContent) {
  92. // logger.info('Content changed: ' + destPath);
  93. contentChanged = 'changed';
  94. }
  95. } catch (err) {
  96. if (err.code === 'ENOENT') {
  97. contentChanged = 'new';
  98. } else {
  99. throw err;
  100. }
  101. }
  102. shouldOverwrite = contentChangedCallback(destPath, contentChanged);
  103. }
  104. if (shouldOverwrite === 'overwrite') {
  105. _fs().default.writeFileSync(destPath, content, {
  106. encoding: 'utf8',
  107. mode: srcPermissions
  108. });
  109. }
  110. }
  111. }
  112. /**
  113. * Same as 'cp' on Unix. Don't do any replacements.
  114. */
  115. function copyBinaryFile(srcPath, destPath, cb) {
  116. let cbCalled = false;
  117. const srcPermissions = _fs().default.statSync(srcPath).mode;
  118. const readStream = _fs().default.createReadStream(srcPath);
  119. readStream.on('error', err => {
  120. done(err);
  121. });
  122. const writeStream = _fs().default.createWriteStream(destPath, {
  123. mode: srcPermissions
  124. });
  125. writeStream.on('error', err => {
  126. done(err);
  127. });
  128. writeStream.on('close', () => {
  129. done();
  130. });
  131. readStream.pipe(writeStream);
  132. function done(err) {
  133. if (!cbCalled) {
  134. cb(err);
  135. cbCalled = true;
  136. }
  137. }
  138. }
  139. var _default = copyAndReplace;
  140. exports.default = _default;
  141. //# sourceMappingURL=copyAndReplace.js.map