diff --git a/local-cli/generator-common/index.js b/local-cli/generator-common/index.js index 6c2111efcb2c66..16b45ec95469a0 100644 --- a/local-cli/generator-common/index.js +++ b/local-cli/generator-common/index.js @@ -125,6 +125,21 @@ function upgradeFileContentChangedCallback( ); } +/** + * @param {string} srcPath + * @param {string} relativeDestDir + * @param {Record} 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, }; diff --git a/local-cli/generator-macos/index.js b/local-cli/generator-macos/index.js index b12538c1b5dfcb..da8dd2e4d16e1d 100644 --- a/local-cli/generator-macos/index.js +++ b/local-cli/generator-macos/index.js @@ -7,6 +7,7 @@ const path = require('path'); const childProcess = require('child_process'); const fs = require('fs'); const { + appendToExistingFile, createDir, copyAndReplaceAll, copyAndReplaceWithChangedCallback, @@ -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)); diff --git a/local-cli/generator-macos/templates/react-native.config.js b/local-cli/generator-macos/templates/react-native.config.js index 6c6da53c7c7925..449319f827f106 100644 --- a/local-cli/generator-macos/templates/react-native.config.js +++ b/local-cli/generator-macos/templates/react-native.config.js @@ -1,22 +1,4 @@ -/** - * 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); @@ -24,10 +6,4 @@ if (process.argv.includes(macSwitch)) { 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', - }; }