@react-native-oh/react-native-harmony-cli 0.0.27 → 0.0.30

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 (140) hide show
  1. package/README.md +12 -0
  2. package/dist/assetResolver.js +6 -0
  3. package/dist/autolinking/Autolinking.js +275 -0
  4. package/dist/autolinking/index.js +23 -0
  5. package/dist/codegen/core/CodeGenerator.js +6 -0
  6. package/dist/codegen/core/CodegenError.js +6 -0
  7. package/dist/codegen/core/EventUtils.js +6 -0
  8. package/dist/codegen/core/TypeAnnotationToTS.js +6 -0
  9. package/dist/codegen/core/UberSchema.js +9 -3
  10. package/dist/codegen/core/index.js +6 -0
  11. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +6 -0
  12. package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +6 -0
  13. package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +6 -0
  14. package/dist/codegen/generators/CppComponentCodeGenerator.js +6 -0
  15. package/dist/codegen/generators/LibGlueCodeGenerator.js +6 -0
  16. package/dist/codegen/generators/NativeModuleCodeGenerator.js +6 -0
  17. package/dist/codegen/generators/SharedComponentCodeGenerator.js +6 -0
  18. package/dist/codegen/generators/UberGeneratorV1.js +6 -0
  19. package/dist/codegen/generators/UberGeneratorV2.js +6 -0
  20. package/dist/codegen/generators/index.js +6 -0
  21. package/dist/codegen/index.js +6 -0
  22. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +6 -0
  23. package/dist/codegen/templates/BasePackageHTemplate.js +6 -0
  24. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +6 -0
  25. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +6 -0
  26. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +6 -0
  27. package/dist/codegen/templates/IndexTSTemplate.js +6 -0
  28. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +6 -0
  29. package/dist/codegen/templates/TurboModuleCppTemplate.js +6 -0
  30. package/dist/codegen/templates/TurboModuleHTemplate.js +6 -0
  31. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +6 -0
  32. package/dist/codegen/templates/index.js +6 -0
  33. package/dist/commands/bundle-harmony.js +7 -1
  34. package/dist/commands/codegen-harmony.js +16 -10
  35. package/dist/commands/codegen-lib-harmony.js +6 -0
  36. package/dist/commands/create-metro-host-constants.js +6 -0
  37. package/dist/commands/index.js +7 -0
  38. package/dist/commands/link-harmony.js +97 -0
  39. package/dist/commands/pack-harmony.js +6 -0
  40. package/dist/commands/unpack-harmony.js +10 -3
  41. package/dist/commands/verify-package-harmony.js +6 -0
  42. package/dist/core/AbsolutePath.js +12 -0
  43. package/dist/core/CodegenConfig.js +6 -0
  44. package/dist/core/DescriptiveError.js +6 -0
  45. package/dist/core/FileSystem.js +33 -0
  46. package/dist/core/OHPackageJSON5.js +6 -0
  47. package/dist/core/PackageJSON.js +15 -7
  48. package/dist/core/ProjectDependenciesManager.js +31 -18
  49. package/dist/core/ValidationError.js +6 -0
  50. package/dist/core/ValueObject.js +6 -0
  51. package/dist/core/fs.js +6 -0
  52. package/dist/core/index.js +7 -0
  53. package/dist/index.js +7 -0
  54. package/dist/io/CliExecutor.js +35 -0
  55. package/dist/io/Logger.js +9 -0
  56. package/dist/io/RealFS.js +68 -0
  57. package/dist/io/TarGz.js +6 -0
  58. package/dist/io/__fixtures__.js +127 -0
  59. package/dist/io/fsUtils.js +20 -6
  60. package/dist/io/index.js +7 -0
  61. package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +6 -0
  62. package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +6 -0
  63. package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +6 -0
  64. package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +6 -0
  65. package/dist/package-verifier/checks/index.js +6 -0
  66. package/dist/package-verifier/index.js +6 -0
  67. package/dist/package-verifier/types.js +6 -0
  68. package/dist/package-verifier/verify-package.js +7 -1
  69. package/jest.config.js +7 -0
  70. package/package.json +8 -2
  71. package/react-native.config.js +7 -0
  72. package/src/assetResolver.ts +7 -0
  73. package/src/autolinking/Autolinking.test.ts +483 -0
  74. package/src/autolinking/Autolinking.ts +384 -0
  75. package/src/autolinking/index.ts +8 -0
  76. package/src/codegen/core/CodeGenerator.ts +7 -0
  77. package/src/codegen/core/CodegenError.ts +7 -0
  78. package/src/codegen/core/EventUtils.ts +7 -0
  79. package/src/codegen/core/TypeAnnotationToTS.test.ts +7 -0
  80. package/src/codegen/core/TypeAnnotationToTS.ts +7 -0
  81. package/src/codegen/core/UberSchema.ts +11 -1
  82. package/src/codegen/core/index.ts +7 -0
  83. package/src/codegen/generators/AppBuildTimeGlueCodeGenerator.ts +7 -0
  84. package/src/codegen/generators/ArkTSComponentCodeGeneratorArkTS.ts +7 -0
  85. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +7 -0
  86. package/src/codegen/generators/CppComponentCodeGenerator.ts +7 -0
  87. package/src/codegen/generators/LibGlueCodeGenerator.ts +7 -0
  88. package/src/codegen/generators/NativeModuleCodeGenerator.ts +7 -0
  89. package/src/codegen/generators/SharedComponentCodeGenerator.ts +7 -0
  90. package/src/codegen/generators/UberGeneratorV1.ts +7 -0
  91. package/src/codegen/generators/UberGeneratorV2.ts +7 -0
  92. package/src/codegen/generators/index.ts +7 -0
  93. package/src/codegen/index.ts +7 -0
  94. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +7 -0
  95. package/src/codegen/templates/BasePackageHTemplate.ts +7 -0
  96. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +7 -0
  97. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +7 -0
  98. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +7 -0
  99. package/src/codegen/templates/IndexTSTemplate.ts +7 -0
  100. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +7 -0
  101. package/src/codegen/templates/TurboModuleCppTemplate.ts +7 -0
  102. package/src/codegen/templates/TurboModuleHTemplate.ts +7 -0
  103. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +7 -0
  104. package/src/codegen/templates/index.ts +7 -0
  105. package/src/commands/bundle-harmony.ts +8 -2
  106. package/src/commands/codegen-harmony.ts +19 -6
  107. package/src/commands/codegen-lib-harmony.ts +7 -0
  108. package/src/commands/create-metro-host-constants.ts +7 -0
  109. package/src/commands/index.ts +8 -0
  110. package/src/commands/link-harmony.ts +102 -0
  111. package/src/commands/pack-harmony.ts +7 -0
  112. package/src/commands/unpack-harmony.ts +12 -3
  113. package/src/commands/verify-package-harmony.ts +7 -0
  114. package/src/core/AbsolutePath.ts +16 -0
  115. package/src/core/CodegenConfig.ts +7 -0
  116. package/src/core/DescriptiveError.ts +7 -0
  117. package/src/core/FileSystem.ts +57 -0
  118. package/src/core/OHPackageJSON5.ts +7 -0
  119. package/src/core/PackageJSON.ts +40 -8
  120. package/src/core/ProjectDependenciesManager.ts +40 -14
  121. package/src/core/ValidationError.ts +7 -0
  122. package/src/core/ValueObject.ts +7 -0
  123. package/src/core/fs.ts +7 -0
  124. package/src/core/index.ts +8 -0
  125. package/src/index.ts +9 -0
  126. package/src/io/CliExecutor.ts +47 -0
  127. package/src/io/Logger.ts +11 -0
  128. package/src/io/RealFS.ts +62 -0
  129. package/src/io/TarGz.ts +7 -0
  130. package/src/io/__fixtures__.ts +135 -0
  131. package/src/io/fsUtils.ts +26 -6
  132. package/src/io/index.ts +8 -0
  133. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +7 -0
  134. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +7 -0
  135. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +7 -0
  136. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +7 -0
  137. package/src/package-verifier/checks/index.ts +7 -0
  138. package/src/package-verifier/index.ts +7 -0
  139. package/src/package-verifier/types.ts +7 -0
  140. package/src/package-verifier/verify-package.ts +9 -1
