@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,98 +1,112 @@
1
- import { AbsolutePath } from '../../core';
2
- import {
3
- CodegenError,
4
- TypeAnnotationToTS,
5
- createEventNameFromEventHandlerName,
6
- getStringifiedTSDefaultValueFromPropTypeAnnotation,
7
- } from '../core';
8
- import type { SpecCodeGenerator } from '../core';
9
- import { SpecSchema } from '../core';
10
- import {
11
- ComponentDescriptorHTemplate,
12
- ComponentJSIBinderHTemplate,
13
- ComponentUtilsTSTemplate,
14
- } from '../templates';
15
-
16
- export class ComponentCodeGenerator implements SpecCodeGenerator {
17
- constructor(
18
- private cppOutputPath: AbsolutePath,
19
- private tsOutputPath: AbsolutePath
20
- ) {}
21
-
22
- generate(schema: SpecSchema): Map<AbsolutePath, string> {
23
- if (schema.type !== 'Component') {
24
- throw new CodegenError({
25
- whatHappened: `ComponentCodeGenerator can't generate code for a module type: ${schema.type}`,
26
- unexpected: true,
27
- });
28
- }
29
- const typeAnnotationToTS = new TypeAnnotationToTS();
30
- const result = new Map<AbsolutePath, string>();
31
- Object.entries(schema.components).map(([name, shape]) => {
32
- const componentDescriptorHTemplate = new ComponentDescriptorHTemplate(
33
- name
34
- );
35
- const componentJSIBinderHTemplate = new ComponentJSIBinderHTemplate(name);
36
- const componentUtilsTSTemplate = new ComponentUtilsTSTemplate(name);
37
-
38
- shape.props.forEach((prop) => {
39
- componentJSIBinderHTemplate.addProp({
40
- name: prop.name,
41
- isObject: prop.typeAnnotation.type === 'ObjectTypeAnnotation',
42
- });
43
-
44
- componentUtilsTSTemplate.addProp({
45
- name: prop.name,
46
- tsType: typeAnnotationToTS.convert(prop.typeAnnotation),
47
- defaultValue: getStringifiedTSDefaultValueFromPropTypeAnnotation(
48
- prop.typeAnnotation
49
- ),
50
- isOptional: prop.optional,
51
- });
52
- });
53
-
54
- shape.commands.forEach((command) => {
55
- componentUtilsTSTemplate.addCommand({
56
- name: command.name,
57
- argsTSType:
58
- '[' +
59
- command.typeAnnotation.params
60
- .map(
61
- (param) =>
62
- typeAnnotationToTS.convert(param.typeAnnotation) +
63
- (param.optional ? '?' : '')
64
- )
65
- .join(', ') +
66
- ']',
67
- });
68
- });
69
-
70
- shape.events.forEach((event) => {
71
- componentUtilsTSTemplate.addEvent({
72
- name: createEventNameFromEventHandlerName(event.name),
73
- payloadTSType: event.typeAnnotation.argument
74
- ? typeAnnotationToTS.convert(event.typeAnnotation.argument)
75
- : 'undefined',
76
- });
77
- componentJSIBinderHTemplate.addEvent({
78
- type: event.bubblingType,
79
- eventHandlerName: event.name,
80
- });
81
- });
82
-
83
- result.set(
84
- this.cppOutputPath.copyWithNewSegment(`${name}ComponentDescriptor.h`),
85
- componentDescriptorHTemplate.build()
86
- );
87
- result.set(
88
- this.cppOutputPath.copyWithNewSegment(`${name}JSIBinder.h`),
89
- componentJSIBinderHTemplate.build()
90
- );
91
- result.set(
92
- this.tsOutputPath.copyWithNewSegment(`${name}.ts`),
93
- componentUtilsTSTemplate.build()
94
- );
95
- });
96
- return result;
97
- }
98
- }
1
+ import { AbsolutePath } from '../../core';
2
+ import {
3
+ CodegenError,
4
+ TypeAnnotationToTS,
5
+ createEventNameFromEventHandlerName,
6
+ getStringifiedTSDefaultValueFromPropTypeAnnotation,
7
+ } from '../core';
8
+ import type { SpecCodeGenerator } from '../core';
9
+ import { SpecSchema } from '../core';
10
+ import {
11
+ ComponentDescriptorHTemplate,
12
+ ComponentJSIBinderHTemplate,
13
+ ComponentUtilsTSTemplate,
14
+ } from '../templates';
15
+ import { GlueCodeComponentDataV1 } from './GlueCodeGenerator';
16
+
17
+ export class ComponentCodeGeneratorArkTS implements SpecCodeGenerator {
18
+ private glueCodeDataItems: GlueCodeComponentDataV1[] = [];
19
+
20
+ constructor(
21
+ private cppOutputPath: AbsolutePath,
22
+ private tsOutputPath: AbsolutePath
23
+ ) {}
24
+
25
+ getGlueCodeData(): GlueCodeComponentDataV1[] {
26
+ return this.glueCodeDataItems;
27
+ }
28
+
29
+ generate(schema: SpecSchema): Map<AbsolutePath, string> {
30
+ if (schema.type !== 'Component') {
31
+ throw new CodegenError({
32
+ whatHappened: `ComponentCodeGenerator can't generate code for a module type: ${schema.type}`,
33
+ unexpected: true,
34
+ });
35
+ }
36
+ const typeAnnotationToTS = new TypeAnnotationToTS();
37
+ const result = new Map<AbsolutePath, string>();
38
+ Object.entries(schema.components).map(([name, shape]) => {
39
+ const componentDescriptorHTemplate = new ComponentDescriptorHTemplate(
40
+ name
41
+ );
42
+ const componentJSIBinderHTemplate = new ComponentJSIBinderHTemplate(
43
+ name,
44
+ 1
45
+ );
46
+ const componentUtilsTSTemplate = new ComponentUtilsTSTemplate(name);
47
+
48
+ shape.props.forEach((prop) => {
49
+ componentJSIBinderHTemplate.addProp({
50
+ name: prop.name,
51
+ isObject: prop.typeAnnotation.type === 'ObjectTypeAnnotation',
52
+ });
53
+
54
+ componentUtilsTSTemplate.addProp({
55
+ name: prop.name,
56
+ tsType: typeAnnotationToTS.convert(prop.typeAnnotation),
57
+ defaultValue: getStringifiedTSDefaultValueFromPropTypeAnnotation(
58
+ prop.typeAnnotation
59
+ ),
60
+ isOptional: prop.optional,
61
+ });
62
+ });
63
+ shape.commands.forEach((command) => {
64
+ componentUtilsTSTemplate.addCommand({
65
+ name: command.name,
66
+ argsTSType:
67
+ '[' +
68
+ command.typeAnnotation.params
69
+ .map(
70
+ (param) =>
71
+ typeAnnotationToTS.convert(param.typeAnnotation) +
72
+ (param.optional ? '?' : '')
73
+ )
74
+ .join(', ') +
75
+ ']',
76
+ });
77
+ });
78
+
79
+ shape.events.forEach((event) => {
80
+ componentUtilsTSTemplate.addEvent({
81
+ name: createEventNameFromEventHandlerName(event.name),
82
+ payloadTSType: event.typeAnnotation.argument
83
+ ? typeAnnotationToTS.convert(event.typeAnnotation.argument)
84
+ : 'undefined',
85
+ });
86
+ componentJSIBinderHTemplate.addEvent({
87
+ type: event.bubblingType,
88
+ eventHandlerName: event.name,
89
+ });
90
+ });
91
+ result.set(
92
+ this.cppOutputPath.copyWithNewSegment(`${name}ComponentDescriptor.h`),
93
+ componentDescriptorHTemplate.build()
94
+ );
95
+ result.set(
96
+ this.cppOutputPath.copyWithNewSegment(`${name}JSIBinder.h`),
97
+ componentJSIBinderHTemplate.build()
98
+ );
99
+ result.set(
100
+ this.tsOutputPath.copyWithNewSegment(`${name}.ts`),
101
+ componentUtilsTSTemplate.build()
102
+ );
103
+ this.glueCodeDataItems.push({
104
+ name,
105
+ eventNames: shape.events.map((e) =>
106
+ createEventNameFromEventHandlerName(e.name)
107
+ ),
108
+ });
109
+ });
110
+ return result;
111
+ }
112
+ }
@@ -0,0 +1,114 @@
1
+ import RNCodegen from '@react-native/codegen/lib/generators/RNCodegen';
2
+ import { AbsolutePath } from '../../core';
3
+ import { CodeGenerator, UberSchema } from '../core';
4
+ import {
5
+ BaseComponentInstanceHTemplate,
6
+ ComponentJSIBinderHTemplate,
7
+ } from '../templates';
8
+ import { GlueCodeComponentDataV2 } from './GlueCodeGenerator';
9
+
10
+ /**
11
+ * NPM package that extends RNOH capabilities.
12
+ */
13
+ export type LibraryData = {
14
+ /**
15
+ * C++ directory name which is used in #include statements
16
+ */
17
+ name: string;
18
+ uberSchema: UberSchema;
19
+ };
20
+
21
+ export class ComponentCodeGeneratorCAPI implements CodeGenerator<LibraryData> {
22
+ private glueCodeComponentsData: GlueCodeComponentDataV2[] = [];
23
+
24
+ constructor(
25
+ /// "shared" — platform independent code that can be shared between platforms
26
+ private onGetSharedOutputPath: (filename: string) => AbsolutePath,
27
+ private onGetRNOHOutputPath: (filename: string) => AbsolutePath
28
+ ) {}
29
+
30
+ getGlueCodeData() {
31
+ return this.glueCodeComponentsData;
32
+ }
33
+
34
+ generate({
35
+ name: libraryName,
36
+ uberSchema,
37
+ }: LibraryData): Map<AbsolutePath, string> {
38
+ const fileContentByPath = new Map<AbsolutePath, string>();
39
+ const generatorFns = [
40
+ RNCodegen.allGenerators.generatePropsH,
41
+ RNCodegen.allGenerators.generatePropsCpp,
42
+ RNCodegen.allGenerators.generateEventEmitterH,
43
+ RNCodegen.allGenerators.generateEventEmitterCpp,
44
+ RNCodegen.allGenerators.generateComponentDescriptorH,
45
+ RNCodegen.allGenerators.generateStateH,
46
+ RNCodegen.allGenerators.generateStateCpp,
47
+ RNCodegen.allGenerators.generateShadowNodeH,
48
+ RNCodegen.allGenerators.generateShadowNodeCpp,
49
+ ];
50
+ generatorFns.forEach((generate) => {
51
+ const filesOutput = generate(
52
+ libraryName,
53
+ uberSchema.getValue(),
54
+ 'UNUSED_packageName',
55
+ true
56
+ );
57
+ filesOutput.forEach((fileContent, filename) => {
58
+ fileContentByPath.set(
59
+ this.onGetSharedOutputPath(filename),
60
+ fileContent
61
+ );
62
+ });
63
+ });
64
+ for (const [filename, schema] of uberSchema
65
+ .getSpecSchemaByFilenameMap()
66
+ .entries()) {
67
+ if (schema.type === 'Component') {
68
+ Object.entries(schema.components).map(([componentName, shape]) => {
69
+ const componentJSIBinderHTemplate = new ComponentJSIBinderHTemplate(
70
+ componentName,
71
+ 2
72
+ );
73
+ const componentInstanceSpecHTemplate =
74
+ new BaseComponentInstanceHTemplate(componentName, libraryName);
75
+ shape.props.forEach((prop) => {
76
+ componentJSIBinderHTemplate.addProp({
77
+ name: prop.name,
78
+ isObject: prop.typeAnnotation.type === 'ObjectTypeAnnotation',
79
+ });
80
+ });
81
+ shape.events.forEach((event) => {
82
+ componentJSIBinderHTemplate.addEvent({
83
+ type: event.bubblingType,
84
+ eventHandlerName: event.name,
85
+ });
86
+ });
87
+ shape.commands.forEach((command) => {
88
+ componentInstanceSpecHTemplate.addCommand({
89
+ name: command.name,
90
+ args: command.typeAnnotation.params.map((param) => ({
91
+ name: param.name,
92
+ typeAnnotation: param.typeAnnotation,
93
+ })),
94
+ });
95
+ });
96
+ fileContentByPath.set(
97
+ this.onGetRNOHOutputPath(`${componentName}JSIBinder.h`),
98
+ componentJSIBinderHTemplate.build()
99
+ );
100
+ fileContentByPath.set(
101
+ this.onGetRNOHOutputPath(`Base${componentName}ComponentInstance.h`),
102
+ componentInstanceSpecHTemplate.build()
103
+ );
104
+ this.glueCodeComponentsData.push({
105
+ name: componentName,
106
+ libraryName,
107
+ });
108
+ });
109
+ }
110
+ }
111
+
112
+ return fileContentByPath;
113
+ }
114
+ }
@@ -0,0 +1,109 @@
1
+ import Case from 'case';
2
+ import { AbsolutePath } from '../../core';
3
+ import { CodeGenerator } from '../core';
4
+ import { IndexTSTemplate, RNOHGeneratedPackageHTemplate } from '../templates';
5
+ import { BasePackageHTemplate } from '../templates/GeneratedPackageHTemplate';
6
+
7
+ export type GlueCodeComponentDataV1 = { name: string; eventNames: string[] };
8
+
9
+ export type GlueCodeComponentDataV2 = { name: string; libraryName: string };
10
+
11
+ export type GlueCodeTurboModuleData = { name: string };
12
+
13
+ export type GlueCodeDataV1 = {
14
+ components: GlueCodeComponentDataV1[];
15
+ turboModules: GlueCodeTurboModuleData[];
16
+ };
17
+
18
+ type LibraryName = string;
19
+ export type GlueCodeDataV2 = Map<
20
+ LibraryName,
21
+ {
22
+ components: GlueCodeComponentDataV2[];
23
+ turboModules: GlueCodeTurboModuleData[];
24
+ }
25
+ >;
26
+
27
+ export type GlueCodeData = {
28
+ v1: GlueCodeDataV1;
29
+ v2: GlueCodeDataV2;
30
+ };
31
+
32
+ /**
33
+ * Generates code based on UberGenerators results. If UberGenerators produce "leaves" (concrete turbo module or component classes),
34
+ * this class generates "branches" (barrel files like index.ts) and "trunks" (package file or files).
35
+ */
36
+ export class GlueCodeGenerator implements CodeGenerator<GlueCodeData> {
37
+ constructor(
38
+ private cppOutputPath: AbsolutePath,
39
+ private etsOutputPath: AbsolutePath
40
+ ) {}
41
+
42
+ generate({ v1, v2 }: GlueCodeData): Map<AbsolutePath, string> {
43
+ const fileContentByPath = new Map<AbsolutePath, string>();
44
+ const generatedPackageH_v1 = new RNOHGeneratedPackageHTemplate();
45
+ const components_indexTS = new IndexTSTemplate();
46
+ const turboModules_indexTS = new IndexTSTemplate();
47
+ const generated_indexETS = new IndexTSTemplate();
48
+
49
+ generated_indexETS.addReexport({ from: './ts' });
50
+ v1.components.forEach(({ name, eventNames }) => {
51
+ components_indexTS.addReexport({ from: `./${name}` });
52
+ generatedPackageH_v1.addComponent({
53
+ name,
54
+ supportedEventNames: eventNames,
55
+ });
56
+ });
57
+
58
+ v1.turboModules.forEach((turboModule) => {
59
+ turboModules_indexTS.addReexport({ from: `./${turboModule.name}` });
60
+ generatedPackageH_v1.addTurboModule(turboModule);
61
+ });
62
+ v2.forEach(({ components, turboModules }, libraryName) => {
63
+ const basePackageClassName = `Base${Case.pascal(libraryName)}Package`;
64
+ const generatedPackageH_v2 = new BasePackageHTemplate(
65
+ basePackageClassName
66
+ );
67
+ components.forEach((component) => {
68
+ generatedPackageH_v2.addComponent(component);
69
+ });
70
+ turboModules.forEach((turboModule) => {
71
+ turboModules_indexTS.addReexport({ from: `./${turboModule.name}` });
72
+ generatedPackageH_v2.addTurboModule(turboModule);
73
+ });
74
+ fileContentByPath.set(
75
+ this.cppOutputPath.copyWithNewSegment(
76
+ libraryName,
77
+ 'RNOH',
78
+ 'generated',
79
+ `${basePackageClassName}.h`
80
+ ),
81
+ generatedPackageH_v2.build()
82
+ );
83
+ });
84
+ fileContentByPath.set(
85
+ this.cppOutputPath.copyWithNewSegment('RNOHGeneratedPackage.h'),
86
+ generatedPackageH_v1.build()
87
+ );
88
+ fileContentByPath.set(
89
+ this.etsOutputPath.copyWithNewSegment('ts.ts'),
90
+ new IndexTSTemplate()
91
+ .addReexport({ from: './components/ts', as: 'RNC' })
92
+ .addReexport({ from: './turboModules/ts', as: 'TM' })
93
+ .build()
94
+ );
95
+ fileContentByPath.set(
96
+ this.etsOutputPath.copyWithNewSegment('components', 'ts.ts'),
97
+ components_indexTS.build()
98
+ );
99
+ fileContentByPath.set(
100
+ this.etsOutputPath.copyWithNewSegment('turboModules', 'ts.ts'),
101
+ turboModules_indexTS.build()
102
+ );
103
+ fileContentByPath.set(
104
+ this.etsOutputPath.copyWithNewSegment('index.ets'),
105
+ generated_indexETS.build()
106
+ );
107
+ return fileContentByPath;
108
+ }
109
+ }
@@ -1,95 +1,115 @@
1
- import { AbsolutePath } from '../../core';
2
- import type { SpecCodeGenerator } from '../core/Codegen';
3
- import { CodegenError } from '../core/CodegenError';
4
- import {
5
- TurboModuleCppTemplate,
6
- TurboModuleHTemplate,
7
- TurboModuleInterfaceTS,
8
- } from '../templates';
9
- import { SpecSchema } from '../core/UberSchema';
10
- import { TypeAnnotationToTS, wrapWithQuotes } from '../core';
11
-
12
- export class NativeModuleCodeGenerator implements SpecCodeGenerator {
13
- constructor(
14
- private cppOutputPath: AbsolutePath,
15
- private etsOutputPath: AbsolutePath
16
- ) {}
17
-
18
- generate(schema: SpecSchema): Map<AbsolutePath, string> {
19
- const result = new Map<AbsolutePath, string>();
20
- if (schema.type !== 'NativeModule') {
21
- throw new CodegenError({
22
- whatHappened: `NativeModuleCodeGenerator can't generate code for module type: ${schema.type}`,
23
- unexpected: true,
24
- });
25
- }
26
- const typeAnnotationToTS = new TypeAnnotationToTS();
27
- const turboModuleInterfaceTS = new TurboModuleInterfaceTS(
28
- schema.moduleName
29
- );
30
- const turboModuleH = new TurboModuleHTemplate(schema.moduleName).build();
31
- result.set(
32
- this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.h`),
33
- turboModuleH
34
- );
35
-
36
- const turboModuleCpp = new TurboModuleCppTemplate(schema.moduleName);
37
-
38
- Object.entries(schema.aliasMap).map(([name, typeAnnotation]) => {
39
- turboModuleInterfaceTS.addAlias({
40
- name: name,
41
- type: typeAnnotationToTS.convert(typeAnnotation),
42
- });
43
- });
44
-
45
- Object.entries(schema.enumMap).map(([name, enumSpec]) => {
46
- turboModuleInterfaceTS.addEnum({
47
- name,
48
- members: enumSpec.members.map((member) => ({
49
- name: member.name,
50
- value:
51
- typeof member.value === 'string'
52
- ? wrapWithQuotes(member.value)
53
- : member.value.toString(),
54
- })),
55
- });
56
- });
57
-
58
- schema.spec.properties.map((prop) => {
59
- if (prop.typeAnnotation.type === 'FunctionTypeAnnotation') {
60
- turboModuleCpp.addMethod({
61
- name: prop.name,
62
- argsCount: prop.typeAnnotation.params.length,
63
- isAsync:
64
- prop.typeAnnotation.returnTypeAnnotation.type ===
65
- 'PromiseTypeAnnotation',
66
- });
67
-
68
- turboModuleInterfaceTS.addMethod({
69
- name: prop.name,
70
- returnType: typeAnnotationToTS.convert(
71
- prop.typeAnnotation.returnTypeAnnotation
72
- ),
73
- stringifiedArgs: prop.typeAnnotation.params
74
- .map(
75
- (param) =>
76
- `${param.name}: ${typeAnnotationToTS.convert(
77
- param.typeAnnotation
78
- )}`
79
- )
80
- .join(', '),
81
- });
82
- }
83
- });
84
- result.set(
85
- this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.cpp`),
86
- turboModuleCpp.build()
87
- );
88
- result.set(
89
- this.etsOutputPath.copyWithNewSegment(`${schema.moduleName}.ts`),
90
- turboModuleInterfaceTS.build()
91
- );
92
-
93
- return result;
94
- }
95
- }
1
+ import { AbsolutePath } from '../../core';
2
+ import type { SpecCodeGenerator } from '../core';
3
+ import {
4
+ TurboModuleCppTemplate,
5
+ TurboModuleHTemplate,
6
+ TurboModuleInterfaceTS,
7
+ } from '../templates';
8
+ import {
9
+ TypeAnnotationToTS,
10
+ wrapWithQuotes,
11
+ SpecSchema,
12
+ CodegenError,
13
+ } from '../core';
14
+ import { GlueCodeTurboModuleData } from './GlueCodeGenerator';
15
+
16
+ /**
17
+ * Generates boilerplate code for Turbo Modules.
18
+ */
19
+ export class NativeModuleCodeGenerator implements SpecCodeGenerator {
20
+ private glueCodeTurboModulesData: GlueCodeTurboModuleData[] = [];
21
+ constructor(
22
+ private cppOutputPath: AbsolutePath,
23
+ private etsOutputPath: AbsolutePath,
24
+ private generatorVersion: number
25
+ ) {}
26
+
27
+ getGlueCodeData() {
28
+ return this.glueCodeTurboModulesData;
29
+ }
30
+
31
+ generate(schema: SpecSchema): Map<AbsolutePath, string> {
32
+ const result = new Map<AbsolutePath, string>();
33
+ if (schema.type !== 'NativeModule') {
34
+ throw new CodegenError({
35
+ whatHappened: `NativeModuleCodeGenerator can't generate code for module type: ${schema.type}`,
36
+ unexpected: true,
37
+ });
38
+ }
39
+ const typeAnnotationToTS = new TypeAnnotationToTS();
40
+ const turboModuleInterfaceTS = new TurboModuleInterfaceTS(
41
+ schema.moduleName,
42
+ this.generatorVersion
43
+ );
44
+ const turboModuleH = new TurboModuleHTemplate(
45
+ schema.moduleName,
46
+ this.generatorVersion
47
+ ).build();
48
+ result.set(
49
+ this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.h`),
50
+ turboModuleH
51
+ );
52
+
53
+ const turboModuleCpp = new TurboModuleCppTemplate(
54
+ schema.moduleName,
55
+ this.generatorVersion
56
+ );
57
+
58
+ Object.entries(schema.aliasMap).map(([name, typeAnnotation]) => {
59
+ turboModuleInterfaceTS.addAlias({
60
+ name: name,
61
+ type: typeAnnotationToTS.convert(typeAnnotation),
62
+ });
63
+ });
64
+
65
+ Object.entries(schema.enumMap).map(([name, enumSpec]) => {
66
+ turboModuleInterfaceTS.addEnum({
67
+ name,
68
+ members: enumSpec.members.map((member) => ({
69
+ name: member.name,
70
+ value:
71
+ typeof member.value === 'string'
72
+ ? wrapWithQuotes(member.value)
73
+ : member.value.toString(),
74
+ })),
75
+ });
76
+ });
77
+
78
+ schema.spec.properties.map((prop) => {
79
+ if (prop.typeAnnotation.type === 'FunctionTypeAnnotation') {
80
+ turboModuleCpp.addMethod({
81
+ name: prop.name,
82
+ argsCount: prop.typeAnnotation.params.length,
83
+ isAsync:
84
+ prop.typeAnnotation.returnTypeAnnotation.type ===
85
+ 'PromiseTypeAnnotation',
86
+ });
87
+
88
+ turboModuleInterfaceTS.addMethod({
89
+ name: prop.name,
90
+ returnType: typeAnnotationToTS.convert(
91
+ prop.typeAnnotation.returnTypeAnnotation
92
+ ),
93
+ stringifiedArgs: prop.typeAnnotation.params
94
+ .map(
95
+ (param) =>
96
+ `${param.name}: ${typeAnnotationToTS.convert(
97
+ param.typeAnnotation
98
+ )}`
99
+ )
100
+ .join(', '),
101
+ });
102
+ }
103
+ });
104
+ result.set(
105
+ this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.cpp`),
106
+ turboModuleCpp.build()
107
+ );
108
+ result.set(
109
+ this.etsOutputPath.copyWithNewSegment(`${schema.moduleName}.ts`),
110
+ turboModuleInterfaceTS.build()
111
+ );
112
+ this.glueCodeTurboModulesData.push({ name: schema.moduleName });
113
+ return result;
114
+ }
115
+ }