@react-native-oh/react-native-harmony-cli 0.0.30 → 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 (234) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -4
  3. package/dist/assetResolver.js +105 -10
  4. package/dist/autolinking/Autolinking.js +21 -26
  5. package/dist/autolinking/index.js +0 -6
  6. package/dist/codegen/core/CodeGenerator.js +0 -6
  7. package/dist/codegen/core/CodegenError.js +0 -6
  8. package/dist/codegen/core/EventUtils.js +4 -13
  9. package/dist/codegen/core/TypeAnnotationToTS.js +3 -9
  10. package/dist/codegen/core/UberSchema.js +8 -21
  11. package/dist/codegen/core/index.js +0 -6
  12. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +2 -7
  13. package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +3 -6
  14. package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +3 -6
  15. package/dist/codegen/generators/CppComponentCodeGenerator.js +3 -6
  16. package/dist/codegen/generators/LibGlueCodeGenerator.js +2 -8
  17. package/dist/codegen/generators/NativeModuleCodeGenerator.js +1 -7
  18. package/dist/codegen/generators/SharedComponentCodeGenerator.js +0 -6
  19. package/dist/codegen/generators/UberGeneratorV1.js +0 -6
  20. package/dist/codegen/generators/UberGeneratorV2.js +0 -6
  21. package/dist/codegen/generators/index.js +0 -6
  22. package/dist/codegen/index.js +0 -6
  23. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +0 -6
  24. package/dist/codegen/templates/BasePackageHTemplate.js +12 -7
  25. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +1 -6
  26. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +15 -6
  27. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +0 -6
  28. package/dist/codegen/templates/IndexTSTemplate.js +0 -6
  29. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +12 -7
  30. package/dist/codegen/templates/TurboModuleCppTemplate.js +0 -6
  31. package/dist/codegen/templates/TurboModuleHTemplate.js +0 -6
  32. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +0 -6
  33. package/dist/codegen/templates/index.js +0 -6
  34. package/dist/commands/bundle-harmony.js +133 -101
  35. package/dist/commands/clean-harmony.js +84 -0
  36. package/dist/commands/codegen-harmony.js +12 -22
  37. package/dist/commands/codegen-lib-harmony.js +15 -25
  38. package/dist/commands/index.js +3 -9
  39. package/dist/commands/init-harmony.js +226 -0
  40. package/dist/commands/init.js +703 -0
  41. package/dist/commands/link-harmony.js +12 -16
  42. package/dist/commands/run-harmony.js +438 -0
  43. package/dist/commands/types.js +2 -0
  44. package/dist/commands/verify-package-harmony.js +7 -11
  45. package/dist/core/AbsolutePath.js +3 -6
  46. package/dist/core/CodegenConfig.js +3 -11
  47. package/dist/core/DescriptiveError.js +7 -9
  48. package/dist/core/FileSystem.js +0 -6
  49. package/dist/core/OHPackageJSON5.js +0 -6
  50. package/dist/core/PackageJSON.js +4 -7
  51. package/dist/core/ProjectDependenciesManager.js +6 -14
  52. package/dist/core/ValidationError.js +0 -6
  53. package/dist/core/ValueObject.js +0 -6
  54. package/dist/core/fs.js +16 -10
  55. package/dist/core/index.js +0 -6
  56. package/dist/index.js +3 -9
  57. package/dist/init/templates/AppScopeAppJSON5Template.js +30 -0
  58. package/dist/init/templates/AppScopeStringVarTemplate.js +28 -0
  59. package/dist/init/templates/EntryIndexTemplate.js +65 -0
  60. package/dist/init/templates/EntryStringVarTemplate.js +38 -0
  61. package/dist/init/templates/HvigorConfigJson5Template.js +44 -0
  62. package/dist/init/templates/MetroConfigTemplate.js +47 -0
  63. package/dist/init/templates/OhPackageJson5Template.js +37 -0
  64. package/dist/init/templates/harmony/AppScope/app.json5 +10 -0
  65. package/dist/init/templates/harmony/AppScope/resources/base/element/string.json +8 -0
  66. package/dist/init/templates/harmony/AppScope/resources/base/media/app_icon.png +0 -0
  67. package/dist/init/templates/harmony/build-profile.json5 +36 -0
  68. package/dist/init/templates/harmony/codelinter.json +32 -0
  69. package/dist/init/templates/harmony/entry/build-profile.json5 +18 -0
  70. package/dist/init/templates/harmony/entry/gitignore +7 -0
  71. package/dist/init/templates/harmony/entry/hvigorfile.ts +13 -0
  72. package/dist/init/templates/harmony/entry/oh-package.json5 +9 -0
  73. package/dist/init/templates/harmony/entry/src/main/cpp/CMakeLists.txt +27 -0
  74. package/dist/init/templates/harmony/entry/src/main/cpp/PackageProvider.cpp +13 -0
  75. package/dist/init/templates/harmony/entry/src/main/cpp/gitignore +4 -0
  76. package/dist/init/templates/harmony/entry/src/main/ets/PackageProvider.ets +8 -0
  77. package/dist/init/templates/harmony/entry/src/main/ets/codegen/gitignore +2 -0
  78. package/dist/init/templates/harmony/entry/src/main/ets/entryability/EntryAbility.ets +11 -0
  79. package/dist/init/templates/harmony/entry/src/main/ets/gitignore +1 -0
  80. package/dist/init/templates/harmony/entry/src/main/ets/pages/.gitkeep +0 -0
  81. package/dist/init/templates/harmony/entry/src/main/ets/workers/RNOHWorker.ets +16 -0
  82. package/dist/init/templates/harmony/entry/src/main/module.json5 +58 -0
  83. package/dist/init/templates/harmony/entry/src/main/resources/base/element/color.json +8 -0
  84. package/dist/init/templates/harmony/entry/src/main/resources/base/element/string.json +16 -0
  85. package/dist/init/templates/harmony/entry/src/main/resources/base/media/background.png +0 -0
  86. package/dist/init/templates/harmony/entry/src/main/resources/base/media/foreground.png +0 -0
  87. package/dist/init/templates/harmony/entry/src/main/resources/base/media/layered_image.json +7 -0
  88. package/dist/init/templates/harmony/entry/src/main/resources/base/media/startIcon.png +0 -0
  89. package/dist/init/templates/harmony/entry/src/main/resources/base/profile/main_pages.json +5 -0
  90. package/dist/init/templates/harmony/entry/src/main/resources/gitignore +1 -0
  91. package/dist/init/templates/harmony/entry/src/ohosTest/ets/test/List.test.ets +2 -0
  92. package/dist/init/templates/harmony/entry/src/ohosTest/module.json5 +13 -0
  93. package/dist/init/templates/harmony/gitignore +16 -0
  94. package/dist/init/templates/harmony/hvigor/hvigor-config.json5 +22 -0
  95. package/dist/init/templates/harmony/hvigorfile.ts +7 -0
  96. package/dist/init/templates/index.js +21 -0
  97. package/dist/io/CliExecutor.js +56 -29
  98. package/dist/io/Logger.js +71 -47
  99. package/dist/io/RealFS.js +10 -6
  100. package/dist/io/TarGz.js +3 -10
  101. package/dist/io/__fixtures__.js +8 -8
  102. package/dist/io/fsUtils.js +1 -8
  103. package/dist/io/index.js +1 -6
  104. package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +1 -8
  105. package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +1 -8
  106. package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +1 -8
  107. package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +1 -8
  108. package/dist/package-verifier/checks/index.js +0 -6
  109. package/dist/package-verifier/index.js +0 -6
  110. package/dist/package-verifier/types.js +0 -6
  111. package/dist/package-verifier/verify-package.js +5 -14
  112. package/jest.config.js +0 -7
  113. package/package.json +20 -16
  114. package/react-native.config.js +0 -7
  115. package/scripts/copy-template.js +18 -0
  116. package/src/assetResolver.ts +117 -11
  117. package/src/autolinking/Autolinking.test.ts +54 -18
  118. package/src/autolinking/Autolinking.ts +32 -31
  119. package/src/autolinking/index.ts +1 -8
  120. package/src/codegen/core/CodeGenerator.ts +0 -7
  121. package/src/codegen/core/CodegenError.ts +0 -7
  122. package/src/codegen/core/EventUtils.ts +3 -12
  123. package/src/codegen/core/TypeAnnotationToTS.test.ts +0 -7
  124. package/src/codegen/core/TypeAnnotationToTS.ts +3 -8
  125. package/src/codegen/core/UberSchema.ts +9 -22
  126. package/src/codegen/core/index.ts +0 -7
  127. package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +5 -8
  128. package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +3 -7
  129. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +3 -7
  130. package/src/codegen/generators/CppComponentCodeGenerator.ts +3 -7
  131. package/src/codegen/generators/LibGlueCodeGenerator.ts +2 -9
  132. package/src/codegen/generators/NativeModuleCodeGenerator.ts +1 -8
  133. package/src/codegen/generators/SharedComponentCodeGenerator.ts +0 -7
  134. package/src/codegen/generators/UberGeneratorV1.ts +0 -7
  135. package/src/codegen/generators/UberGeneratorV2.ts +0 -7
  136. package/src/codegen/generators/index.ts +0 -7
  137. package/src/codegen/index.ts +0 -7
  138. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +0 -7
  139. package/src/codegen/templates/BasePackageHTemplate.ts +13 -8
  140. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +1 -7
  141. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +24 -8
  142. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +0 -7
  143. package/src/codegen/templates/IndexTSTemplate.ts +0 -7
  144. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +13 -8
  145. package/src/codegen/templates/TurboModuleCppTemplate.ts +0 -7
  146. package/src/codegen/templates/TurboModuleHTemplate.ts +0 -7
  147. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +0 -7
  148. package/src/codegen/templates/index.ts +0 -7
  149. package/src/commands/bundle-harmony.ts +178 -118
  150. package/src/commands/clean-harmony.ts +54 -0
  151. package/src/commands/codegen-harmony.ts +13 -25
  152. package/src/commands/codegen-lib-harmony.ts +16 -28
  153. package/src/commands/index.ts +3 -10
  154. package/src/commands/init-harmony.ts +299 -0
  155. package/src/commands/init.ts +755 -0
  156. package/src/commands/link-harmony.ts +14 -19
  157. package/src/commands/run-harmony.ts +478 -0
  158. package/src/commands/types.ts +14 -0
  159. package/src/commands/verify-package-harmony.ts +9 -12
  160. package/src/core/AbsolutePath.ts +4 -7
  161. package/src/core/CodegenConfig.ts +3 -12
  162. package/src/core/DescriptiveError.ts +8 -15
  163. package/src/core/FileSystem.ts +5 -8
  164. package/src/core/OHPackageJSON5.ts +0 -7
  165. package/src/core/PackageJSON.ts +4 -13
  166. package/src/core/ProjectDependenciesManager.ts +14 -22
  167. package/src/core/ValidationError.ts +0 -7
  168. package/src/core/ValueObject.ts +0 -7
  169. package/src/core/fs.ts +14 -8
  170. package/src/core/index.ts +0 -7
  171. package/src/index.ts +8 -15
  172. package/src/init/templates/AppScopeAppJSON5Template.ts +24 -0
  173. package/src/init/templates/AppScopeStringVarTemplate.ts +22 -0
  174. package/src/init/templates/EntryIndexTemplate.ts +59 -0
  175. package/src/init/templates/EntryStringVarTemplate.ts +31 -0
  176. package/src/init/templates/HvigorConfigJson5Template.ts +40 -0
  177. package/src/init/templates/MetroConfigTemplate.ts +41 -0
  178. package/src/init/templates/OhPackageJson5Template.ts +31 -0
  179. package/src/init/templates/harmony/AppScope/app.json5 +10 -0
  180. package/src/init/templates/harmony/AppScope/resources/base/element/string.json +8 -0
  181. package/src/init/templates/harmony/AppScope/resources/base/media/app_icon.png +0 -0
  182. package/src/init/templates/harmony/build-profile.json5 +36 -0
  183. package/src/init/templates/harmony/codelinter.json +32 -0
  184. package/src/init/templates/harmony/entry/build-profile.json5 +18 -0
  185. package/src/init/templates/harmony/entry/gitignore +7 -0
  186. package/src/init/templates/harmony/entry/hvigorfile.ts +13 -0
  187. package/src/init/templates/harmony/entry/oh-package.json5 +9 -0
  188. package/src/init/templates/harmony/entry/src/main/cpp/CMakeLists.txt +27 -0
  189. package/src/init/templates/harmony/entry/src/main/cpp/PackageProvider.cpp +13 -0
  190. package/src/init/templates/harmony/entry/src/main/cpp/gitignore +4 -0
  191. package/src/init/templates/harmony/entry/src/main/ets/PackageProvider.ets +8 -0
  192. package/src/init/templates/harmony/entry/src/main/ets/codegen/gitignore +2 -0
  193. package/src/init/templates/harmony/entry/src/main/ets/entryability/EntryAbility.ets +11 -0
  194. package/src/init/templates/harmony/entry/src/main/ets/gitignore +1 -0
  195. package/src/init/templates/harmony/entry/src/main/ets/pages/.gitkeep +0 -0
  196. package/src/init/templates/harmony/entry/src/main/ets/workers/RNOHWorker.ets +16 -0
  197. package/src/init/templates/harmony/entry/src/main/module.json5 +58 -0
  198. package/src/init/templates/harmony/entry/src/main/resources/base/element/color.json +8 -0
  199. package/src/init/templates/harmony/entry/src/main/resources/base/element/string.json +16 -0
  200. package/src/init/templates/harmony/entry/src/main/resources/base/media/background.png +0 -0
  201. package/src/init/templates/harmony/entry/src/main/resources/base/media/foreground.png +0 -0
  202. package/src/init/templates/harmony/entry/src/main/resources/base/media/layered_image.json +7 -0
  203. package/src/init/templates/harmony/entry/src/main/resources/base/media/startIcon.png +0 -0
  204. package/src/init/templates/harmony/entry/src/main/resources/base/profile/main_pages.json +5 -0
  205. package/src/init/templates/harmony/entry/src/main/resources/gitignore +1 -0
  206. package/src/init/templates/harmony/entry/src/ohosTest/ets/test/List.test.ets +2 -0
  207. package/src/init/templates/harmony/entry/src/ohosTest/module.json5 +13 -0
  208. package/src/init/templates/harmony/gitignore +16 -0
  209. package/src/init/templates/harmony/hvigor/hvigor-config.json5 +22 -0
  210. package/src/init/templates/harmony/hvigorfile.ts +7 -0
  211. package/src/init/templates/index.ts +5 -0
  212. package/src/io/CliExecutor.ts +55 -40
  213. package/src/io/Logger.ts +77 -51
  214. package/src/io/RealFS.ts +15 -8
  215. package/src/io/TarGz.ts +1 -8
  216. package/src/io/__fixtures__.ts +15 -18
  217. package/src/io/fsUtils.ts +1 -7
  218. package/src/io/index.ts +1 -7
  219. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +0 -7
  220. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +0 -7
  221. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +0 -7
  222. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +0 -7
  223. package/src/package-verifier/checks/index.ts +0 -7
  224. package/src/package-verifier/index.ts +0 -7
  225. package/src/package-verifier/types.ts +0 -7
  226. package/src/package-verifier/verify-package.ts +4 -13
  227. package/tsconfig.build.json +2 -5
  228. package/tsconfig.json +4 -0
  229. package/dist/commands/create-metro-host-constants.js +0 -67
  230. package/dist/commands/pack-harmony.js +0 -106
  231. package/dist/commands/unpack-harmony.js +0 -191
  232. package/src/commands/create-metro-host-constants.ts +0 -55
  233. package/src/commands/pack-harmony.ts +0 -128
  234. package/src/commands/unpack-harmony.ts +0 -240
