@react-native-oh/react-native-harmony-cli 0.0.26 → 0.0.28

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 +25 -0
  3. package/dist/autolinking/Autolinking.js +275 -0
  4. package/dist/autolinking/index.js +23 -0
  5. package/dist/codegen/core/CodeGenerator.js +8 -0
  6. package/dist/codegen/core/CodegenError.js +13 -0
  7. package/dist/codegen/core/EventUtils.js +25 -0
  8. package/dist/codegen/core/TypeAnnotationToTS.js +138 -0
  9. package/dist/codegen/core/UberSchema.js +109 -0
  10. package/dist/codegen/core/index.js +27 -0
  11. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +67 -0
  12. package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +82 -0
  13. package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +80 -0
  14. package/dist/codegen/generators/CppComponentCodeGenerator.js +62 -0
  15. package/dist/codegen/generators/LibGlueCodeGenerator.js +56 -0
  16. package/dist/codegen/generators/NativeModuleCodeGenerator.js +79 -0
  17. package/dist/codegen/generators/SharedComponentCodeGenerator.js +40 -0
  18. package/dist/codegen/generators/UberGeneratorV1.js +56 -0
  19. package/dist/codegen/generators/UberGeneratorV2.js +77 -0
  20. package/dist/codegen/generators/index.js +30 -0
  21. package/dist/codegen/index.js +24 -0
  22. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +111 -0
  23. package/dist/codegen/templates/BasePackageHTemplate.js +136 -0
  24. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +68 -0
  25. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +91 -0
  26. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +198 -0
  27. package/dist/codegen/templates/IndexTSTemplate.js +58 -0
  28. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +130 -0
  29. package/dist/codegen/templates/TurboModuleCppTemplate.js +59 -0
  30. package/dist/codegen/templates/TurboModuleHTemplate.js +48 -0
  31. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +81 -0
  32. package/dist/codegen/templates/index.js +31 -0
  33. package/dist/commands/bundle-harmony.js +185 -0
  34. package/dist/commands/codegen-harmony.js +207 -0
  35. package/dist/commands/codegen-lib-harmony.js +259 -0
  36. package/dist/commands/create-metro-host-constants.js +67 -0
  37. package/dist/commands/index.js +30 -0
  38. package/dist/commands/link-harmony.js +97 -0
  39. package/dist/commands/pack-harmony.js +106 -0
  40. package/dist/commands/unpack-harmony.js +191 -0
  41. package/dist/commands/verify-package-harmony.js +128 -0
  42. package/dist/core/AbsolutePath.js +59 -0
  43. package/dist/core/CodegenConfig.js +63 -0
  44. package/dist/core/DescriptiveError.js +55 -0
  45. package/dist/core/FileSystem.js +33 -0
  46. package/dist/core/OHPackageJSON5.js +29 -0
  47. package/dist/core/PackageJSON.js +66 -0
  48. package/dist/core/ProjectDependenciesManager.js +83 -0
  49. package/dist/core/ValidationError.js +12 -0
  50. package/dist/core/ValueObject.js +8 -0
  51. package/dist/core/fs.js +42 -0
  52. package/dist/core/index.js +32 -0
  53. package/dist/index.js +22 -0
  54. package/dist/io/CliExecutor.js +35 -0
  55. package/dist/io/Logger.js +80 -0
  56. package/dist/io/RealFS.js +68 -0
  57. package/dist/io/TarGz.js +34 -0
  58. package/dist/io/__fixtures__.js +127 -0
  59. package/dist/io/fsUtils.js +37 -0
  60. package/dist/io/index.js +26 -0
  61. package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +44 -0
  62. package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +38 -0
  63. package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +32 -0
  64. package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +57 -0
  65. package/dist/package-verifier/checks/index.js +26 -0
  66. package/dist/package-verifier/index.js +24 -0
  67. package/dist/package-verifier/types.js +8 -0
  68. package/dist/package-verifier/verify-package.js +107 -0
  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 +15 -1
  82. package/src/codegen/core/index.ts +7 -0
  83. package/src/codegen/generators/{GlueCodeGenerator.ts → AppBuildTimeGlueCodeGenerator.ts} +20 -9
  84. package/src/codegen/generators/{ComponentCodeGeneratorArkTS.ts → ArkTSComponentCodeGeneratorArkTS.ts} +18 -5
  85. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +119 -0
  86. package/src/codegen/generators/{ComponentCodeGeneratorCAPI.ts → CppComponentCodeGenerator.ts} +21 -43
  87. package/src/codegen/generators/LibGlueCodeGenerator.ts +98 -0
  88. package/src/codegen/generators/NativeModuleCodeGenerator.ts +14 -5
  89. package/src/codegen/generators/SharedComponentCodeGenerator.ts +57 -0
  90. package/src/codegen/generators/UberGeneratorV1.ts +19 -8
  91. package/src/codegen/generators/UberGeneratorV2.ts +39 -16
  92. package/src/codegen/generators/index.ts +12 -3
  93. package/src/codegen/index.ts +7 -0
  94. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +19 -7
  95. package/src/codegen/templates/{GeneratedPackageHTemplate.ts → BasePackageHTemplate.ts} +52 -12
  96. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +9 -1
  97. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +15 -10
  98. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +20 -8
  99. package/src/codegen/templates/IndexTSTemplate.ts +18 -9
  100. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +8 -0
  101. package/src/codegen/templates/TurboModuleCppTemplate.ts +16 -9
  102. package/src/codegen/templates/TurboModuleHTemplate.ts +16 -9
  103. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +20 -9
  104. package/src/codegen/templates/index.ts +7 -0
  105. package/src/commands/bundle-harmony.ts +8 -1
  106. package/src/commands/codegen-harmony.ts +43 -13
  107. package/src/commands/codegen-lib-harmony.ts +394 -0
  108. package/src/commands/create-metro-host-constants.ts +7 -0
  109. package/src/commands/index.ts +9 -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 +11 -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
