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

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 (228) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -4
  3. package/dist/autolinking/Autolinking.js +13 -16
  4. package/dist/autolinking/index.js +0 -6
  5. package/dist/codegen/core/CodeGenerator.js +0 -6
  6. package/dist/codegen/core/CodegenError.js +0 -6
  7. package/dist/codegen/core/EventUtils.js +0 -6
  8. package/dist/codegen/core/TypeAnnotationToTS.js +0 -6
  9. package/dist/codegen/core/UberSchema.js +2 -11
  10. package/dist/codegen/core/index.js +0 -6
  11. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +2 -7
  12. package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +0 -6
  13. package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +0 -6
  14. package/dist/codegen/generators/CppComponentCodeGenerator.js +0 -6
  15. package/dist/codegen/generators/LibGlueCodeGenerator.js +2 -8
  16. package/dist/codegen/generators/NativeModuleCodeGenerator.js +1 -7
  17. package/dist/codegen/generators/SharedComponentCodeGenerator.js +0 -6
  18. package/dist/codegen/generators/UberGeneratorV1.js +0 -6
  19. package/dist/codegen/generators/UberGeneratorV2.js +0 -6
  20. package/dist/codegen/generators/index.js +0 -6
  21. package/dist/codegen/index.js +0 -6
  22. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +0 -6
  23. package/dist/codegen/templates/BasePackageHTemplate.js +12 -7
  24. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +1 -6
  25. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +0 -6
  26. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +0 -6
  27. package/dist/codegen/templates/IndexTSTemplate.js +0 -6
  28. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +12 -7
  29. package/dist/codegen/templates/TurboModuleCppTemplate.js +0 -6
  30. package/dist/codegen/templates/TurboModuleHTemplate.js +0 -6
  31. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +0 -6
  32. package/dist/codegen/templates/index.js +0 -6
  33. package/dist/commands/bundle-harmony.js +131 -36
  34. package/dist/commands/clean-harmony.js +84 -0
  35. package/dist/commands/codegen-harmony.js +0 -6
  36. package/dist/commands/codegen-lib-harmony.js +6 -12
  37. package/dist/commands/index.js +2 -9
  38. package/dist/commands/init-harmony.js +226 -0
  39. package/dist/commands/init.js +0 -0
  40. package/dist/commands/link-harmony.js +12 -16
  41. package/dist/commands/types.js +0 -6
  42. package/dist/commands/verify-package-harmony.js +6 -10
  43. package/dist/core/AbsolutePath.js +3 -6
  44. package/dist/core/CodegenConfig.js +0 -6
  45. package/dist/core/DescriptiveError.js +0 -6
  46. package/dist/core/FileSystem.js +0 -6
  47. package/dist/core/OHPackageJSON5.js +0 -6
  48. package/dist/core/ProjectDependenciesManager.js +6 -14
  49. package/dist/core/ValidationError.js +0 -6
  50. package/dist/core/ValueObject.js +0 -6
  51. package/dist/core/fs.js +13 -6
  52. package/dist/core/index.js +0 -6
  53. package/dist/index.js +2 -9
  54. package/dist/init/templates/AppScopeAppJSON5Template.js +30 -0
  55. package/dist/init/templates/AppScopeStringVarTemplate.js +28 -0
  56. package/dist/init/templates/EntryIndexTemplate.js +65 -0
  57. package/dist/init/templates/EntryStringVarTemplate.js +38 -0
  58. package/dist/init/templates/HvigorConfigJson5Template.js +44 -0
  59. package/dist/init/templates/MetroConfigTemplate.js +47 -0
  60. package/dist/init/templates/OhPackageJson5Template.js +37 -0
  61. package/dist/init/templates/harmony/AppScope/app.json5 +10 -0
  62. package/dist/init/templates/harmony/AppScope/resources/base/element/string.json +8 -0
  63. package/dist/init/templates/harmony/AppScope/resources/base/media/app_icon.png +0 -0
  64. package/dist/init/templates/harmony/build-profile.json5 +36 -0
  65. package/dist/init/templates/harmony/codelinter.json +32 -0
  66. package/dist/init/templates/harmony/entry/build-profile.json5 +18 -0
  67. package/dist/init/templates/harmony/entry/gitignore +7 -0
  68. package/dist/init/templates/harmony/entry/hvigorfile.ts +13 -0
  69. package/dist/init/templates/harmony/entry/oh-package.json5 +9 -0
  70. package/dist/init/templates/harmony/entry/src/main/cpp/CMakeLists.txt +27 -0
  71. package/dist/init/templates/harmony/entry/src/main/cpp/PackageProvider.cpp +13 -0
  72. package/dist/init/templates/harmony/entry/src/main/cpp/gitignore +4 -0
  73. package/dist/init/templates/harmony/entry/src/main/ets/PackageProvider.ets +8 -0
  74. package/dist/init/templates/harmony/entry/src/main/ets/codegen/gitignore +2 -0
  75. package/dist/init/templates/harmony/entry/src/main/ets/entryability/EntryAbility.ets +11 -0
  76. package/dist/init/templates/harmony/entry/src/main/ets/gitignore +1 -0
  77. package/dist/init/templates/harmony/entry/src/main/ets/pages/.gitkeep +0 -0
  78. package/dist/init/templates/harmony/entry/src/main/ets/workers/RNOHWorker.ets +16 -0
  79. package/dist/init/templates/harmony/entry/src/main/module.json5 +58 -0
  80. package/dist/init/templates/harmony/entry/src/main/resources/base/element/color.json +8 -0
  81. package/dist/init/templates/harmony/entry/src/main/resources/base/element/string.json +16 -0
  82. package/dist/init/templates/harmony/entry/src/main/resources/base/media/background.png +0 -0
  83. package/dist/init/templates/harmony/entry/src/main/resources/base/media/foreground.png +0 -0
  84. package/dist/init/templates/harmony/entry/src/main/resources/base/media/layered_image.json +7 -0
  85. package/dist/init/templates/harmony/entry/src/main/resources/base/media/startIcon.png +0 -0
  86. package/dist/init/templates/harmony/entry/src/main/resources/base/profile/main_pages.json +5 -0
  87. package/dist/init/templates/harmony/entry/src/main/resources/gitignore +1 -0
  88. package/dist/init/templates/harmony/entry/src/ohosTest/ets/test/List.test.ets +2 -0
  89. package/dist/init/templates/harmony/entry/src/ohosTest/module.json5 +13 -0
  90. package/dist/init/templates/harmony/gitignore +16 -0
  91. package/dist/init/templates/harmony/hvigor/hvigor-config.json5 +22 -0
  92. package/dist/init/templates/harmony/hvigorfile.ts +7 -0
  93. package/dist/init/templates/index.js +21 -0
  94. package/dist/io/CliExecutor.js +0 -6
  95. package/dist/io/Logger.js +0 -6
  96. package/dist/io/RealFS.js +10 -6
  97. package/dist/io/TarGz.js +2 -8
  98. package/dist/io/__fixtures__.js +0 -6
  99. package/dist/io/fsUtils.js +0 -6
  100. package/dist/io/index.js +1 -6
  101. package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +0 -6
  102. package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +0 -6
  103. package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +0 -6
  104. package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +0 -6
  105. package/dist/package-verifier/checks/index.js +0 -6
  106. package/dist/package-verifier/index.js +0 -6
  107. package/dist/package-verifier/types.js +0 -6
  108. package/dist/package-verifier/verify-package.js +1 -7
  109. package/jest.config.js +0 -7
  110. package/package.json +15 -18
  111. package/react-native.config.js +0 -7
  112. package/scripts/copy-template.js +18 -0
  113. package/src/autolinking/Autolinking.test.ts +54 -18
  114. package/src/autolinking/Autolinking.ts +23 -20
  115. package/src/autolinking/index.ts +1 -8
  116. package/src/codegen/core/CodeGenerator.ts +0 -7
  117. package/src/codegen/core/CodegenError.ts +0 -7
  118. package/src/codegen/core/EventUtils.ts +0 -7
  119. package/src/codegen/core/TypeAnnotationToTS.test.ts +0 -7
  120. package/src/codegen/core/TypeAnnotationToTS.ts +3 -8
  121. package/src/codegen/core/UberSchema.ts +3 -12
  122. package/src/codegen/core/index.ts +0 -7
  123. package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +5 -8
  124. package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +0 -7
  125. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +0 -7
  126. package/src/codegen/generators/CppComponentCodeGenerator.ts +0 -7
  127. package/src/codegen/generators/LibGlueCodeGenerator.ts +2 -9
  128. package/src/codegen/generators/NativeModuleCodeGenerator.ts +1 -8
  129. package/src/codegen/generators/SharedComponentCodeGenerator.ts +0 -7
  130. package/src/codegen/generators/UberGeneratorV1.ts +0 -7
  131. package/src/codegen/generators/UberGeneratorV2.ts +0 -7
  132. package/src/codegen/generators/index.ts +0 -7
  133. package/src/codegen/index.ts +0 -7
  134. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +0 -7
  135. package/src/codegen/templates/BasePackageHTemplate.ts +13 -8
  136. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +1 -7
  137. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +0 -7
  138. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +0 -7
  139. package/src/codegen/templates/IndexTSTemplate.ts +0 -7
  140. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +13 -8
  141. package/src/codegen/templates/TurboModuleCppTemplate.ts +0 -7
  142. package/src/codegen/templates/TurboModuleHTemplate.ts +0 -7
  143. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +0 -7
  144. package/src/codegen/templates/index.ts +0 -7
  145. package/src/commands/bundle-harmony.ts +174 -39
  146. package/src/commands/clean-harmony.ts +54 -0
  147. package/src/commands/codegen-harmony.ts +0 -7
  148. package/src/commands/codegen-lib-harmony.ts +6 -13
  149. package/src/commands/index.ts +2 -10
  150. package/src/commands/init-harmony.ts +299 -0
  151. package/src/commands/link-harmony.ts +13 -18
  152. package/src/commands/types.ts +0 -7
  153. package/src/commands/verify-package-harmony.ts +7 -10
  154. package/src/core/AbsolutePath.ts +4 -7
  155. package/src/core/CodegenConfig.ts +0 -7
  156. package/src/core/DescriptiveError.ts +0 -7
  157. package/src/core/FileSystem.ts +5 -8
  158. package/src/core/OHPackageJSON5.ts +0 -7
  159. package/src/core/PackageJSON.ts +0 -6
  160. package/src/core/ProjectDependenciesManager.ts +14 -22
  161. package/src/core/ValidationError.ts +0 -7
  162. package/src/core/ValueObject.ts +0 -7
  163. package/src/core/fs.ts +13 -7
  164. package/src/core/index.ts +0 -7
  165. package/src/index.ts +4 -13
  166. package/src/init/templates/AppScopeAppJSON5Template.ts +24 -0
  167. package/src/init/templates/AppScopeStringVarTemplate.ts +22 -0
  168. package/src/init/templates/EntryIndexTemplate.ts +59 -0
  169. package/src/init/templates/EntryStringVarTemplate.ts +31 -0
  170. package/src/init/templates/HvigorConfigJson5Template.ts +40 -0
  171. package/src/init/templates/MetroConfigTemplate.ts +41 -0
  172. package/src/init/templates/OhPackageJson5Template.ts +31 -0
  173. package/src/init/templates/harmony/AppScope/app.json5 +10 -0
  174. package/src/init/templates/harmony/AppScope/resources/base/element/string.json +8 -0
  175. package/src/init/templates/harmony/AppScope/resources/base/media/app_icon.png +0 -0
  176. package/src/init/templates/harmony/build-profile.json5 +36 -0
  177. package/src/init/templates/harmony/codelinter.json +32 -0
  178. package/src/init/templates/harmony/entry/build-profile.json5 +18 -0
  179. package/src/init/templates/harmony/entry/gitignore +7 -0
  180. package/src/init/templates/harmony/entry/hvigorfile.ts +13 -0
  181. package/src/init/templates/harmony/entry/oh-package.json5 +9 -0
  182. package/src/init/templates/harmony/entry/src/main/cpp/CMakeLists.txt +27 -0
  183. package/src/init/templates/harmony/entry/src/main/cpp/PackageProvider.cpp +13 -0
  184. package/src/init/templates/harmony/entry/src/main/cpp/gitignore +4 -0
  185. package/src/init/templates/harmony/entry/src/main/ets/PackageProvider.ets +8 -0
  186. package/src/init/templates/harmony/entry/src/main/ets/codegen/gitignore +2 -0
  187. package/src/init/templates/harmony/entry/src/main/ets/entryability/EntryAbility.ets +11 -0
  188. package/src/init/templates/harmony/entry/src/main/ets/gitignore +1 -0
  189. package/src/init/templates/harmony/entry/src/main/ets/pages/.gitkeep +0 -0
  190. package/src/init/templates/harmony/entry/src/main/ets/workers/RNOHWorker.ets +16 -0
  191. package/src/init/templates/harmony/entry/src/main/module.json5 +58 -0
  192. package/src/init/templates/harmony/entry/src/main/resources/base/element/color.json +8 -0
  193. package/src/init/templates/harmony/entry/src/main/resources/base/element/string.json +16 -0
  194. package/src/init/templates/harmony/entry/src/main/resources/base/media/background.png +0 -0
  195. package/src/init/templates/harmony/entry/src/main/resources/base/media/foreground.png +0 -0
  196. package/src/init/templates/harmony/entry/src/main/resources/base/media/layered_image.json +7 -0
  197. package/src/init/templates/harmony/entry/src/main/resources/base/media/startIcon.png +0 -0
  198. package/src/init/templates/harmony/entry/src/main/resources/base/profile/main_pages.json +5 -0
  199. package/src/init/templates/harmony/entry/src/main/resources/gitignore +1 -0
  200. package/src/init/templates/harmony/entry/src/ohosTest/ets/test/List.test.ets +2 -0
  201. package/src/init/templates/harmony/entry/src/ohosTest/module.json5 +13 -0
  202. package/src/init/templates/harmony/gitignore +16 -0
  203. package/src/init/templates/harmony/hvigor/hvigor-config.json5 +22 -0
  204. package/src/init/templates/harmony/hvigorfile.ts +7 -0
  205. package/src/init/templates/index.ts +5 -0
  206. package/src/io/CliExecutor.ts +1 -8
  207. package/src/io/Logger.ts +0 -7
  208. package/src/io/RealFS.ts +15 -8
  209. package/src/io/TarGz.ts +1 -8
  210. package/src/io/__fixtures__.ts +1 -8
  211. package/src/io/fsUtils.ts +1 -7
  212. package/src/io/index.ts +1 -7
  213. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +0 -7
  214. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +0 -7
  215. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +0 -7
  216. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +0 -7
  217. package/src/package-verifier/checks/index.ts +0 -7
  218. package/src/package-verifier/index.ts +0 -7
  219. package/src/package-verifier/types.ts +0 -7
  220. package/src/package-verifier/verify-package.ts +0 -7
  221. package/tsconfig.build.json +2 -5
  222. package/tsconfig.json +4 -0
  223. package/dist/commands/create-metro-host-constants.js +0 -67
  224. package/dist/commands/pack-harmony.js +0 -106
  225. package/dist/commands/unpack-harmony.js +0 -189
  226. package/src/commands/create-metro-host-constants.ts +0 -55
  227. package/src/commands/pack-harmony.ts +0 -128
  228. package/src/commands/unpack-harmony.ts +0 -238
