2015-10-16 15:20:29 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2015-10-16 15:20:29 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
2015-10-16 15:20:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2016-11-19 02:25:02 +00:00
|
|
|
const path = require('path');
|
2015-10-16 15:20:29 +00:00
|
|
|
|
2016-11-19 02:25:02 +00:00
|
|
|
// Binary files, don't process these (avoid decoding as utf8)
|
|
|
|
const binaryExtensions = ['.png', '.jar'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
function copyAndReplace(
|
|
|
|
srcPath,
|
|
|
|
destPath,
|
|
|
|
replacements,
|
|
|
|
contentChangedCallback,
|
|
|
|
) {
|
2016-11-19 02:25:02 +00:00
|
|
|
if (fs.lstatSync(srcPath).isDirectory()) {
|
|
|
|
if (!fs.existsSync(destPath)) {
|
|
|
|
fs.mkdirSync(destPath);
|
|
|
|
}
|
|
|
|
// Not recursive
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const extension = path.extname(srcPath);
|
|
|
|
if (binaryExtensions.indexOf(extension) !== -1) {
|
|
|
|
// Binary file
|
|
|
|
let shouldOverwrite = 'overwrite';
|
|
|
|
if (contentChangedCallback) {
|
|
|
|
const newContentBuffer = fs.readFileSync(srcPath);
|
|
|
|
let contentChanged = 'identical';
|
|
|
|
try {
|
|
|
|
const origContentBuffer = fs.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') {
|
2018-05-11 19:43:49 +00:00
|
|
|
copyBinaryFile(srcPath, destPath, err => {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2016-11-19 02:25:02 +00:00
|
|
|
});
|
2015-10-16 15:20:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-19 02:25:02 +00:00
|
|
|
// Text file
|
|
|
|
const srcPermissions = fs.statSync(srcPath).mode;
|
|
|
|
let content = fs.readFileSync(srcPath, 'utf8');
|
2018-05-11 19:43:49 +00:00
|
|
|
Object.keys(replacements).forEach(
|
|
|
|
regex =>
|
|
|
|
(content = content.replace(
|
|
|
|
new RegExp(regex, 'g'),
|
|
|
|
replacements[regex],
|
|
|
|
)),
|
2015-10-16 15:20:29 +00:00
|
|
|
);
|
2016-11-19 02:25:02 +00:00
|
|
|
|
|
|
|
let shouldOverwrite = 'overwrite';
|
|
|
|
if (contentChangedCallback) {
|
|
|
|
// Check if contents changed and ask to overwrite
|
|
|
|
let contentChanged = 'identical';
|
|
|
|
try {
|
|
|
|
const origContent = fs.readFileSync(destPath, 'utf8');
|
|
|
|
if (content !== origContent) {
|
|
|
|
//console.log('Content changed: ' + destPath);
|
|
|
|
contentChanged = 'changed';
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
contentChanged = 'new';
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shouldOverwrite = contentChangedCallback(destPath, contentChanged);
|
|
|
|
}
|
|
|
|
if (shouldOverwrite === 'overwrite') {
|
|
|
|
fs.writeFileSync(destPath, content, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
mode: srcPermissions,
|
|
|
|
});
|
|
|
|
}
|
2015-10-16 15:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 02:25:02 +00:00
|
|
|
/**
|
|
|
|
* Same as 'cp' on Unix. Don't do any replacements.
|
|
|
|
*/
|
|
|
|
function copyBinaryFile(srcPath, destPath, cb) {
|
|
|
|
let cbCalled = false;
|
|
|
|
const srcPermissions = fs.statSync(srcPath).mode;
|
|
|
|
const readStream = fs.createReadStream(srcPath);
|
|
|
|
readStream.on('error', function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
const writeStream = fs.createWriteStream(destPath, {
|
2018-05-11 19:43:49 +00:00
|
|
|
mode: srcPermissions,
|
2016-11-19 02:25:02 +00:00
|
|
|
});
|
|
|
|
writeStream.on('error', function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
writeStream.on('close', function(ex) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
readStream.pipe(writeStream);
|
|
|
|
function done(err) {
|
|
|
|
if (!cbCalled) {
|
|
|
|
cb(err);
|
|
|
|
cbCalled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 22:33:54 +00:00
|
|
|
|
2016-11-19 02:25:02 +00:00
|
|
|
module.exports = copyAndReplace;
|