Typing scripts used for test-e2e-local (#42610)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42610

Changelog: [Internal] Adding more typing to internal scripts used by test-e2e-local

Reviewed By: NickGerleman

Differential Revision: D52988733

fbshipit-source-id: 6e187b7c9560b30def014e8932ab82ed07cf8488
This commit is contained in:
Luna Wei
2024-01-24 15:39:06 -08:00
committed by Facebook GitHub Bot
parent 3d097d46e3
commit f322dc7a84
6 changed files with 134 additions and 71 deletions
+40 -26
View File
@@ -4,6 +4,7 @@
* 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
*/
@@ -19,11 +20,16 @@ const {
generateiOSArtifacts,
} = require('./release-utils');
const fs = require('fs');
// $FlowIgnore[cannot-resolve-module]
const {spawn} = require('node:child_process');
const os = require('os');
const path = require('path');
const {cp, exec} = require('shelljs');
/*::
type BuildType = 'dry-run' | 'release' | 'nightly' | 'prealpha';
*/
/*
* Android related utils - leverages android tooling
*/
@@ -32,24 +38,29 @@ const {cp, exec} = require('shelljs');
// here's the reference folder:
// https://github.com/react-native-community/cli/blob/main/packages/cli-platform-android/src/commands/runAndroid
const emulatorCommand = process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/emulator/emulator`
: 'emulator';
const emulatorCommand =
process.env.ANDROID_HOME != null
? `${process.env.ANDROID_HOME}/emulator/emulator`
: 'emulator';
const getEmulators = () => {
const emulatorsOutput = exec(`${emulatorCommand} -list-avds`).stdout;
return emulatorsOutput.split(os.EOL).filter(name => name !== '');
};
const launchEmulator = emulatorName => {
const launchEmulator = (emulatorName /*: string */) => {
// we need both options 'cause reasons:
// from docs: "When using the detached option to start a long-running process, the process will not stay running in the background after the parent exits unless it is provided with a stdio configuration that is not connected to the parent. If the parent's stdio is inherited, the child will remain attached to the controlling terminal."
// here: https://nodejs.org/api/child_process.html#optionsdetached
const child_process = spawn(emulatorCommand, [`@${emulatorName}`], {
detached: true,
stdio: 'ignore',
});
const child_process /*: child_process$ChildProcess */ = spawn(
emulatorCommand,
[`@${emulatorName}`],
{
detached: true,
stdio: 'ignore',
},
);
child_process.unref();
};
@@ -107,8 +118,8 @@ function maybeLaunchAndroidEmulator() {
// inspired by CLI again https://github.com/react-native-community/cli/blob/main/packages/cli-tools/src/isPackagerRunning.ts
function isPackagerRunning(
packagerPort = process.env.RCT_METRO_PORT || '8081',
) {
packagerPort /*: string */ = process.env.RCT_METRO_PORT ?? '8081',
) /*: 'running' | 'unrecognized' | 'not_running' */ {
try {
const status = exec(`curl http://localhost:${packagerPort}/status`, {
silent: true,
@@ -121,7 +132,7 @@ function isPackagerRunning(
}
// this is a very limited implementation of how this should work
function launchPackagerInSeparateWindow(folderPath) {
function launchPackagerInSeparateWindow(folderPath /*: string */) {
const command = `tell application "Terminal" to do script "cd ${folderPath} && yarn start"`;
exec(`osascript -e '${command}' >/dev/null <<EOF`);
}
@@ -146,8 +157,11 @@ function checkPackagerRunning() {
* - @circleciToken a valid CircleCI Token.
* - @branchName the branch of the name we want to use to fetch the artifacts.
*/
async function setupCircleCIArtifacts(circleciToken, branchName) {
if (!circleciToken) {
async function setupCircleCIArtifacts(
circleciToken /*: ?string */,
branchName /*: string */,
) /*: Promise<?typeof circleCIArtifactsUtils> */ {
if (circleciToken == null) {
return null;
}
@@ -161,9 +175,9 @@ async function setupCircleCIArtifacts(circleciToken, branchName) {
}
async function downloadArtifactsFromCircleCI(
circleCIArtifacts,
mavenLocalPath,
localNodeTGZPath,
circleCIArtifacts /*: typeof circleCIArtifactsUtils */,
mavenLocalPath /*: string */,
localNodeTGZPath /*: string */,
) {
const mavenLocalURL = await circleCIArtifacts.artifactURLForMavenLocal();
const hermesURL = await circleCIArtifacts.artifactURLHermesDebug();
@@ -187,9 +201,9 @@ async function downloadArtifactsFromCircleCI(
}
function buildArtifactsLocally(
releaseVersion,
buildType,
reactNativePackagePath,
releaseVersion /*: string */,
buildType /*: BuildType */,
reactNativePackagePath /*: string */,
) {
// this is needed to generate the Android artifacts correctly
const exitCode = exec(
@@ -264,13 +278,13 @@ function buildArtifactsLocally(
* - @hermesPath the path to hermes for iOS
*/
async function prepareArtifacts(
circleCIArtifacts,
mavenLocalPath,
localNodeTGZPath,
releaseVersion,
buildType,
reactNativePackagePath,
) {
circleCIArtifacts /*: ?typeof circleCIArtifactsUtils */,
mavenLocalPath /*: string */,
localNodeTGZPath /*: string */,
releaseVersion /*: string */,
buildType /*: BuildType */,
reactNativePackagePath /*: string */,
) /*: Promise<string> */ {
return circleCIArtifacts != null
? await downloadArtifactsFromCircleCI(
circleCIArtifacts,