|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +const {REPO_ROOT} = require('../shared/consts'); |
| 12 | +const {spawnSync} = require('node:child_process'); |
| 13 | + |
| 14 | +function moduleExists(modulePath /*: string */) /*: boolean */ { |
| 15 | + try { |
| 16 | + require.resolve(modulePath); |
| 17 | + return true; |
| 18 | + } catch { |
| 19 | + return false; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Resolved before requiring so that a failure to load the module is not |
| 24 | +// mistaken for its absence in the open source repo. |
| 25 | +const resolveFb /*: ?(validate: boolean) => number */ = moduleExists( |
| 26 | + './build-android-api.fb.js', |
| 27 | +) |
| 28 | + ? // $FlowFixMe[cannot-resolve-module] - not resolvable in OSS |
| 29 | + require('./build-android-api.fb.js') |
| 30 | + : null; |
| 31 | + |
| 32 | +function runGradle(validate /*: boolean */) /*: number */ { |
| 33 | + const gradlew = process.platform === 'win32' ? 'gradlew.bat' : './gradlew'; |
| 34 | + const task = `:packages:react-native:ReactAndroid:${ |
| 35 | + validate ? 'apiCheck' : 'apiDump' |
| 36 | + }`; |
| 37 | + const {status, error} = spawnSync(gradlew, [task], { |
| 38 | + cwd: REPO_ROOT, |
| 39 | + stdio: 'inherit', |
| 40 | + shell: process.platform === 'win32', |
| 41 | + }); |
| 42 | + if (error != null) { |
| 43 | + console.error(`Failed to run ${gradlew}: ${error.message}`); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + return status ?? 1; |
| 47 | +} |
| 48 | + |
| 49 | +function main() /*: number */ { |
| 50 | + const args = process.argv.slice(2); |
| 51 | + if (args.includes('--help') || args.includes('-h')) { |
| 52 | + console.log(` |
| 53 | + Usage: node ./scripts/android-api/build-android-api.js [--validate] |
| 54 | +
|
| 55 | + Regenerate the React Native Android public API snapshot |
| 56 | + (packages/react-native/ReactAndroid/api/ReactAndroid.api). |
| 57 | +
|
| 58 | + Options: |
| 59 | + --validate Verify the committed snapshot is up to date instead of |
| 60 | + regenerating it. Exits non-zero when it differs. |
| 61 | + `); |
| 62 | + return 0; |
| 63 | + } |
| 64 | + const validate = args.includes('--validate'); |
| 65 | + |
| 66 | + // In a Meta-internal checkout the .fb.js backend (stripped from the open |
| 67 | + // source repo) generates the snapshot with Buck instead of Gradle. |
| 68 | + if (resolveFb != null) { |
| 69 | + return resolveFb(validate); |
| 70 | + } |
| 71 | + return runGradle(validate); |
| 72 | +} |
| 73 | + |
| 74 | +process.exitCode = main(); |
0 commit comments