diff --git a/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js b/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js index 08b75933d86..117dcb0f596 100644 --- a/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js +++ b/packages/react-native-codegen/src/cli/combine/__tests__/combine-utils-test.js @@ -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(); }); }); diff --git a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js index fbcc50be7c7..790b47d4fcf 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js +++ b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js @@ -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); diff --git a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js index 01af2f8b29b..108edf99d81 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js +++ b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js @@ -51,6 +51,7 @@ function combineSchemas(files: Array): SchemaType { function expandDirectoriesIntoFiles( fileList: Array, platform: ?string, + exclude: ?RegExp, ): Array { 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, 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, 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); } diff --git a/packages/react-native-codegen/src/cli/combine/combine-utils.js b/packages/react-native-codegen/src/cli/combine/combine-utils.js index 5dad6e041b6..a54a17ce26b 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-utils.js +++ b/packages/react-native-codegen/src/cli/combine/combine-utils.js @@ -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; diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTask.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTask.kt index 3c65387a403..828a145b007 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTask.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTask.kt @@ -67,6 +67,8 @@ abstract class GenerateCodegenSchemaTask : Exec() { .cliPath(workingDir), "--platform", "android", + "--exclude", + "NativeSampleTurboModule", generatedSchemaFile.get().asFile.cliPath(workingDir), jsRootDir.asFile.get().cliPath(workingDir), )) diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTaskTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTaskTest.kt index b97c1b29a20..0d0f1599238 100644 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTaskTest.kt +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateCodegenSchemaTaskTest.kt @@ -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, ), diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index f2ec937fa1c..a4c07c48720 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -267,7 +267,11 @@ function generateSchemaInfo(library) { library: library, schema: utils .getCombineJSToSchema() - .combineSchemasInFileList([pathToJavaScriptSources], 'ios'), + .combineSchemasInFileList( + [pathToJavaScriptSources], + 'ios', + /NativeSampleTurboModule/, + ), }; } diff --git a/packages/react-native/scripts/react_native_pods_utils/script_phases.sh b/packages/react-native/scripts/react_native_pods_utils/script_phases.sh index ab1eddfca72..aff355264ff 100755 --- a/packages/react-native/scripts/react_native_pods_utils/script_phases.sh +++ b/packages/react-native/scripts/react_native_pods_utils/script_phases.sh @@ -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 () {