Remove hard-coded SampleTurboModule exclude (#41574)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41574

The codegen scripts hard-code to exclude NativeSampleTurboModule.

This diff modifies the codegen infra (i.e: combine-js-to-schema-cli.js) to accept an argument: --exclude <regex>.

Auxiliary changes:
- Refactor argument parsing to rely on node's util.parseArgs

Changelog: [Internal]

Reviewed By: javache, dmytrorykun

Differential Revision: D51471526

fbshipit-source-id: 98b88058f8c4b6fa4d776d96a1eb0f15144906a8
This commit is contained in:
Ramanpreet Nara
2023-11-22 13:52:51 -08:00
committed by Facebook GitHub Bot
parent 3ee4b8f5a1
commit 4e92016a09
8 changed files with 69 additions and 23 deletions
@@ -103,9 +103,19 @@ describe('filterJSFile', () => {
});
describe('When the file is NativeSampleTurboModule', () => {
it('returns false', () => {
it('returns true', () => {
const file = 'NativeSampleTurboModule.js';
const result = filterJSFile(file);
expect(result).toBeTruthy();
});
it('returns false, when excluded', () => {
const file = 'NativeSampleTurboModule.js';
const result = filterJSFile(
file,
null,
new RegExp('NativeSampleTurboModule'),
);
expect(result).toBeFalsy();
});
});
@@ -16,6 +16,8 @@ const {
} = require('./combine-js-to-schema');
const {parseArgs} = require('./combine-utils');
const {platform, outfile, fileList} = parseArgs(process.argv);
const parsedArgs = parseArgs(process.argv);
combineSchemasInFileListAndWriteToFile(fileList, platform, outfile);
const {platform, outfile, fileList, exclude} = parsedArgs;
combineSchemasInFileListAndWriteToFile(fileList, platform, outfile, exclude);
@@ -51,6 +51,7 @@ function combineSchemas(files: Array<string>): SchemaType {
function expandDirectoriesIntoFiles(
fileList: Array<string>,
platform: ?string,
exclude: ?RegExp,
): Array<string> {
return fileList
.flatMap(file => {
@@ -66,14 +67,19 @@ function expandDirectoriesIntoFiles(
// windowsPathsNoEscape: true,
});
})
.filter(element => filterJSFile(element, platform));
.filter(element => filterJSFile(element, platform, exclude));
}
function combineSchemasInFileList(
fileList: Array<string>,
platform: ?string,
exclude: ?RegExp,
): SchemaType {
const expandedFileList = expandDirectoriesIntoFiles(fileList, platform);
const expandedFileList = expandDirectoriesIntoFiles(
fileList,
platform,
exclude,
);
const combined = combineSchemas(expandedFileList);
if (Object.keys(combined.modules).length === 0) {
console.error(
@@ -87,8 +93,9 @@ function combineSchemasInFileListAndWriteToFile(
fileList: Array<string>,
platform: ?string,
outfile: string,
exclude: ?RegExp,
): void {
const combined = combineSchemasInFileList(fileList, platform);
const combined = combineSchemasInFileList(fileList, platform, exclude);
const formattedSchema = JSON.stringify(combined, null, 2);
fs.writeFileSync(outfile, formattedSchema);
}
@@ -12,26 +12,41 @@
'use strict';
const path = require('path');
const util = require('util');
function parseArgs(args: string[]): {
platform: ?string,
outfile: string,
fileList: string[],
exclude: ?RegExp,
} {
if (args.length > 2 && ['-p', '--platform'].indexOf(args[2]) >= 0) {
const [outfile, ...fileList] = args.slice(4);
return {
platform: args[3],
outfile,
fileList,
};
}
const parsedArgs = util.parseArgs({
args: args.slice(2),
options: {
platform: {
short: 'p',
type: 'string',
},
exclude: {
short: 'e',
type: 'string',
},
},
allowPositionals: true,
});
const {
values: {platform, exclude},
positionals: files,
} = parsedArgs;
const [outfile, ...fileList] = files;
const [outfile, ...fileList] = args.slice(2);
return {
platform: null,
platform: platform ?? null,
outfile,
fileList,
exclude: exclude != null && exclude !== '' ? new RegExp(exclude) : null,
};
}
@@ -43,19 +58,21 @@ function parseArgs(args: string[]): {
* Returns: `true` if the file can be used to generate some code; `false` otherwise
*
*/
function filterJSFile(file: string, currentPlatform: ?string): boolean {
function filterJSFile(
file: string,
currentPlatform: ?string,
excludeRegExp: ?RegExp,
): boolean {
const isSpecFile = /^(Native.+|.+NativeComponent)/.test(path.basename(file));
const isNotNativeUIManager = !file.endsWith('NativeUIManager.js');
const isNotNativeSampleTurboModule = !file.endsWith(
'NativeSampleTurboModule.js',
);
const isNotTest = !file.includes('__tests');
const isNotExcluded = excludeRegExp == null || !excludeRegExp.test(file);
const isNotTSTypeDefinition = !file.endsWith('.d.ts');
const isValidCandidate =
isSpecFile &&
isNotNativeUIManager &&
isNotNativeSampleTurboModule &&
isNotExcluded &&
isNotTest &&
isNotTSTypeDefinition;
@@ -67,6 +67,8 @@ abstract class GenerateCodegenSchemaTask : Exec() {
.cliPath(workingDir),
"--platform",
"android",
"--exclude",
"NativeSampleTurboModule",
generatedSchemaFile.get().asFile.cliPath(workingDir),
jsRootDir.asFile.get().cliPath(workingDir),
))
@@ -145,6 +145,8 @@ class GenerateCodegenSchemaTaskTest {
File(codegenDir, "lib/cli/combine/combine-js-to-schema-cli.js").toString(),
"--platform",
"android",
"--exclude",
"NativeSampleTurboModule",
File(outputDir, "schema.json").toString(),
jsRootDir.toString(),
),
@@ -180,6 +182,8 @@ class GenerateCodegenSchemaTaskTest {
.path,
"--platform",
"android",
"--exclude",
"NativeSampleTurboModule",
File(outputDir, "schema.json").relativeTo(project.projectDir).path,
jsRootDir.relativeTo(project.projectDir).path,
),
@@ -267,7 +267,11 @@ function generateSchemaInfo(library) {
library: library,
schema: utils
.getCombineJSToSchema()
.combineSchemasInFileList([pathToJavaScriptSources], 'ios'),
.combineSchemasInFileList(
[pathToJavaScriptSources],
'ios',
/NativeSampleTurboModule/,
),
};
}
@@ -76,7 +76,7 @@ generateCodegenSchemaFromJavaScript () {
# shellcheck disable=SC2086
# $JS_SRCS not having double quotations is intentional
"$NODE_BINARY" "$CODEGEN_CLI_PATH/lib/cli/combine/combine-js-to-schema-cli.js" "$GENERATED_SCHEMA_FILE" $JS_SRCS
"$NODE_BINARY" "$CODEGEN_CLI_PATH/lib/cli/combine/combine-js-to-schema-cli.js" --exclude NativeSampleTurboModule "$GENERATED_SCHEMA_FILE" $JS_SRCS
}
generateCodegenArtifactsFromSchema () {