@react-native-oh/react-native-harmony-cli 0.0.30 → 0.77.18-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.
- package/LICENSE +21 -0
- package/README.md +4 -4
- package/dist/assetResolver.js +105 -10
- package/dist/autolinking/Autolinking.js +21 -26
- package/dist/autolinking/index.js +0 -6
- package/dist/codegen/core/CodeGenerator.js +0 -6
- package/dist/codegen/core/CodegenError.js +0 -6
- package/dist/codegen/core/EventUtils.js +4 -13
- package/dist/codegen/core/TypeAnnotationToTS.js +3 -9
- package/dist/codegen/core/UberSchema.js +8 -21
- package/dist/codegen/core/index.js +0 -6
- package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +2 -7
- package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +3 -6
- package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +3 -6
- package/dist/codegen/generators/CppComponentCodeGenerator.js +3 -6
- package/dist/codegen/generators/LibGlueCodeGenerator.js +2 -8
- package/dist/codegen/generators/NativeModuleCodeGenerator.js +1 -7
- package/dist/codegen/generators/SharedComponentCodeGenerator.js +0 -6
- package/dist/codegen/generators/UberGeneratorV1.js +0 -6
- package/dist/codegen/generators/UberGeneratorV2.js +0 -6
- package/dist/codegen/generators/index.js +0 -6
- package/dist/codegen/index.js +0 -6
- package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +0 -6
- package/dist/codegen/templates/BasePackageHTemplate.js +12 -7
- package/dist/codegen/templates/ComponentDescriptorHTemplate.js +1 -6
- package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +15 -6
- package/dist/codegen/templates/ComponentUtilsTSTemplate.js +0 -6
- package/dist/codegen/templates/IndexTSTemplate.js +0 -6
- package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +12 -7
- package/dist/codegen/templates/TurboModuleCppTemplate.js +0 -6
- package/dist/codegen/templates/TurboModuleHTemplate.js +0 -6
- package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +0 -6
- package/dist/codegen/templates/index.js +0 -6
- package/dist/commands/bundle-harmony.js +133 -101
- package/dist/commands/clean-harmony.js +84 -0
- package/dist/commands/codegen-harmony.js +12 -22
- package/dist/commands/codegen-lib-harmony.js +15 -25
- package/dist/commands/index.js +3 -9
- package/dist/commands/init-harmony.js +226 -0
- package/dist/commands/init.js +703 -0
- package/dist/commands/link-harmony.js +12 -16
- package/dist/commands/run-harmony.js +438 -0
- package/dist/commands/types.js +2 -0
- package/dist/commands/verify-package-harmony.js +7 -11
- package/dist/core/AbsolutePath.js +3 -6
- package/dist/core/CodegenConfig.js +3 -11
- package/dist/core/DescriptiveError.js +7 -9
- package/dist/core/FileSystem.js +0 -6
- package/dist/core/OHPackageJSON5.js +0 -6
- package/dist/core/PackageJSON.js +4 -7
- package/dist/core/ProjectDependenciesManager.js +6 -14
- package/dist/core/ValidationError.js +0 -6
- package/dist/core/ValueObject.js +0 -6
- package/dist/core/fs.js +16 -10
- package/dist/core/index.js +0 -6
- package/dist/index.js +3 -9
- package/dist/init/templates/AppScopeAppJSON5Template.js +30 -0
- package/dist/init/templates/AppScopeStringVarTemplate.js +28 -0
- package/dist/init/templates/EntryIndexTemplate.js +65 -0
- package/dist/init/templates/EntryStringVarTemplate.js +38 -0
- package/dist/init/templates/HvigorConfigJson5Template.js +44 -0
- package/dist/init/templates/MetroConfigTemplate.js +47 -0
- package/dist/init/templates/OhPackageJson5Template.js +37 -0
- package/dist/init/templates/harmony/AppScope/app.json5 +10 -0
- package/dist/init/templates/harmony/AppScope/resources/base/element/string.json +8 -0
- package/dist/init/templates/harmony/AppScope/resources/base/media/app_icon.png +0 -0
- package/dist/init/templates/harmony/build-profile.json5 +36 -0
- package/dist/init/templates/harmony/codelinter.json +32 -0
- package/dist/init/templates/harmony/entry/build-profile.json5 +18 -0
- package/dist/init/templates/harmony/entry/gitignore +7 -0
- package/dist/init/templates/harmony/entry/hvigorfile.ts +13 -0
- package/dist/init/templates/harmony/entry/oh-package.json5 +9 -0
- package/dist/init/templates/harmony/entry/src/main/cpp/CMakeLists.txt +27 -0
- package/dist/init/templates/harmony/entry/src/main/cpp/PackageProvider.cpp +13 -0
- package/dist/init/templates/harmony/entry/src/main/cpp/gitignore +4 -0
- package/dist/init/templates/harmony/entry/src/main/ets/PackageProvider.ets +8 -0
- package/dist/init/templates/harmony/entry/src/main/ets/codegen/gitignore +2 -0
- package/dist/init/templates/harmony/entry/src/main/ets/entryability/EntryAbility.ets +11 -0
- package/dist/init/templates/harmony/entry/src/main/ets/gitignore +1 -0
- package/dist/init/templates/harmony/entry/src/main/ets/pages/.gitkeep +0 -0
- package/dist/init/templates/harmony/entry/src/main/ets/workers/RNOHWorker.ets +16 -0
- package/dist/init/templates/harmony/entry/src/main/module.json5 +58 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/element/color.json +8 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/element/string.json +16 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/media/background.png +0 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/media/foreground.png +0 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/media/layered_image.json +7 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/media/startIcon.png +0 -0
- package/dist/init/templates/harmony/entry/src/main/resources/base/profile/main_pages.json +5 -0
- package/dist/init/templates/harmony/entry/src/main/resources/gitignore +1 -0
- package/dist/init/templates/harmony/entry/src/ohosTest/ets/test/List.test.ets +2 -0
- package/dist/init/templates/harmony/entry/src/ohosTest/module.json5 +13 -0
- package/dist/init/templates/harmony/gitignore +16 -0
- package/dist/init/templates/harmony/hvigor/hvigor-config.json5 +22 -0
- package/dist/init/templates/harmony/hvigorfile.ts +7 -0
- package/dist/init/templates/index.js +21 -0
- package/dist/io/CliExecutor.js +56 -29
- package/dist/io/Logger.js +71 -47
- package/dist/io/RealFS.js +10 -6
- package/dist/io/TarGz.js +3 -10
- package/dist/io/__fixtures__.js +8 -8
- package/dist/io/fsUtils.js +1 -8
- package/dist/io/index.js +1 -6
- package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +1 -8
- package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +1 -8
- package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +1 -8
- package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +1 -8
- package/dist/package-verifier/checks/index.js +0 -6
- package/dist/package-verifier/index.js +0 -6
- package/dist/package-verifier/types.js +0 -6
- package/dist/package-verifier/verify-package.js +5 -14
- package/harmony/rnoh-hvigor-plugin-0.77.18.tgz +0 -0
- package/jest.config.js +0 -7
- package/package.json +20 -16
- package/react-native.config.js +0 -7
- package/scripts/copy-template.js +18 -0
- package/src/assetResolver.ts +117 -11
- package/src/autolinking/Autolinking.test.ts +54 -18
- package/src/autolinking/Autolinking.ts +32 -31
- package/src/autolinking/index.ts +1 -8
- package/src/codegen/core/CodeGenerator.ts +0 -7
- package/src/codegen/core/CodegenError.ts +0 -7
- package/src/codegen/core/EventUtils.ts +3 -12
- package/src/codegen/core/TypeAnnotationToTS.test.ts +0 -7
- package/src/codegen/core/TypeAnnotationToTS.ts +3 -8
- package/src/codegen/core/UberSchema.ts +9 -22
- package/src/codegen/core/index.ts +0 -7
- package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +5 -8
- package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +3 -7
- package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +3 -7
- package/src/codegen/generators/CppComponentCodeGenerator.ts +3 -7
- package/src/codegen/generators/LibGlueCodeGenerator.ts +2 -9
- package/src/codegen/generators/NativeModuleCodeGenerator.ts +1 -8
- package/src/codegen/generators/SharedComponentCodeGenerator.ts +0 -7
- package/src/codegen/generators/UberGeneratorV1.ts +0 -7
- package/src/codegen/generators/UberGeneratorV2.ts +0 -7
- package/src/codegen/generators/index.ts +0 -7
- package/src/codegen/index.ts +0 -7
- package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +0 -7
- package/src/codegen/templates/BasePackageHTemplate.ts +13 -8
- package/src/codegen/templates/ComponentDescriptorHTemplate.ts +1 -7
- package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +24 -8
- package/src/codegen/templates/ComponentUtilsTSTemplate.ts +0 -7
- package/src/codegen/templates/IndexTSTemplate.ts +0 -7
- package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +13 -8
- package/src/codegen/templates/TurboModuleCppTemplate.ts +0 -7
- package/src/codegen/templates/TurboModuleHTemplate.ts +0 -7
- package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +0 -7
- package/src/codegen/templates/index.ts +0 -7
- package/src/commands/bundle-harmony.ts +178 -118
- package/src/commands/clean-harmony.ts +54 -0
- package/src/commands/codegen-harmony.ts +13 -25
- package/src/commands/codegen-lib-harmony.ts +16 -28
- package/src/commands/index.ts +3 -10
- package/src/commands/init-harmony.ts +299 -0
- package/src/commands/init.ts +755 -0
- package/src/commands/link-harmony.ts +14 -19
- package/src/commands/run-harmony.ts +478 -0
- package/src/commands/types.ts +14 -0
- package/src/commands/verify-package-harmony.ts +9 -12
- package/src/core/AbsolutePath.ts +4 -7
- package/src/core/CodegenConfig.ts +3 -12
- package/src/core/DescriptiveError.ts +8 -15
- package/src/core/FileSystem.ts +5 -8
- package/src/core/OHPackageJSON5.ts +0 -7
- package/src/core/PackageJSON.ts +4 -13
- package/src/core/ProjectDependenciesManager.ts +14 -22
- package/src/core/ValidationError.ts +0 -7
- package/src/core/ValueObject.ts +0 -7
- package/src/core/fs.ts +14 -8
- package/src/core/index.ts +0 -7
- package/src/index.ts +8 -15
- package/src/init/templates/AppScopeAppJSON5Template.ts +24 -0
- package/src/init/templates/AppScopeStringVarTemplate.ts +22 -0
- package/src/init/templates/EntryIndexTemplate.ts +59 -0
- package/src/init/templates/EntryStringVarTemplate.ts +31 -0
- package/src/init/templates/HvigorConfigJson5Template.ts +40 -0
- package/src/init/templates/MetroConfigTemplate.ts +41 -0
- package/src/init/templates/OhPackageJson5Template.ts +31 -0
- package/src/init/templates/harmony/AppScope/app.json5 +10 -0
- package/src/init/templates/harmony/AppScope/resources/base/element/string.json +8 -0
- package/src/init/templates/harmony/AppScope/resources/base/media/app_icon.png +0 -0
- package/src/init/templates/harmony/build-profile.json5 +36 -0
- package/src/init/templates/harmony/codelinter.json +32 -0
- package/src/init/templates/harmony/entry/build-profile.json5 +18 -0
- package/src/init/templates/harmony/entry/gitignore +7 -0
- package/src/init/templates/harmony/entry/hvigorfile.ts +13 -0
- package/src/init/templates/harmony/entry/oh-package.json5 +9 -0
- package/src/init/templates/harmony/entry/src/main/cpp/CMakeLists.txt +27 -0
- package/src/init/templates/harmony/entry/src/main/cpp/PackageProvider.cpp +13 -0
- package/src/init/templates/harmony/entry/src/main/cpp/gitignore +4 -0
- package/src/init/templates/harmony/entry/src/main/ets/PackageProvider.ets +8 -0
- package/src/init/templates/harmony/entry/src/main/ets/codegen/gitignore +2 -0
- package/src/init/templates/harmony/entry/src/main/ets/entryability/EntryAbility.ets +11 -0
- package/src/init/templates/harmony/entry/src/main/ets/gitignore +1 -0
- package/src/init/templates/harmony/entry/src/main/ets/pages/.gitkeep +0 -0
- package/src/init/templates/harmony/entry/src/main/ets/workers/RNOHWorker.ets +16 -0
- package/src/init/templates/harmony/entry/src/main/module.json5 +58 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/element/color.json +8 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/element/string.json +16 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/media/background.png +0 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/media/foreground.png +0 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/media/layered_image.json +7 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/media/startIcon.png +0 -0
- package/src/init/templates/harmony/entry/src/main/resources/base/profile/main_pages.json +5 -0
- package/src/init/templates/harmony/entry/src/main/resources/gitignore +1 -0
- package/src/init/templates/harmony/entry/src/ohosTest/ets/test/List.test.ets +2 -0
- package/src/init/templates/harmony/entry/src/ohosTest/module.json5 +13 -0
- package/src/init/templates/harmony/gitignore +16 -0
- package/src/init/templates/harmony/hvigor/hvigor-config.json5 +22 -0
- package/src/init/templates/harmony/hvigorfile.ts +7 -0
- package/src/init/templates/index.ts +5 -0
- package/src/io/CliExecutor.ts +55 -40
- package/src/io/Logger.ts +77 -51
- package/src/io/RealFS.ts +15 -8
- package/src/io/TarGz.ts +1 -8
- package/src/io/__fixtures__.ts +15 -18
- package/src/io/fsUtils.ts +1 -7
- package/src/io/index.ts +1 -7
- package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +0 -7
- package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +0 -7
- package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +0 -7
- package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +0 -7
- package/src/package-verifier/checks/index.ts +0 -7
- package/src/package-verifier/index.ts +0 -7
- package/src/package-verifier/types.ts +0 -7
- package/src/package-verifier/verify-package.ts +4 -13
- package/tsconfig.build.json +2 -5
- package/tsconfig.json +4 -0
- package/dist/commands/create-metro-host-constants.js +0 -67
- package/dist/commands/pack-harmony.js +0 -106
- package/dist/commands/unpack-harmony.js +0 -191
- package/src/commands/create-metro-host-constants.ts +0 -55
- package/src/commands/pack-harmony.ts +0 -128
- package/src/commands/unpack-harmony.ts +0 -240
|
@@ -1,10 +1,3 @@
|
|
|
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
1
|
import mustache from 'mustache';
|
|
9
2
|
|
|
10
3
|
const TEMPLATE = `
|
|
@@ -28,6 +21,14 @@ class {{name}}JSIBinder : public ViewComponentJSIBinder {
|
|
|
28
21
|
return object;
|
|
29
22
|
}
|
|
30
23
|
|
|
24
|
+
facebook::jsi::Object createCommands(facebook::jsi::Runtime &rt) override {
|
|
25
|
+
auto commands = ViewComponentJSIBinder::createCommands(rt);
|
|
26
|
+
{{#commands}}
|
|
27
|
+
commands.setProperty(rt, "{{name}}", "{{name}}");
|
|
28
|
+
{{/commands}}
|
|
29
|
+
return commands;
|
|
30
|
+
}
|
|
31
|
+
|
|
31
32
|
facebook::jsi::Object createBubblingEventTypes(facebook::jsi::Runtime &rt) override {
|
|
32
33
|
facebook::jsi::Object events(rt);
|
|
33
34
|
{{#bubblingEvents}}
|
|
@@ -52,6 +53,10 @@ type ComponentJSIBinderHProp = {
|
|
|
52
53
|
isObject: boolean;
|
|
53
54
|
};
|
|
54
55
|
|
|
56
|
+
type ComponentJSIBinderHCommand = {
|
|
57
|
+
name: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
55
60
|
type ComponentJSIBinderHEvent = {
|
|
56
61
|
type: 'direct' | 'bubble';
|
|
57
62
|
eventHandlerName: string;
|
|
@@ -59,14 +64,22 @@ type ComponentJSIBinderHEvent = {
|
|
|
59
64
|
|
|
60
65
|
export class ComponentJSIBinderHTemplate {
|
|
61
66
|
private props: ComponentJSIBinderHProp[] = [];
|
|
67
|
+
private commands: ComponentJSIBinderHCommand[] = [];
|
|
62
68
|
private events: ComponentJSIBinderHEvent[] = [];
|
|
63
69
|
|
|
64
|
-
constructor(
|
|
70
|
+
constructor(
|
|
71
|
+
private name: string,
|
|
72
|
+
private codegenNoticeLines: string[]
|
|
73
|
+
) {}
|
|
65
74
|
|
|
66
75
|
addProp(prop: ComponentJSIBinderHProp) {
|
|
67
76
|
this.props.push(prop);
|
|
68
77
|
}
|
|
69
78
|
|
|
79
|
+
addCommand(command: ComponentJSIBinderHCommand) {
|
|
80
|
+
this.commands.push(command);
|
|
81
|
+
}
|
|
82
|
+
|
|
70
83
|
addEvent(event: ComponentJSIBinderHEvent) {
|
|
71
84
|
this.events.push(event);
|
|
72
85
|
}
|
|
@@ -81,6 +94,9 @@ export class ComponentJSIBinderHTemplate {
|
|
|
81
94
|
name: prop.name,
|
|
82
95
|
type: prop.isObject ? '"Object"' : 'true',
|
|
83
96
|
})),
|
|
97
|
+
commands: this.commands.map((command) => ({
|
|
98
|
+
name: command.name,
|
|
99
|
+
})),
|
|
84
100
|
bubblingEvents: this.events
|
|
85
101
|
.filter((event) => event.type === 'bubble')
|
|
86
102
|
.map((event) => ({
|
|
@@ -1,10 +1,3 @@
|
|
|
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
1
|
import mustache from 'mustache';
|
|
9
2
|
|
|
10
3
|
const TEMPLATE = `
|
|
@@ -49,16 +42,26 @@ class GeneratedEventEmitRequestHandler : public EventEmitRequestHandler {
|
|
|
49
42
|
public:
|
|
50
43
|
void handleEvent(Context const &ctx) override {
|
|
51
44
|
auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
|
|
45
|
+
auto componentName = ctx.shadowViewRegistry->getComponentName(ctx.tag);
|
|
46
|
+
|
|
52
47
|
if (eventEmitter == nullptr) {
|
|
53
48
|
return;
|
|
54
49
|
}
|
|
55
50
|
|
|
51
|
+
std::vector<std::string> supportedComponentNames = {
|
|
52
|
+
{{#arkTSComponents}}
|
|
53
|
+
"{{name}}",
|
|
54
|
+
{{/arkTSComponents}}
|
|
55
|
+
};
|
|
56
|
+
|
|
56
57
|
std::vector<std::string> supportedEventNames = {
|
|
57
58
|
{{#supportedEvents}}
|
|
58
59
|
"{{name}}",
|
|
59
60
|
{{/supportedEvents}}
|
|
60
61
|
};
|
|
61
|
-
|
|
62
|
+
|
|
63
|
+
if (std::find(supportedComponentNames.begin(), supportedComponentNames.end(), componentName) != supportedComponentNames.end() &&
|
|
64
|
+
std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
|
|
62
65
|
eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
|
|
63
66
|
}
|
|
64
67
|
}
|
|
@@ -105,6 +108,7 @@ type RNOHGeneratedPackageTurboModule = {
|
|
|
105
108
|
type RNOHGeneratedPackageComponent = {
|
|
106
109
|
name: string;
|
|
107
110
|
supportedEventNames: string[];
|
|
111
|
+
isArkTSComponent: boolean;
|
|
108
112
|
};
|
|
109
113
|
|
|
110
114
|
export class RNOHGeneratedPackageHTemplate {
|
|
@@ -130,6 +134,7 @@ export class RNOHGeneratedPackageHTemplate {
|
|
|
130
134
|
return mustache.render(TEMPLATE.trimStart(), {
|
|
131
135
|
turboModules: this.turboModules,
|
|
132
136
|
components: this.components,
|
|
137
|
+
arkTSComponents: this.components.filter((component) => component.isArkTSComponent),
|
|
133
138
|
supportedEvents: this.supportedEventNames.map((eventName) => ({
|
|
134
139
|
name: eventName,
|
|
135
140
|
})),
|
|
@@ -1,10 +1,3 @@
|
|
|
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
1
|
export * from './TurboModuleHTemplate';
|
|
9
2
|
export * from './TurboModuleCppTemplate';
|
|
10
3
|
export * from './RNOHGeneratedPackageHTemplate';
|
|
@@ -1,27 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 '@react-native-community/cli-types';
|
|
9
|
-
import fs from 'fs';
|
|
1
|
+
import { Command } from './types';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'os';
|
|
10
4
|
import fse from 'fs-extra';
|
|
11
5
|
import Metro from 'metro';
|
|
12
6
|
import { RunBuildOptions as BuildOptions } from 'metro';
|
|
13
7
|
import MetroServer from 'metro/src/Server';
|
|
14
8
|
import pathUtils from 'path';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
9
|
+
import { copyAssets } from '../assetResolver';
|
|
10
|
+
import { execSync } from 'child_process';
|
|
11
|
+
import { Logger } from '../io';
|
|
12
|
+
import { DescriptiveError } from '../core';
|
|
13
|
+
import { randomUUID } from 'node:crypto';
|
|
14
|
+
|
|
15
|
+
type MetroConfig = Awaited<ReturnType<typeof Metro.loadConfig>>;
|
|
17
16
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
'
|
|
17
|
+
const HERMESC_BIN_PATH = {
|
|
18
|
+
darwin: pathUtils.normalize('osx-bin/hermesc'),
|
|
19
|
+
linux: pathUtils.normalize('linux64-bin/hermesc'),
|
|
20
|
+
win32: pathUtils.normalize('win64-bin/hermesc.exe'),
|
|
21
|
+
};
|
|
22
|
+
const HERMESC_PATH_PREFIX = pathUtils.normalize(
|
|
23
|
+
'./node_modules/react-native/sdks/hermesc/'
|
|
24
|
+
);
|
|
25
|
+
const HARMONY_RESOURCE_PATH = pathUtils.normalize(
|
|
26
|
+
'./harmony/entry/src/main/resources/rawfile'
|
|
27
|
+
);
|
|
28
|
+
const ASSETS_DEFAULT_DEST_PATH = pathUtils.normalize(
|
|
29
|
+
'./harmony/entry/src/main/resources/rawfile/assets'
|
|
30
|
+
);
|
|
21
31
|
|
|
22
32
|
type AssetData = Metro.AssetData;
|
|
23
33
|
type Bundle = { code: string; map: string };
|
|
24
34
|
type Path = string;
|
|
35
|
+
type OSType = 'darwin' | 'linux' | 'win32';
|
|
25
36
|
|
|
26
37
|
export const commandBundleHarmony: Command = {
|
|
27
38
|
name: 'bundle-harmony',
|
|
@@ -39,57 +50,131 @@ export const commandBundleHarmony: Command = {
|
|
|
39
50
|
description:
|
|
40
51
|
'Path to the root JS file, either absolute or relative to JS root',
|
|
41
52
|
default: 'index.js',
|
|
53
|
+
parse: (val: string) => pathUtils.normalize(val),
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: '--reset-cache',
|
|
57
|
+
description: 'Removes cached files',
|
|
58
|
+
default: false,
|
|
42
59
|
},
|
|
43
60
|
{
|
|
44
61
|
name: '--config <path>',
|
|
45
62
|
description: 'Path to the Metro configuration file',
|
|
63
|
+
parse: (val: string) => pathUtils.normalize(val),
|
|
46
64
|
},
|
|
47
65
|
{
|
|
48
66
|
name: '--bundle-output <path>',
|
|
67
|
+
description: `File path where to store the resulting bundle (default: "${HARMONY_RESOURCE_PATH}${pathUtils.sep}bundle.harmony.js")`,
|
|
68
|
+
parse: (val: string) => pathUtils.normalize(val),
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: '--js-engine <string>',
|
|
49
72
|
description:
|
|
50
|
-
'
|
|
51
|
-
default:
|
|
73
|
+
'JavaScript engine used to run the bundle. Supported engines: "hermes" and "any" (default: "any"). Setting this option to "hermes" will generate a HBC bundle instead of a JS bundle.',
|
|
74
|
+
default: 'any',
|
|
75
|
+
parse: (val: string) => {
|
|
76
|
+
if (val !== 'any' && val !== 'hermes') {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`Only "any" and "hermes" are supported as JavaScript engines, but "${val}" was provided`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return val;
|
|
82
|
+
},
|
|
52
83
|
},
|
|
53
84
|
{
|
|
54
85
|
name: '--assets-dest <path>',
|
|
55
86
|
description:
|
|
56
87
|
'Directory name where to store assets referenced in the bundle',
|
|
57
88
|
default: ASSETS_DEFAULT_DEST_PATH,
|
|
89
|
+
parse: (val: string) => pathUtils.normalize(val),
|
|
58
90
|
},
|
|
59
91
|
{
|
|
60
92
|
name: '--sourcemap-output <path>',
|
|
61
93
|
description:
|
|
62
94
|
'File name where to store the resulting source map, ex. /tmp/groups.map',
|
|
95
|
+
parse: (val: string) => pathUtils.normalize(val),
|
|
63
96
|
},
|
|
64
97
|
{
|
|
65
98
|
name: '--minify [boolean]',
|
|
66
99
|
description: 'Allows overriding whether bundle is minified',
|
|
67
100
|
parse: (val: string) => val !== 'false',
|
|
68
101
|
},
|
|
102
|
+
{
|
|
103
|
+
name: '--hermesc-dir <path>',
|
|
104
|
+
description:
|
|
105
|
+
'Path to hermesc directory. Relevant when --js-engine set to "hermes".',
|
|
106
|
+
default: HERMESC_PATH_PREFIX,
|
|
107
|
+
parse: (val: string) => pathUtils.normalize(val),
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: '--hermesc-options <string...>',
|
|
111
|
+
description:
|
|
112
|
+
'Additional options to pass to hermesc when generating HBC bundle. Example: --hermesc-options O g reuse-prop-cache. Relevant when --js-engine set to "hermes".',
|
|
113
|
+
parse: (val, prev) => (prev ? [...prev, val] : [val]),
|
|
114
|
+
},
|
|
69
115
|
],
|
|
70
116
|
func: async (argv, config, args: any) => {
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
117
|
+
const logger = new Logger();
|
|
118
|
+
try {
|
|
119
|
+
const defaultBundleName =
|
|
120
|
+
args.jsEngine === 'hermes' ? 'hermes_bundle.hbc' : 'bundle.harmony.js';
|
|
121
|
+
const bundleOutput: string =
|
|
122
|
+
args.bundleOutput ??
|
|
123
|
+
`${HARMONY_RESOURCE_PATH}${pathUtils.sep}${defaultBundleName}`;
|
|
124
|
+
const shouldGenerateHbcBundle = bundleOutput
|
|
125
|
+
.toLowerCase()
|
|
126
|
+
.endsWith('hbc');
|
|
127
|
+
await fse.ensureDir(pathUtils.dirname(bundleOutput));
|
|
128
|
+
const assetsDest: Path = args.assetsDest;
|
|
129
|
+
await fse.ensureDir(assetsDest);
|
|
130
|
+
const buildOptions: BuildOptions = {
|
|
131
|
+
entry: args.entryFile,
|
|
132
|
+
platform: 'harmony',
|
|
133
|
+
minify: args.minify !== undefined ? args.minify : !args.dev,
|
|
134
|
+
dev: args.dev,
|
|
135
|
+
sourceMap: args.sourcemapOutput,
|
|
136
|
+
sourceMapUrl: args.sourcemapOutput,
|
|
137
|
+
};
|
|
138
|
+
const metroConfig = await loadMetroConfig({
|
|
139
|
+
config: args.config,
|
|
140
|
+
resetCache: args.resetCache,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const bundle = await createBundle(metroConfig, buildOptions);
|
|
144
|
+
const assets = await retrieveAssetsData(metroConfig, buildOptions);
|
|
145
|
+
|
|
146
|
+
if (shouldGenerateHbcBundle) {
|
|
147
|
+
await saveHbcBundle(
|
|
148
|
+
logger,
|
|
149
|
+
bundle.code,
|
|
150
|
+
bundleOutput,
|
|
151
|
+
args.hermescDir,
|
|
152
|
+
args.hermescOptions
|
|
153
|
+
);
|
|
154
|
+
} else {
|
|
155
|
+
await saveJSBundle(logger, bundle, bundleOutput);
|
|
156
|
+
}
|
|
157
|
+
maybeSaveSourceMap(logger, bundle, args.sourcemapOutput);
|
|
158
|
+
await copyAssets(logger, assets, assetsDest);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
if (err instanceof DescriptiveError) {
|
|
161
|
+
logger.descriptiveError(err);
|
|
162
|
+
return;
|
|
163
|
+
} else {
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
84
167
|
},
|
|
85
168
|
};
|
|
86
169
|
|
|
87
|
-
async function loadMetroConfig(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
170
|
+
async function loadMetroConfig(options: {
|
|
171
|
+
config: Path | undefined;
|
|
172
|
+
resetCache: boolean;
|
|
173
|
+
}): Promise<MetroConfig> {
|
|
174
|
+
if (!options.config) {
|
|
175
|
+
delete options.config;
|
|
176
|
+
}
|
|
177
|
+
return await Metro.loadConfig(options);
|
|
93
178
|
}
|
|
94
179
|
|
|
95
180
|
async function createBundle(
|
|
@@ -101,17 +186,68 @@ async function createBundle(
|
|
|
101
186
|
return (await Metro.runBuild(metroConfig, buildOptions)) as unknown as Bundle;
|
|
102
187
|
}
|
|
103
188
|
|
|
104
|
-
async function
|
|
105
|
-
|
|
189
|
+
async function saveHbcBundle(
|
|
190
|
+
logger: Logger,
|
|
191
|
+
bundleCode: string,
|
|
106
192
|
bundleOutput: Path,
|
|
107
|
-
|
|
193
|
+
hermescPath: string,
|
|
194
|
+
hermescOptions: string[] | undefined
|
|
195
|
+
) {
|
|
196
|
+
if (
|
|
197
|
+
process.platform !== 'darwin' &&
|
|
198
|
+
process.platform !== 'linux' &&
|
|
199
|
+
process.platform !== 'win32'
|
|
200
|
+
) {
|
|
201
|
+
throw new Error(`Unsupported platform ${process.platform} (⊙︿⊙)`);
|
|
202
|
+
}
|
|
203
|
+
const resolvedHermescPath = pathUtils.resolve(
|
|
204
|
+
hermescPath,
|
|
205
|
+
HERMESC_BIN_PATH[process.platform as OSType]
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
if (!fs.existsSync(resolvedHermescPath)) {
|
|
209
|
+
throw new DescriptiveError({
|
|
210
|
+
whatHappened: `Couldn't find hermesc at ${resolvedHermescPath}`,
|
|
211
|
+
whatCanUserDo: [
|
|
212
|
+
'Find hermesc dir and provide it by using --hermesc-dir argument. hermesc dir should be in react-native package.',
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const tmpBundlePath = `${os.tmpdir()}${pathUtils.sep}b-${randomUUID()}.harmony.js`;
|
|
218
|
+
await fse.writeFile(tmpBundlePath, bundleCode);
|
|
219
|
+
const hbcFilePath = bundleOutput.endsWith('.hbc')
|
|
220
|
+
? bundleOutput
|
|
221
|
+
: pathUtils.join(pathUtils.dirname(bundleOutput), 'hermes_bundle.hbc');
|
|
222
|
+
const hermescOptionsString =
|
|
223
|
+
hermescOptions?.map((e) => `-${e}`).join(' ') || '';
|
|
224
|
+
execSync(
|
|
225
|
+
`${resolvedHermescPath} ${hermescOptionsString} --emit-binary -out ${hbcFilePath} ${tmpBundlePath}`,
|
|
226
|
+
{
|
|
227
|
+
stdio: 'inherit',
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
fs.unlinkSync(tmpBundlePath);
|
|
231
|
+
logger.info((s) => `Created ${hbcFilePath}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function saveJSBundle(
|
|
235
|
+
logger: Logger,
|
|
236
|
+
bundle: Bundle,
|
|
237
|
+
bundleOutput: Path
|
|
108
238
|
) {
|
|
109
|
-
await fse.ensureDir(pathUtils.dirname(bundleOutput));
|
|
110
239
|
fs.writeFileSync(bundleOutput, bundle.code);
|
|
111
|
-
|
|
240
|
+
logger.info((s) => `Created ${bundleOutput}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function maybeSaveSourceMap(
|
|
244
|
+
logger: Logger,
|
|
245
|
+
bundle: Bundle,
|
|
246
|
+
sourceMapOutput: Path | undefined
|
|
247
|
+
) {
|
|
112
248
|
if (sourceMapOutput) {
|
|
113
249
|
fs.writeFileSync(sourceMapOutput, bundle.map);
|
|
114
|
-
|
|
250
|
+
logger.info((s) => `Created ${sourceMapOutput}`);
|
|
115
251
|
}
|
|
116
252
|
}
|
|
117
253
|
|
|
@@ -125,85 +261,9 @@ async function retrieveAssetsData(
|
|
|
125
261
|
...MetroServer.DEFAULT_BUNDLE_OPTIONS,
|
|
126
262
|
...buildOptions,
|
|
127
263
|
entryFile: buildOptions.entry,
|
|
264
|
+
bundleType: 'todo',
|
|
128
265
|
});
|
|
129
266
|
} finally {
|
|
130
267
|
metroServer.end();
|
|
131
268
|
}
|
|
132
269
|
}
|
|
133
|
-
|
|
134
|
-
async function copyAssets(
|
|
135
|
-
assetsData: readonly AssetData[],
|
|
136
|
-
assetsDest: Path
|
|
137
|
-
): Promise<void> {
|
|
138
|
-
if (assetsDest == null) {
|
|
139
|
-
console.warn('Assets destination folder is not set, skipping...');
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
await fse.ensureDir(assetsDest);
|
|
143
|
-
const fileDestBySrc: Record<Path, Path> = {};
|
|
144
|
-
for (const asset of assetsData) {
|
|
145
|
-
const idx = getHighestQualityFileIdx(asset);
|
|
146
|
-
fileDestBySrc[asset.files[idx]] = pathUtils.join(
|
|
147
|
-
assetsDest,
|
|
148
|
-
getAssetDestRelativePath(asset)
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
return copyFiles(fileDestBySrc);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function getHighestQualityFileIdx(assetData: AssetData): number {
|
|
155
|
-
let result = 0;
|
|
156
|
-
let maxScale = -1;
|
|
157
|
-
for (let idx = 0; idx < assetData.scales.length; idx++) {
|
|
158
|
-
const scale = assetData.scales[idx];
|
|
159
|
-
if (scale > maxScale) {
|
|
160
|
-
maxScale = scale;
|
|
161
|
-
result = idx;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return result;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function copyFiles(fileDestBySrc: Record<Path, Path>) {
|
|
168
|
-
const fileSources = Object.keys(fileDestBySrc);
|
|
169
|
-
if (fileSources.length === 0) {
|
|
170
|
-
return Promise.resolve();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
console.info(`Copying ${fileSources.length} asset files`);
|
|
174
|
-
return new Promise<void>((resolve, reject) => {
|
|
175
|
-
const copyNext = (error?: Error) => {
|
|
176
|
-
if (error) {
|
|
177
|
-
reject(error);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
if (fileSources.length === 0) {
|
|
181
|
-
console.info('Done copying assets');
|
|
182
|
-
resolve();
|
|
183
|
-
} else {
|
|
184
|
-
// fileSources.length === 0 is checked in previous branch, so this is string
|
|
185
|
-
const src = fileSources.shift()!;
|
|
186
|
-
const dest = fileDestBySrc[src];
|
|
187
|
-
copyFile(src, dest, copyNext);
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
copyNext();
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function copyFile(
|
|
195
|
-
src: string,
|
|
196
|
-
dest: string,
|
|
197
|
-
onFinished: (error?: Error) => void
|
|
198
|
-
): void {
|
|
199
|
-
const destDir = pathUtils.dirname(dest);
|
|
200
|
-
fs.mkdir(destDir, { recursive: true }, (err?) => {
|
|
201
|
-
if (err) {
|
|
202
|
-
onFinished(err);
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
fs.createReadStream(src)
|
|
206
|
-
.pipe(fs.createWriteStream(dest))
|
|
207
|
-
.on('finish', onFinished);
|
|
208
|
-
});
|
|
209
|
-
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { AbsolutePath, maybeReadFile } from '../core';
|
|
2
|
+
import { Command } from './types';
|
|
3
|
+
import JSON5 from 'json5';
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import * as crypto from 'node:crypto';
|
|
8
|
+
|
|
9
|
+
const COMMAND_NAME = 'clean-harmony';
|
|
10
|
+
export const commandCleanHarmony: Command = {
|
|
11
|
+
name: COMMAND_NAME,
|
|
12
|
+
description: 'Remove cached files in the OpenHarmony project.',
|
|
13
|
+
options: [
|
|
14
|
+
{
|
|
15
|
+
name: "--harmony-project-path <path>",
|
|
16
|
+
description: "Specifies the root of your OpenHarmony project.",
|
|
17
|
+
default: "./harmony"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
func: async (_argv, _config, rawArgs: any) => {
|
|
21
|
+
const harmonyProjectPath = new AbsolutePath(rawArgs.harmonyProjectPath);
|
|
22
|
+
const md5Path = crypto.createHash("md5").update(harmonyProjectPath.getValue()).digest("hex");
|
|
23
|
+
const hvigorCachePath = new AbsolutePath(path.join(os.homedir(), ".hvigor", "project_caches", md5Path));
|
|
24
|
+
|
|
25
|
+
if (fs.existsSync(hvigorCachePath.getValue())) {
|
|
26
|
+
fs.rmSync(hvigorCachePath.getValue(), { recursive: true, force: true });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const buildProfilePath = harmonyProjectPath.copyWithNewSegment('build-profile.json5');
|
|
30
|
+
const buildProfileContent = maybeReadFile(buildProfilePath);
|
|
31
|
+
|
|
32
|
+
if (buildProfileContent) {
|
|
33
|
+
const buildProfile = JSON5.parse(buildProfileContent);
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(buildProfile.modules)) {
|
|
36
|
+
buildProfile.modules.forEach((module: any) => {
|
|
37
|
+
if (module && module.srcPath) {
|
|
38
|
+
const modulePath = harmonyProjectPath.copyWithNewSegment(module.srcPath);
|
|
39
|
+
const cxxPath = modulePath.copyWithNewSegment('.cxx');
|
|
40
|
+
const buildPath = modulePath.copyWithNewSegment('build');
|
|
41
|
+
|
|
42
|
+
if (fs.existsSync(cxxPath.getValue())) {
|
|
43
|
+
fs.rmSync(cxxPath.getValue(), { recursive: true, force: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (fs.existsSync(buildPath.getValue())) {
|
|
47
|
+
fs.rmSync(buildPath.getValue(), { recursive: true, force: true });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|