@react-native-oh/react-native-harmony-cli 0.0.36 → 0.0.40

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 (137) hide show
  1. package/README.md +11 -11
  2. package/dist/assetResolver.js +74 -43
  3. package/dist/autolinking/Autolinking.js +183 -179
  4. package/dist/codegen/core/CodegenError.js +23 -3
  5. package/dist/codegen/core/EventUtils.js +3 -3
  6. package/dist/codegen/core/TypeAnnotationToTS.js +28 -24
  7. package/dist/codegen/core/UberSchema.js +96 -57
  8. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +42 -28
  9. package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +34 -27
  10. package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +33 -25
  11. package/dist/codegen/generators/CppComponentCodeGenerator.js +28 -23
  12. package/dist/codegen/generators/LibGlueCodeGenerator.js +29 -26
  13. package/dist/codegen/generators/NativeModuleCodeGenerator.js +30 -25
  14. package/dist/codegen/generators/SharedComponentCodeGenerator.js +15 -15
  15. package/dist/codegen/generators/UberGeneratorV1.js +24 -18
  16. package/dist/codegen/generators/UberGeneratorV2.js +36 -28
  17. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +24 -50
  18. package/dist/codegen/templates/BasePackageHTemplate.js +21 -109
  19. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +8 -53
  20. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +27 -75
  21. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +29 -162
  22. package/dist/codegen/templates/IndexTSTemplate.js +17 -37
  23. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +24 -108
  24. package/dist/codegen/templates/TurboModuleCppTemplate.js +15 -38
  25. package/dist/codegen/templates/TurboModuleHTemplate.js +11 -32
  26. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +19 -52
  27. package/dist/commands/bundle-harmony.js +138 -53
  28. package/dist/commands/codegen-harmony.js +175 -104
  29. package/dist/commands/codegen-lib-harmony.js +116 -76
  30. package/dist/commands/create-metro-host-constants.js +47 -18
  31. package/dist/commands/init.js +961 -537
  32. package/dist/commands/link-harmony.js +71 -33
  33. package/dist/commands/pack-harmony.js +108 -47
  34. package/dist/commands/run-harmony.js +520 -352
  35. package/dist/commands/unpack-harmony.js +198 -105
  36. package/dist/commands/verify-package-harmony.js +108 -54
  37. package/dist/core/AbsolutePath.js +47 -31
  38. package/dist/core/CodegenConfig.js +22 -24
  39. package/dist/core/DescriptiveError.js +37 -18
  40. package/dist/core/FileSystem.js +18 -14
  41. package/dist/core/OHPackageJSON5.js +23 -17
  42. package/dist/core/PackageJSON.js +42 -26
  43. package/dist/core/ProjectDependenciesManager.js +96 -46
  44. package/dist/core/ValidationError.js +22 -2
  45. package/dist/core/fs.js +6 -9
  46. package/dist/index.js +1 -1
  47. package/dist/io/CliExecutor.js +98 -46
  48. package/dist/io/Logger.js +46 -40
  49. package/dist/io/RealFS.js +88 -35
  50. package/dist/io/TarGz.js +8 -11
  51. package/dist/io/__fixtures__.js +141 -79
  52. package/dist/io/fsUtils.js +12 -13
  53. package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +47 -19
  54. package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +41 -13
  55. package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +38 -8
  56. package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +54 -22
  57. package/dist/package-verifier/verify-package.js +132 -55
  58. package/dist/simulator/LaunchSimulator.js +395 -0
  59. package/dist/simulator/index.js +23 -0
  60. package/jest.config.js +12 -12
  61. package/package.json +78 -70
  62. package/react-native.config.js +17 -17
  63. package/tsconfig.build.json +6 -6
  64. package/tsconfig.json +2 -2
  65. package/.prettierrc +0 -6
  66. package/src/assetResolver.ts +0 -134
  67. package/src/autolinking/Autolinking.test.ts +0 -483
  68. package/src/autolinking/Autolinking.ts +0 -382
  69. package/src/autolinking/index.ts +0 -8
  70. package/src/codegen/core/CodeGenerator.ts +0 -16
  71. package/src/codegen/core/CodegenError.ts +0 -10
  72. package/src/codegen/core/EventUtils.ts +0 -23
  73. package/src/codegen/core/TypeAnnotationToTS.test.ts +0 -82
  74. package/src/codegen/core/TypeAnnotationToTS.ts +0 -195
  75. package/src/codegen/core/UberSchema.ts +0 -152
  76. package/src/codegen/core/index.ts +0 -12
  77. package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +0 -120
  78. package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +0 -128
  79. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +0 -122
  80. package/src/codegen/generators/CppComponentCodeGenerator.ts +0 -95
  81. package/src/codegen/generators/LibGlueCodeGenerator.ts +0 -98
  82. package/src/codegen/generators/NativeModuleCodeGenerator.ts +0 -124
  83. package/src/codegen/generators/SharedComponentCodeGenerator.ts +0 -57
  84. package/src/codegen/generators/UberGeneratorV1.ts +0 -75
  85. package/src/codegen/generators/UberGeneratorV2.ts +0 -114
  86. package/src/codegen/generators/index.ts +0 -15
  87. package/src/codegen/index.ts +0 -9
  88. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +0 -128
  89. package/src/codegen/templates/BasePackageHTemplate.ts +0 -147
  90. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +0 -63
  91. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +0 -121
  92. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +0 -222
  93. package/src/codegen/templates/IndexTSTemplate.ts +0 -62
  94. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +0 -138
  95. package/src/codegen/templates/TurboModuleCppTemplate.ts +0 -61
  96. package/src/codegen/templates/TurboModuleHTemplate.ts +0 -42
  97. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +0 -101
  98. package/src/codegen/templates/index.ts +0 -16
  99. package/src/commands/bundle-harmony.ts +0 -134
  100. package/src/commands/codegen-harmony.ts +0 -297
  101. package/src/commands/codegen-lib-harmony.ts +0 -389
  102. package/src/commands/create-metro-host-constants.ts +0 -55
  103. package/src/commands/index.ts +0 -16
  104. package/src/commands/init.ts +0 -755
  105. package/src/commands/link-harmony.ts +0 -102
  106. package/src/commands/pack-harmony.ts +0 -128
  107. package/src/commands/run-harmony.ts +0 -478
  108. package/src/commands/types.ts +0 -21
  109. package/src/commands/unpack-harmony.ts +0 -238
  110. package/src/commands/verify-package-harmony.ts +0 -113
  111. package/src/core/AbsolutePath.ts +0 -66
  112. package/src/core/CodegenConfig.ts +0 -67
  113. package/src/core/DescriptiveError.ts +0 -68
  114. package/src/core/FileSystem.ts +0 -57
  115. package/src/core/OHPackageJSON5.ts +0 -30
  116. package/src/core/PackageJSON.ts +0 -104
  117. package/src/core/ProjectDependenciesManager.ts +0 -103
  118. package/src/core/ValidationError.ts +0 -8
  119. package/src/core/ValueObject.ts +0 -9
  120. package/src/core/fs.ts +0 -38
  121. package/src/core/index.ts +0 -17
  122. package/src/index.ts +0 -33
  123. package/src/io/CliExecutor.ts +0 -69
  124. package/src/io/Logger.ts +0 -112
  125. package/src/io/RealFS.ts +0 -62
  126. package/src/io/TarGz.ts +0 -33
  127. package/src/io/__fixtures__.ts +0 -139
  128. package/src/io/fsUtils.ts +0 -44
  129. package/src/io/index.ts +0 -11
  130. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +0 -35
  131. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +0 -31
  132. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +0 -25
  133. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +0 -54
  134. package/src/package-verifier/checks/index.ts +0 -11
  135. package/src/package-verifier/index.ts +0 -9
  136. package/src/package-verifier/types.ts +0 -19
  137. package/src/package-verifier/verify-package.ts +0 -123
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE-MIT file in the root directory of this source tree.
6
- */
7
-
8
- export * from './core';
9
- export * from './generators';
@@ -1,128 +0,0 @@
1
- /**
2
- * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE-MIT file in the root directory of this source tree.
6
- */
7
-
8
- import mustache from 'mustache';
9
- import Case from 'case';
10
- import { TypeAnnotation } from '../core';
11
-
12
- const TEMPLATE = `
13
- /**
14
- {{#codegenNoticeLines}}
15
- * {{{line}}}
16
- {{/codegenNoticeLines}}
17
- */
18
-
19
- #pragma once
20
-
21
- #include "RNOH/CppComponentInstance.h"
22
- #include <react/renderer/components/{{libraryName}}/ShadowNodes.h>
23
-
24
- namespace rnoh {
25
- class Base{{name}}ComponentInstance
26
- : public CppComponentInstance<facebook::react::{{name}}ShadowNode> {
27
- public:
28
- using CppComponentInstance::CppComponentInstance;
29
-
30
- void handleCommand(std::string const &commandName, folly::dynamic const &args) override {
31
- {{#commands}}
32
- if (commandName == "{{commandName}}") {
33
- this->on{{CommandName}}Command({{argsFromDynamic}});
34
- }
35
- {{/commands}}
36
- }
37
-
38
- {{#commands}}
39
- virtual void on{{CommandName}}Command({{argsDeclaration}}) = 0;
40
- {{/commands}}
41
- };
42
- } // namespace rnoh
43
- `;
44
-
45
- type BaseComponentInstanceHCommand = {
46
- name: string;
47
- args: { name: string; typeAnnotation: TypeAnnotation }[];
48
- };
49
-
50
- export class BaseComponentInstanceHTemplate {
51
- private commands: BaseComponentInstanceHCommand[] = [];
52
-
53
- constructor(
54
- private componentName: string,
55
- private libraryName: string,
56
- private codegenNoticeLines: string[]
57
- ) {}
58
-
59
- addCommand(command: BaseComponentInstanceHCommand) {
60
- this.commands.push(command);
61
- }
62
-
63
- build(): string {
64
- return mustache.render(TEMPLATE, {
65
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
66
- line,
67
- })),
68
- name: this.componentName,
69
- libraryName: this.libraryName,
70
- commands: this.commands.map(({ name, args }) => {
71
- return {
72
- commandName: name,
73
- CommandName: Case.pascal(name),
74
- argsDeclaration: args
75
- .map(
76
- ({ name, typeAnnotation }) =>
77
- `${cppTypeFromTypeAnnotation(typeAnnotation)} ${name}`
78
- )
79
- .join(', '),
80
- argsFromDynamic: args
81
- .map(
82
- ({ typeAnnotation }, index) =>
83
- `args[${index}]${dynamicExtractorFromTypeAnnotation(
84
- typeAnnotation
85
- )}`
86
- )
87
- .join(', '),
88
- };
89
- }),
90
- });
91
- }
92
- }
93
-
94
- function cppTypeFromTypeAnnotation(typeAnnotation: TypeAnnotation): string {
95
- switch (typeAnnotation.type) {
96
- case 'BooleanTypeAnnotation':
97
- return 'bool';
98
- case 'Int32TypeAnnotation':
99
- return 'int';
100
- case 'FloatTypeAnnotation':
101
- return 'float';
102
- case 'DoubleTypeAnnotation':
103
- return 'double';
104
- case 'StringTypeAnnotation':
105
- return 'std::string';
106
- default:
107
- return 'folly::dynamic';
108
- }
109
- }
110
-
111
- function dynamicExtractorFromTypeAnnotation(
112
- typeAnnotation: TypeAnnotation
113
- ): string {
114
- switch (typeAnnotation.type) {
115
- case 'BooleanTypeAnnotation':
116
- return '.asBool()';
117
- case 'Int32TypeAnnotation':
118
- return '.asInt()';
119
- case 'FloatTypeAnnotation':
120
- return '.asDouble()';
121
- case 'DoubleTypeAnnotation':
122
- return '.asDouble()';
123
- case 'StringTypeAnnotation':
124
- return '.asString()';
125
- default:
126
- return '';
127
- }
128
- }
@@ -1,147 +0,0 @@
1
- /**
2
- * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE-MIT file in the root directory of this source tree.
6
- */
7
-
8
- import mustache from 'mustache';
9
-
10
- const TEMPLATE = `
11
- /**
12
- {{#codegenNoticeLines}}
13
- * {{{line}}}
14
- {{/codegenNoticeLines}}
15
- */
16
-
17
- #pragma once
18
-
19
- {{#libraries}}
20
- #include <react/renderer/components/{{name}}/ComponentDescriptors.h>
21
- {{/libraries}}
22
- #include "RNOH/Package.h"
23
- #include "RNOH/ArkTSTurboModule.h"
24
- {{#turboModules}}
25
- #include "RNOH/generated/turbo_modules/{{name}}.h"
26
- {{/turboModules}}
27
- {{#components}}
28
- #include "RNOH/generated/components/{{name}}JSIBinder.h"
29
- {{/components}}
30
-
31
- namespace rnoh {
32
-
33
- class {{name}}TurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
34
- public:
35
- SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
36
- {{#turboModules}}
37
- if (name == "{{name}}") {
38
- return std::make_shared<{{name}}>(ctx, name);
39
- }
40
- {{/turboModules}}
41
- return nullptr;
42
- };
43
- };
44
-
45
- class {{name}}EventEmitRequestHandler : public EventEmitRequestHandler {
46
- public:
47
- void handleEvent(Context const &ctx) override {
48
- auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
49
- if (eventEmitter == nullptr) {
50
- return;
51
- }
52
-
53
- std::vector<std::string> supportedEventNames = {
54
- {{#arkTSEvents}}
55
- "{{name}}",
56
- {{/arkTSEvents}}
57
- };
58
- if (std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
59
- eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
60
- }
61
- }
62
- };
63
-
64
-
65
- class {{name}} : public Package {
66
- public:
67
- {{name}}(Package::Context ctx) : Package(ctx){};
68
-
69
- std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
70
- return std::make_unique<{{name}}TurboModuleFactoryDelegate>();
71
- }
72
-
73
- std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
74
- return {
75
- {{#components}}
76
- facebook::react::concreteComponentDescriptorProvider<facebook::react::{{name}}ComponentDescriptor>(),
77
- {{/components}}
78
- };
79
- }
80
-
81
- ComponentJSIBinderByString createComponentJSIBinderByName() override {
82
- return {
83
- {{#components}}
84
- {"{{name}}", std::make_shared<{{name}}JSIBinder>()},
85
- {{/components}}
86
- };
87
- };
88
-
89
- EventEmitRequestHandlers createEventEmitRequestHandlers() override {
90
- return {
91
- std::make_shared<{{name}}EventEmitRequestHandler>(),
92
- };
93
- }
94
- };
95
-
96
- } // namespace rnoh
97
- `;
98
-
99
- type GeneratedPackageTurboModule = {
100
- name: string;
101
- };
102
-
103
- type GeneratedPackageComponent = {
104
- name: string;
105
- libraryCppName: string;
106
- };
107
-
108
- type ArkTSEvent = {
109
- name: string;
110
- };
111
-
112
- export class BasePackageHTemplate {
113
- private turboModules: GeneratedPackageTurboModule[] = [];
114
- private components: GeneratedPackageComponent[] = [];
115
- private arkTSEvents: ArkTSEvent[] = [];
116
- private libraryCppNames = new Set<string>();
117
-
118
- constructor(private name: string, private codegenNoticeLines: string[]) {}
119
-
120
- addTurboModule(turboModule: GeneratedPackageTurboModule) {
121
- this.turboModules.push(turboModule);
122
- }
123
-
124
- addComponent(component: GeneratedPackageComponent) {
125
- this.components.push(component);
126
- this.libraryCppNames.add(component.libraryCppName);
127
- }
128
-
129
- addArkTSEvent(arkTSEvent: ArkTSEvent) {
130
- this.arkTSEvents.push(arkTSEvent);
131
- }
132
-
133
- build(): string {
134
- return mustache.render(TEMPLATE.trimStart(), {
135
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
136
- line,
137
- })),
138
- name: this.name,
139
- turboModules: this.turboModules,
140
- components: this.components,
141
- arkTSEvents: this.arkTSEvents,
142
- libraries: Array.from(this.libraryCppNames).map((name) => ({
143
- name,
144
- })),
145
- });
146
- }
147
- }
@@ -1,63 +0,0 @@
1
- /**
2
- * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE-MIT file in the root directory of this source tree.
6
- */
7
-
8
- import mustache from 'mustache';
9
-
10
- const TEMPLATE = `
11
- /**
12
- * This code was generated by "react-native codegen-harmony"
13
- *
14
- * Do not edit this file as changes may cause incorrect behavior and will be
15
- * lost once the code is regenerated.
16
- *
17
- * @generatorVersion: 1
18
- */
19
-
20
- #pragma once
21
-
22
- #include <react/renderer/core/ConcreteComponentDescriptor.h>
23
- #include <react/renderer/components/view/ConcreteViewShadowNode.h>
24
- #include <react/renderer/components/view/ViewShadowNode.h>
25
-
26
- namespace facebook {
27
- namespace react {
28
-
29
- constexpr char {{name}}ComponentName[] = "{{name}}";
30
-
31
- class {{name}}Props : public ViewProps {
32
- public:
33
- {{name}}Props() = default;
34
-
35
- {{name}}Props(const PropsParserContext &context, const {{name}}Props &sourceProps, const RawProps &rawProps)
36
- : ViewProps(context, sourceProps, rawProps) {}
37
- };
38
-
39
- using {{name}}ShadowNode = ConcreteViewShadowNode<
40
- {{name}}ComponentName,
41
- {{name}}Props,
42
- ViewEventEmitter>;
43
-
44
- class {{name}}ComponentDescriptor final
45
- : public ConcreteComponentDescriptor<{{name}}ShadowNode> {
46
- public:
47
- {{name}}ComponentDescriptor(ComponentDescriptorParameters const &parameters)
48
- : ConcreteComponentDescriptor(parameters) {}
49
- };
50
-
51
- } // namespace react
52
- } // namespace facebook
53
- `;
54
-
55
- export class ComponentDescriptorHTemplate {
56
- constructor(private name: string) {}
57
-
58
- build(): string {
59
- return mustache.render(TEMPLATE, {
60
- name: this.name,
61
- });
62
- }
63
- }
@@ -1,121 +0,0 @@
1
- /**
2
- * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE-MIT file in the root directory of this source tree.
6
- */
7
-
8
- import mustache from 'mustache';
9
-
10
- const TEMPLATE = `
11
- /**
12
- {{#codegenNoticeLines}}
13
- * {{{line}}}
14
- {{/codegenNoticeLines}}
15
- */
16
-
17
- #pragma once
18
- #include "RNOHCorePackage/ComponentBinders/ViewComponentJSIBinder.h"
19
-
20
- namespace rnoh {
21
- class {{name}}JSIBinder : public ViewComponentJSIBinder {
22
- protected:
23
- facebook::jsi::Object createNativeProps(facebook::jsi::Runtime &rt) override {
24
- auto object = ViewComponentJSIBinder::createNativeProps(rt);
25
- {{#props}}
26
- object.setProperty(rt, "{{name}}", {{{type}}});
27
- {{/props}}
28
- return object;
29
- }
30
-
31
- facebook::jsi::Object createCommands(facebook::jsi::Runtime &rt) override {
32
- auto commands = ViewComponentJSIBinder::createCommands(rt);
33
- {{#commands}}
34
- commands.setProperty(rt, "{{name}}", "{{name}}");
35
- {{/commands}}
36
- return commands;
37
- }
38
-
39
- facebook::jsi::Object createBubblingEventTypes(facebook::jsi::Runtime &rt) override {
40
- facebook::jsi::Object events(rt);
41
- {{#bubblingEvents}}
42
- events.setProperty(rt, "{{TOPname}}", createBubblingCapturedEvent(rt, "{{name}}"));
43
- {{/bubblingEvents}}
44
- return events;
45
- }
46
-
47
- facebook::jsi::Object createDirectEventTypes(facebook::jsi::Runtime &rt) override {
48
- facebook::jsi::Object events(rt);
49
- {{#directEvents}}
50
- events.setProperty(rt, "{{TOPname}}", createDirectEvent(rt, "{{name}}"));
51
- {{/directEvents}}
52
- return events;
53
- }
54
- };
55
- } // namespace rnoh
56
- `;
57
-
58
- type ComponentJSIBinderHProp = {
59
- name: string;
60
- isObject: boolean;
61
- };
62
-
63
- type ComponentJSIBinderHCommand = {
64
- name: string;
65
- };
66
-
67
- type ComponentJSIBinderHEvent = {
68
- type: 'direct' | 'bubble';
69
- eventHandlerName: string;
70
- };
71
-
72
- export class ComponentJSIBinderHTemplate {
73
- private props: ComponentJSIBinderHProp[] = [];
74
- private commands: ComponentJSIBinderHCommand[] = [];
75
- private events: ComponentJSIBinderHEvent[] = [];
76
-
77
- constructor(
78
- private name: string,
79
- private codegenNoticeLines: string[]
80
- ) {}
81
-
82
- addProp(prop: ComponentJSIBinderHProp) {
83
- this.props.push(prop);
84
- }
85
-
86
- addCommand(command: ComponentJSIBinderHCommand) {
87
- this.commands.push(command);
88
- }
89
-
90
- addEvent(event: ComponentJSIBinderHEvent) {
91
- this.events.push(event);
92
- }
93
-
94
- build(): string {
95
- return mustache.render(TEMPLATE.trimStart(), {
96
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
97
- line,
98
- })),
99
- name: this.name,
100
- props: this.props.map((prop) => ({
101
- name: prop.name,
102
- type: prop.isObject ? '"Object"' : 'true',
103
- })),
104
- commands: this.commands.map((command) => ({
105
- name: command.name,
106
- })),
107
- bubblingEvents: this.events
108
- .filter((event) => event.type === 'bubble')
109
- .map((event) => ({
110
- TOPname: event.eventHandlerName.replace('on', 'top'),
111
- name: event.eventHandlerName,
112
- })),
113
- directEvents: this.events
114
- .filter((event) => event.type === 'direct')
115
- .map((event) => ({
116
- TOPname: event.eventHandlerName.replace('on', 'top'),
117
- name: event.eventHandlerName,
118
- })),
119
- });
120
- }
121
- }
@@ -1,222 +0,0 @@
1
- /**
2
- * Copyright (c) 2024 Huawei Technologies Co., Ltd.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE-MIT file in the root directory of this source tree.
6
- */
7
-
8
- import mustache from 'mustache';
9
-
10
- const TEMPLATE = `
11
- /**
12
- {{#codegenNoticeLines}}
13
- * {{{line}}}
14
- {{/codegenNoticeLines}}
15
- */
16
-
17
- import {
18
- Descriptor as ComponentDescriptor,
19
- ViewBaseProps,
20
- ViewRawProps,
21
- ViewDescriptorWrapperBase,
22
- ColorValue,
23
- Color,
24
- RNInstance,
25
- Tag,
26
- RNComponentCommandReceiver,
27
- ViewPropsSelector,
28
- } from '{{{rnohImport}}}';
29
-
30
-
31
- export namespace {{name}} {
32
- export const NAME = "{{name}}" as const
33
-
34
- export interface DirectRawProps {
35
- {{#props}}
36
- {{name}}{{optionalityMarker}}: {{{type}}};
37
- {{/props}}
38
- }
39
-
40
- export interface Props extends ViewBaseProps {}
41
-
42
- export interface State {}
43
-
44
- export interface RawProps extends ViewRawProps, DirectRawProps {}
45
-
46
- export class PropsSelector extends ViewPropsSelector<Props, RawProps> {
47
- {{#simpleProps}}
48
- get {{name}}() {
49
- return this.rawProps.{{name}}{{{orDefaultValue}}};
50
- }
51
-
52
- {{/simpleProps}}
53
-
54
- {{#complexProps}}
55
- get {{name}}() {
56
- {{{implementation}}}
57
- }
58
-
59
- {{/complexProps}}
60
- }
61
-
62
- export type Descriptor = ComponentDescriptor<
63
- typeof NAME,
64
- Props,
65
- State,
66
- RawProps
67
- >;
68
-
69
- export class DescriptorWrapper extends ViewDescriptorWrapperBase<
70
- typeof NAME,
71
- Props,
72
- State,
73
- RawProps,
74
- PropsSelector
75
- > {
76
- protected createPropsSelector() {
77
- return new PropsSelector(this.descriptor.props, this.descriptor.rawProps)
78
- }
79
- }
80
-
81
- export interface EventPayloadByName {
82
- {{#events}}
83
- "{{name}}": {{{payloadTSType}}}
84
- {{/events}}
85
- }
86
-
87
- export class EventEmitter {
88
- constructor(private rnInstance: RNInstance, private tag: Tag) {}
89
-
90
- emit<TEventName extends keyof EventPayloadByName>(eventName: TEventName, payload: EventPayloadByName[TEventName]) {
91
- this.rnInstance.emitComponentEvent(this.tag, eventName, payload)
92
- }
93
- }
94
-
95
- export interface CommandArgvByName {
96
- {{#commands}}
97
- "{{name}}": {{{argsTSType}}}
98
- {{/commands}}
99
- }
100
-
101
- export class CommandReceiver {
102
- private listenersByCommandName = new Map<string, Set<(...args: any[]) => void>>()
103
- private cleanUp: (() => void) | undefined = undefined
104
-
105
- constructor(private componentCommandReceiver: RNComponentCommandReceiver, private tag: Tag) {
106
- }
107
-
108
- subscribe<TCommandName extends keyof CommandArgvByName>(commandName: TCommandName, listener: (argv: CommandArgvByName[TCommandName]) => void) {
109
- if (!this.listenersByCommandName.has(commandName)) {
110
- this.listenersByCommandName.set(commandName, new Set())
111
- }
112
- this.listenersByCommandName.get(commandName)!.add(listener)
113
- const hasRegisteredCommandReceiver = !!this.cleanUp
114
- if (!hasRegisteredCommandReceiver) {
115
- this.cleanUp = this.componentCommandReceiver.registerCommandCallback(this.tag, (commandName: string, argv: any[]) => {
116
- if (this.listenersByCommandName.has(commandName)) {
117
- const listeners = this.listenersByCommandName.get(commandName)!
118
- listeners.forEach(listener => {
119
- listener(argv)
120
- })
121
- }
122
- })
123
- }
124
-
125
- return () => {
126
- this.listenersByCommandName.get(commandName)?.delete(listener)
127
- if (this.listenersByCommandName.get(commandName)?.size ?? 0 === 0) {
128
- this.listenersByCommandName.delete(commandName)
129
- }
130
- if (this.listenersByCommandName.size === 0) {
131
- this.cleanUp?.()
132
- }
133
- }
134
- }
135
- }
136
-
137
- }
138
- `;
139
-
140
- const COLOR_VALUE_GETTER_TEMPLATE = ` if (this.rawProps.{{name}}) {
141
- return Color.fromColorValue(this.rawProps.{{name}})
142
- } else {
143
- return new Color({ r: 0, g: 0, b: 0, a: 255})
144
- }`;
145
-
146
- type Prop = {
147
- name: string;
148
- tsType: string;
149
- defaultValue: string | null;
150
- isOptional: boolean;
151
- };
152
-
153
- type Event = {
154
- name: string;
155
- payloadTSType: string;
156
- };
157
-
158
- type Command = {
159
- name: string;
160
- argsTSType: string;
161
- };
162
-
163
- export class ComponentUtilsTSTemplate {
164
- private props: Prop[] = [];
165
- private events: Event[] = [];
166
- private commands: Command[] = [];
167
- private getterTemplateByTSType = new Map<string, string>();
168
-
169
- constructor(
170
- private name: string,
171
- private codegenNoticeLines: string[],
172
- private rnohImport: string
173
- ) {
174
- this.getterTemplateByTSType.set('ColorValue', COLOR_VALUE_GETTER_TEMPLATE);
175
- }
176
-
177
- addProp(prop: Prop) {
178
- this.props.push(prop);
179
- }
180
-
181
- addEvent(event: Event) {
182
- this.events.push(event);
183
- }
184
-
185
- addCommand(command: Command) {
186
- this.commands.push(command);
187
- }
188
-
189
- build(): string {
190
- return mustache.render(TEMPLATE.trimStart(), {
191
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
192
- line,
193
- })),
194
- name: this.name,
195
- rnohImport: this.rnohImport,
196
- props: this.props.map((prop) => ({
197
- name: prop.name,
198
- type: prop.tsType,
199
- optionalityMarker: prop.isOptional ? '?' : '',
200
- })),
201
- events: this.events,
202
- commands: this.commands,
203
- simpleProps: this.props
204
- .filter((prop) => !this.getterTemplateByTSType.has(prop.tsType))
205
- .map((prop) => ({
206
- name: prop.name,
207
- type: prop.tsType,
208
- orDefaultValue: prop.defaultValue ? ` ?? ${prop.defaultValue}` : '',
209
- })),
210
- complexProps: this.props
211
- .filter((prop) => this.getterTemplateByTSType.has(prop.tsType))
212
- .map((prop) => ({
213
- name: prop.name,
214
- implementation: mustache.render(
215
- this.getterTemplateByTSType.get(prop.tsType)!,
216
- { name: prop.name },
217
- {}
218
- ),
219
- })),
220
- });
221
- }
222
- }