@react-native-oh/react-native-harmony-cli 0.0.24 → 0.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/.prettierrc +6 -6
  2. package/jest.config.js +5 -5
  3. package/package.json +57 -52
  4. package/react-native.config.js +10 -10
  5. package/src/assetResolver.ts +21 -44
  6. package/src/codegen/core/CodeGenerator.ts +9 -0
  7. package/src/codegen/core/CodegenError.ts +3 -3
  8. package/src/codegen/core/EventUtils.ts +18 -18
  9. package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -75
  10. package/src/codegen/core/TypeAnnotationToTS.ts +188 -188
  11. package/src/codegen/core/UberSchema.ts +142 -99
  12. package/src/codegen/core/index.ts +5 -5
  13. package/src/codegen/generators/{ComponentCodeGenerator.ts → ComponentCodeGeneratorArkTS.ts} +112 -98
  14. package/src/codegen/generators/ComponentCodeGeneratorCAPI.ts +114 -0
  15. package/src/codegen/generators/GlueCodeGenerator.ts +109 -0
  16. package/src/codegen/generators/NativeModuleCodeGenerator.ts +115 -95
  17. package/src/codegen/generators/UberGeneratorV1.ts +64 -0
  18. package/src/codegen/generators/UberGeneratorV2.ts +91 -0
  19. package/src/codegen/generators/index.ts +6 -3
  20. package/src/codegen/index.ts +2 -2
  21. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +116 -0
  22. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +55 -49
  23. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +93 -85
  24. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +210 -202
  25. package/src/codegen/templates/GeneratedPackageHTemplate.ts +107 -0
  26. package/src/codegen/templates/IndexTSTemplate.ts +53 -33
  27. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +130 -124
  28. package/src/codegen/templates/TurboModuleCppTemplate.ts +54 -47
  29. package/src/codegen/templates/TurboModuleHTemplate.ts +35 -26
  30. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +90 -81
  31. package/src/codegen/templates/index.ts +9 -8
  32. package/src/commands/bundle-harmony.ts +203 -202
  33. package/src/commands/codegen-harmony.ts +272 -143
  34. package/src/commands/create-metro-host-constants.ts +48 -48
  35. package/src/commands/index.ts +6 -5
  36. package/src/commands/pack-harmony.ts +121 -121
  37. package/src/commands/unpack-harmony.ts +231 -231
  38. package/src/commands/verify-package-harmony.ts +106 -0
  39. package/src/core/AbsolutePath.ts +50 -43
  40. package/src/core/CodegenConfig.ts +62 -54
  41. package/src/core/DescriptiveError.ts +61 -61
  42. package/src/core/OHPackageJSON5.ts +23 -0
  43. package/src/core/PackageJSON.ts +75 -63
  44. package/src/core/ProjectDependenciesManager.ts +77 -77
  45. package/src/core/ValidationError.ts +1 -1
  46. package/src/core/ValueObject.ts +2 -2
  47. package/src/core/fs.ts +31 -31
  48. package/src/core/index.ts +9 -8
  49. package/src/index.ts +20 -18
  50. package/src/io/Logger.ts +68 -68
  51. package/src/io/TarGz.ts +26 -0
  52. package/src/io/fsUtils.ts +24 -24
  53. package/src/io/index.ts +3 -2
  54. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +28 -0
  55. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +24 -0
  56. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +18 -0
  57. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +47 -0
  58. package/src/package-verifier/checks/index.ts +4 -0
  59. package/src/package-verifier/index.ts +2 -0
  60. package/src/package-verifier/types.ts +12 -0
  61. package/src/package-verifier/verify-package.ts +117 -0
  62. package/tsconfig.build.json +6 -6
  63. package/tsconfig.json +2 -2
  64. package/dist/assetResolver.js +0 -41
  65. package/dist/codegen/core/Codegen.js +0 -35
  66. package/dist/codegen/core/CodegenError.js +0 -7
  67. package/dist/codegen/core/EventUtils.js +0 -19
  68. package/dist/codegen/core/TypeAnnotationToTS.js +0 -132
  69. package/dist/codegen/core/UberSchema.js +0 -74
  70. package/dist/codegen/core/index.js +0 -21
  71. package/dist/codegen/generators/ComponentCodeGenerator.js +0 -66
  72. package/dist/codegen/generators/NativeModuleCodeGenerator.js +0 -64
  73. package/dist/codegen/generators/PackageCodeGenerator.js +0 -52
  74. package/dist/codegen/generators/index.js +0 -19
  75. package/dist/codegen/index.js +0 -18
  76. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +0 -55
  77. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +0 -78
  78. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +0 -181
  79. package/dist/codegen/templates/IndexTSTemplate.js +0 -31
  80. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +0 -117
  81. package/dist/codegen/templates/TurboModuleCppTemplate.js +0 -45
  82. package/dist/codegen/templates/TurboModuleHTemplate.js +0 -32
  83. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +0 -64
  84. package/dist/codegen/templates/index.js +0 -24
  85. package/dist/commands/bundle-harmony.js +0 -179
  86. package/dist/commands/codegen-harmony.js +0 -110
  87. package/dist/commands/create-metro-host-constants.js +0 -61
  88. package/dist/commands/index.js +0 -21
  89. package/dist/commands/pack-harmony.js +0 -100
  90. package/dist/commands/unpack-harmony.js +0 -184
  91. package/dist/core/AbsolutePath.js +0 -41
  92. package/dist/core/CodegenConfig.js +0 -53
  93. package/dist/core/DescriptiveError.js +0 -49
  94. package/dist/core/PackageJSON.js +0 -53
  95. package/dist/core/ProjectDependenciesManager.js +0 -70
  96. package/dist/core/ValidationError.js +0 -6
  97. package/dist/core/ValueObject.js +0 -2
  98. package/dist/core/fs.js +0 -36
  99. package/dist/core/index.js +0 -24
  100. package/dist/index.js +0 -13
  101. package/dist/io/Logger.js +0 -71
  102. package/dist/io/fsUtils.js +0 -23
  103. package/dist/io/index.js +0 -18
  104. package/src/codegen/core/Codegen.ts +0 -44
  105. package/src/codegen/generators/PackageCodeGenerator.ts +0 -73
