Skip to content

Commit 5e6b791

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Allow to opt-out certain modules from automatic type generation
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
1 parent 1257122 commit 5e6b791

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

scripts/build/build-types.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ const {parseArgs} = require('util');
2121
const TYPES_DIR = 'types_generated';
2222
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**';
2323

24+
// Files matching these patterns will be translated to TypeScript
2425
const SOURCE_PATTERNS = [
2526
// Start with Animated only
2627
path.join(PACKAGES_DIR, 'react-native/Libraries/Animated/**/*.js'),
2728
// TODO(T210505412): Include input packages, e.g. virtualized-lists
2829
];
2930

31+
// Files matching these patterns will not be translated to TypeScript, instead their explicit TypeScript definitions will be copied over
32+
const SUBPATH_OVERRIDES = {
33+
[path.join(PACKAGES_DIR, 'react-native/Libraries/Animated/**/*.js')]: [
34+
'react-native/Libraries/Animated/*.d.ts',
35+
],
36+
};
37+
3038
const config = {
3139
options: {
3240
help: {type: 'boolean'},
@@ -62,9 +70,15 @@ async function main() {
6270
'\n',
6371
);
6472

73+
const subpathsToOverride = Object.keys(SUBPATH_OVERRIDES);
74+
6575
await Promise.all(
6676
files.map(async file => {
67-
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
77+
// Ignore files that are explicitly excluded and those with the explicitly defined types
78+
if (
79+
micromatch.isMatch(file, IGNORE_PATTERN) ||
80+
subpathsToOverride.some(subpath => micromatch.isMatch(file, subpath))
81+
) {
6882
return;
6983
}
7084

@@ -91,6 +105,21 @@ async function main() {
91105
}
92106
}),
93107
);
108+
109+
const typeDefinitions = Object.values(SUBPATH_OVERRIDES)
110+
.flatMap(typePaths => typePaths)
111+
.flatMap(srcPath =>
112+
glob.sync(path.join(PACKAGES_DIR, srcPath), {nodir: true}),
113+
);
114+
115+
await Promise.all(
116+
typeDefinitions.map(async file => {
117+
const buildPath = getBuildPath(file);
118+
const source = await fs.readFile(file, 'utf-8');
119+
await fs.mkdir(path.dirname(buildPath), {recursive: true});
120+
await fs.writeFile(buildPath, source);
121+
}),
122+
);
94123
}
95124

96125
function getPackageName(file /*: string */) /*: string */ {

0 commit comments

Comments
 (0)