Skip to content

Commit

Permalink
fix: remove automatic config file generation (#2203)
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski authored Dec 13, 2023
1 parent 772880f commit 6b8607c
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 115 deletions.
3 changes: 1 addition & 2 deletions __e2e__/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ exports[`shows up current config without unnecessary output 1`] = `
},
"project": {
"ios": {
"sourceDir": "<<REPLACED_ROOT>>/TestProject/ios",
"automaticPodsInstallation": true
"sourceDir": "<<REPLACED_ROOT>>/TestProject/ios"
},
"android": {
"sourceDir": "<<REPLACED_ROOT>>/TestProject/android",
Expand Down
7 changes: 1 addition & 6 deletions __e2e__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
39 changes: 0 additions & 39 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const customTemplateCopiedFiles = [
'file',
'node_modules',
'package.json',
'react-native.config.js',
'yarn.lock',
];

Expand Down Expand Up @@ -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, ''),
);
});
68 changes: 0 additions & 68 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -114,7 +112,6 @@ async function createFromTemplate({
skipInstall,
packageName,
installCocoaPods,
version,
}: TemplateOptions) {
logger.debug('Initializing new project');
logger.log(banner);
Expand Down Expand Up @@ -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 ');
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6b8607c

Please sign in to comment.