@react-native-oh/react-native-harmony-cli 0.0.37 → 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 (138) 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 +41 -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 -120
  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 -119
  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 +518 -374
  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 +301 -217
  59. package/jest.config.js +12 -12
  60. package/package.json +78 -70
  61. package/react-native.config.js +17 -17
  62. package/tsconfig.build.json +6 -6
  63. package/tsconfig.json +2 -2
  64. package/.prettierrc +0 -6
  65. package/src/assetResolver.ts +0 -134
  66. package/src/autolinking/Autolinking.test.ts +0 -483
  67. package/src/autolinking/Autolinking.ts +0 -382
  68. package/src/autolinking/index.ts +0 -8
  69. package/src/codegen/core/CodeGenerator.ts +0 -16
  70. package/src/codegen/core/CodegenError.ts +0 -10
  71. package/src/codegen/core/EventUtils.ts +0 -23
  72. package/src/codegen/core/TypeAnnotationToTS.test.ts +0 -82
  73. package/src/codegen/core/TypeAnnotationToTS.ts +0 -195
  74. package/src/codegen/core/UberSchema.ts +0 -152
  75. package/src/codegen/core/index.ts +0 -12
  76. package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +0 -124
  77. package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +0 -128
  78. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +0 -122
  79. package/src/codegen/generators/CppComponentCodeGenerator.ts +0 -95
  80. package/src/codegen/generators/LibGlueCodeGenerator.ts +0 -98
  81. package/src/codegen/generators/NativeModuleCodeGenerator.ts +0 -124
  82. package/src/codegen/generators/SharedComponentCodeGenerator.ts +0 -57
  83. package/src/codegen/generators/UberGeneratorV1.ts +0 -75
  84. package/src/codegen/generators/UberGeneratorV2.ts +0 -114
  85. package/src/codegen/generators/index.ts +0 -15
  86. package/src/codegen/index.ts +0 -9
  87. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +0 -128
  88. package/src/codegen/templates/BasePackageHTemplate.ts +0 -159
  89. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +0 -63
  90. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +0 -121
  91. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +0 -222
  92. package/src/codegen/templates/IndexTSTemplate.ts +0 -62
  93. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +0 -150
  94. package/src/codegen/templates/TurboModuleCppTemplate.ts +0 -61
  95. package/src/codegen/templates/TurboModuleHTemplate.ts +0 -42
  96. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +0 -101
  97. package/src/codegen/templates/index.ts +0 -16
  98. package/src/commands/bundle-harmony.ts +0 -134
  99. package/src/commands/codegen-harmony.ts +0 -297
  100. package/src/commands/codegen-lib-harmony.ts +0 -389
  101. package/src/commands/create-metro-host-constants.ts +0 -55
  102. package/src/commands/index.ts +0 -16
  103. package/src/commands/init.ts +0 -755
  104. package/src/commands/link-harmony.ts +0 -102
  105. package/src/commands/pack-harmony.ts +0 -128
  106. package/src/commands/run-harmony.ts +0 -506
  107. package/src/commands/types.ts +0 -21
  108. package/src/commands/unpack-harmony.ts +0 -238
  109. package/src/commands/verify-package-harmony.ts +0 -113
  110. package/src/core/AbsolutePath.ts +0 -66
  111. package/src/core/CodegenConfig.ts +0 -67
  112. package/src/core/DescriptiveError.ts +0 -68
  113. package/src/core/FileSystem.ts +0 -57
  114. package/src/core/OHPackageJSON5.ts +0 -30
  115. package/src/core/PackageJSON.ts +0 -104
  116. package/src/core/ProjectDependenciesManager.ts +0 -103
  117. package/src/core/ValidationError.ts +0 -8
  118. package/src/core/ValueObject.ts +0 -9
  119. package/src/core/fs.ts +0 -38
  120. package/src/core/index.ts +0 -17
  121. package/src/index.ts +0 -33
  122. package/src/io/CliExecutor.ts +0 -69
  123. package/src/io/Logger.ts +0 -112
  124. package/src/io/RealFS.ts +0 -62
  125. package/src/io/TarGz.ts +0 -33
  126. package/src/io/__fixtures__.ts +0 -139
  127. package/src/io/fsUtils.ts +0 -44
  128. package/src/io/index.ts +0 -11
  129. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +0 -35
  130. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +0 -31
  131. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +0 -25
  132. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +0 -54
  133. package/src/package-verifier/checks/index.ts +0 -11
  134. package/src/package-verifier/index.ts +0 -9
  135. package/src/package-verifier/types.ts +0 -19
  136. package/src/package-verifier/verify-package.ts +0 -123
  137. package/src/simulator/LaunchSimulator.ts +0 -352
  138. package/src/simulator/index.ts +0 -8
