From d32ca8e899bfbb0a76aac89a48abe8bb88de1dfc Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 3 Apr 2025 08:31:23 -0700 Subject: [PATCH] Move build-types config into own module (#50450) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50450 Intends to be reused by the incoming `buildApiSnapshot` behaviour. Changelog: [Internal] Reviewed By: iwoplaza Differential Revision: D72306509 fbshipit-source-id: 9e03c95f2469e10c05bdd0be39d4e217a6cc197f --- scripts/build-types/buildGeneratedTypes.js | 18 +++-------- scripts/build-types/config.js | 37 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 scripts/build-types/config.js diff --git a/scripts/build-types/buildGeneratedTypes.js b/scripts/build-types/buildGeneratedTypes.js index 97bb597c627..c595f8d529f 100644 --- a/scripts/build-types/buildGeneratedTypes.js +++ b/scripts/build-types/buildGeneratedTypes.js @@ -10,6 +10,7 @@ */ const {PACKAGES_DIR, REPO_ROOT} = require('../consts'); +const {ENTRY_POINT, IGNORE_PATTERNS, TYPES_OUTPUT_DIR} = require('./config'); const getRequireStack = require('./resolution/getRequireStack'); const translatedModuleTemplate = require('./templates/translatedModule.d.ts-template'); const translateSourceFile = require('./translateSourceFile'); @@ -17,15 +18,6 @@ const {promises: fs} = require('fs'); const micromatch = require('micromatch'); const path = require('path'); -const OUTPUT_DIR = 'types_generated'; - -const IGNORE_PATTERNS = [ - '**/__{tests,mocks,fixtures,flowtests}__/**', - '**/*.{macos,windows}.js', -]; - -const ENTRY_POINTS = ['packages/react-native/index.js.flow']; - /** * Build generated TypeScript types for react-native. * @@ -37,9 +29,7 @@ const ENTRY_POINTS = ['packages/react-native/index.js.flow']; * along with our own pre and post-processing. */ async function buildGeneratedTypes(): Promise { - const files = new Set( - ENTRY_POINTS.map(file => path.join(REPO_ROOT, file)), - ); + const files = new Set([path.join(REPO_ROOT, ENTRY_POINT)]); const translatedFiles = new Set(); const dependencyEdges: DependencyEdges = []; const allErrors: Array = []; @@ -67,7 +57,7 @@ async function buildGeneratedTypes(): Promise { await fs.copyFile( path.join(__dirname, 'templates/tsconfig.json'), - path.join(PACKAGES_DIR, 'react-native', OUTPUT_DIR, 'tsconfig.json'), + path.join(PACKAGES_DIR, 'react-native', TYPES_OUTPUT_DIR, 'tsconfig.json'), ); if (allErrors.length > 0) { @@ -133,7 +123,7 @@ function getBuildPath(file: string): string { return path.join( packageDir, file - .replace(packageDir, OUTPUT_DIR) + .replace(packageDir, TYPES_OUTPUT_DIR) .replace(/\.js\.flow$/, '.js') .replace(/\.js$/, '.d.ts'), ); diff --git a/scripts/build-types/config.js b/scripts/build-types/config.js new file mode 100644 index 00000000000..208dd798729 --- /dev/null +++ b/scripts/build-types/config.js @@ -0,0 +1,37 @@ +/** + * 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 + * @format + * @oncall react_native + */ + +/** + * The root entry point for type generation (main entry point for the + * react-native package). + */ +const ENTRY_POINT = 'packages/react-native/index.js.flow'; + +/** + * Ignore patterns for source files that should not be considered for + * translation. + */ +const IGNORE_PATTERNS = [ + '**/__{tests,mocks,fixtures,flowtests}__/**', + '**/*.{macos,windows}.js', +]; + +/** + * The output directory name for generated type definitions in each translated + * package. + */ +const TYPES_OUTPUT_DIR = 'types_generated'; + +module.exports = { + ENTRY_POINT, + IGNORE_PATTERNS, + TYPES_OUTPUT_DIR, +};