Files
react-native/scripts/build/build-types.js
T
Alex HuntandFacebook GitHub Bot b8ee2b3503 Add source transformation pipeline to build-types (#48893)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48893

Updates `build-types` to support source file AST transforms, and templates out an initial `stripPrivateProperties` transform.

- Also, parallelise file translation via `Promise.all`.

Changelog: [Internal]

Reviewed By: iwoplaza

Differential Revision: D68558012

fbshipit-source-id: 6eb3881fcf30bf8f4ba045522f6569fbbad14f62
2025-01-23 08:29:09 -08:00

116 lines
2.8 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/
const {PACKAGES_DIR, REPO_ROOT} = require('../consts');
const translateSourceFile = require('./build-types/translateSourceFile');
const chalk = require('chalk');
const {promises: fs} = require('fs');
const glob = require('glob');
const micromatch = require('micromatch');
const path = require('path');
const {parseArgs} = require('util');
const TYPES_DIR = 'types_generated';
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**';
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
];
const config = {
options: {
help: {type: 'boolean'},
},
};
async function main() {
const {
values: {help},
} = parseArgs(config);
if (help) {
console.log(`
Usage: node ./scripts/build/build-types.js
[Experimental] Build generated TypeScript types for react-native.
`);
process.exitCode = 0;
return;
}
const files = SOURCE_PATTERNS.flatMap(srcPath =>
glob.sync(path.join(srcPath, ''), {
nodir: true,
}),
);
console.log(
'\n' +
chalk.bold.inverse.yellow(
'EXPERIMENTAL - Building generated react-native package types',
) +
'\n',
);
await Promise.all(
files.map(async file => {
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
return;
}
const buildPath = getBuildPath(file);
const source = await fs.readFile(file, 'utf-8');
await fs.mkdir(path.dirname(buildPath), {recursive: true});
try {
const typescriptDef = await translateSourceFile(source);
if (
/Unsupported feature: Translating ".*" is currently not supported/.test(
typescriptDef,
)
) {
throw new Error(
'Syntax unsupported by flow-api-translator used in ' + file,
);
}
await fs.writeFile(buildPath, typescriptDef);
} catch (e) {
console.error(`Failed to build ${path.relative(REPO_ROOT, file)}`);
}
}),
);
}
function getPackageName(file /*: string */) /*: string */ {
return path.relative(PACKAGES_DIR, file).split(path.sep)[0];
}
function getBuildPath(file /*: string */) /*: string */ {
const packageDir = path.join(PACKAGES_DIR, getPackageName(file));
return path.join(
packageDir,
file
.replace(packageDir, TYPES_DIR)
.replace(/\.flow\.js$/, '.js')
.replace(/\.js$/, '.d.ts'),
);
}
if (require.main === module) {
// eslint-disable-next-line no-void
void main();
}