@rnv/engine-rn-windows
Version:
ReNative Engine to build for Windows platform with react native support.
300 lines • 13.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.walk = exports.resolveContents = exports.createDir = exports.copyBinaryFile = exports.upgradeFileContentChangedCallback = exports.alwaysOverwriteContentChangedCallback = exports.copyAndReplaceWithChangedCallback = exports.copyAndReplaceAll = exports.copyAndReplace = void 0;
var tslib_1 = require("tslib");
/**
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
* @format
*/
var fs_1 = tslib_1.__importDefault(require("fs"));
var path_1 = tslib_1.__importDefault(require("path"));
var mustache_1 = tslib_1.__importDefault(require("mustache"));
var core_1 = require("@rnv/core");
var logInfo = core_1.Logger.logInfo;
var defaultOptions = {
overwrite: false,
logging: false,
};
function walk(current) {
if (!fs_1.default.lstatSync(current).isDirectory()) {
return [current];
}
var files = fs_1.default.readdirSync(current).map(function (child) { return walk(path_1.default.join(current, child)); });
var result = [];
return result.concat.apply([current], files);
}
exports.walk = walk;
/**
* Get a source file and replace parts of its contents.
* @param srcPath Path to the source file.
* @param replacements e.g. {'TextToBeReplaced': 'Replacement'}
* @return The contents of the file with the replacements applied.
*/
function resolveContents(srcPath, replacements) {
var content = fs_1.default.readFileSync(srcPath, 'utf8');
if (content.includes('\r\n')) {
// CRLF file, make sure multiline replacements are also CRLF
for (var _i = 0, _a = Object.keys(replacements); _i < _a.length; _i++) {
var key = _a[_i];
if (typeof replacements[key] === 'string') {
replacements[key] = replacements[key].replace(/(?<!\r)\n/g, '\r\n');
}
}
}
else {
// LF file, make sure multiline replacements are also LF
for (var _b = 0, _c = Object.keys(replacements); _b < _c.length; _b++) {
var key = _c[_b];
if (typeof replacements[key] === 'string') {
replacements[key] = replacements[key].replace(/\r\n/g, '\n');
}
}
}
if (replacements.useMustache) {
content = mustache_1.default.render(content, replacements);
(replacements.regExpPatternsToRemove || []).forEach(function (regexPattern) {
content = content.replace(new RegExp(regexPattern, 'g'), '');
});
}
else {
Object.keys(replacements).forEach(function (regex) {
content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
});
}
return content;
}
exports.resolveContents = resolveContents;
// Binary files, don't process these (avoid decoding as utf8)
var 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, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var extension, shouldOverwrite, newContentBuffer, contentChanged, origContentBuffer, srcPermissions, content, shouldOverwrite, contentChanged, origContent;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (fs_1.default.lstatSync(srcPath).isDirectory()) {
if (!fs_1.default.existsSync(destPath)) {
fs_1.default.mkdirSync(destPath);
}
// Not recursive
return [2 /*return*/];
}
extension = path_1.default.extname(srcPath);
if (!(binaryExtensions.indexOf(extension) !== -1)) return [3 /*break*/, 3];
shouldOverwrite = 'overwrite';
if (!contentChangedCallback) return [3 /*break*/, 2];
newContentBuffer = fs_1.default.readFileSync(srcPath);
contentChanged = 'identical';
try {
origContentBuffer = fs_1.default.readFileSync(destPath);
if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
contentChanged = 'changed';
}
}
catch (err) {
if (err.code === 'ENOENT') {
contentChanged = 'new';
}
else {
throw err;
}
}
return [4 /*yield*/, contentChangedCallback(destPath, contentChanged)];
case 1:
shouldOverwrite = _a.sent();
_a.label = 2;
case 2:
if (shouldOverwrite === 'overwrite') {
copyBinaryFile(srcPath, destPath, function (err) {
if (err) {
throw err;
}
});
}
return [3 /*break*/, 6];
case 3:
srcPermissions = fs_1.default.statSync(srcPath).mode;
content = resolveContents(srcPath, replacements);
shouldOverwrite = 'overwrite';
if (!contentChangedCallback) return [3 /*break*/, 5];
contentChanged = 'identical';
try {
origContent = fs_1.default.readFileSync(destPath, 'utf8');
if (content !== origContent) {
if (options.logging && options.overwrite) {
logInfo("[changed] ".concat(destPath));
}
contentChanged = 'changed';
}
}
catch (err) {
if (err.code === 'ENOENT') {
contentChanged = 'new';
}
else {
throw err;
}
}
return [4 /*yield*/, contentChangedCallback(destPath, contentChanged)];
case 4:
shouldOverwrite = _a.sent();
_a.label = 5;
case 5:
if (shouldOverwrite === 'overwrite') {
fs_1.default.writeFileSync(destPath, content, {
encoding: 'utf8',
mode: srcPermissions,
});
}
_a.label = 6;
case 6: return [2 /*return*/];
}
});
});
}
exports.copyAndReplace = copyAndReplace;
/**
* Same as 'cp' on Unix. Don't do any replacements.
*/
function copyBinaryFile(srcPath, destPath, cb) {
var cbCalled = false;
var srcPermissions = fs_1.default.statSync(srcPath).mode;
var readStream = fs_1.default.createReadStream(srcPath);
readStream.on('error', function (err) {
done(err);
});
var writeStream = fs_1.default.createWriteStream(destPath, {
mode: srcPermissions,
});
writeStream.on('error', function (err) {
done(err);
});
writeStream.on('close', function () {
done();
});
readStream.pipe(writeStream);
function done(err) {
if (!cbCalled) {
cb(err);
cbCalled = true;
}
}
}
exports.copyBinaryFile = copyBinaryFile;
function createDir(destPath) {
if (!fs_1.default.existsSync(destPath)) {
fs_1.default.mkdirSync(destPath);
}
}
exports.createDir = createDir;
// eslint-disable-next-line max-len
function copyAndReplaceWithChangedCallback(srcPath, destRoot, relativeDestPath, replacements, options) {
if (options === void 0) { options = defaultOptions; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var contentChangedCallback;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!replacements) {
// eslint-disable-next-line no-param-reassign
replacements = {};
}
contentChangedCallback = options.overwrite
? // eslint-disable-next-line max-len
function (_, contentChanged) {
return alwaysOverwriteContentChangedCallback(srcPath, relativeDestPath, contentChanged, options);
}
: function (_, contentChanged) { return upgradeFileContentChangedCallback(srcPath, relativeDestPath, contentChanged, options); };
return [4 /*yield*/, copyAndReplace(srcPath, path_1.default.join(destRoot, relativeDestPath), replacements, contentChangedCallback, options)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
exports.copyAndReplaceWithChangedCallback = copyAndReplaceWithChangedCallback;
function copyAndReplaceAll(srcPath, destPath, relativeDestDir, replacements, options) {
if (options === void 0) { options = defaultOptions; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _i, _a, absoluteSrcFilePath, filename, relativeDestPath;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_i = 0, _a = walk(srcPath);
_b.label = 1;
case 1:
if (!(_i < _a.length)) return [3 /*break*/, 4];
absoluteSrcFilePath = _a[_i];
filename = path_1.default.relative(srcPath, absoluteSrcFilePath);
relativeDestPath = path_1.default.join(relativeDestDir, filename);
// eslint-disable-next-line max-len
return [4 /*yield*/, copyAndReplaceWithChangedCallback(absoluteSrcFilePath, destPath, relativeDestPath, replacements, options)];
case 2:
// eslint-disable-next-line max-len
_b.sent();
_b.label = 3;
case 3:
_i++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
});
}
exports.copyAndReplaceAll = copyAndReplaceAll;
function alwaysOverwriteContentChangedCallback(absoluteSrcFilePath, relativeDestPath, contentChanged, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
if (contentChanged === 'new') {
logInfo("[new] ".concat(relativeDestPath));
return [2 /*return*/, 'overwrite'];
}
if (contentChanged === 'changed') {
if (options.logging) {
logInfo("[changed] ".concat(relativeDestPath, " [overwriting]"));
}
return [2 /*return*/, 'overwrite'];
}
if (contentChanged === 'identical') {
return [2 /*return*/, 'keep'];
}
throw new Error("Unknown file changed state: ".concat(relativeDestPath, ", ").concat(contentChanged));
});
});
}
exports.alwaysOverwriteContentChangedCallback = alwaysOverwriteContentChangedCallback;
function upgradeFileContentChangedCallback(absoluteSrcFilePath, relativeDestPath, contentChanged, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
if (contentChanged === 'new') {
if (options.logging) {
logInfo("[new] ".concat(relativeDestPath));
}
return [2 /*return*/, 'overwrite'];
}
if (contentChanged === 'changed') {
return [2 /*return*/, 'keep'];
}
if (contentChanged === 'identical') {
return [2 /*return*/, 'keep'];
}
throw new Error("Unknown file changed state: ".concat(relativeDestPath, ", ").concat(contentChanged));
});
});
}
exports.upgradeFileContentChangedCallback = upgradeFileContentChangedCallback;
//# sourceMappingURL=index.js.map