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

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 (87) hide show
  1. package/dist/autolinking/Autolinking.js +14 -11
  2. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +2 -1
  3. package/dist/codegen/generators/LibGlueCodeGenerator.js +2 -2
  4. package/dist/codegen/generators/UberGeneratorV1.js +9 -2
  5. package/dist/codegen/generators/UberGeneratorV2.js +8 -1
  6. package/dist/codegen/templates/BasePackageHTemplate.js +12 -1
  7. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +12 -1
  8. package/dist/commands/codegen-harmony.js +16 -5
  9. package/dist/commands/run-harmony.js +49 -27
  10. package/dist/core/ProjectDependenciesManager.js +5 -7
  11. package/dist/simulator/LaunchSimulator.js +318 -0
  12. package/dist/simulator/index.js +23 -0
  13. package/harmony/rnoh-hvigor-plugin-0.3.0.tgz +0 -0
  14. package/package.json +13 -5
  15. package/.prettierrc +0 -6
  16. package/src/assetResolver.ts +0 -134
  17. package/src/autolinking/Autolinking.test.ts +0 -483
  18. package/src/autolinking/Autolinking.ts +0 -382
  19. package/src/autolinking/index.ts +0 -8
  20. package/src/codegen/core/CodeGenerator.ts +0 -16
  21. package/src/codegen/core/CodegenError.ts +0 -10
  22. package/src/codegen/core/EventUtils.ts +0 -23
  23. package/src/codegen/core/TypeAnnotationToTS.test.ts +0 -82
  24. package/src/codegen/core/TypeAnnotationToTS.ts +0 -195
  25. package/src/codegen/core/UberSchema.ts +0 -152
  26. package/src/codegen/core/index.ts +0 -12
  27. package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +0 -120
  28. package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +0 -128
  29. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +0 -122
  30. package/src/codegen/generators/CppComponentCodeGenerator.ts +0 -95
  31. package/src/codegen/generators/LibGlueCodeGenerator.ts +0 -98
  32. package/src/codegen/generators/NativeModuleCodeGenerator.ts +0 -124
  33. package/src/codegen/generators/SharedComponentCodeGenerator.ts +0 -57
  34. package/src/codegen/generators/UberGeneratorV1.ts +0 -75
  35. package/src/codegen/generators/UberGeneratorV2.ts +0 -114
  36. package/src/codegen/generators/index.ts +0 -15
  37. package/src/codegen/index.ts +0 -9
  38. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +0 -128
  39. package/src/codegen/templates/BasePackageHTemplate.ts +0 -147
  40. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +0 -63
  41. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +0 -121
  42. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +0 -222
  43. package/src/codegen/templates/IndexTSTemplate.ts +0 -62
  44. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +0 -138
  45. package/src/codegen/templates/TurboModuleCppTemplate.ts +0 -61
  46. package/src/codegen/templates/TurboModuleHTemplate.ts +0 -42
  47. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +0 -101
  48. package/src/codegen/templates/index.ts +0 -16
  49. package/src/commands/bundle-harmony.ts +0 -134
  50. package/src/commands/codegen-harmony.ts +0 -297
  51. package/src/commands/codegen-lib-harmony.ts +0 -389
  52. package/src/commands/create-metro-host-constants.ts +0 -55
  53. package/src/commands/index.ts +0 -16
  54. package/src/commands/init.ts +0 -755
  55. package/src/commands/link-harmony.ts +0 -102
  56. package/src/commands/pack-harmony.ts +0 -128
  57. package/src/commands/run-harmony.ts +0 -478
  58. package/src/commands/types.ts +0 -21
  59. package/src/commands/unpack-harmony.ts +0 -238
  60. package/src/commands/verify-package-harmony.ts +0 -113
  61. package/src/core/AbsolutePath.ts +0 -66
  62. package/src/core/CodegenConfig.ts +0 -67
  63. package/src/core/DescriptiveError.ts +0 -68
  64. package/src/core/FileSystem.ts +0 -57
  65. package/src/core/OHPackageJSON5.ts +0 -30
  66. package/src/core/PackageJSON.ts +0 -104
  67. package/src/core/ProjectDependenciesManager.ts +0 -103
  68. package/src/core/ValidationError.ts +0 -8
  69. package/src/core/ValueObject.ts +0 -9
  70. package/src/core/fs.ts +0 -38
  71. package/src/core/index.ts +0 -17
  72. package/src/index.ts +0 -33
  73. package/src/io/CliExecutor.ts +0 -69
  74. package/src/io/Logger.ts +0 -112
  75. package/src/io/RealFS.ts +0 -62
  76. package/src/io/TarGz.ts +0 -33
  77. package/src/io/__fixtures__.ts +0 -139
  78. package/src/io/fsUtils.ts +0 -44
  79. package/src/io/index.ts +0 -11
  80. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +0 -35
  81. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +0 -31
  82. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +0 -25
  83. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +0 -54
  84. package/src/package-verifier/checks/index.ts +0 -11
  85. package/src/package-verifier/index.ts +0 -9
  86. package/src/package-verifier/types.ts +0 -19
  87. 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
- }