From 358791cd6c79e320725fb485c270137fd5206cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 13 Dec 2023 10:09:00 +0100 Subject: [PATCH] fix: remove automatic config file generation --- __e2e__/__snapshots__/config.test.ts.snap | 3 +- __e2e__/config.test.ts | 7 +-- __e2e__/init.test.ts | 39 ------------- packages/cli/src/commands/init/init.ts | 68 ----------------------- 4 files changed, 2 insertions(+), 115 deletions(-) diff --git a/__e2e__/__snapshots__/config.test.ts.snap b/__e2e__/__snapshots__/config.test.ts.snap index 0b074bbbb..4d0436496 100644 --- a/__e2e__/__snapshots__/config.test.ts.snap +++ b/__e2e__/__snapshots__/config.test.ts.snap @@ -81,8 +81,7 @@ exports[`shows up current config without unnecessary output 1`] = ` }, "project": { "ios": { - "sourceDir": "<>/TestProject/ios", - "automaticPodsInstallation": true + "sourceDir": "<>/TestProject/ios" }, "android": { "sourceDir": "<>/TestProject/android", diff --git a/__e2e__/config.test.ts b/__e2e__/config.test.ts index cd2c5560c..1a5cd8681 100644 --- a/__e2e__/config.test.ts +++ b/__e2e__/config.test.ts @@ -52,17 +52,12 @@ beforeAll(() => { writeFiles(DIR, {}); // Initialise React Native project - runCLI(DIR, ['init', 'TestProject']); + runCLI(DIR, ['init', 'TestProject', '--install-pods']); // Link CLI to the project spawnScript('yarn', ['link', ...addRNCPrefix(packages)], { cwd: path.join(DIR, 'TestProject'), }); - - // Install pods after linking packages because Podfile uses `use_native_modules` function that executes `config` command. In case there was introduce breaking change in `cli-config` package, it will fail since it will be using old version of the package. - spawnScript('pod', ['install'], { - cwd: path.join(DIR, 'TestProject', 'ios'), - }); }); afterAll(() => { diff --git a/__e2e__/init.test.ts b/__e2e__/init.test.ts index 9804c32ed..3ce9f77bf 100644 --- a/__e2e__/init.test.ts +++ b/__e2e__/init.test.ts @@ -26,7 +26,6 @@ const customTemplateCopiedFiles = [ 'file', 'node_modules', 'package.json', - 'react-native.config.js', 'yarn.lock', ]; @@ -194,41 +193,3 @@ test('init --platform-name should work for out of tree platform', () => { expect(dirFiles.length).toBeGreaterThan(0); }); - -test('should not create custom config file if installed version is below 0.73', () => { - createCustomTemplateFiles(); - - runCLI(DIR, ['init', PROJECT_NAME, '--skip-install', '--version', '0.72.0']); - - let dirFiles = fs.readdirSync(path.join(DIR, PROJECT_NAME)); - - expect(dirFiles).not.toContain('react-native.config.js'); -}); - -test('should create custom config file if installed version is latest (starting from 0.73)', () => { - createCustomTemplateFiles(); - - runCLI(DIR, ['init', PROJECT_NAME, '--skip-install']); - - let dirFiles = fs.readdirSync(path.join(DIR, PROJECT_NAME)); - - expect(dirFiles).toContain('react-native.config.js'); - const fileContent = fs.readFileSync( - path.join(DIR, PROJECT_NAME, 'react-native.config.js'), - 'utf8', - ); - - const configFileContent = ` - module.exports = { - project: { - ios: { - automaticPodsInstallation: true - } - } - }`; - - //normalize all white-spaces for easier comparision - expect(fileContent.replace(/\s+/g, '')).toEqual( - configFileContent.replace(/\s+/g, ''), - ); -}); diff --git a/packages/cli/src/commands/init/init.ts b/packages/cli/src/commands/init/init.ts index 46c81a3d2..ee68fbf0a 100644 --- a/packages/cli/src/commands/init/init.ts +++ b/packages/cli/src/commands/init/init.ts @@ -29,8 +29,6 @@ import {getNpmVersionIfAvailable} from '../../tools/npm'; import {getYarnVersionIfAvailable} from '../../tools/yarn'; import {createHash} from 'crypto'; import createGitRepository from './createGitRepository'; -import deepmerge from 'deepmerge'; -import semver from 'semver'; const DEFAULT_VERSION = 'latest'; @@ -114,7 +112,6 @@ async function createFromTemplate({ skipInstall, packageName, installCocoaPods, - version, }: TemplateOptions) { logger.debug('Initializing new project'); logger.log(banner); @@ -178,15 +175,6 @@ async function createFromTemplate({ packageName, }); - const coerceRnVersion = semver.valid(semver.coerce(version)); - - if ( - version === 'latest' || - (coerceRnVersion && semver.satisfies(coerceRnVersion, '>=0.73.0')) - ) { - createDefaultConfigFile(projectDirectory, loader); - } - const {postInitScript} = templateConfig; if (postInitScript) { loader.info('Executing post init script '); @@ -289,62 +277,6 @@ function createTemplateUri(options: Options, version: string): string { return options.template || `${platform}@${version}`; } -//remove quotes from object keys to match the linter rules of the template -function sanitizeConfigFile(fileContent: string) { - return fileContent.replace(/"([^"]+)":/g, '$1:'); -} - -/* -Starting from 0.73, react-native.config.js is created by CLI during the init process. -It contains automaticPodsInstallation flag set to true by default. -This flag is used by CLI to determine whether to install CocoaPods dependencies when running ios commands or not. -It's created by CLI rather than being a part of a template to avoid displaying this file in the Upgrade Helper, -as it might bring confusion for existing projects where this change might not be applicable. -For more details, see https://github.com/react-native-community/cli/blob/main/docs/projects.md#projectiosautomaticpodsinstallation -*/ -function createDefaultConfigFile(directory: string, loader: Loader) { - const cliConfigContent = { - project: { - ios: { - automaticPodsInstallation: true, - }, - }, - }; - const configFileContent = `module.exports = ${JSON.stringify( - cliConfigContent, - null, - 2, - )}`; - const filepath = 'react-native.config.js'; - try { - if (!doesDirectoryExist(path.join(directory, filepath))) { - fs.writeFileSync(filepath, sanitizeConfigFile(configFileContent), { - encoding: 'utf-8', - }); - } else { - const existingConfigFile = require(path.join(directory, filepath)); - - const mergedConfig = deepmerge(existingConfigFile, cliConfigContent); - const output = `module.exports = ${JSON.stringify( - mergedConfig, - null, - 2, - )};`; - - fs.writeFileSync(filepath, sanitizeConfigFile(output), { - encoding: 'utf-8', - }); - } - loader.succeed(); - } catch { - loader.warn( - `Could not create custom ${chalk.bold( - 'react-native.config.js', - )} file. You can create it manually in your project's root folder with the following content: \n\n${configFileContent}`, - ); - } -} - async function createProject( projectName: string, directory: string,