From 9ab813cd5672860b1f44e738051d7ec204665a6c Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Mon, 27 Jan 2025 06:23:58 -0800 Subject: [PATCH] Allow to opt-out certain modules from automatic type generation (#48969) Summary: Changelog: [Internal] Allow to opt-out certain modules from the type translation and use manually prepared types instead. The override can contain either an array of files to use for a given glob, or another glob to match files that should be copied. Differential Revision: D68707899 --- scripts/build/build-types.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/scripts/build/build-types.js b/scripts/build/build-types.js index 4ef8dc39eab998..f7619d31b5fa2a 100644 --- a/scripts/build/build-types.js +++ b/scripts/build/build-types.js @@ -21,12 +21,21 @@ const {parseArgs} = require('util'); const TYPES_DIR = 'types_generated'; const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**'; +// Files matching these patterns will be translated to TypeScript const SOURCE_PATTERNS = [ // Start with Animated only - path.join(PACKAGES_DIR, 'react-native/Libraries/Animated/**/*.js'), + 'react-native/Libraries/Animated/**/*.js', // TODO(T210505412): Include input packages, e.g. virtualized-lists ]; +// Files matching these patterns will not be translated to TypeScript, +// instead their explicit TypeScript definitions will be copied over +const SUBPATH_OVERRIDES /*: Record>*/ = { + '**/react-native/Libraries/Animated/**/*.js': [ + 'react-native/Libraries/Animated/*.d.ts', + ], +}; + const config = { options: { help: {type: 'boolean'}, @@ -49,7 +58,7 @@ async function main() { } const files = SOURCE_PATTERNS.flatMap(srcPath => - glob.sync(path.join(srcPath, ''), { + glob.sync(path.join(PACKAGES_DIR, srcPath), { nodir: true, }), ); @@ -62,9 +71,15 @@ async function main() { '\n', ); + const subpathsToOverride = Object.keys(SUBPATH_OVERRIDES); + await Promise.all( files.map(async file => { - if (micromatch.isMatch(file, IGNORE_PATTERN)) { + // Ignore files that are explicitly excluded and those with the explicitly defined types + if ( + micromatch.isMatch(file, IGNORE_PATTERN) || + subpathsToOverride.some(subpath => micromatch.isMatch(file, subpath)) + ) { return; } @@ -91,6 +106,21 @@ async function main() { } }), ); + + const typeDefinitions = Object.values(SUBPATH_OVERRIDES) + .flatMap(typePaths => typePaths) + .flatMap(srcPath => + glob.sync(path.join(PACKAGES_DIR, srcPath), {nodir: true}), + ); + + await Promise.all( + typeDefinitions.map(async file => { + const buildPath = getBuildPath(file); + const source = await fs.readFile(file, 'utf-8'); + await fs.mkdir(path.dirname(buildPath), {recursive: true}); + await fs.writeFile(buildPath, source); + }), + ); } function getPackageName(file /*: string */) /*: string */ {