Skip to content

Commit 9ab813c

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
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
1 parent 46e86dc commit 9ab813c

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

scripts/build/build-types.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ 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
26-
path.join(PACKAGES_DIR, 'react-native/Libraries/Animated/**/*.js'),
27+
'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,
32+
// instead their explicit TypeScript definitions will be copied over
33+
const SUBPATH_OVERRIDES /*: Record<string, $ReadOnlyArray<string>>*/ = {
34+
'**/react-native/Libraries/Animated/**/*.js': [
35+
'react-native/Libraries/Animated/*.d.ts',
36+
],
37+
};
38+
3039
const config = {
3140
options: {
3241
help: {type: 'boolean'},
@@ -49,7 +58,7 @@ async function main() {
4958
}
5059

5160
const files = SOURCE_PATTERNS.flatMap(srcPath =>
52-
glob.sync(path.join(srcPath, ''), {
61+
glob.sync(path.join(PACKAGES_DIR, srcPath), {
5362
nodir: true,
5463
}),
5564
);
@@ -62,9 +71,15 @@ async function main() {
6271
'\n',
6372
);
6473

74+
const subpathsToOverride = Object.keys(SUBPATH_OVERRIDES);
75+
6576
await Promise.all(
6677
files.map(async file => {
67-
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
78+
// Ignore files that are explicitly excluded and those with the explicitly defined types
79+
if (
80+
micromatch.isMatch(file, IGNORE_PATTERN) ||
81+
subpathsToOverride.some(subpath => micromatch.isMatch(file, subpath))
82+
) {
6883
return;
6984
}
7085

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

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

0 commit comments

Comments
 (0)