react-native/scripts/releases/remove-new-arch-flags.js
Alex Hunt 76598de621 Reorganise and document release script entry points (#42774)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42774

Reorganise release scripts so that command entry points are grouped based on execution context, which also reflects dependencies between scripts.

Also:

- Document the current behaviours of these scripts.
- Relocate utils out of the root contents.
- Replace `exec` call to `set-rn-version` script with function import.

NOTE: `yarn trigger-react-native-release` (documented command in release process) is unchanged, since this is aliased from `package.json`.

```
├── releases
│   ├── templates/
│   ├── utils/
│   ├── remove-new-arch-flags.js
│   ├── set-rn-version.js
│   └── update-template-package.js
├── releases-ci
│   ├── prepare-package-for-release.js
│   └── publish-npm.js
└── releases-local
    └── trigger-react-native-release.js
```

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D53274341

fbshipit-source-id: eec2befc43e7a47fd821b2e2bcc818ddffbb6cf7
2024-02-01 06:02:17 -08:00

111 lines
2.6 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react-native
*/
'use strict';
const fs = require('fs');
const path = require('path');
function removeNewArchFlags() {
console.log('Removing new arch flags');
const cwd = getValidCwd();
const iosBackups = removeFlagsForiOS(cwd);
const androidBackups = flipNewArchFlagForAndroid(cwd);
iosBackups.concat(androidBackups).forEach(file => {
fs.unlinkSync(file);
});
}
// === Helpers ===
function getValidCwd() /*: string*/ {
const cwd = process.cwd();
if (!cwd.endsWith('react-native-github')) {
throw new Error(
`Please call this script from react-native root folder. Current path: ${cwd}`,
);
}
return cwd;
}
function replaceContentsOfFile(
contentToBeReplaced /*: string | RegExp*/,
replacement /*: string*/,
filepath /*: string*/,
) /*: string*/ {
const content = fs.readFileSync(filepath, 'utf8');
const backupPath = `${filepath}.bak`;
fs.writeFileSync(backupPath, content, 'utf8');
let newContent = content.replaceAll(contentToBeReplaced, replacement);
fs.writeFileSync(filepath, newContent, 'utf8');
return backupPath;
}
function removeContentsFromFile(
contentToBeRemoved /*: string | RegExp*/,
filepath /*: string*/,
) /*: string*/ {
return replaceContentsOfFile(contentToBeRemoved, '', filepath);
}
function removeFlagsForiOS(cwd /*: string*/) /*: $ReadOnlyArray<string>*/ {
let backupPath /*: Array<string>*/ = [];
const iosPath = path.join(
cwd,
'/packages/react-native/scripts/react_native_pods.rb',
);
backupPath.push(
removeContentsFromFile(
/ {2}fabric_enabled: false,\n {2}new_arch_enabled: NewArchitectureHelper.new_arch_enabled,\n/g,
iosPath,
),
);
return backupPath;
}
function newArchEnabledGradleProps(boolValue /*: boolean*/) /*: string */ {
return `newArchEnabled=${boolValue.toString()}`;
}
function flipNewArchFlagForAndroid(
cwd /*: string */,
) /*: $ReadOnlyArray<string>*/ {
let backupPath /*: Array<string> */ = [];
// Gradle.properties
const gradlePropertiesPath = path.join(
cwd,
'/packages/react-native/template/android/gradle.properties',
);
backupPath.push(
replaceContentsOfFile(
new RegExp(newArchEnabledGradleProps(false), 'g'),
newArchEnabledGradleProps(true),
gradlePropertiesPath,
),
);
return backupPath;
}
// ===============
module.exports = {
removeNewArchFlags,
};
if (require.main === module) {
removeNewArchFlags();
}