123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- function _fs() {
- const data = _interopRequireDefault(require("fs"));
- _fs = function () {
- return data;
- };
- return data;
- }
- function _path() {
- const data = _interopRequireDefault(require("path"));
- _path = function () {
- return data;
- };
- return data;
- }
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- /**
- * Copyright (c) Facebook, Inc. and its affiliates.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- *
- * @format
- */
- // Binary files, don't process these (avoid decoding as utf8)
- const binaryExtensions = ['.png', '.jar', '.keystore'];
- /**
- * Copy a file to given destination, replacing parts of its contents.
- * @param srcPath Path to a file to be copied.
- * @param destPath Destination path.
- * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
- * @param contentChangedCallback
- * Used when upgrading projects. Based on if file contents would change
- * when being replaced, allows the caller to specify whether the file
- * should be replaced or not.
- * If null, files will be overwritten.
- * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
- */
- function copyAndReplace(srcPath, destPath, replacements, contentChangedCallback) {
- if (_fs().default.lstatSync(srcPath).isDirectory()) {
- if (!_fs().default.existsSync(destPath)) {
- _fs().default.mkdirSync(destPath);
- } // Not recursive
- return;
- }
- const extension = _path().default.extname(srcPath);
- if (binaryExtensions.indexOf(extension) !== -1) {
- // Binary file
- let shouldOverwrite = 'overwrite';
- if (contentChangedCallback) {
- const newContentBuffer = _fs().default.readFileSync(srcPath);
- let contentChanged = 'identical';
- try {
- const origContentBuffer = _fs().default.readFileSync(destPath);
- if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
- contentChanged = 'changed';
- }
- } catch (err) {
- if (err.code === 'ENOENT') {
- contentChanged = 'new';
- } else {
- throw err;
- }
- }
- shouldOverwrite = contentChangedCallback(destPath, contentChanged);
- }
- if (shouldOverwrite === 'overwrite') {
- copyBinaryFile(srcPath, destPath, err => {
- if (err) {
- throw err;
- }
- });
- }
- } else {
- // Text file
- const srcPermissions = _fs().default.statSync(srcPath).mode;
- let content = _fs().default.readFileSync(srcPath, 'utf8');
- Object.keys(replacements).forEach(regex => {
- content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
- });
- let shouldOverwrite = 'overwrite';
- if (contentChangedCallback) {
- // Check if contents changed and ask to overwrite
- let contentChanged = 'identical';
- try {
- const origContent = _fs().default.readFileSync(destPath, 'utf8');
- if (content !== origContent) {
- // logger.info('Content changed: ' + destPath);
- contentChanged = 'changed';
- }
- } catch (err) {
- if (err.code === 'ENOENT') {
- contentChanged = 'new';
- } else {
- throw err;
- }
- }
- shouldOverwrite = contentChangedCallback(destPath, contentChanged);
- }
- if (shouldOverwrite === 'overwrite') {
- _fs().default.writeFileSync(destPath, content, {
- encoding: 'utf8',
- mode: srcPermissions
- });
- }
- }
- }
- /**
- * Same as 'cp' on Unix. Don't do any replacements.
- */
- function copyBinaryFile(srcPath, destPath, cb) {
- let cbCalled = false;
- const srcPermissions = _fs().default.statSync(srcPath).mode;
- const readStream = _fs().default.createReadStream(srcPath);
- readStream.on('error', err => {
- done(err);
- });
- const writeStream = _fs().default.createWriteStream(destPath, {
- mode: srcPermissions
- });
- writeStream.on('error', err => {
- done(err);
- });
- writeStream.on('close', () => {
- done();
- });
- readStream.pipe(writeStream);
- function done(err) {
- if (!cbCalled) {
- cb(err);
- cbCalled = true;
- }
- }
- }
- var _default = copyAndReplace;
- exports.default = _default;
- //# sourceMappingURL=copyAndReplace.js.map
|