Skip to content

Commit

Permalink
Allow to opt-out certain modules from automatic type generation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
j-piasecki authored and facebook-github-bot committed Jan 27, 2025
1 parent 1257122 commit 5e6b791
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion scripts/build/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ 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'),
// 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 = {
[path.join(PACKAGES_DIR, 'react-native/Libraries/Animated/**/*.js')]: [
'react-native/Libraries/Animated/*.d.ts',
],
};

const config = {
options: {
help: {type: 'boolean'},
Expand Down Expand Up @@ -62,9 +70,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;
}

Expand All @@ -91,6 +105,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 */ {
Expand Down

0 comments on commit 5e6b791

Please sign in to comment.