@@ -1,62 +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
- {{#reexports}}
18
- export * {{as}}from "{{{from}}}"
19
- {{/reexports}}
20
- `;
21
-
22
- const TEMPLATE_EMPTY = `
23
- /**
24
- {{#codegenNoticeLines}}
25
- * {{{line}}}
26
- {{/codegenNoticeLines}}
27
- */
28
-
29
- export {}
30
- `;
31
-
32
- type Reexport = {
33
- from: string;
34
- as?: string;
35
- };
36
-
37
- export class IndexTSTemplate {
38
- private reexports: Reexport[] = [];
39
-
40
- constructor(private codegenNoticeLines: string[]) {}
41
-
42
- addReexport(reexport: Reexport) {
43
- this.reexports.push(reexport);
44
- return this;
45
- }
46
-
47
- build(): string {
48
- if (this.reexports.length === 0) {
49
- return mustache.render(TEMPLATE_EMPTY, {});
50
- }
51
-
52
- return mustache.render(TEMPLATE.trimStart(), {
53
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
54
- line,
55
- })),
56
- reexports: this.reexports.map((reexport) => ({
57
- from: reexport.from,
58
- as: reexport.as ? `as ${reexport.as} ` : '',
59
- })),
60
- });
61
- }
62
- }
@@ -1,150 +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 "RNOH/Package.h"
23
- #include "RNOH/ArkTSTurboModule.h"
24
- {{#turboModules}}
25
- #include "generated/{{name}}.h"
26
- {{/turboModules}}
27
- {{#components}}
28
- #include "generated/{{name}}ComponentDescriptor.h"
29
- {{/components}}
30
- {{#components}}
31
- #include "generated/{{name}}JSIBinder.h"
32
- {{/components}}
33
-
34
- namespace rnoh {
35
-
36
- class RNOHGeneratedPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
37
- public:
38
- SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
39
- {{#turboModules}}
40
- if (name == "{{name}}") {
41
- return std::make_shared<{{name}}>(ctx, name);
42
- }
43
- {{/turboModules}}
44
- return nullptr;
45
- };
46
- };
47
-
48
- class GeneratedEventEmitRequestHandler : public EventEmitRequestHandler {
49
- public:
50
- void handleEvent(Context const &ctx) override {
51
- auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
52
- auto componentName = ctx.shadowViewRegistry->getComponentName(ctx.tag);
53
-
54
- if (eventEmitter == nullptr) {
55
- return;
56
- }
57
-
58
- std::vector<std::string> supportedComponentNames = {
59
- {{#arkTSComponents}}
60
- "{{name}}",
61
- {{/arkTSComponents}}
62
- };
63
-
64
- std::vector<std::string> supportedEventNames = {
65
- {{#supportedEvents}}
66
- "{{name}}",
67
- {{/supportedEvents}}
68
- };
69
-
70
- if (std::find(supportedComponentNames.begin(), supportedComponentNames.end(), componentName) != supportedComponentNames.end() &&
71
- std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
72
- eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
73
- }
74
- }
75
- };
76
-
77
- class RNOHGeneratedPackage : public Package {
78
- public:
79
- RNOHGeneratedPackage(Package::Context ctx) : Package(ctx){};
80
-
81
- std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
82
- return std::make_unique<RNOHGeneratedPackageTurboModuleFactoryDelegate>();
83
- }
84
-
85
- std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
86
- return {
87
- {{#components}}
88
- facebook::react::concreteComponentDescriptorProvider<facebook::react::{{name}}ComponentDescriptor>(),
89
- {{/components}}
90
- };
91
- }
92
-
93
- ComponentJSIBinderByString createComponentJSIBinderByName() override {
94
- return {
95
- {{#components}}
96
- {"{{name}}", std::make_shared<{{name}}JSIBinder>()},
97
- {{/components}}
98
- };
99
- };
100
-
101
- EventEmitRequestHandlers createEventEmitRequestHandlers() override {
102
- return {
103
- std::make_shared<GeneratedEventEmitRequestHandler>(),
104
- };
105
- }
106
- };
107
-
108
- } // namespace rnoh
109
- `;
110
-
111
- type RNOHGeneratedPackageTurboModule = {
112
- name: string;
113
- };
114
-
115
- type RNOHGeneratedPackageComponent = {
116
- name: string;
117
- supportedEventNames: string[];
118
- isArkTSComponent: boolean;
119
- };
120
-
121
- export class RNOHGeneratedPackageHTemplate {
122
- private turboModules: RNOHGeneratedPackageTurboModule[] = [];
123
- private components: RNOHGeneratedPackageComponent[] = [];
124
- private supportedEventNames: string[] = [];
125
-
126
- constructor() {}
127
-
128
- addTurboModule(turboModule: RNOHGeneratedPackageTurboModule) {
129
- this.turboModules.push(turboModule);
130
- }
131
-
132
- addComponent(component: RNOHGeneratedPackageComponent) {
133
- this.components.push(component);
134
- this.supportedEventNames = [
135
- ...this.supportedEventNames,
136
- ...component.supportedEventNames,
137
- ];
138
- }
139
-
140
- build(): string {
141
- return mustache.render(TEMPLATE.trimStart(), {
142
- turboModules: this.turboModules,
143
- components: this.components,
144
- arkTSComponents: this.components.filter((component) => component.isArkTSComponent),
145
- supportedEvents: this.supportedEventNames.map((eventName) => ({
146
- name: eventName,
147
- })),
148
- });
149
- }
150
- }
@@ -1,61 +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
- #include "{{className}}.h"
18
-
19
- namespace rnoh {
20
- using namespace facebook;
21
-
22
- {{className}}::{{className}}(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) {
23
- methodMap_ = {
24
- {{#methods}}
25
- ARK_{{asyncMarker}}METHOD_METADATA({{name}}, {{argsCount}}),
26
- {{/methods}}
27
- };
28
- }
29
-
30
- } // namespace rnoh
31
- `;
32
-
33
- export type TurboModuleCppTemplateMethod = {
34
- name: string;
35
- isAsync: boolean;
36
- argsCount: number;
37
- };
38
-
39
- export class TurboModuleCppTemplate {
40
- private methods: TurboModuleCppTemplateMethod[] = [];
41
-
42
- constructor(private name: string, private codegenNoticeLines: string[]) {}
43
-
44
- addMethod(method: TurboModuleCppTemplateMethod) {
45
- this.methods.push(method);
46
- }
47
-
48
- build(): string {
49
- return mustache.render(TEMPLATE.trimStart(), {
50
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
51
- line,
52
- })),
53
- className: this.name,
54
- methods: this.methods.map((method) => ({
55
- name: method.name,
56
- argsCount: method.argsCount,
57
- asyncMarker: method.isAsync ? 'ASYNC_' : undefined,
58
- })),
59
- });
60
- }
61
- }
@@ -1,42 +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
- #include "RNOH/ArkTSTurboModule.h"
20
-
21
- namespace rnoh {
22
-
23
- class JSI_EXPORT {{className}} : public ArkTSTurboModule {
24
- public:
25
- {{className}}(const ArkTSTurboModule::Context ctx, const std::string name);
26
- };
27
-
28
- } // namespace rnoh
29
- `;
30
-
31
- export class TurboModuleHTemplate {
32
- constructor(private name: string, private codegenNoticeLines: string[]) {}
33
-
34
- build(): string {
35
- return mustache.render(TEMPLATE.trimStart(), {
36
- className: this.name,
37
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
38
- line,
39
- })),
40
- });
41
- }
42
- }
@@ -1,101 +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 { Tag } from "{{{rnohImport}}}"
18
-
19
- export namespace {{name}} {
20
- export const NAME = '{{name}}' as const
21
-
22
- {{#enums}}
23
- export enum {{name}} {
24
- {{#members}}
25
- {{name}} = {{{value}}},
26
- {{/members}}
27
- }
28
-
29
- {{/enums}}
30
- {{#aliases}}
31
- export type {{name}} = {{{type}}}
32
-
33
- {{/aliases}}
34
- export interface Spec {
35
- {{#methods}}
36
- {{name}}({{{stringifiedArgs}}}): {{{returnType}}};
37
-
38
- {{/methods}}
39
- }
40
- }
41
- `;
42
-
43
- type Alias = {
44
- name: string;
45
- type: string;
46
- };
47
-
48
- type EnumModel = {
49
- name: string;
50
- members: {
51
- name: string;
52
- value: string;
53
- }[];
54
- };
55
-
56
- type Method = {
57
- name: string;
58
- stringifiedArgs: string;
59
- returnType: string;
60
- };
61
-
62
- export class TurboModuleInterfaceTS {
63
- private methods: Method[] = [];
64
- private aliases: Alias[] = [];
65
- private enumModels: EnumModel[] = [];
66
-
67
- constructor(
68
- private name: string,
69
- private codegenNoticeLines: string[],
70
- private rnohImport: string
71
- ) {}
72
-
73
- addAlias(alias: Alias) {
74
- this.aliases.push(alias);
75
- }
76
-
77
- addEnum(enumModel: EnumModel) {
78
- this.enumModels.push(enumModel);
79
- }
80
-
81
- addMethod(method: Method) {
82
- this.methods.push(method);
83
- }
84
-
85
- build(): string {
86
- return mustache.render(TEMPLATE.trimStart(), {
87
- codegenNoticeLines: this.codegenNoticeLines.map((line) => ({
88
- line,
89
- })),
90
- name: this.name,
91
- aliases: this.aliases,
92
- enums: this.enumModels,
93
- rnohImport: this.rnohImport,
94
- methods: this.methods.map((method) => ({
95
- name: method.name,
96
- stringifiedArgs: method.stringifiedArgs,
97
- returnType: method.returnType,
98
- })),
99
- });
100
- }
101
- }
@@ -1,16 +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 './TurboModuleHTemplate';
9
- export * from './TurboModuleCppTemplate';
10
- export * from './RNOHGeneratedPackageHTemplate';
11
- export * from './ComponentJSIBinderHTemplate';
12
- export * from './ComponentDescriptorHTemplate';
13
- export * from './ComponentUtilsTSTemplate';
14
- export * from './IndexTSTemplate';
15
- export * from './TurboModuleSpecTSTemplate';
16
- export * from './BaseComponentInstanceHTemplate';
@@ -1,134 +0,0 @@
1
- /**
2
- * Copyright (c) 2024-2025 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 { Command } from './types';
9
- import fs from 'fs';
10
- import fse from 'fs-extra';
11
- import Metro from 'metro';
12
- import { RunBuildOptions as BuildOptions } from 'metro';
13
- import MetroServer from 'metro/src/Server';
14
- import pathUtils from 'path';
15
- import { copyAssets } from '../assetResolver';
16
- import { ConfigT as MetroConfig } from 'metro-config';
17
- import { Logger } from '../io';
18
-
19
- const ARK_RESOURCE_PATH = './harmony/entry/src/main/resources/rawfile';
20
- const ASSETS_DEFAULT_DEST_PATH =
21
- './harmony/entry/src/main/resources/rawfile/assets';
22
-
23
- type AssetData = Metro.AssetData;
24
- type Bundle = { code: string; map: string };
25
- type Path = string;
26
-
27
- export const commandBundleHarmony: Command = {
28
- name: 'bundle-harmony',
29
- description:
30
- 'Creates JS bundle, creates a special cpp header containing the JS code, copies assets directory to the project.',
31
- options: [
32
- {
33
- name: '--dev [boolean]',
34
- description: 'If false, warnings are disabled and the bundle is minified',
35
- parse: (val: string) => val !== 'false',
36
- default: true,
37
- },
38
- {
39
- name: '--entry-file <path>',
40
- description:
41
- 'Path to the root JS file, either absolute or relative to JS root',
42
- default: 'index.js',
43
- },
44
- {
45
- name: '--config <path>',
46
- description: 'Path to the Metro configuration file',
47
- },
48
- {
49
- name: '--bundle-output <path>',
50
- description:
51
- 'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
52
- default: ARK_RESOURCE_PATH + '/bundle.harmony.js',
53
- },
54
- {
55
- name: '--assets-dest <path>',
56
- description:
57
- 'Directory name where to store assets referenced in the bundle',
58
- default: ASSETS_DEFAULT_DEST_PATH,
59
- },
60
- {
61
- name: '--sourcemap-output <path>',
62
- description:
63
- 'File name where to store the resulting source map, ex. /tmp/groups.map',
64
- },
65
- {
66
- name: '--minify [boolean]',
67
- description: 'Allows overriding whether bundle is minified',
68
- parse: (val: string) => val !== 'false',
69
- },
70
- ],
71
- func: async (argv, config, args: any) => {
72
- const logger = new Logger();
73
- const buildOptions: BuildOptions = {
74
- entry: args.entryFile,
75
- platform: 'harmony',
76
- minify: args.minify !== undefined ? args.minify : !args.dev,
77
- dev: args.dev,
78
- sourceMap: args.sourcemapOutput,
79
- sourceMapUrl: args.sourcemapOutput,
80
- };
81
- const metroConfig = await loadMetroConfig(args.config);
82
- const bundle = await createBundle(metroConfig, buildOptions);
83
- await saveBundle(bundle, args.bundleOutput, args.sourcemapOutput);
84
- const assets = await retrieveAssetsData(metroConfig, buildOptions);
85
- copyAssets(logger, assets, args.assetsDest);
86
- },
87
- };
88
-
89
- async function loadMetroConfig(
90
- configPath: Path | undefined
91
- ): Promise<MetroConfig> {
92
- return configPath
93
- ? await Metro.loadConfig({ config: configPath })
94
- : await Metro.loadConfig();
95
- }
96
-
97
- async function createBundle(
98
- metroConfig: MetroConfig,
99
- buildOptions: BuildOptions
100
- ): Promise<Bundle> {
101
- // casting needed as Metro.runBuild returns Promise<{code: string, map: string}>
102
- // despite being typed as Promise<void>
103
- return (await Metro.runBuild(metroConfig, buildOptions)) as unknown as Bundle;
104
- }
105
-
106
- async function saveBundle(
107
- bundle: Bundle,
108
- bundleOutput: Path,
109
- sourceMapOutput: Path | undefined
110
- ) {
111
- await fse.ensureDir(pathUtils.dirname(bundleOutput));
112
- fs.writeFileSync(bundleOutput, bundle.code);
113
- console.log(`[CREATED] ${bundleOutput}`);
114
- if (sourceMapOutput) {
115
- fs.writeFileSync(sourceMapOutput, bundle.map);
116
- console.log(`[CREATED] ${sourceMapOutput}`);
117
- }
118
- }
119
-
120
- async function retrieveAssetsData(
121
- metroConfig: MetroConfig,
122
- buildOptions: BuildOptions
123
- ): Promise<readonly AssetData[]> {
124
- const metroServer = new MetroServer(metroConfig);
125
- try {
126
- return await metroServer.getAssets({
127
- ...MetroServer.DEFAULT_BUNDLE_OPTIONS,
128
- ...buildOptions,
129
- entryFile: buildOptions.entry,
130
- });
131
- } finally {
132
- metroServer.end();
133
- }
134
- }