@@ -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
@@ -1,9 +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 { AbsolutePath } from './AbsolutePath';
3
9
  import { PackageJSON } from './PackageJSON';
10
+ import { FS } from '../core';
4
11
 
5
12
  export class ProjectDependency {
6
13
  constructor(
14
+ private fs: FS,
7
15
  private packageRootPath: AbsolutePath,
8
16
  private projectRootPath: AbsolutePath
9
17
  ) {}
@@ -16,8 +24,26 @@ export class ProjectDependency {
16
24
  return this.readPackageJSON().getCodegenConfigs();
17
25
  }
18
26
 
27
+ getHarFilePath(): AbsolutePath | null {
28
+ const packageHarmonyPath =
29
+ this.packageRootPath.copyWithNewSegment('harmony');
30
+ if (!this.fs.existsSync(packageHarmonyPath)) {
31
+ return null;
32
+ }
33
+
34
+ const harFilePaths = this.fs.findFilePathsWithExtensions(
35
+ packageHarmonyPath,
36
+ ['har']
37
+ );
38
+ if (harFilePaths.length !== 1) {
39
+ return null;
40
+ }
41
+ return harFilePaths[0];
42
+ }
43
+
19
44
  readPackageJSON() {
20
45
  return PackageJSON.fromProjectRootPath(
46
+ this.fs,
21
47
  this.packageRootPath,
22
48
  this.projectRootPath
23
49
  );
@@ -25,7 +51,7 @@ export class ProjectDependency {
25
51
  }
26
52
 
27
53
  export class ProjectDependenciesManager {
28
- constructor(private projectRootPath: AbsolutePath) {}
54
+ constructor(private fs: FS, private projectRootPath: AbsolutePath) {}
29
55
 
30
56
  async forEachAsync(
31
57
  cb: (dependency: ProjectDependency) => Promise<void> | void
@@ -40,32 +66,32 @@ export class ProjectDependenciesManager {
40
66
  cb: (dependency: ProjectDependency) => Promise<void> | void,
41
67
  directoryPath: AbsolutePath
42
68
  ) {
43
- if (!fs.existsSync(directoryPath.getValue())) {
69
+ if (!this.fs.existsSync(directoryPath)) {
44
70
  return;
45
71
  }
46
- for (let dirOrFileName of fs.readdirSync(directoryPath.getValue())) {
47
- const potentialDependencyRootPath =
48
- directoryPath.copyWithNewSegment(dirOrFileName);
49
- if (fs.lstatSync(potentialDependencyRootPath.getValue()).isDirectory()) {
50
- if (dirOrFileName.startsWith('.')) {
72
+ for (let dirent of this.fs.readDirentsSync(directoryPath)) {
73
+ if (dirent.isDirectory()) {
74
+ if (dirent.name.startsWith('.')) {
51
75
  continue;
52
- } else if (dirOrFileName.startsWith('@')) {
76
+ } else if (dirent.name.startsWith('@')) {
53
77
  await this.forEachDependencyInDirectory(
54
78
  cb,
55
- directoryPath.copyWithNewSegment(dirOrFileName)
79
+ directoryPath.copyWithNewSegment(dirent.name)
56
80
  );
57
81
  } else {
82
+ const potentialDependencyRootPath = directoryPath.copyWithNewSegment(
83
+ dirent.name
84
+ );
58
85
  if (
59
- !fs.existsSync(
60
- potentialDependencyRootPath
61
- .copyWithNewSegment('package.json')
62
- .getValue()
86
+ !this.fs.existsSync(
87
+ potentialDependencyRootPath.copyWithNewSegment('package.json')
63
88
  )
64
89
  ) {
65
90
  continue;
66
91
  }
67
92
  await cb(
68
93
  new ProjectDependency(
94
+ this.fs,
69
95
  potentialDependencyRootPath,
70
96
  this.projectRootPath
71
97
  )
@@ -1 +1,8 @@
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 ValidationError extends Error {}
@@ -1,2 +1,9 @@
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
  /** https://en.wikipedia.org/wiki/Value_object */
2
9
  export interface ValueObject {}
package/src/core/fs.ts CHANGED
@@ -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 { DescriptiveError } from './DescriptiveError';
package/src/core/index.ts CHANGED
@@ -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 './AbsolutePath';
2
9
  export * from './ValidationError';
3
10
  export * from './DescriptiveError';
@@ -7,3 +14,4 @@ export * from './ProjectDependenciesManager';
7
14
  export * from './CodegenConfig';
8
15
  export * from './fs';
9
16
  export * from './OHPackageJSON5';
17
+ export * from './FileSystem';
package/src/index.ts CHANGED
@@ -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 { Config } from '@react-native-community/cli-types';
2
9
  import {
3
10
  commandBundleHarmony,
@@ -6,6 +13,8 @@ import {
6
13
  commandCreateMetroConstantsHarmony,
7
14
  commandCodegenHarmony,
8
15
  commandVerifyPackageHarmony,
16
+ commandCodegenLibHarmony,
17
+ commandLinkHarmony,
9
18
  } from './commands';
10
19
 
11
20
  export const config = {
@@ -16,5 +25,7 @@ export const config = {
16
25
  commandCreateMetroConstantsHarmony,
17
26
  commandCodegenHarmony,
18
27
  commandVerifyPackageHarmony,
28
+ commandCodegenLibHarmony,
29
+ commandLinkHarmony,
19
30
  ],
20
31
  } satisfies Partial<Config>;
@@ -0,0 +1,47 @@
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 '../core';
9
+
10
+ export abstract class CliExecutor {
11
+ abstract run(
12
+ command: string,
13
+ options?: {
14
+ args?: Record<string, string | number | boolean>;
15
+ cwd?: AbsolutePath;
16
+ }
17
+ ): string;
18
+
19
+ protected stringifyCliArgs(
20
+ args: Record<string, string | number | boolean>
21
+ ): string {
22
+ return Object.entries(args)
23
+ .filter(([_, value]) => {
24
+ if (typeof value === 'boolean') {
25
+ return value;
26
+ }
27
+ return value !== undefined && value !== null;
28
+ })
29
+ .map(([key, value]) => {
30
+ const formattedKey = toKebabCase(key);
31
+
32
+ if (typeof value === 'boolean') {
33
+ return `--${formattedKey}`;
34
+ }
35
+
36
+ return `--${formattedKey} ${value}`;
37
+ })
38
+ .join(' ');
39
+ }
40
+ }
41
+
42
+ function toKebabCase(str: string): string {
43
+ return str
44
+ .replace(/([a-z])([A-Z])/g, '$1-$2')
45
+ .replace(/[\s_]+/g, '-')
46
+ .toLowerCase();
47
+ }
package/src/io/Logger.ts CHANGED
@@ -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 chalk from 'chalk';
2
9
  import { DescriptiveError } from '../core';
3
10
 
@@ -13,6 +20,10 @@ export class Logger {
13
20
  console.info(prepareMsg(chalk));
14
21
  }
15
22
  }
23
+
24
+ warn(prepareMsg: (styles: typeof chalk) => string) {
25
+ console.warn(prepareMsg(chalk));
26
+ }
16
27
 
17
28
  debug(prepareMsg: (styles: typeof chalk) => string) {
18
29
  console.debug(prepareMsg(chalk));
@@ -0,0 +1,62 @@
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 '../core';
9
+ import { Dirent, FS } from '../core/FileSystem';
10
+ import fs, { Dirent as NodeFSDirent } from 'node:fs';
11
+
12
+ class RealFSDirent extends Dirent {
13
+ constructor(private nodeFSDirent: NodeFSDirent) {
14
+ super();
15
+ }
16
+
17
+ isDirectory(): boolean {
18
+ return this.nodeFSDirent.isDirectory();
19
+ }
20
+
21
+ get name(): string {
22
+ return this.nodeFSDirent.name;
23
+ }
24
+ }
25
+
26
+ export class RealFS extends FS {
27
+ existsSync(path: AbsolutePath): boolean {
28
+ return fs.existsSync(path.toString());
29
+ }
30
+
31
+ readTextSync(path: AbsolutePath): string {
32
+ return fs.readFileSync(path.toString()).toString();
33
+ }
34
+
35
+ async readText(path: AbsolutePath): Promise<string> {
36
+ return new Promise((resolve, reject) => {
37
+ fs.readFile(path.toString(), undefined, (err, data) => {
38
+ if (err) {
39
+ reject(err);
40
+ return;
41
+ }
42
+ resolve(data.toString());
43
+ });
44
+ });
45
+ }
46
+
47
+ writeTextSync(path: AbsolutePath, data: string): void {
48
+ fs.writeFileSync(path.toString(), data);
49
+ }
50
+
51
+ readDirentsSync(
52
+ path: AbsolutePath,
53
+ options?: { recursive?: boolean | undefined } | undefined
54
+ ): Dirent[] {
55
+ return fs
56
+ .readdirSync(path.toString(), {
57
+ withFileTypes: true,
58
+ recursive: options?.recursive,
59
+ })
60
+ .map((rawDirent) => new RealFSDirent(rawDirent));
61
+ }
62
+ }
package/src/io/TarGz.ts CHANGED
@@ -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 zlib from 'zlib';
3
10
  import tar from 'tar';
@@ -0,0 +1,135 @@
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, DescriptiveError, Dirent, FS } from '../core';
9
+ import { CliExecutor } from './CliExecutor';
10
+ import { IFs, memfs, NestedDirectoryJSON } from 'memfs';
11
+ import RawMemDirent from 'memfs/lib/Dirent';
12
+ import { Logger } from './Logger';
13
+ import chalk from 'chalk';
14
+
15
+ export class FakeCliExecutor extends CliExecutor {
16
+ private commands: string[] = [];
17
+
18
+ constructor(private onRun: (command: string) => string) {
19
+ super();
20
+ }
21
+
22
+ run(
23
+ command: string,
24
+ options: {
25
+ args?: Record<string, string | number | boolean>;
26
+ cwd?: AbsolutePath;
27
+ } = {}
28
+ ): string {
29
+ let commandWithArgs = command;
30
+ if (options.args) {
31
+ commandWithArgs += ' ' + this.stringifyCliArgs(options.args);
32
+ }
33
+ this.commands.push(commandWithArgs);
34
+ return this.onRun(commandWithArgs);
35
+ }
36
+
37
+ getCommands() {
38
+ return this.commands;
39
+ }
40
+ }
41
+
42
+ class MemFSDirent extends Dirent {
43
+ constructor(private rawDirent: RawMemDirent) {
44
+ super();
45
+ }
46
+
47
+ isDirectory(): boolean {
48
+ return this.rawDirent.isDirectory();
49
+ }
50
+
51
+ get name(): string {
52
+ return this.rawDirent.name.toString();
53
+ }
54
+ }
55
+ export class MemFS extends FS {
56
+ private fs: IFs;
57
+
58
+ constructor(initialFileStructure: NestedDirectoryJSON) {
59
+ super();
60
+ this.fs = memfs(initialFileStructure, './').fs;
61
+ }
62
+
63
+ existsSync(path: AbsolutePath): boolean {
64
+ return this.fs.existsSync(path.toString());
65
+ }
66
+
67
+ readTextSync(path: AbsolutePath): string {
68
+ return this.fs.readFileSync(path.toString()).toString();
69
+ }
70
+
71
+ async readText(path: AbsolutePath): Promise<string> {
72
+ return new Promise((resolve, reject) => {
73
+ this.fs.readFile(path.toString(), {}, (err, data) => {
74
+ if (err) {
75
+ reject(err);
76
+ return;
77
+ }
78
+ resolve(data?.toString() ?? '');
79
+ });
80
+ });
81
+ }
82
+
83
+ writeTextSync(path: AbsolutePath, data: string): void {
84
+ return this.fs.writeFileSync(path.toString(), data);
85
+ }
86
+
87
+ readDirentsSync(
88
+ path: AbsolutePath,
89
+ options?: { recursive?: boolean | undefined } | undefined
90
+ ): Dirent[] {
91
+ return this.fs
92
+ .readdirSync(path.toString(), {
93
+ withFileTypes: true,
94
+ recursive: options?.recursive,
95
+ })
96
+ .flatMap((dataOutOrDirent) => {
97
+ if (!(dataOutOrDirent instanceof RawMemDirent)) {
98
+ return [];
99
+ }
100
+ return [new MemFSDirent(dataOutOrDirent)];
101
+ });
102
+ }
103
+ }
104
+
105
+ export class MockedLogger extends Logger {
106
+ private logs: { type: 'debug' | 'info' | 'warn' | 'error'; msg: string }[] =
107
+ [];
108
+
109
+ debug(prepareMsg: (styles: typeof import('chalk')) => string): void {
110
+ chalk.level = 0;
111
+ this.logs.push({ type: 'debug', msg: prepareMsg(chalk) });
112
+ }
113
+
114
+ info(
115
+ prepareMsg: (styles: typeof import('chalk')) => string,
116
+ options?: { prefix?: boolean }
117
+ ): void {
118
+ chalk.level = 0;
119
+ this.logs.push({ type: 'info', msg: prepareMsg(chalk) });
120
+ }
121
+
122
+ warn(prepareMsg: (styles: typeof chalk) => string): void {
123
+ chalk.level = 0;
124
+ this.logs.push({ type: 'warn', msg: prepareMsg(chalk) });
125
+ }
126
+
127
+ descriptiveError(error: DescriptiveError): void {
128
+ chalk.level = 0;
129
+ this.logs.push({ type: 'error', msg: error.getMessage() });
130
+ }
131
+
132
+ getLogs() {
133
+ return this.logs;
134
+ }
135
+ }
package/src/io/fsUtils.ts CHANGED
@@ -1,21 +1,41 @@
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
- import fs from 'node:fs';
3
9
  import { AbsolutePath } from '../core';
10
+ import { IFs } from 'memfs';
11
+ import MemDirent from 'memfs/lib/Dirent';
12
+ import nodeFS from 'node:fs';
13
+ import { Dirent } from 'node:fs';
4
14
 
5
15
  export function findFilePathsWithExtensions(
6
16
  dirPath: AbsolutePath,
7
- fileExtensions: readonly string[]
17
+ fileExtensions: readonly string[],
18
+ options: { fs?: IFs | typeof nodeFS } = {}
8
19
  ): AbsolutePath[] {
20
+ const fs = (options?.fs ?? nodeFS) as typeof nodeFS;
9
21
  let results: AbsolutePath[] = [];
10
22
  const files = fs.readdirSync(dirPath.getValue(), { withFileTypes: true });
11
23
  for (const file of files) {
12
- const path = dirPath.copyWithNewSegment(file.name);
13
- if (file.isDirectory()) {
24
+ if (
25
+ !(file instanceof Dirent) &&
26
+ !((file as MemDirent) instanceof MemDirent)
27
+ ) {
28
+ continue;
29
+ }
30
+ const path = dirPath.copyWithNewSegment(file.name.toString());
31
+ if (fs.lstatSync(path.toString()).isDirectory()) {
14
32
  results = results.concat(
15
- findFilePathsWithExtensions(path, fileExtensions)
33
+ findFilePathsWithExtensions(path, fileExtensions, options)
16
34
  );
17
35
  } else if (
18
- fileExtensions.includes(pathUtils.extname(file.name).substring(1))
36
+ fileExtensions.includes(
37
+ pathUtils.extname(file.name.toString()).substring(1)
38
+ )
19
39
  ) {
20
40
  results.push(path);
21
41
  }
package/src/io/index.ts CHANGED
@@ -1,3 +1,11 @@
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 './Logger';
2
9
  export * from './fsUtils';
3
10
  export * from './TarGz';
11
+ export * from './RealFS';
@@ -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 '../../core';
3
10
  import { Check } from '../types';
@@ -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 '../../core';
3
10
  import { Check } from '../types';