@@ -1,3 +1,10 @@
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
+
1
8
  import mustache from 'mustache';
2
9
 
3
10
  const TEMPLATE = `
@@ -1,3 +1,10 @@
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
+
1
8
  import mustache from 'mustache';
2
9
 
3
10
  const TEMPLATE = `
@@ -1,3 +1,10 @@
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
+
1
8
  import mustache from 'mustache';
2
9
 
3
10
  const TEMPLATE = `
@@ -1,3 +1,10 @@
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
+
1
8
  import mustache from 'mustache';
2
9
 
3
10
  const TEMPLATE = `
@@ -1,3 +1,10 @@
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
+
1
8
  import mustache from 'mustache';
2
9
 
3
10
  const TEMPLATE = `
@@ -1,3 +1,10 @@
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
+
1
8
  import mustache from 'mustache';
2
9
 
3
10
  const TEMPLATE = `
@@ -1,3 +1,10 @@
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
+
1
8
  export * from './TurboModuleHTemplate';
2
9
  export * from './TurboModuleCppTemplate';
3
10
  export * from './RNOHGeneratedPackageHTemplate';
@@ -1,3 +1,10 @@
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
+
1
8
  import { Command } from '@react-native-community/cli-types';
2
9
  import fs from 'fs';
3
10
  import fse from 'fs-extra';