@@ -1,55 +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 os from 'node:os';
10
- import fs from 'node:fs';
11
- import pathUtils from 'node:path';
12
-
13
- export const commandCreateMetroConstantsHarmony: Command = {
14
- name: 'create-metro-host-constants-harmony',
15
- description:
16
- 'Creates a Typescript file that exports constant with IP of the host machine. That IP is later used to connect to the Metro server from a physical device.',
17
- options: [
18
- {
19
- name: '--output <string>',
20
- description: 'Output file',
21
- default: './harmony/entry/src/main/ets/codegen/MetroHostConstants.ts',
22
- },
23
- ],
24
- func: async (argv, config, args: any) => {
25
- createMetroHostConstants(pathUtils.resolve(args.output));
26
- },
27
- };
28
-
29
- function createMetroHostConstants(outputPath: string): void {
30
- const networkInterfacesByName = os.networkInterfaces();
31
- const getIP = () => {
32
- for (let name of Object.keys(networkInterfacesByName)) {
33
- for (let net of networkInterfacesByName[name] ?? []) {
34
- // skip over non-ipv4 and internal (i.e. 127.0.0.1) addresses
35
- if (net.family === 'IPv4' && !net.internal) {
36
- return net.address;
37
- }
38
- }
39
- }
40
- return;
41
- };
42
- const ip = getIP();
43
- const tsCode = [
44
- '// This file is autogenerated. Do not modify it manually.',
45
- '',
46
- `export const METRO_SERVER_IP = '${ip}';\n`,
47
- ];
48
- fs.writeFile(outputPath, tsCode.join('\n'), (err) => {
49
- if (err) {
50
- console.error(`Error writing to ${outputPath}:`, err);
51
- } else {
52
- console.log(`Created file at ${outputPath}`);
53
- }
54
- });
55
- }
@@ -1,128 +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 { globSync } from 'glob';
10
- import pathUtils from 'path';
11
- import tar from 'tar';
12
- import fs from 'fs';
13
- import { ValidationError } from '../core';
14
- import ignore, { Ignore } from 'ignore';
15
- import JSON5 from 'json5';
16
-
17
- export const commandPackHarmony: Command = {
18
- name: 'pack-harmony',
19
- description: 'Pack native code. Used for package development.',
20
- options: [
21
- {
22
- name: '--oh-module-path <string>',
23
- description: 'Path to the OH module',
24
- },
25
- {
26
- name: '--harmony-dir-path <string>',
27
- description: 'Path to the harmony dir',
28
- },
29
- {
30
- name: '--package-json-path <string>',
31
- description: 'Path to the package.json',
32
- },
33
- ],
34
- func: async (argv, config, args: any) => {
35
- try {
36
- await pack(
37
- pathUtils.resolve(args.harmonyDirPath),
38
- pathUtils.resolve(args.ohModulePath),
39
- pathUtils.resolve(args.packageJsonPath)
40
- );
41
- } catch (err) {
42
- if (err instanceof ValidationError) {
43
- console.error('[ERROR]', err.message);
44
- process.exit(1);
45
- } else {
46
- throw err;
47
- }
48
- }
49
- },
50
- };
51
-
52
- async function pack(
53
- harmonyDirPath: string,
54
- ohModuleDirPath: string,
55
- packageJSONPath: string
56
- ) {
57
- syncVersions(
58
- packageJSONPath,
59
- pathUtils.join(ohModuleDirPath, 'oh-package.json5')
60
- );
61
- if (!fs.existsSync(harmonyDirPath)) {
62
- fs.mkdirSync(harmonyDirPath, { recursive: true });
63
- }
64
- await createTGZFromDir(
65
- ohModuleDirPath,
66
- pathUtils.join(
67
- harmonyDirPath,
68
- `${pathUtils.parse(ohModuleDirPath).name}.tar.gz`
69
- )
70
- );
71
- await createTGZFromDir(
72
- harmonyDirPath,
73
- pathUtils.join(pathUtils.dirname(harmonyDirPath), 'harmony.tar.gz')
74
- );
75
- }
76
-
77
- function syncVersions(
78
- packageJSONFilePath: string,
79
- ohPackageJSONFilePath: string
80
- ) {
81
- const packageJSONFileContent = JSON.parse(
82
- fs.readFileSync(packageJSONFilePath, { encoding: 'utf-8' })
83
- );
84
- if (fs.existsSync(ohPackageJSONFilePath)) {
85
- const ohPackageJSONFileContent = JSON5.parse(
86
- fs.readFileSync(ohPackageJSONFilePath, { encoding: 'utf-8' })
87
- );
88
- ohPackageJSONFileContent['version'] = packageJSONFileContent['version'];
89
- fs.writeFileSync(
90
- ohPackageJSONFilePath,
91
- JSON5.stringify(ohPackageJSONFileContent, null, 2)
92
- );
93
- }
94
- }
95
-
96
- async function createTGZFromDir(dirPath: string, outputFilePath: string) {
97
- const ig = getIgnoreUtil(pathUtils.join(dirPath, '.tarignore'));
98
- const filesToInclude = globSync('**', {
99
- cwd: dirPath,
100
- nodir: true,
101
- }).filter((path) => !ig.ignores(path));
102
- await createArchive(outputFilePath, dirPath, filesToInclude);
103
- }
104
-
105
- function getIgnoreUtil(tarignorePath: string): Ignore {
106
- if (!fs.existsSync(tarignorePath)) {
107
- return ignore();
108
- }
109
- return ignore().add(fs.readFileSync(tarignorePath).toString());
110
- }
111
-
112
- async function createArchive(
113
- outputFilePath: string,
114
- packageDirPath: string,
115
- filesToInclude: string[]
116
- ) {
117
- if (filesToInclude.length === 0) {
118
- throw new ValidationError('Nothing to pack');
119
- }
120
- await tar.c(
121
- {
122
- gzip: true,
123
- file: outputFilePath,
124
- cwd: packageDirPath,
125
- },
126
- filesToInclude
127
- );
128
- }
@@ -1,238 +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 { Logger, RealFS } from '../io';
10
-
11
- import pathUtils from 'path';
12
- import fs from 'fs';
13
- import zlib from 'zlib';
14
- import tar from 'tar';
15
- import JSON5 from 'json5';
16
- import {
17
- ValidationError,
18
- AbsolutePath,
19
- ProjectDependenciesManager,
20
- } from '../core';
21
-
22
- export const commandUnpackHarmony: Command = {
23
- name: 'unpack-harmony',
24
- description: 'Unpacks native code.',
25
- options: [
26
- {
27
- name: '--project-root-path <path>',
28
- description: 'Relative path to package root',
29
- default: '.',
30
- },
31
- {
32
- name: '--output-dir <string>',
33
- description: 'Output directory to which OH modules should be unpacked to',
34
- default: './harmony/rnoh_modules',
35
- },
36
- ],
37
- func: async (argv, config, args: any) => {
38
- try {
39
- await unpack(
40
- new AbsolutePath(args.projectRootPath),
41
- new AbsolutePath(args.outputDir)
42
- );
43
- } catch (err) {
44
- if (err instanceof ValidationError) {
45
- console.error('[ERROR]', err.message);
46
- process.exit(1);
47
- } else {
48
- throw err;
49
- }
50
- }
51
- },
52
- };
53
-
54
- async function unpack(
55
- projectRootPath: AbsolutePath,
56
- outputDirPath: AbsolutePath
57
- ) {
58
- const logger = new Logger();
59
- const fs = new RealFS();
60
- const projectDependenciesManager = new ProjectDependenciesManager(
61
- fs,
62
- projectRootPath
63
- );
64
- logger.info((styles) => styles.bold(`Unpacking RNOH modules`));
65
- let unpackedArchivesCount = 0;
66
- let skippedArchivesCount = 0;
67
- await projectDependenciesManager.forEachAsync(async (dependency) => {
68
- const packageJSON = dependency.readPackageJSON();
69
- const harmonyArchivePath = dependency
70
- .getRootPath()
71
- .copyWithNewSegment('harmony.tar.gz');
72
- const pkgHarmonyDirPath = dependency
73
- .getRootPath()
74
- .copyWithNewSegment('harmony');
75
- const harmonyTarGzPathStr = dependency
76
- .getRootPath()
77
- .copyWithNewSegment('harmony.tar.gz')
78
- .relativeTo(projectRootPath)
79
- .getValue();
80
- if (
81
- packageJSON.version &&
82
- shouldUnpack(
83
- harmonyArchivePath,
84
- pkgHarmonyDirPath,
85
- outputDirPath,
86
- packageJSON.version
87
- )
88
- ) {
89
- if (!fs.existsSync(pkgHarmonyDirPath)) {
90
- /**
91
- * After fresh install, harmony.tar.gz wasn't unpacked to harmony. Updating npm package should remove unpacked harmony dir.
92
- */
93
- logger.info(
94
- (styles) =>
95
- `[${styles.cyan('unpacking')}] ${styles.gray(harmonyTarGzPathStr)}`
96
- );
97
- await unpackTarGz(harmonyArchivePath, pkgHarmonyDirPath);
98
- unpackedArchivesCount++;
99
- } else {
100
- skippedArchivesCount++;
101
- logger.info(
102
- (styles) =>
103
- `[${styles.yellow('skipped')}] ${styles.gray(
104
- harmonyTarGzPathStr
105
- )}`
106
- );
107
- }
108
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
109
- for (const ohModuleInfo of ohModulesInfo) {
110
- const moduleArchivePathStr = dependency
111
- .getRootPath()
112
- .copyWithNewSegment('harmony', ohModuleInfo.archiveFileName)
113
- .relativeTo(projectRootPath)
114
- .getValue();
115
- logger.info(
116
- (styles) =>
117
- `[${styles.cyan('unpacking')}] ${styles.gray(moduleArchivePathStr)}`
118
- );
119
- unpackedArchivesCount++;
120
- await unpackTarGz(
121
- ohModuleInfo.archivePath,
122
- ohModuleInfo.computedModulePath
123
- );
124
- }
125
- } else {
126
- if (fs.existsSync(harmonyArchivePath)) {
127
- skippedArchivesCount++;
128
- logger.info(
129
- (styles) =>
130
- `[${styles.yellow('skipped')}] ${styles.gray(
131
- harmonyTarGzPathStr
132
- )}`
133
- );
134
- }
135
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
136
- for (const ohModuleInfo of ohModulesInfo) {
137
- const moduleArchivePathStr = dependency
138
- .getRootPath()
139
- .copyWithNewSegment('harmony', ohModuleInfo.archiveFileName)
140
- .relativeTo(projectRootPath)
141
- .getValue();
142
- skippedArchivesCount++;
143
- logger.info(
144
- (styles) =>
145
- `[${styles.yellow('skipped')}] ${styles.gray(
146
- moduleArchivePathStr
147
- )}`
148
- );
149
- }
150
- }
151
- });
152
- logger.info(
153
- (styles) =>
154
- `\nUnpacked: ${styles.cyan(
155
- unpackedArchivesCount
156
- )}, Skipped: ${styles.yellow(skippedArchivesCount)}`
157
- );
158
- }
159
-
160
- function shouldUnpack(
161
- harmonyArchivePath: AbsolutePath,
162
- pkgHarmonyDirPath: AbsolutePath,
163
- outputDirPath: AbsolutePath,
164
- nodePackageVersion: string
165
- ) {
166
- if (!fs.existsSync(harmonyArchivePath.getValue())) return false;
167
- if (!fs.existsSync(pkgHarmonyDirPath.getValue())) return true;
168
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
169
- for (const ohModuleInfo of ohModulesInfo) {
170
- if (
171
- !fs.existsSync(ohModuleInfo.computedModulePath.getValue()) ||
172
- getOhPackageVersion(ohModuleInfo.computedModulePath) !==
173
- nodePackageVersion
174
- ) {
175
- return true;
176
- }
177
- }
178
- return false;
179
- }
180
-
181
- function getOhPackageVersion(modulePath: AbsolutePath) {
182
- const ohPackagePath = modulePath.copyWithNewSegment('oh-package.json5');
183
- if (!fs.existsSync(ohPackagePath.getValue())) return null;
184
- const content = fs.readFileSync(ohPackagePath.getValue(), {
185
- encoding: 'utf-8',
186
- });
187
- return JSON5.parse(content)['version'];
188
- }
189
-
190
- function getOhModulesInfo(
191
- pkgHarmonyDirPath: AbsolutePath,
192
- outputDirPath: AbsolutePath
193
- ) {
194
- const results: {
195
- archivePath: AbsolutePath;
196
- archiveFileName: string;
197
- computedModulePath: AbsolutePath;
198
- computedModuleName: string;
199
- }[] = [];
200
- if (!fs.existsSync(pkgHarmonyDirPath.getValue())) return [];
201
- const names = fs.readdirSync(pkgHarmonyDirPath.getValue());
202
- for (const name of names) {
203
- if (name.endsWith('.tar.gz')) {
204
- const archivePath = pkgHarmonyDirPath.copyWithNewSegment(name);
205
- const computedModuleName = pathUtils
206
- .parse(archivePath.getValue())
207
- .name.split('.')[0];
208
- const computedModulePath =
209
- outputDirPath.copyWithNewSegment(computedModuleName);
210
- results.push({
211
- archivePath,
212
- archiveFileName: name,
213
- computedModulePath,
214
- computedModuleName,
215
- });
216
- }
217
- }
218
- return results;
219
- }
220
-
221
- function unpackTarGz(archivePath: AbsolutePath, outputDirPath: AbsolutePath) {
222
- if (!fs.existsSync(outputDirPath.getValue())) {
223
- fs.mkdirSync(outputDirPath.getValue(), { recursive: true });
224
- }
225
- const readStream = fs.createReadStream(archivePath.getValue());
226
- const writeStream = tar.extract({
227
- cwd: outputDirPath.getValue(),
228
- });
229
- readStream.pipe(zlib.createGunzip()).pipe(writeStream);
230
- return new Promise((resolve, reject) => {
231
- writeStream.on('finish', () => {
232
- resolve(undefined);
233
- });
234
- writeStream.on('error', (err) => {
235
- reject(err);
236
- });
237
- });
238
- }