@@ -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 { Command } from '@react-native-community/cli-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,240 +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 { Command } from '@react-native-community/cli-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
- prefix: true,
66
- });
67
- let unpackedArchivesCount = 0;
68
- let skippedArchivesCount = 0;
69
- await projectDependenciesManager.forEachAsync(async (dependency) => {
70
- const packageJSON = dependency.readPackageJSON();
71
- const harmonyArchivePath = dependency
72
- .getRootPath()
73
- .copyWithNewSegment('harmony.tar.gz');
74
- const pkgHarmonyDirPath = dependency
75
- .getRootPath()
76
- .copyWithNewSegment('harmony');
77
- const harmonyTarGzPathStr = dependency
78
- .getRootPath()
79
- .copyWithNewSegment('harmony.tar.gz')
80
- .relativeTo(projectRootPath)
81
- .getValue();
82
- if (
83
- packageJSON.version &&
84
- shouldUnpack(
85
- harmonyArchivePath,
86
- pkgHarmonyDirPath,
87
- outputDirPath,
88
- packageJSON.version
89
- )
90
- ) {
91
- if (!fs.existsSync(pkgHarmonyDirPath)) {
92
- /**
93
- * After fresh install, harmony.tar.gz wasn't unpacked to harmony. Updating npm package should remove unpacked harmony dir.
94
- */
95
- logger.info(
96
- (styles) =>
97
- `[${styles.cyan('unpacking')}] ${styles.gray(harmonyTarGzPathStr)}`
98
- );
99
- await unpackTarGz(harmonyArchivePath, pkgHarmonyDirPath);
100
- unpackedArchivesCount++;
101
- } else {
102
- skippedArchivesCount++;
103
- logger.info(
104
- (styles) =>
105
- `[${styles.yellow('skipped')}] ${styles.gray(
106
- harmonyTarGzPathStr
107
- )}`
108
- );
109
- }
110
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
111
- for (const ohModuleInfo of ohModulesInfo) {
112
- const moduleArchivePathStr = dependency
113
- .getRootPath()
114
- .copyWithNewSegment('harmony', ohModuleInfo.archiveFileName)
115
- .relativeTo(projectRootPath)
116
- .getValue();
117
- logger.info(
118
- (styles) =>
119
- `[${styles.cyan('unpacking')}] ${styles.gray(moduleArchivePathStr)}`
120
- );
121
- unpackedArchivesCount++;
122
- await unpackTarGz(
123
- ohModuleInfo.archivePath,
124
- ohModuleInfo.computedModulePath
125
- );
126
- }
127
- } else {
128
- if (fs.existsSync(harmonyArchivePath)) {
129
- skippedArchivesCount++;
130
- logger.info(
131
- (styles) =>
132
- `[${styles.yellow('skipped')}] ${styles.gray(
133
- harmonyTarGzPathStr
134
- )}`
135
- );
136
- }
137
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
138
- for (const ohModuleInfo of ohModulesInfo) {
139
- const moduleArchivePathStr = dependency
140
- .getRootPath()
141
- .copyWithNewSegment('harmony', ohModuleInfo.archiveFileName)
142
- .relativeTo(projectRootPath)
143
- .getValue();
144
- skippedArchivesCount++;
145
- logger.info(
146
- (styles) =>
147
- `[${styles.yellow('skipped')}] ${styles.gray(
148
- moduleArchivePathStr
149
- )}`
150
- );
151
- }
152
- }
153
- });
154
- logger.info(
155
- (styles) =>
156
- `\nUnpacked: ${styles.cyan(
157
- unpackedArchivesCount
158
- )}, Skipped: ${styles.yellow(skippedArchivesCount)}`
159
- );
160
- }
161
-
162
- function shouldUnpack(
163
- harmonyArchivePath: AbsolutePath,
164
- pkgHarmonyDirPath: AbsolutePath,
165
- outputDirPath: AbsolutePath,
166
- nodePackageVersion: string
167
- ) {
168
- if (!fs.existsSync(harmonyArchivePath.getValue())) return false;
169
- if (!fs.existsSync(pkgHarmonyDirPath.getValue())) return true;
170
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
171
- for (const ohModuleInfo of ohModulesInfo) {
172
- if (
173
- !fs.existsSync(ohModuleInfo.computedModulePath.getValue()) ||
174
- getOhPackageVersion(ohModuleInfo.computedModulePath) !==
175
- nodePackageVersion
176
- ) {
177
- return true;
178
- }
179
- }
180
- return false;
181
- }
182
-
183
- function getOhPackageVersion(modulePath: AbsolutePath) {
184
- const ohPackagePath = modulePath.copyWithNewSegment('oh-package.json5');
185
- if (!fs.existsSync(ohPackagePath.getValue())) return null;
186
- const content = fs.readFileSync(ohPackagePath.getValue(), {
187
- encoding: 'utf-8',
188
- });
189
- return JSON5.parse(content)['version'];
190
- }
191
-
192
- function getOhModulesInfo(
193
- pkgHarmonyDirPath: AbsolutePath,
194
- outputDirPath: AbsolutePath
195
- ) {
196
- const results: {
197
- archivePath: AbsolutePath;
198
- archiveFileName: string;
199
- computedModulePath: AbsolutePath;
200
- computedModuleName: string;
201
- }[] = [];
202
- if (!fs.existsSync(pkgHarmonyDirPath.getValue())) return [];
203
- const names = fs.readdirSync(pkgHarmonyDirPath.getValue());
204
- for (const name of names) {
205
- if (name.endsWith('.tar.gz')) {
206
- const archivePath = pkgHarmonyDirPath.copyWithNewSegment(name);
207
- const computedModuleName = pathUtils
208
- .parse(archivePath.getValue())
209
- .name.split('.')[0];
210
- const computedModulePath =
211
- outputDirPath.copyWithNewSegment(computedModuleName);
212
- results.push({
213
- archivePath,
214
- archiveFileName: name,
215
- computedModulePath,
216
- computedModuleName,
217
- });
218
- }
219
- }
220
- return results;
221
- }
222
-
223
- function unpackTarGz(archivePath: AbsolutePath, outputDirPath: AbsolutePath) {
224
- if (!fs.existsSync(outputDirPath.getValue())) {
225
- fs.mkdirSync(outputDirPath.getValue(), { recursive: true });
226
- }
227
- const readStream = fs.createReadStream(archivePath.getValue());
228
- const writeStream = tar.extract({
229
- cwd: outputDirPath.getValue(),
230
- });
231
- readStream.pipe(zlib.createGunzip()).pipe(writeStream);
232
- return new Promise((resolve, reject) => {
233
- writeStream.on('finish', () => {
234
- resolve(undefined);
235
- });
236
- writeStream.on('error', (err) => {
237
- reject(err);
238
- });
239
- });
240
- }