@@ -118,7 +125,6 @@ async function retrieveAssetsData(
118
125
  ...MetroServer.DEFAULT_BUNDLE_OPTIONS,
119
126
  ...buildOptions,
120
127
  entryFile: buildOptions.entry,
121
- bundleType: 'todo',
122
128
  });
123
129
  } finally {
124
130
  metroServer.end();
@@ -188,7 +194,7 @@ function copyFiles(fileDestBySrc: Record<Path, Path>) {
188
194
  function copyFile(
189
195
  src: string,
190
196
  dest: string,
191
- onFinished: (error: Error) => void
197
+ onFinished: (error?: Error) => void
192
198
  ): void {
193
199
  const destDir = pathUtils.dirname(dest);
194
200
  fs.mkdir(destDir, { recursive: true }, (err?) => {
@@ -1,10 +1,17 @@
1
- import fs from 'node:fs';
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
+
2
8
  import pathUtils from 'node:path';
3
9
  import Case from 'case';
4
10
  import { Command } from '@react-native-community/cli-types';
5
11
  import {
6
12
  AbsolutePath,
7
13
  DescriptiveError,
14
+ FS,
8
15
  PackageJSON,
9
16
  ProjectDependenciesManager,
10
17
  maybeMakeDirectories,
@@ -18,7 +25,7 @@ import {
18
25
  AppBuildTimeGlueCodeGenerator,
19
26
  CodegenError,
20
27
  } from '../codegen';
21
- import { Logger } from '../io';
28
+ import { Logger, RealFS } from '../io';
22
29
 
23
30
  export const commandCodegenHarmony: Command = {
24
31
  name: 'codegen-harmony',
@@ -53,6 +60,7 @@ export const commandCodegenHarmony: Command = {
53
60
  ],
54
61
  func: async (_argv, _config, args: any) => {
55
62
  const logger = new Logger();
63
+ const fs = new RealFS();
56
64
  try {
57
65
  const MAX_SUPPORTED_CODEGEN_VERSION = 2;
58
66
  // prepare the input data
@@ -64,6 +72,7 @@ export const commandCodegenHarmony: Command = {
64
72
  const cppOutputPath = new AbsolutePath(args.cppOutputPath);
65
73
  const projectRootPath = new AbsolutePath(args.projectRootPath);
66
74
  const uberSchemaFromArkTSLibraries = await UberSchema.fromProject(
75
+ fs,
67
76
  projectRootPath,
68
77
  (codegenVersion, packageName) => {
69
78
  throwErrorIfUnsupportedCodegenVersion(
@@ -75,6 +84,7 @@ export const commandCodegenHarmony: Command = {
75
84
  }
76
85
  );
77
86
  const cApiLibrariesData = await collectLibrariesData(
87
+ fs,
78
88
  projectRootPath,
79
89
  (codegenVersion, packageName) => {
80
90
  throwErrorIfUnsupportedCodegenVersion(
@@ -132,12 +142,13 @@ export const commandCodegenHarmony: Command = {
132
142
 
133
143
  // output the results
134
144
  if (args.debug) {
135
- const uberSchema = await UberSchema.fromProject(projectRootPath);
145
+ const uberSchema = await UberSchema.fromProject(fs, projectRootPath);
136
146
  logger.debug((styles) =>
137
147
  styles.gray(JSON.stringify(uberSchema.getValue(), null, 2))
138
148
  );
139
149
  }
140
150
  saveCodegenResult(
151
+ fs,
141
152
  fileContentByPath,
142
153
  cppOutputPath,
143
154
  etsOutputPath,
@@ -168,13 +179,14 @@ function validateArgs(args: any) {
168
179
  }
169
180
 
170
181
  async function collectLibrariesData(
182
+ fs: FS,
171
183
  projectRootPath: AbsolutePath,
172
184
  onShouldAcceptCodegenConfig: (version: number, packageName: string) => boolean
173
185
  ): Promise<LibraryData[]> {
174
186
  const packageJSONs: PackageJSON[] = [
175
- PackageJSON.fromProjectRootPath(projectRootPath, projectRootPath),
187
+ PackageJSON.fromProjectRootPath(fs, projectRootPath, projectRootPath),
176
188
  ];
177
- await new ProjectDependenciesManager(projectRootPath).forEachAsync(
189
+ await new ProjectDependenciesManager(fs, projectRootPath).forEachAsync(
178
190
  (dependency) => {
179
191
  packageJSONs.push(dependency.readPackageJSON());
180
192
  }
@@ -221,6 +233,7 @@ function throwErrorIfUnsupportedCodegenVersion(
221
233
  }
222
234
 
223
235
  function saveCodegenResult(
236
+ fs: FS,
224
237
  fileContentByPath: Map<AbsolutePath, string>,
225
238
  cppOutputPath: AbsolutePath,
226
239
  etsOutputPath: AbsolutePath,
@@ -240,7 +253,7 @@ function saveCodegenResult(
240
253
  prepareDirectory(path.getDirectoryPath(), enableSafetyCheck);
241
254
  });
242
255
  fileContentByPath.forEach((fileContent, path) => {
243
- fs.writeFileSync(path.getValue(), fileContent);
256
+ fs.writeTextSync(path, fileContent);
244
257
  });
245
258
  }
246
259
 
@@ -1,3 +1,10 @@
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
+
1
8
  import { Command } from '@react-native-community/cli-types';
2
9
  import {
3
10
  AbsolutePath,
@@ -1,3 +1,10 @@
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
+
1
8
  import { Command } from '@react-native-community/cli-types';
2
9
  import os from 'node:os';
3
10
  import fs from 'node:fs';
@@ -1,3 +1,10 @@
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
+
1
8
  export * from './bundle-harmony';
2
9
  export * from './pack-harmony';
3
10
  export * from './unpack-harmony';
@@ -5,3 +12,4 @@ export * from './create-metro-host-constants';
5
12
  export * from './codegen-harmony';
6
13
  export * from './verify-package-harmony';
7
14
  export * from './codegen-lib-harmony';
15
+ export * from './link-harmony';
@@ -0,0 +1,102 @@
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 { AbsolutePath, DescriptiveError } from '../core';
10
+ import { Logger, RealFS } from '../io';
11
+ import { Autolinking } from '../autolinking';
12
+
13
+ const COMMAND_NAME = 'link-harmony';
14
+
15
+ export const commandLinkHarmony: Command = {
16
+ name: COMMAND_NAME,
17
+ description:
18
+ 'Handles the automatic linking of RNOH libraries. This command is meant to be used by the RNOH HvigorPlugin.',
19
+ options: [
20
+ {
21
+ name: '--harmony-project-path <path>',
22
+ description: 'Specifies the root of your OpenHarmony project.',
23
+ default: './harmony',
24
+ },
25
+ {
26
+ name: '--node-modules-path <path>',
27
+ description:
28
+ 'Defines the path to the node_modules directory which holds npm packages.',
29
+ default: './node_modules',
30
+ },
31
+ {
32
+ name: '--cmake-autolink-path-relative-to-harmony <path>',
33
+ description:
34
+ 'Path relative to the root of your OpenHarmony project for the generated CMake file involved in autolinking.',
35
+ default: './entry/src/main/cpp/autolinking.cmake',
36
+ },
37
+ {
38
+ name: '--cpp-rnoh-packages-factory-path-relative-to-harmony <path>',
39
+ description:
40
+ 'Path relative to the root of your OpenHarmony project, for the generated file used to link packages on C++ side.',
41
+ default: './entry/src/main/cpp/RNOHPackagesFactory.h',
42
+ },
43
+ {
44
+ name: '--ets-rnoh-packages-factory-path-relative-to-harmony <path>',
45
+ description:
46
+ 'Path relative to the root of your OpenHarmony project, for the generated file used to link packages on ETS side.',
47
+ default: './entry/src/main/ets/RNOHPackagesFactory.ets',
48
+ },
49
+ {
50
+ name: '--oh-package-path-relative-to-harmony <path>',
51
+ description:
52
+ 'Path to the oh-package.json5 relative to the root of your OpenHarmony project.',
53
+ default: './oh-package.json5',
54
+ },
55
+ {
56
+ name: '--exclude-npm-packages [string]',
57
+ description:
58
+ "Lists npm package names to be excluded from the autolinking process, separated by semicolons. By default every library is included. Can't be used with --included-npm-packages.",
59
+ },
60
+ {
61
+ name: '--include-npm-packages [string]',
62
+ description:
63
+ "Lists npm package names to be included from the autolinking process, separated by semicolons. By default every library is included. Can't be used with --excluded-npm-packages.",
64
+ },
65
+ ],
66
+ func: async (_argv, _config, rawArgs: any) => {
67
+ const logger = new Logger();
68
+ const fs = new RealFS();
69
+ const autolinking = new Autolinking(fs, logger);
70
+ try {
71
+ const config = await autolinking.prepareInput({
72
+ harmonyProjectPath: new AbsolutePath(rawArgs.harmonyProjectPath),
73
+ nodeModulesPath: new AbsolutePath(rawArgs.nodeModulesPath),
74
+ cmakeAutolinkPathRelativeToHarmony:
75
+ rawArgs.cmakeAutolinkPathRelativeToHarmony,
76
+ cppRNOHPackagesFactoryPathRelativeToHarmony:
77
+ rawArgs.cppRnohPackagesFactoryPathRelativeToHarmony,
78
+ etsRNOHPackagesFactoryPathRelativeToHarmony:
79
+ rawArgs.etsRnohPackagesFactoryPathRelativeToHarmony,
80
+ ohPackagePathRelativeToHarmony: rawArgs.ohPackagePathRelativeToHarmony,
81
+ excludedNpmPackageNames: new Set(
82
+ rawArgs.excludeNpmPackages
83
+ ? (rawArgs.excludeNpmPackages ?? '').split(';')
84
+ : undefined
85
+ ),
86
+ includedNpmPackageNames: new Set(
87
+ rawArgs.includeNpmPackages
88
+ ? (rawArgs.includeNpmPackages ?? '').split(';')
89
+ : undefined
90
+ ),
91
+ });
92
+ const result = autolinking.evaluate(config);
93
+ autolinking.saveAndLogOutput(result);
94
+ } catch (err) {
95
+ if (err instanceof DescriptiveError && !err.isUnexpected()) {
96
+ logger.descriptiveError(err);
97
+ return;
98
+ }
99
+ throw err;
100
+ }
101
+ },
102
+ };
@@ -1,3 +1,10 @@
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
+
1
8
  import { Command } from '@react-native-community/cli-types';
2
9
  import { globSync } from 'glob';
3
10
  import pathUtils from 'path';
@@ -1,5 +1,12 @@
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
+
1
8
  import { Command } from '@react-native-community/cli-types';
2
- import { Logger } from '../io';
9
+ import { Logger, RealFS } from '../io';
3
10
 
4
11
  import pathUtils from 'path';
5
12
  import fs from 'fs';
@@ -49,7 +56,9 @@ async function unpack(
49
56
  outputDirPath: AbsolutePath
50
57
  ) {
51
58
  const logger = new Logger();
59
+ const fs = new RealFS();
52
60
  const projectDependenciesManager = new ProjectDependenciesManager(
61
+ fs,
53
62
  projectRootPath
54
63
  );
55
64
  logger.info((styles) => styles.bold(`Unpacking RNOH modules`), {
@@ -79,7 +88,7 @@ async function unpack(
79
88
  packageJSON.version
80
89
  )
81
90
  ) {
82
- if (!fs.existsSync(pkgHarmonyDirPath.getValue())) {
91
+ if (!fs.existsSync(pkgHarmonyDirPath)) {
83
92
  /**
84
93
  * After fresh install, harmony.tar.gz wasn't unpacked to harmony. Updating npm package should remove unpacked harmony dir.
85
94
  */
@@ -116,7 +125,7 @@ async function unpack(
116
125
  );
117
126
  }
118
127
  } else {
119
- if (fs.existsSync(harmonyArchivePath.getValue())) {
128
+ if (fs.existsSync(harmonyArchivePath)) {
120
129
  skippedArchivesCount++;
121
130
  logger.info(
122
131
  (styles) =>
@@ -1,3 +1,10 @@
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
+
1
8
  import { Command } from '@react-native-community/cli-types';
2
9
  import { getLoader, Loader } from '@react-native-community/cli-tools';
3
10
  import chalk from 'chalk';
@@ -1,3 +1,10 @@
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
+
1
8
  import pathUtils from 'node:path';
2
9
  import { ValueObject } from './ValueObject';
3
10
 
@@ -7,6 +14,7 @@ export class AbsolutePath implements ValueObject {
7
14
  }
8
15
 
9
16
  private value: string = '';
17
+
10
18
  constructor(path: string) {
11
19
  this.value = pathUtils.resolve(path);
12
20
  }
@@ -15,6 +23,10 @@ export class AbsolutePath implements ValueObject {
15
23
  return this.value;
16
24
  }
17
25
 
26
+ toString(): string {
27
+ return this.value;
28
+ }
29
+
18
30
  copyWithNewSegment(...relativePath: string[]): AbsolutePath {
19
31
  return new AbsolutePath(pathUtils.join(this.getValue(), ...relativePath));
20
32
  }
@@ -47,4 +59,8 @@ class RelativePath implements ValueObject {
47
59
  getValue() {
48
60
  return this.value;
49
61
  }
62
+
63
+ toString() {
64
+ return this.value;
65
+ }
50
66
  }
@@ -1,3 +1,10 @@
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
+
1
8
  import fs from 'node:fs';
2
9
  import { AbsolutePath } from './AbsolutePath';
3
10
  import { findFilePathsWithExtensions } from '../io';
@@ -1,3 +1,10 @@
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
+
1
8
  export class DescriptiveError extends Error {
2
9
  constructor(
3
10
  private data:
@@ -0,0 +1,57 @@
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 { AbsolutePath } from './AbsolutePath';
9
+ import pathUtils from 'node:path';
10
+
11
+ export abstract class Dirent {
12
+ abstract isDirectory(): boolean;
13
+
14
+ abstract get name(): string;
15
+ }
16
+
17
+ export abstract class FS {
18
+ abstract writeTextSync(path: AbsolutePath, data: string): void;
19
+
20
+ abstract readTextSync(path: AbsolutePath): string;
21
+
22
+ abstract readText(path: AbsolutePath): Promise<string>;
23
+
24
+ abstract existsSync(path: AbsolutePath): boolean;
25
+
26
+ abstract readDirentsSync(
27
+ path: AbsolutePath,
28
+ options?:
29
+ | {
30
+ recursive?: boolean | undefined;
31
+ }
32
+ | undefined
33
+ ): Dirent[];
34
+
35
+ findFilePathsWithExtensions(
36
+ dirPath: AbsolutePath,
37
+ fileExtensions: readonly string[]
38
+ ): AbsolutePath[] {
39
+ let results: AbsolutePath[] = [];
40
+ const dirents = this.readDirentsSync(dirPath);
41
+ for (const dirent of dirents) {
42
+ const path = dirPath.copyWithNewSegment(dirent.name.toString());
43
+ if (dirent.isDirectory()) {
44
+ results = results.concat(
45
+ this.findFilePathsWithExtensions(path, fileExtensions)
46
+ );
47
+ } else if (
48
+ fileExtensions.includes(
49
+ pathUtils.extname(dirent.name.toString()).substring(1)
50
+ )
51
+ ) {
52
+ results.push(path);
53
+ }
54
+ }
55
+ return results;
56
+ }
57
+ }
@@ -1,3 +1,10 @@
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
+
1
8
  import json5 from 'json5';
2
9
  import fs from 'node:fs';
3
10
  import { AbsolutePath } from './AbsolutePath';
@@ -1,15 +1,29 @@
1
- import fs from 'fs';
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
+ */
2
7
  import { AbsolutePath } from './AbsolutePath';
3
- import { CodegenConfig } from './CodegenConfig';
8
+ import { CodegenConfig, RawCodegenConfig } from './CodegenConfig';
4
9
  import { DescriptiveError } from './DescriptiveError';
10
+ import { FS } from './FileSystem';
11
+
12
+ type RawAutolinkingConfig = {
13
+ etsPackageClassName?: string;
14
+ cppPackageClassName?: string;
15
+ cmakeLibraryTargetName?: string;
16
+ ohPackageName?: string;
17
+ };
5
18
 
6
19
  export class PackageJSON {
7
20
  static fromProjectRootPath(
21
+ fs: FS,
8
22
  packageRootPath: AbsolutePath,
9
23
  projectRootPath: AbsolutePath
10
24
  ) {
11
25
  const packageJSONPath = packageRootPath.copyWithNewSegment('package.json');
12
- if (!fs.existsSync(packageJSONPath.getValue())) {
26
+ if (!fs.existsSync(packageJSONPath)) {
13
27
  throw new DescriptiveError({
14
28
  whatHappened: "Couldn't find 'package.json'",
15
29
  whatCanUserDo: {
@@ -21,11 +35,19 @@ export class PackageJSON {
21
35
  extraData: packageJSONPath.getValue(),
22
36
  });
23
37
  }
24
- return new PackageJSON(
25
- JSON.parse(fs.readFileSync(packageJSONPath.getValue()).toString()),
26
- packageRootPath,
27
- projectRootPath
28
- );
38
+ try {
39
+ return new PackageJSON(
40
+ JSON.parse(fs.readTextSync(packageJSONPath).toString()),
41
+ packageRootPath,
42
+ projectRootPath
43
+ );
44
+ } catch (err) {
45
+ throw new DescriptiveError({
46
+ whatHappened: "Couldn't parse package.json",
47
+ whatCanUserDo: {},
48
+ extraData: { packageJSONPath, err },
49
+ });
50
+ }
29
51
  }
30
52
 
31
53
  private constructor(
@@ -42,6 +64,16 @@ export class PackageJSON {
42
64
  return this.rawPackageJSON.version;
43
65
  }
44
66
 
67
+ get harmony():
68
+ | undefined
69
+ | {
70
+ alias?: string;
71
+ codegenConfig?: RawCodegenConfig | RawCodegenConfig[];
72
+ autolinking?: RawAutolinkingConfig | null | true;
73
+ } {
74
+ return this.rawPackageJSON.harmony;
75
+ }
76
+
45
77
  getCodegenConfigs(): CodegenConfig[] {
46
78
  /**
47
79
  * Shared codegenConfig is not used on purpose. Android, iOS and other platforms shouldn't