@@ -21,31 +21,69 @@ const path = require('path');
2121const flowParser = new FlowParser ( ) ;
2222const typescriptParser = new TypeScriptParser ( ) ;
2323
24- function combineSchemas ( files : Array < string > ) : SchemaType {
25- return files . reduce (
26- ( merged , filename ) => {
27- const contents = fs . readFileSync ( filename , 'utf8' ) ;
24+ function combineSchemas (
25+ files : Array < string > ,
26+ excludeInterfaceOnly ?: boolean ,
27+ excludeUnimplemented ?: boolean ,
28+ ) : {
29+ interfaceOnly : SchemaType ,
30+ unimplemented : SchemaType ,
31+ everythingElse : SchemaType ,
32+ } {
33+ let interfaceOnly : SchemaType = { modules : { } } ;
34+ let unimplemented : SchemaType = { modules : { } } ;
35+ let everythingElse : SchemaType = { modules : { } } ;
2836
29- if (
30- contents &&
31- ( / e x p o r t \s + d e f a u l t \s + \( ? c o d e g e n N a t i v e C o m p o n e n t < / . test ( contents ) ||
32- / e x t e n d s T u r b o M o d u l e / . test ( contents ) )
33- ) {
34- const isTypeScript =
35- path . extname ( filename ) === '.ts' || path . extname ( filename ) === '.tsx' ;
37+ files . forEach ( filename => {
38+ const contents = fs . readFileSync ( filename , 'utf8' ) ;
3639
37- const parser = isTypeScript ? typescriptParser : flowParser ;
40+ if (
41+ contents &&
42+ ( / e x p o r t \s + d e f a u l t \s + \( ? c o d e g e n N a t i v e C o m p o n e n t < / . test ( contents ) ||
43+ / e x t e n d s T u r b o M o d u l e / . test ( contents ) )
44+ ) {
45+ const isTypeScript =
46+ path . extname ( filename ) === '.ts' || path . extname ( filename ) === '.tsx' ;
47+ const parser = isTypeScript ? typescriptParser : flowParser ;
48+ const schema = parser . parseFile ( filename ) ;
3849
39- const schema = parser . parseFile ( filename ) ;
50+ if ( ! schema || ! schema . modules ) {
51+ return ;
52+ }
53+
54+ const isInterfaceOnly = / i n t e r f a c e O n l y : \s * t r u e / . test ( contents ) ;
55+ const isUnimplemented =
56+ / U n i m p l e m e n t e d N a t i v e V i e w N a t i v e C o m p o n e n t \. j s $ / . test ( filename ) ;
4057
41- if ( schema && schema . modules ) {
42- merged . modules = { ...merged . modules , ...schema . modules } ;
58+ if ( isInterfaceOnly ) {
59+ if ( excludeInterfaceOnly !== true ) {
60+ interfaceOnly = {
61+ modules : { ...interfaceOnly . modules , ...schema . modules } ,
62+ } ;
63+ } else {
64+ console . log ( `Excluding interfaceOnly component: ${ filename } ` ) ;
65+ }
66+ } else if ( isUnimplemented ) {
67+ if ( excludeUnimplemented !== true ) {
68+ unimplemented = {
69+ modules : { ...unimplemented . modules , ...schema . modules } ,
70+ } ;
71+ } else {
72+ console . log ( `Excluding unimplemented component: ${ filename } ` ) ;
4373 }
74+ } else {
75+ everythingElse = {
76+ modules : { ...everythingElse . modules , ...schema . modules } ,
77+ } ;
4478 }
45- return merged ;
46- } ,
47- { modules : { } } ,
48- ) ;
79+ }
80+ } ) ;
81+
82+ return {
83+ interfaceOnly,
84+ unimplemented,
85+ everythingElse,
86+ } ;
4987}
5088
5189function expandDirectoriesIntoFiles (
@@ -74,14 +112,28 @@ function combineSchemasInFileList(
74112 fileList: Array< string > ,
75113 platform: ?string,
76114 exclude: ?RegExp,
77- ) : SchemaType {
115+ excludeInterfaceOnly: boolean,
116+ excludeUnimplemented: boolean,
117+ ): {
118+ interfaceOnly : SchemaType ,
119+ unimplemented : SchemaType ,
120+ everythingElse : SchemaType ,
121+ } {
78122 const expandedFileList = expandDirectoriesIntoFiles (
79123 fileList ,
80124 platform ,
81125 exclude ,
82126 ) ;
83- const combined = combineSchemas ( expandedFileList ) ;
84- if ( Object . keys ( combined . modules ) . length === 0 ) {
127+ const combined = combineSchemas (
128+ expandedFileList ,
129+ excludeInterfaceOnly ,
130+ excludeUnimplemented ,
131+ ) ;
132+ if (
133+ Object . keys ( combined . interfaceOnly . modules ) . length === 0 &&
134+ Object . keys ( combined . unimplemented . modules ) . length === 0 &&
135+ Object . keys ( combined . everythingElse . modules ) . length === 0
136+ ) {
85137 console . error (
86138 'No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules.' ,
87139 ) ;
@@ -94,9 +146,36 @@ function combineSchemasInFileListAndWriteToFile(
94146 platform: ?string,
95147 outfile: string,
96148 exclude: ?RegExp,
149+ excludeInterfaceOnly: boolean,
150+ excludeUnimplemented: boolean,
97151): void {
98- const combined = combineSchemasInFileList ( fileList , platform , exclude ) ;
99- const formattedSchema = JSON . stringify ( combined ) ;
152+ const combined = combineSchemasInFileList (
153+ fileList ,
154+ platform ,
155+ exclude ,
156+ excludeInterfaceOnly ,
157+ excludeUnimplemented ,
158+ ) ;
159+
160+ // Determine which schema to write based on flags
161+ let schemaToWrite ;
162+ if ( excludeInterfaceOnly && excludeUnimplemented ) {
163+ schemaToWrite = combined . everythingElse ;
164+ } else if (excludeInterfaceOnly) {
165+ schemaToWrite = combined . unimplemented ;
166+ } else if (excludeUnimplemented) {
167+ schemaToWrite = combined . interfaceOnly ;
168+ } else {
169+ // If no exclusions, combine all schemas
170+ schemaToWrite = {
171+ modules : {
172+ ...combined . interfaceOnly . modules ,
173+ ...combined . unimplemented . modules ,
174+ ...combined . everythingElse . modules ,
175+ } ,
176+ } ;
177+ }
178+ const formattedSchema = JSON.stringify(schemaToWrite);
100179 fs.writeFileSync(outfile, formattedSchema);
101180}
102181
0 commit comments