Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to opt-out certain modules from automatic type generation #48969

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions scripts/build/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, $ReadOnlyArray<string>>*/ = {
'**/react-native/Libraries/Animated/**/*.js': [
'react-native/Libraries/Animated/*.d.ts',
],
};

const config = {
options: {
help: {type: 'boolean'},
Expand All @@ -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,
}),
);
Expand All @@ -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;
}

Expand All @@ -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 */ {
Expand Down
Loading