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

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