Skip to content
Merged
Prev Previous commit
Make react-native.config.js be appended to existing file.
  • Loading branch information
tom-un committed Apr 23, 2020
commit 70a5ee3a2abc213366afabb45d0c98fe8e5d66e1
17 changes: 16 additions & 1 deletion local-cli/generator-common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ function upgradeFileContentChangedCallback(
);
}

/**
* @param {string} srcPath
* @param {string} relativeDestDir
* @param {Record<string, string>} replacements
*/
function appendToExistingFile(srcPath, relativeDestDir, replacements) {
walk(srcPath).forEach(absoluteSrcFilePath => {
const filename = path.relative(srcPath, absoluteSrcFilePath);
const relativeDestPath = path.join(relativeDestDir, replaceInPath(filename, replacements));

const templateFileContents = fs.readFileSync(absoluteSrcFilePath, { encoding: 'UTF8' });
fs.appendFileSync(relativeDestPath, templateFileContents);
});
}

module.exports = {
createDir, copyAndReplaceWithChangedCallback, copyAndReplaceAll,
appendToExistingFile, createDir, copyAndReplaceWithChangedCallback, copyAndReplaceAll,
};
4 changes: 4 additions & 0 deletions local-cli/generator-macos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const path = require('path');
const childProcess = require('child_process');
const fs = require('fs');
const {
appendToExistingFile,
createDir,
copyAndReplaceAll,
copyAndReplaceWithChangedCallback,
Expand Down Expand Up @@ -60,6 +61,9 @@ function copyProjectTemplateAndReplace(

[
{ from: path.join(srcRootPath, 'react-native.config.js'), to: 'react-native.config.js' },
].forEach((mapping) => appendToExistingFile(mapping.from, mapping.to, templateVars));

[
{ from: path.join(srcRootPath, 'metro.config.macos.js'), to: 'metro.config.macos.js' },
].forEach((mapping) => copyAndReplaceWithChangedCallback(mapping.from, destPath, mapping.to, templateVars, options.overwrite));

Expand Down
24 changes: 0 additions & 24 deletions local-cli/generator-macos/templates/react-native.config.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
/**
* This cli config is needed for the coexistance of react-native and other
* out-of-tree implementations such react-native-macos.
* The following issue is tracked by
* https://github.com/react-native-community/discussions-and-proposals/issues/182
*
* The work-around involves having a metro.config.js for each out-of-tree
* platform, i.e. metro.config.js for react-native and
* metro.config.macos.js for react-native-macos.
* This react-native.config.js looks for a --use-react-native-macos
* switch and when present pushes --config=metro.config.macos.js
* and specifies reactNativePath: 'node_modules/react-native-macos'.
* The metro.config.js has to blacklist 'node_modules/react-native-macos',
* and conversely metro.config.macos.js has to blacklist 'node_modules/react-native'.
*/
'use strict';

const macSwitch = '--use-react-native-macos';
const windowsSwitch = '--use-react-native-windows';

if (process.argv.includes(macSwitch)) {
process.argv = process.argv.filter(arg => arg !== macSwitch);
process.argv.push('--config=metro.config.macos.js');
module.exports = {
reactNativePath: 'node_modules/react-native-macos',
};
} else if (process.argv.includes(windowsSwitch)) {
process.argv = process.argv.filter(arg => arg !== windowsSwitch);
process.argv.push('--config=metro.config.windows.js');
module.exports = {
reactNativePath: 'node_modules/react-native-windows',
};
}