@@ -1,188 +1,188 @@
1
- import {
2
- BooleanTypeAnnotation,
3
- DoubleTypeAnnotation,
4
- FloatTypeAnnotation,
5
- Int32TypeAnnotation,
6
- MixedTypeAnnotation,
7
- ObjectTypeAnnotation,
8
- PropTypeAnnotation,
9
- ReservedPropTypeAnnotation,
10
- ReservedTypeAnnotation,
11
- StringTypeAnnotation,
12
- Nullable,
13
- NativeModuleTypeAnnotation,
14
- UnionTypeAnnotationMemberType,
15
- } from '@react-native/codegen/lib/CodegenSchema';
16
-
17
- type StringEnumTypeAnnotation = {
18
- readonly type: 'StringEnumTypeAnnotation';
19
- readonly options: readonly string[];
20
- };
21
-
22
- type Int32EnumTypeAnnotation = {
23
- readonly type: 'Int32EnumTypeAnnotation';
24
- readonly options: readonly number[];
25
- };
26
-
27
- type ArrayTypeAnnotation = {
28
- readonly type: 'ArrayTypeAnnotation';
29
- readonly elementType: TypeAnnotation;
30
- };
31
-
32
- export type TypeAnnotation =
33
- | BooleanTypeAnnotation
34
- | StringTypeAnnotation
35
- | DoubleTypeAnnotation
36
- | FloatTypeAnnotation
37
- | Int32TypeAnnotation
38
- | ReservedPropTypeAnnotation
39
- | ReservedTypeAnnotation
40
- | StringEnumTypeAnnotation
41
- | Int32EnumTypeAnnotation
42
- | MixedTypeAnnotation
43
- | ArrayTypeAnnotation
44
- | NativeModuleTypeAnnotation
45
- | Nullable<NativeModuleTypeAnnotation>
46
- | ObjectTypeAnnotation<TypeAnnotation>;
47
-
48
- export class TypeAnnotationToTS {
49
- constructor(private aliasPrefix: string = '') {}
50
-
51
- convert(typeAnnotation: TypeAnnotation | undefined): string {
52
- if (!typeAnnotation) {
53
- return 'unknown';
54
- }
55
- switch (typeAnnotation.type) {
56
- case 'BooleanTypeAnnotation':
57
- return 'boolean';
58
- case 'DoubleTypeAnnotation':
59
- return 'number';
60
- case 'FloatTypeAnnotation':
61
- return 'number';
62
- case 'Int32EnumTypeAnnotation':
63
- return typeAnnotation.options.join(' | ');
64
- case 'Int32TypeAnnotation':
65
- return 'number';
66
- case 'MixedTypeAnnotation':
67
- return 'unknown';
68
- case 'ReservedTypeAnnotation':
69
- switch (typeAnnotation.name) {
70
- case 'RootTag':
71
- return 'Tag';
72
- default:
73
- return 'unknown';
74
- }
75
- case 'ReservedPropTypeAnnotation':
76
- switch (typeAnnotation.name) {
77
- case 'ColorPrimitive':
78
- return 'ColorValue';
79
- case 'DimensionPrimitive':
80
- return 'unknown';
81
- case 'EdgeInsetsPrimitive':
82
- return 'unknown';
83
- case 'ImageRequestPrimitive':
84
- return 'unknown';
85
- case 'ImageSourcePrimitive':
86
- return 'unknown';
87
- case 'PointPrimitive':
88
- return 'unknown';
89
- default:
90
- return 'unknown';
91
- }
92
- case 'StringEnumTypeAnnotation':
93
- return typeAnnotation.options
94
- .map((option) => {
95
- if (typeof option !== 'string') {
96
- return option;
97
- }
98
- return wrapWithQuotes(option);
99
- })
100
- .join(' | ');
101
- case 'StringTypeAnnotation':
102
- return 'string';
103
- case 'FunctionTypeAnnotation':
104
- const { params, returnTypeAnnotation } = typeAnnotation;
105
- const stringifiedArgs = params
106
- .map(
107
- (param) => `${param.name}: ${this.convert(param.typeAnnotation)}`
108
- )
109
- .join(', ');
110
- return `(${stringifiedArgs}) => ${this.convert(returnTypeAnnotation)}`;
111
-
112
- case 'EnumDeclaration':
113
- return typeAnnotation.name;
114
- case 'GenericObjectTypeAnnotation':
115
- return 'Object';
116
- case 'TypeAliasTypeAnnotation':
117
- return this.aliasPrefix + typeAnnotation.name;
118
- case 'UnionTypeAnnotation':
119
- return this.convertUnionMemberAnnotation(typeAnnotation.memberType);
120
- case 'NumberTypeAnnotation':
121
- return 'number';
122
- case 'NullableTypeAnnotation':
123
- return `null | ${this.convert(typeAnnotation.typeAnnotation)}`;
124
- case 'ObjectTypeAnnotation':
125
- return this.convertObjectTypeAnnotation(typeAnnotation);
126
- case 'PromiseTypeAnnotation':
127
- return `Promise<${this.convert(typeAnnotation.elementType)}>`;
128
- case 'VoidTypeAnnotation':
129
- return 'void';
130
- case 'ArrayTypeAnnotation':
131
- return `${this.convert(typeAnnotation.elementType)}[]`;
132
- default:
133
- return 'unknown';
134
- }
135
- }
136
-
137
- private convertUnionMemberAnnotation(
138
- memberAnnotation: UnionTypeAnnotationMemberType
139
- ) {
140
- switch (memberAnnotation) {
141
- case 'NumberTypeAnnotation':
142
- return 'number';
143
- case 'ObjectTypeAnnotation':
144
- return 'Object';
145
- case 'StringTypeAnnotation':
146
- return 'string';
147
- }
148
- }
149
-
150
- private convertObjectTypeAnnotation(
151
- objectTypeAnnotation: ObjectTypeAnnotation<
152
- TypeAnnotation | Nullable<NativeModuleTypeAnnotation>
153
- >
154
- ): string {
155
- const props = objectTypeAnnotation.properties
156
- .map((prop) => {
157
- const maybeOptionalSymbol = prop.optional ? '?' : '';
158
- return `${prop.name}${maybeOptionalSymbol}: ${this.convert(
159
- prop.typeAnnotation
160
- )}`;
161
- })
162
- .join(', ');
163
- if (props.length === 0) {
164
- return '{}';
165
- }
166
- return `{${props}}`;
167
- }
168
- }
169
-
170
- export function getStringifiedTSDefaultValueFromPropTypeAnnotation(
171
- propTypeAnnotation: PropTypeAnnotation
172
- ): string | null {
173
- if ('default' in propTypeAnnotation && propTypeAnnotation.default !== null) {
174
- if (typeof propTypeAnnotation.default === 'string') {
175
- return wrapWithQuotes(propTypeAnnotation.default);
176
- }
177
- return String(propTypeAnnotation.default);
178
- }
179
- return null;
180
- }
181
-
182
- export function wrapWithQuotes(str: string): string {
183
- if (str.includes("'")) {
184
- return `"${str}"`;
185
- } else {
186
- return `'${str}'`;
187
- }
188
- }
1
+ import {
2
+ BooleanTypeAnnotation,
3
+ DoubleTypeAnnotation,
4
+ FloatTypeAnnotation,
5
+ Int32TypeAnnotation,
6
+ MixedTypeAnnotation,
7
+ ObjectTypeAnnotation,
8
+ PropTypeAnnotation,
9
+ ReservedPropTypeAnnotation,
10
+ ReservedTypeAnnotation,
11
+ StringTypeAnnotation,
12
+ Nullable,
13
+ NativeModuleTypeAnnotation,
14
+ UnionTypeAnnotationMemberType,
15
+ } from '@react-native/codegen/lib/CodegenSchema';
16
+
17
+ type StringEnumTypeAnnotation = {
18
+ readonly type: 'StringEnumTypeAnnotation';
19
+ readonly options: readonly string[];
20
+ };
21
+
22
+ type Int32EnumTypeAnnotation = {
23
+ readonly type: 'Int32EnumTypeAnnotation';
24
+ readonly options: readonly number[];
25
+ };
26
+
27
+ type ArrayTypeAnnotation = {
28
+ readonly type: 'ArrayTypeAnnotation';
29
+ readonly elementType: TypeAnnotation;
30
+ };
31
+
32
+ export type TypeAnnotation =
33
+ | BooleanTypeAnnotation
34
+ | StringTypeAnnotation
35
+ | DoubleTypeAnnotation
36
+ | FloatTypeAnnotation
37
+ | Int32TypeAnnotation
38
+ | ReservedPropTypeAnnotation
39
+ | ReservedTypeAnnotation
40
+ | StringEnumTypeAnnotation
41
+ | Int32EnumTypeAnnotation
42
+ | MixedTypeAnnotation
43
+ | ArrayTypeAnnotation
44
+ | NativeModuleTypeAnnotation
45
+ | Nullable<NativeModuleTypeAnnotation>
46
+ | ObjectTypeAnnotation<TypeAnnotation>;
47
+
48
+ export class TypeAnnotationToTS {
49
+ constructor(private aliasPrefix: string = '') {}
50
+
51
+ convert(typeAnnotation: TypeAnnotation | undefined): string {
52
+ if (!typeAnnotation) {
53
+ return 'unknown';
54
+ }
55
+ switch (typeAnnotation.type) {
56
+ case 'BooleanTypeAnnotation':
57
+ return 'boolean';
58
+ case 'DoubleTypeAnnotation':
59
+ return 'number';
60
+ case 'FloatTypeAnnotation':
61
+ return 'number';
62
+ case 'Int32EnumTypeAnnotation':
63
+ return typeAnnotation.options.join(' | ');
64
+ case 'Int32TypeAnnotation':
65
+ return 'number';
66
+ case 'MixedTypeAnnotation':
67
+ return 'unknown';
68
+ case 'ReservedTypeAnnotation':
69
+ switch (typeAnnotation.name) {
70
+ case 'RootTag':
71
+ return 'Tag';
72
+ default:
73
+ return 'unknown';
74
+ }
75
+ case 'ReservedPropTypeAnnotation':
76
+ switch (typeAnnotation.name) {
77
+ case 'ColorPrimitive':
78
+ return 'ColorValue';
79
+ case 'DimensionPrimitive':
80
+ return 'unknown';
81
+ case 'EdgeInsetsPrimitive':
82
+ return 'unknown';
83
+ case 'ImageRequestPrimitive':
84
+ return 'unknown';
85
+ case 'ImageSourcePrimitive':
86
+ return 'unknown';
87
+ case 'PointPrimitive':
88
+ return 'unknown';
89
+ default:
90
+ return 'unknown';
91
+ }
92
+ case 'StringEnumTypeAnnotation':
93
+ return typeAnnotation.options
94
+ .map((option) => {
95
+ if (typeof option !== 'string') {
96
+ return option;
97
+ }
98
+ return wrapWithQuotes(option);
99
+ })
100
+ .join(' | ');
101
+ case 'StringTypeAnnotation':
102
+ return 'string';
103
+ case 'FunctionTypeAnnotation':
104
+ const { params, returnTypeAnnotation } = typeAnnotation;
105
+ const stringifiedArgs = params
106
+ .map(
107
+ (param) => `${param.name}: ${this.convert(param.typeAnnotation)}`
108
+ )
109
+ .join(', ');
110
+ return `(${stringifiedArgs}) => ${this.convert(returnTypeAnnotation)}`;
111
+
112
+ case 'EnumDeclaration':
113
+ return typeAnnotation.name;
114
+ case 'GenericObjectTypeAnnotation':
115
+ return 'Object';
116
+ case 'TypeAliasTypeAnnotation':
117
+ return this.aliasPrefix + typeAnnotation.name;
118
+ case 'UnionTypeAnnotation':
119
+ return this.convertUnionMemberAnnotation(typeAnnotation.memberType);
120
+ case 'NumberTypeAnnotation':
121
+ return 'number';
122
+ case 'NullableTypeAnnotation':
123
+ return `null | ${this.convert(typeAnnotation.typeAnnotation)}`;
124
+ case 'ObjectTypeAnnotation':
125
+ return this.convertObjectTypeAnnotation(typeAnnotation);
126
+ case 'PromiseTypeAnnotation':
127
+ return `Promise<${this.convert(typeAnnotation.elementType)}>`;
128
+ case 'VoidTypeAnnotation':
129
+ return 'void';
130
+ case 'ArrayTypeAnnotation':
131
+ return `${this.convert(typeAnnotation.elementType)}[]`;
132
+ default:
133
+ return 'unknown';
134
+ }
135
+ }
136
+
137
+ private convertUnionMemberAnnotation(
138
+ memberAnnotation: UnionTypeAnnotationMemberType
139
+ ) {
140
+ switch (memberAnnotation) {
141
+ case 'NumberTypeAnnotation':
142
+ return 'number';
143
+ case 'ObjectTypeAnnotation':
144
+ return 'Object';
145
+ case 'StringTypeAnnotation':
146
+ return 'string';
147
+ }
148
+ }
149
+
150
+ private convertObjectTypeAnnotation(
151
+ objectTypeAnnotation: ObjectTypeAnnotation<
152
+ TypeAnnotation | Nullable<NativeModuleTypeAnnotation>
153
+ >
154
+ ): string {
155
+ const props = objectTypeAnnotation.properties
156
+ .map((prop) => {
157
+ const maybeOptionalSymbol = prop.optional ? '?' : '';
158
+ return `${prop.name}${maybeOptionalSymbol}: ${this.convert(
159
+ prop.typeAnnotation
160
+ )}`;
161
+ })
162
+ .join(', ');
163
+ if (props.length === 0) {
164
+ return '{}';
165
+ }
166
+ return `{${props}}`;
167
+ }
168
+ }
169
+
170
+ export function getStringifiedTSDefaultValueFromPropTypeAnnotation(
171
+ propTypeAnnotation: PropTypeAnnotation
172
+ ): string | null {
173
+ if ('default' in propTypeAnnotation && propTypeAnnotation.default !== null) {
174
+ if (typeof propTypeAnnotation.default === 'string') {
175
+ return wrapWithQuotes(propTypeAnnotation.default);
176
+ }
177
+ return String(propTypeAnnotation.default);
178
+ }
179
+ return null;
180
+ }
181
+
182
+ export function wrapWithQuotes(str: string): string {
183
+ if (str.includes("'")) {
184
+ return `"${str}"`;
185
+ } else {
186
+ return `'${str}'`;
187
+ }
188
+ }
@@ -1,99 +1,142 @@
1
- import {
2
- ComponentSchema,
3
- NativeModuleSchema,
4
- SchemaType as RawUberSchema,
5
- } from '@react-native/codegen/lib/CodegenSchema';
6
- import {
7
- AbsolutePath,
8
- ValueObject,
9
- PackageJSON,
10
- ProjectDependenciesManager,
11
- CodegenConfig,
12
- } from '../../core';
13
- // @ts-expect-error
14
- import extractUberSchemaFromSpecFilePaths_ from '@react-native/codegen/lib/cli/combine/combine-js-to-schema.js';
15
- import { CodegenError } from './CodegenError';
16
-
17
- function createRawUberSchemaFromSpecFilePaths(
18
- projectSourceFilePaths: AbsolutePath[]
19
- ): RawUberSchema {
20
- return extractUberSchemaFromSpecFilePaths_(
21
- projectSourceFilePaths.map((p) => p.getValue())
22
- );
23
- }
24
-
25
- export type SpecSchema = ComponentSchema | NativeModuleSchema;
26
- export type SpecSchemaType = SpecSchema['type'];
27
- type FindSpecSchemaByType<
28
- TType extends SpecSchemaType,
29
- TSpecSchema = SpecSchema
30
- > = TSpecSchema extends { type: TType } ? TSpecSchema : never;
31
-
32
- export class UberSchema implements ValueObject {
33
- static async fromProjectRootPath(
34
- projectRootPath: AbsolutePath
35
- ): Promise<UberSchema> {
36
- const packageJSON = PackageJSON.fromProjectRootPath(
37
- projectRootPath,
38
- projectRootPath
39
- );
40
- const codegenConfigs: CodegenConfig[] = [];
41
- const appCodegenConfig = packageJSON.maybeCreateCodegenConfig();
42
- if (appCodegenConfig) {
43
- codegenConfigs.push(appCodegenConfig);
44
- }
45
- await new ProjectDependenciesManager(projectRootPath).forEachAsync(
46
- (dependency) => {
47
- const codegenConfig = dependency.maybeCreateCodegenConfig();
48
- if (codegenConfig) {
49
- codegenConfigs.push(codegenConfig);
50
- }
51
- }
52
- );
53
- try {
54
- return new UberSchema(
55
- createRawUberSchemaFromSpecFilePaths(
56
- codegenConfigs.flatMap((codegenConfig) =>
57
- codegenConfig.getSpecFilePaths()
58
- )
59
- )
60
- );
61
- } catch (err) {
62
- if (err instanceof Error) {
63
- throw new CodegenError({
64
- whatHappened: "Couldn't create the schema",
65
- whatCanUserDo: {
66
- default: [
67
- `There's probably at least one spec file defined in your project or in a third-party package that breaks some code generation restrictions. Please check the message below. If it's ambiguous, debug the problem with divide and conquer strategy.\n\n${err.message}`,
68
- ],
69
- },
70
- });
71
- }
72
- throw err;
73
- }
74
- }
75
-
76
- private constructor(private schemaValue: RawUberSchema) {}
77
-
78
- getValue(): RawUberSchema {
79
- return this.schemaValue;
80
- }
81
-
82
- findAllSpecSchemasByType<TSpecSchemaType extends SpecSchemaType>(
83
- schemaType: TSpecSchemaType
84
- ): FindSpecSchemaByType<TSpecSchemaType>[] {
85
- return Object.values(this.schemaValue.modules)
86
- .filter((module) => module.type === schemaType)
87
- .map((module) => module as FindSpecSchemaByType<TSpecSchemaType>);
88
- }
89
-
90
- getSpecSchemaByFilenameMap(): ReadonlyMap<string, SpecSchema> {
91
- return Object.entries(this.schemaValue.modules).reduce(
92
- (acc, [moduleName, moduleSchema]) => {
93
- acc.set(moduleName, moduleSchema);
94
- return acc;
95
- },
96
- new Map<string, SpecSchema>()
97
- );
98
- }
99
- }
1
+ import {
2
+ ComponentSchema,
3
+ NativeModuleSchema,
4
+ SchemaType as RawUberSchema,
5
+ } from '@react-native/codegen/lib/CodegenSchema';
6
+ import {
7
+ AbsolutePath,
8
+ ValueObject,
9
+ PackageJSON,
10
+ ProjectDependenciesManager,
11
+ CodegenConfig,
12
+ } from '../../core';
13
+ // @ts-expect-error
14
+ import extractUberSchemaFromSpecFilePaths_ from '@react-native/codegen/lib/cli/combine/combine-js-to-schema.js';
15
+ import { CodegenError } from './CodegenError';
16
+
17
+ function createRawUberSchemaFromSpecFilePaths(
18
+ projectSourceFilePaths: AbsolutePath[]
19
+ ): RawUberSchema {
20
+ return extractUberSchemaFromSpecFilePaths_(
21
+ projectSourceFilePaths.map((p) => p.getValue())
22
+ );
23
+ }
24
+
25
+ export type SpecSchema = ComponentSchema | NativeModuleSchema;
26
+ export type SpecSchemaType = SpecSchema['type'];
27
+ type FindSpecSchemaByType<
28
+ TType extends SpecSchemaType,
29
+ TSpecSchema = SpecSchema
30
+ > = TSpecSchema extends { type: TType } ? TSpecSchema : never;
31
+
32
+ /**
33
+ * Contains component and turbo module (NativeModule) schemas. The "Uber" word is used here to highlight that SpecSchemas don't extend this class.
34
+ */
35
+ export class UberSchema implements ValueObject {
36
+ static fromCodegenConfig(codegenConfig: CodegenConfig): UberSchema {
37
+ try {
38
+ return new UberSchema(
39
+ createRawUberSchemaFromSpecFilePaths(codegenConfig.getSpecFilePaths())
40
+ );
41
+ } catch (err) {
42
+ if (err instanceof Error) {
43
+ throw new CodegenError({
44
+ whatHappened: "Couldn't create the schema",
45
+ whatCanUserDo: {
46
+ default: [
47
+ `There's probably at least one spec file defined in your project or in a third-party package that breaks some code generation restrictions. Please check the message below. If it's ambiguous, debug the problem with divide and conquer strategy.\n\n${err.message}`,
48
+ ],
49
+ },
50
+ });
51
+ }
52
+ throw err;
53
+ }
54
+ }
55
+
56
+ static async fromProject(
57
+ projectRootPath: AbsolutePath,
58
+ onShouldAcceptCodegenConfig?: (
59
+ codegenVersion: number,
60
+ packageName: string
61
+ ) => boolean
62
+ ): Promise<UberSchema> {
63
+ const onShouldAcceptCodegenConfig_ =
64
+ onShouldAcceptCodegenConfig ?? ((x: number) => true);
65
+ const packageJSON = PackageJSON.fromProjectRootPath(
66
+ projectRootPath,
67
+ projectRootPath
68
+ );
69
+ const acceptedCodegenConfigs: CodegenConfig[] = [];
70
+ const appCodegenConfigs = packageJSON.getCodegenConfigs();
71
+ for (const codegenConfig of appCodegenConfigs) {
72
+ if (
73
+ onShouldAcceptCodegenConfig_(
74
+ codegenConfig.getVersion(),
75
+ packageJSON.name
76
+ )
77
+ ) {
78
+ acceptedCodegenConfigs.push(codegenConfig);
79
+ }
80
+ }
81
+ await new ProjectDependenciesManager(projectRootPath).forEachAsync(
82
+ (dependency) => {
83
+ const codegenConfigs = dependency.getCodegenConfigs();
84
+ for (const codegenConfig of codegenConfigs) {
85
+ if (
86
+ onShouldAcceptCodegenConfig_(
87
+ codegenConfig.getVersion(),
88
+ dependency.readPackageJSON().name
89
+ )
90
+ ) {
91
+ acceptedCodegenConfigs.push(codegenConfig);
92
+ }
93
+ }
94
+ }
95
+ );
96
+ try {
97
+ return new UberSchema(
98
+ createRawUberSchemaFromSpecFilePaths(
99
+ acceptedCodegenConfigs.flatMap((codegenConfig) =>
100
+ codegenConfig.getSpecFilePaths()
101
+ )
102
+ )
103
+ );
104
+ } catch (err) {
105
+ if (err instanceof Error) {
106
+ throw new CodegenError({
107
+ whatHappened: "Couldn't create the schema",
108
+ whatCanUserDo: {
109
+ default: [
110
+ `There's probably at least one spec file defined in your project or in a third-party package that breaks some code generation restrictions. Please check the message below. If it's ambiguous, debug the problem with divide and conquer strategy.\n\n${err.message}`,
111
+ ],
112
+ },
113
+ });
114
+ }
115
+ throw err;
116
+ }
117
+ }
118
+
119
+ private constructor(private schemaValue: RawUberSchema) {}
120
+
121
+ getValue(): RawUberSchema {
122
+ return this.schemaValue;
123
+ }
124
+
125
+ findAllSpecSchemasByType<TSpecSchemaType extends SpecSchemaType>(
126
+ schemaType: TSpecSchemaType
127
+ ): FindSpecSchemaByType<TSpecSchemaType>[] {
128
+ return Object.values(this.schemaValue.modules)
129
+ .filter((module) => module.type === schemaType)
130
+ .map((module) => module as FindSpecSchemaByType<TSpecSchemaType>);
131
+ }
132
+
133
+ getSpecSchemaByFilenameMap(): ReadonlyMap<string, SpecSchema> {
134
+ return Object.entries(this.schemaValue.modules).reduce(
135
+ (acc, [moduleName, moduleSchema]) => {
136
+ acc.set(moduleName, moduleSchema);
137
+ return acc;
138
+ },
139
+ new Map<string, SpecSchema>()
140
+ );
141
+ }
142
+ }
@@ -1,5 +1,5 @@
1
- export * from './Codegen';
2
- export * from './UberSchema';
3
- export * from './CodegenError';
4
- export * from './EventUtils';
5
- export * from './TypeAnnotationToTS';
1
+ export * from './CodeGenerator';
2
+ export * from './UberSchema';
3
+ export * from './CodegenError';
4
+ export * from './EventUtils';
5
+ export * from './TypeAnnotationToTS';