@react-native-oh/react-native-harmony-cli 0.0.25 → 0.0.26-2

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 (94) hide show
  1. package/.prettierrc +6 -6
  2. package/dist/assetResolver.js +1 -23
  3. package/dist/codegen/core/CodeGenerator.js +2 -0
  4. package/dist/codegen/core/UberSchema.js +35 -9
  5. package/dist/codegen/core/index.js +1 -1
  6. package/dist/codegen/generators/{ComponentCodeGenerator.js → ComponentCodeGeneratorArkTS.js} +12 -4
  7. package/dist/codegen/generators/ComponentCodeGeneratorCAPI.js +79 -0
  8. package/dist/codegen/generators/GlueCodeGenerator.js +60 -0
  9. package/dist/codegen/generators/NativeModuleCodeGenerator.js +14 -6
  10. package/dist/codegen/generators/UberGeneratorV1.js +49 -0
  11. package/dist/codegen/generators/UberGeneratorV2.js +60 -0
  12. package/dist/codegen/generators/index.js +5 -2
  13. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +103 -0
  14. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +42 -36
  15. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +45 -36
  16. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +135 -127
  17. package/dist/codegen/templates/GeneratedPackageHTemplate.js +101 -0
  18. package/dist/codegen/templates/IndexTSTemplate.js +23 -5
  19. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +88 -82
  20. package/dist/codegen/templates/TurboModuleCppTemplate.js +26 -18
  21. package/dist/codegen/templates/TurboModuleHTemplate.js +27 -17
  22. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +37 -27
  23. package/dist/codegen/templates/index.js +1 -0
  24. package/dist/commands/codegen-harmony.js +99 -14
  25. package/dist/core/AbsolutePath.js +6 -0
  26. package/dist/core/CodegenConfig.js +8 -4
  27. package/dist/core/PackageJSON.js +8 -3
  28. package/dist/core/ProjectDependenciesManager.js +2 -2
  29. package/jest.config.js +5 -5
  30. package/package.json +57 -56
  31. package/react-native.config.js +10 -10
  32. package/src/assetResolver.ts +21 -44
  33. package/src/codegen/core/CodeGenerator.ts +9 -0
  34. package/src/codegen/core/CodegenError.ts +3 -3
  35. package/src/codegen/core/EventUtils.ts +18 -18
  36. package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -75
  37. package/src/codegen/core/TypeAnnotationToTS.ts +188 -188
  38. package/src/codegen/core/UberSchema.ts +142 -99
  39. package/src/codegen/core/index.ts +5 -5
  40. package/src/codegen/generators/{ComponentCodeGenerator.ts → ComponentCodeGeneratorArkTS.ts} +112 -98
  41. package/src/codegen/generators/ComponentCodeGeneratorCAPI.ts +114 -0
  42. package/src/codegen/generators/GlueCodeGenerator.ts +109 -0
  43. package/src/codegen/generators/NativeModuleCodeGenerator.ts +115 -95
  44. package/src/codegen/generators/UberGeneratorV1.ts +64 -0
  45. package/src/codegen/generators/UberGeneratorV2.ts +91 -0
  46. package/src/codegen/generators/index.ts +6 -3
  47. package/src/codegen/index.ts +2 -2
  48. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +116 -0
  49. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +55 -49
  50. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +93 -85
  51. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +210 -202
  52. package/src/codegen/templates/GeneratedPackageHTemplate.ts +107 -0
  53. package/src/codegen/templates/IndexTSTemplate.ts +53 -33
  54. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +130 -124
  55. package/src/codegen/templates/TurboModuleCppTemplate.ts +54 -47
  56. package/src/codegen/templates/TurboModuleHTemplate.ts +35 -26
  57. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +90 -81
  58. package/src/codegen/templates/index.ts +9 -8
  59. package/src/commands/bundle-harmony.ts +203 -203
  60. package/src/commands/codegen-harmony.ts +272 -143
  61. package/src/commands/create-metro-host-constants.ts +48 -48
  62. package/src/commands/index.ts +6 -6
  63. package/src/commands/pack-harmony.ts +121 -121
  64. package/src/commands/unpack-harmony.ts +231 -231
  65. package/src/commands/verify-package-harmony.ts +106 -106
  66. package/src/core/AbsolutePath.ts +50 -43
  67. package/src/core/CodegenConfig.ts +62 -54
  68. package/src/core/DescriptiveError.ts +61 -61
  69. package/src/core/OHPackageJSON5.ts +23 -23
  70. package/src/core/PackageJSON.ts +75 -63
  71. package/src/core/ProjectDependenciesManager.ts +77 -77
  72. package/src/core/ValidationError.ts +1 -1
  73. package/src/core/ValueObject.ts +2 -2
  74. package/src/core/fs.ts +31 -31
  75. package/src/core/index.ts +9 -9
  76. package/src/index.ts +20 -20
  77. package/src/io/Logger.ts +68 -68
  78. package/src/io/TarGz.ts +26 -26
  79. package/src/io/fsUtils.ts +24 -24
  80. package/src/io/index.ts +3 -3
  81. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +28 -28
  82. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +24 -24
  83. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +18 -18
  84. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +47 -47
  85. package/src/package-verifier/checks/index.ts +4 -4
  86. package/src/package-verifier/index.ts +2 -2
  87. package/src/package-verifier/types.ts +12 -12
  88. package/src/package-verifier/verify-package.ts +117 -117
  89. package/tsconfig.build.json +6 -6
  90. package/tsconfig.json +2 -2
  91. package/dist/codegen/core/Codegen.js +0 -35
  92. package/dist/codegen/generators/PackageCodeGenerator.js +0 -52
  93. package/src/codegen/core/Codegen.ts +0 -44
  94. package/src/codegen/generators/PackageCodeGenerator.ts +0 -73
@@ -1,43 +1,50 @@
1
- import pathUtils from 'node:path';
2
- import { ValueObject } from './ValueObject';
3
-
4
- export class AbsolutePath implements ValueObject {
5
- static fromSegments(...segments: string[]) {
6
- return new AbsolutePath(pathUtils.join(...segments));
7
- }
8
-
9
- private value: string = '';
10
- constructor(path: string) {
11
- this.value = pathUtils.resolve(path);
12
- }
13
-
14
- getValue(): string {
15
- return this.value;
16
- }
17
-
18
- copyWithNewSegment(...relativePath: string[]): AbsolutePath {
19
- return new AbsolutePath(pathUtils.join(this.getValue(), ...relativePath));
20
- }
21
-
22
- getExtension(): string | null {
23
- const maybeExtWithDot = pathUtils.extname(this.value);
24
- if (maybeExtWithDot) {
25
- return maybeExtWithDot.substring(1);
26
- }
27
- return null;
28
- }
29
-
30
- relativeTo(absolutePath: AbsolutePath): RelativePath {
31
- return new RelativePath(
32
- pathUtils.relative(absolutePath.getValue(), this.value)
33
- );
34
- }
35
- }
36
-
37
- class RelativePath implements ValueObject {
38
- constructor(private value: string) {}
39
-
40
- getValue() {
41
- return this.value;
42
- }
43
- }
1
+ import pathUtils from 'node:path';
2
+ import { ValueObject } from './ValueObject';
3
+
4
+ export class AbsolutePath implements ValueObject {
5
+ static fromSegments(...segments: string[]) {
6
+ return new AbsolutePath(pathUtils.join(...segments));
7
+ }
8
+
9
+ private value: string = '';
10
+ constructor(path: string) {
11
+ this.value = pathUtils.resolve(path);
12
+ }
13
+
14
+ getValue(): string {
15
+ return this.value;
16
+ }
17
+
18
+ copyWithNewSegment(...relativePath: string[]): AbsolutePath {
19
+ return new AbsolutePath(pathUtils.join(this.getValue(), ...relativePath));
20
+ }
21
+
22
+ getExtension(): string | null {
23
+ const maybeExtWithDot = pathUtils.extname(this.value);
24
+ if (maybeExtWithDot) {
25
+ return maybeExtWithDot.substring(1);
26
+ }
27
+ return null;
28
+ }
29
+
30
+ getDirectoryPath(): AbsolutePath {
31
+ const dirPath = pathUtils.extname(this.value)
32
+ ? pathUtils.dirname(this.value)
33
+ : this.value;
34
+ return new AbsolutePath(dirPath);
35
+ }
36
+
37
+ relativeTo(absolutePath: AbsolutePath): RelativePath {
38
+ return new RelativePath(
39
+ pathUtils.relative(absolutePath.getValue(), this.value)
40
+ );
41
+ }
42
+ }
43
+
44
+ class RelativePath implements ValueObject {
45
+ constructor(private value: string) {}
46
+
47
+ getValue() {
48
+ return this.value;
49
+ }
50
+ }
@@ -1,54 +1,62 @@
1
- import fs from 'node:fs';
2
- import { AbsolutePath } from './AbsolutePath';
3
- import { findFilePathsWithExtensions } from '../io';
4
- import { DescriptiveError } from './DescriptiveError';
5
-
6
- export type RawCodegenConfig = {
7
- specPaths: string[];
8
- };
9
-
10
- export class CodegenConfig {
11
- static fromRawCodegenConfig(
12
- rawCodegenConfig: RawCodegenConfig,
13
- packageRootPath: AbsolutePath,
14
- projectRootPath: AbsolutePath,
15
- packageName: string
16
- ) {
17
- const SUPPORTED_SPEC_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'];
18
- const specPaths = (rawCodegenConfig.specPaths ?? []).map((rawSpecPath) => {
19
- if (rawSpecPath.startsWith('~')) {
20
- return projectRootPath.copyWithNewSegment(rawSpecPath.replace('~', ''));
21
- } else {
22
- return packageRootPath.copyWithNewSegment(rawSpecPath);
23
- }
24
- });
25
- const specFilePaths = specPaths.flatMap((specPath) => {
26
- if (!fs.existsSync(specPath.getValue())) {
27
- throw new DescriptiveError({
28
- whatHappened: `No such file or directory: ${specPath.getValue()}`,
29
- whatCanUserDo: {
30
- default: [
31
- `Please verify in "${packageName}/package.json::harmony::codegenConfig". If you are not the maintainer of this package, please report this problem to the package maintainer.`,
32
- ],
33
- },
34
- });
35
- }
36
- if (fs.lstatSync(specPath.getValue()).isDirectory()) {
37
- return findFilePathsWithExtensions(specPath, SUPPORTED_SPEC_EXTENSIONS);
38
- } else {
39
- const ext = specPath.getExtension();
40
- if (ext && SUPPORTED_SPEC_EXTENSIONS.includes(ext)) {
41
- return [specPath];
42
- }
43
- return [];
44
- }
45
- });
46
- return new CodegenConfig(specFilePaths);
47
- }
48
-
49
- private constructor(private specFilePaths: AbsolutePath[]) {}
50
-
51
- getSpecFilePaths(): AbsolutePath[] {
52
- return this.specFilePaths;
53
- }
54
- }
1
+ import fs from 'node:fs';
2
+ import { AbsolutePath } from './AbsolutePath';
3
+ import { findFilePathsWithExtensions } from '../io';
4
+ import { DescriptiveError } from './DescriptiveError';
5
+
6
+ export type RawCodegenConfig = {
7
+ specPaths: string[];
8
+ version?: number;
9
+ };
10
+
11
+ export class CodegenConfig {
12
+ static fromRawCodegenConfig(
13
+ rawCodegenConfig: RawCodegenConfig,
14
+ packageRootPath: AbsolutePath,
15
+ projectRootPath: AbsolutePath,
16
+ packageName: string
17
+ ) {
18
+ const SUPPORTED_SPEC_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'];
19
+ const specPaths = (rawCodegenConfig.specPaths ?? []).map((rawSpecPath) => {
20
+ if (rawSpecPath.startsWith('~')) {
21
+ return projectRootPath.copyWithNewSegment(rawSpecPath.replace('~', ''));
22
+ } else {
23
+ return packageRootPath.copyWithNewSegment(rawSpecPath);
24
+ }
25
+ });
26
+ const specFilePaths = specPaths.flatMap((specPath) => {
27
+ if (!fs.existsSync(specPath.getValue())) {
28
+ throw new DescriptiveError({
29
+ whatHappened: `No such file or directory: ${specPath.getValue()}`,
30
+ whatCanUserDo: {
31
+ default: [
32
+ `Please verify "${packageName}/package.json::harmony::codegenConfig". If you are not the maintainer of this package, please report this problem to the package maintainer.`,
33
+ ],
34
+ },
35
+ });
36
+ }
37
+ if (fs.lstatSync(specPath.getValue()).isDirectory()) {
38
+ return findFilePathsWithExtensions(specPath, SUPPORTED_SPEC_EXTENSIONS);
39
+ } else {
40
+ const ext = specPath.getExtension();
41
+ if (ext && SUPPORTED_SPEC_EXTENSIONS.includes(ext)) {
42
+ return [specPath];
43
+ }
44
+ return [];
45
+ }
46
+ });
47
+ return new CodegenConfig(specFilePaths, rawCodegenConfig.version ?? 1);
48
+ }
49
+
50
+ private constructor(
51
+ private specFilePaths: AbsolutePath[],
52
+ private version: number
53
+ ) {}
54
+
55
+ getSpecFilePaths(): AbsolutePath[] {
56
+ return this.specFilePaths;
57
+ }
58
+
59
+ getVersion(): number {
60
+ return this.version;
61
+ }
62
+ }
@@ -1,61 +1,61 @@
1
- export class DescriptiveError extends Error {
2
- constructor(
3
- private data:
4
- | {
5
- whatHappened: string;
6
- whatCanUserDo: {
7
- default?: string[];
8
- rnAppDeveloper?: string[];
9
- rnPackageDeveloper?: string[];
10
- };
11
- extraData?: any;
12
- }
13
- | {
14
- whatHappened: string;
15
- unexpected: true;
16
- extraData?: any;
17
- }
18
- ) {
19
- super(data.whatHappened);
20
- }
21
-
22
- isUnexpected(): boolean {
23
- return 'unexpected' in this.data;
24
- }
25
-
26
- getMessage(): string {
27
- return this.data.whatHappened;
28
- }
29
-
30
- getSuggestionsByRole() {
31
- if ('whatCanUserDo' in this.data) {
32
- return this.data.whatCanUserDo;
33
- } else {
34
- return null;
35
- }
36
- }
37
-
38
- getDetails(): string {
39
- if (!this.data.extraData) {
40
- return '';
41
- }
42
- if (typeof this.data.extraData === 'string') {
43
- return this.data.extraData.trim();
44
- }
45
- if (this.data.extraData instanceof Error) {
46
- let lines = [
47
- `${this.data.extraData.name}: ${this.data.extraData.message}`,
48
- ];
49
- for (let stackEntry of (this.data.extraData.stack ?? '').split('\n')) {
50
- lines.push('');
51
- lines.push(stackEntry);
52
- }
53
- return lines.join('\n');
54
- }
55
- try {
56
- return JSON.stringify(this.data.extraData, null, 2);
57
- } catch (err) {
58
- return '';
59
- }
60
- }
61
- }
1
+ export class DescriptiveError extends Error {
2
+ constructor(
3
+ private data:
4
+ | {
5
+ whatHappened: string;
6
+ whatCanUserDo: {
7
+ default?: string[];
8
+ rnAppDeveloper?: string[];
9
+ rnPackageDeveloper?: string[];
10
+ };
11
+ extraData?: any;
12
+ }
13
+ | {
14
+ whatHappened: string;
15
+ unexpected: true;
16
+ extraData?: any;
17
+ }
18
+ ) {
19
+ super(data.whatHappened);
20
+ }
21
+
22
+ isUnexpected(): boolean {
23
+ return 'unexpected' in this.data;
24
+ }
25
+
26
+ getMessage(): string {
27
+ return this.data.whatHappened;
28
+ }
29
+
30
+ getSuggestionsByRole() {
31
+ if ('whatCanUserDo' in this.data) {
32
+ return this.data.whatCanUserDo;
33
+ } else {
34
+ return null;
35
+ }
36
+ }
37
+
38
+ getDetails(): string {
39
+ if (!this.data.extraData) {
40
+ return '';
41
+ }
42
+ if (typeof this.data.extraData === 'string') {
43
+ return this.data.extraData.trim();
44
+ }
45
+ if (this.data.extraData instanceof Error) {
46
+ let lines = [
47
+ `${this.data.extraData.name}: ${this.data.extraData.message}`,
48
+ ];
49
+ for (let stackEntry of (this.data.extraData.stack ?? '').split('\n')) {
50
+ lines.push('');
51
+ lines.push(stackEntry);
52
+ }
53
+ return lines.join('\n');
54
+ }
55
+ try {
56
+ return JSON.stringify(this.data.extraData, null, 2);
57
+ } catch (err) {
58
+ return '';
59
+ }
60
+ }
61
+ }
@@ -1,23 +1,23 @@
1
- import json5 from 'json5';
2
- import fs from 'node:fs';
3
- import { AbsolutePath } from './AbsolutePath';
4
-
5
- export class OHPackageJSON5 {
6
- static fromPath(ohPackageJson5Path: AbsolutePath) {
7
- return new OHPackageJSON5(
8
- json5.parse(
9
- fs.readFileSync(ohPackageJson5Path.getValue(), { encoding: 'utf-8' })
10
- )
11
- );
12
- }
13
-
14
- private constructor(private rawData: Record<string, any>) {}
15
-
16
- get name(): string {
17
- return this.rawData['name'];
18
- }
19
-
20
- get version(): string {
21
- return this.rawData['version'];
22
- }
23
- }
1
+ import json5 from 'json5';
2
+ import fs from 'node:fs';
3
+ import { AbsolutePath } from './AbsolutePath';
4
+
5
+ export class OHPackageJSON5 {
6
+ static fromPath(ohPackageJson5Path: AbsolutePath) {
7
+ return new OHPackageJSON5(
8
+ json5.parse(
9
+ fs.readFileSync(ohPackageJson5Path.getValue(), { encoding: 'utf-8' })
10
+ )
11
+ );
12
+ }
13
+
14
+ private constructor(private rawData: Record<string, any>) {}
15
+
16
+ get name(): string {
17
+ return this.rawData['name'];
18
+ }
19
+
20
+ get version(): string {
21
+ return this.rawData['version'];
22
+ }
23
+ }
@@ -1,63 +1,75 @@
1
- import fs from 'fs';
2
- import { AbsolutePath } from './AbsolutePath';
3
- import { CodegenConfig } from './CodegenConfig';
4
- import { DescriptiveError } from './DescriptiveError';
5
-
6
- export class PackageJSON {
7
- static fromProjectRootPath(
8
- packageRootPath: AbsolutePath,
9
- projectRootPath: AbsolutePath
10
- ) {
11
- const packageJSONPath = packageRootPath.copyWithNewSegment('package.json');
12
- if (!fs.existsSync(packageJSONPath.getValue())) {
13
- throw new DescriptiveError({
14
- whatHappened: "Couldn't find 'package.json'",
15
- whatCanUserDo: {
16
- default: [
17
- 'Check if the path to project root is correct',
18
- 'Try changing working directory to project root',
19
- ],
20
- },
21
- extraData: packageJSONPath.getValue(),
22
- });
23
- }
24
- return new PackageJSON(
25
- JSON.parse(fs.readFileSync(packageJSONPath.getValue()).toString()),
26
- packageRootPath,
27
- projectRootPath
28
- );
29
- }
30
-
31
- private constructor(
32
- private rawPackageJSON: Record<string, any>,
33
- private packageRootPath: AbsolutePath,
34
- private projectRootPath: AbsolutePath
35
- ) {}
36
-
37
- get name(): string {
38
- return this.rawPackageJSON.name;
39
- }
40
-
41
- get version(): string {
42
- return this.rawPackageJSON.version;
43
- }
44
-
45
- maybeCreateCodegenConfig(): CodegenConfig | null {
46
- /**
47
- * Shared codegenConfig is not used on purpose. Android, iOS and other platforms shouldn't
48
- * generate code for harmony-specific packages. The shape of this codegenConfig is different
49
- * that the shape of the shared config, to allow specifying which specific turbo modules or
50
- * fabric components should be code generated.
51
- */
52
- const rawCodegenConfig = this.rawPackageJSON.harmony?.codegenConfig;
53
- if (!rawCodegenConfig) {
54
- return null;
55
- }
56
- return CodegenConfig.fromRawCodegenConfig(
57
- rawCodegenConfig,
58
- this.packageRootPath,
59
- this.projectRootPath,
60
- this.name
61
- );
62
- }
63
- }
1
+ import fs from 'fs';
2
+ import { AbsolutePath } from './AbsolutePath';
3
+ import { CodegenConfig } from './CodegenConfig';
4
+ import { DescriptiveError } from './DescriptiveError';
5
+
6
+ export class PackageJSON {
7
+ static fromProjectRootPath(
8
+ packageRootPath: AbsolutePath,
9
+ projectRootPath: AbsolutePath
10
+ ) {
11
+ const packageJSONPath = packageRootPath.copyWithNewSegment('package.json');
12
+ if (!fs.existsSync(packageJSONPath.getValue())) {
13
+ throw new DescriptiveError({
14
+ whatHappened: "Couldn't find 'package.json'",
15
+ whatCanUserDo: {
16
+ default: [
17
+ 'Check if the path to project root is correct',
18
+ 'Try changing working directory to project root',
19
+ ],
20
+ },
21
+ extraData: packageJSONPath.getValue(),
22
+ });
23
+ }
24
+ return new PackageJSON(
25
+ JSON.parse(fs.readFileSync(packageJSONPath.getValue()).toString()),
26
+ packageRootPath,
27
+ projectRootPath
28
+ );
29
+ }
30
+
31
+ private constructor(
32
+ private rawPackageJSON: Record<string, any>,
33
+ private packageRootPath: AbsolutePath,
34
+ private projectRootPath: AbsolutePath
35
+ ) {}
36
+
37
+ get name(): string {
38
+ return this.rawPackageJSON.name;
39
+ }
40
+
41
+ get version(): string {
42
+ return this.rawPackageJSON.version;
43
+ }
44
+
45
+ getCodegenConfigs(): CodegenConfig[] {
46
+ /**
47
+ * Shared codegenConfig is not used on purpose. Android, iOS and other platforms shouldn't
48
+ * generate code for harmony-specific packages. The shape of this codegenConfig is different
49
+ * that the shape of the shared config, to allow specifying which specific turbo modules or
50
+ * fabric components should be code generated.
51
+ */
52
+ const rawCodegenConfig = this.rawPackageJSON.harmony?.codegenConfig;
53
+ if (!rawCodegenConfig) {
54
+ return [];
55
+ }
56
+ if (Array.isArray(rawCodegenConfig)) {
57
+ return rawCodegenConfig.map((config) =>
58
+ CodegenConfig.fromRawCodegenConfig(
59
+ config,
60
+ this.packageRootPath,
61
+ this.projectRootPath,
62
+ this.name
63
+ )
64
+ );
65
+ }
66
+ return [
67
+ CodegenConfig.fromRawCodegenConfig(
68
+ rawCodegenConfig,
69
+ this.packageRootPath,
70
+ this.projectRootPath,
71
+ this.name
72
+ ),
73
+ ];
74
+ }
75
+ }
@@ -1,77 +1,77 @@
1
- import fs from 'node:fs';
2
- import { AbsolutePath } from './AbsolutePath';
3
- import { PackageJSON } from './PackageJSON';
4
-
5
- export class ProjectDependency {
6
- constructor(
7
- private packageRootPath: AbsolutePath,
8
- private projectRootPath: AbsolutePath
9
- ) {}
10
-
11
- getRootPath() {
12
- return this.packageRootPath;
13
- }
14
-
15
- maybeCreateCodegenConfig() {
16
- return this.readPackageJSON().maybeCreateCodegenConfig();
17
- }
18
-
19
- readPackageJSON() {
20
- return PackageJSON.fromProjectRootPath(
21
- this.packageRootPath,
22
- this.projectRootPath
23
- );
24
- }
25
- }
26
-
27
- export class ProjectDependenciesManager {
28
- constructor(private projectRootPath: AbsolutePath) {}
29
-
30
- async forEachAsync(
31
- cb: (dependency: ProjectDependency) => Promise<void> | void
32
- ) {
33
- await this.forEachDependencyInDirectory(
34
- cb,
35
- this.projectRootPath.copyWithNewSegment('node_modules')
36
- );
37
- }
38
-
39
- private async forEachDependencyInDirectory(
40
- cb: (dependency: ProjectDependency) => Promise<void> | void,
41
- directoryPath: AbsolutePath
42
- ) {
43
- if (!fs.existsSync(directoryPath.getValue())) {
44
- return;
45
- }
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('.')) {
51
- continue;
52
- } else if (dirOrFileName.startsWith('@')) {
53
- await this.forEachDependencyInDirectory(
54
- cb,
55
- directoryPath.copyWithNewSegment(dirOrFileName)
56
- );
57
- } else {
58
- if (
59
- !fs.existsSync(
60
- potentialDependencyRootPath
61
- .copyWithNewSegment('package.json')
62
- .getValue()
63
- )
64
- ) {
65
- continue;
66
- }
67
- await cb(
68
- new ProjectDependency(
69
- potentialDependencyRootPath,
70
- this.projectRootPath
71
- )
72
- );
73
- }
74
- }
75
- }
76
- }
77
- }
1
+ import fs from 'node:fs';
2
+ import { AbsolutePath } from './AbsolutePath';
3
+ import { PackageJSON } from './PackageJSON';
4
+
5
+ export class ProjectDependency {
6
+ constructor(
7
+ private packageRootPath: AbsolutePath,
8
+ private projectRootPath: AbsolutePath
9
+ ) {}
10
+
11
+ getRootPath() {
12
+ return this.packageRootPath;
13
+ }
14
+
15
+ getCodegenConfigs() {
16
+ return this.readPackageJSON().getCodegenConfigs();
17
+ }
18
+
19
+ readPackageJSON() {
20
+ return PackageJSON.fromProjectRootPath(
21
+ this.packageRootPath,
22
+ this.projectRootPath
23
+ );
24
+ }
25
+ }
26
+
27
+ export class ProjectDependenciesManager {
28
+ constructor(private projectRootPath: AbsolutePath) {}
29
+
30
+ async forEachAsync(
31
+ cb: (dependency: ProjectDependency) => Promise<void> | void
32
+ ) {
33
+ await this.forEachDependencyInDirectory(
34
+ cb,
35
+ this.projectRootPath.copyWithNewSegment('node_modules')
36
+ );
37
+ }
38
+
39
+ private async forEachDependencyInDirectory(
40
+ cb: (dependency: ProjectDependency) => Promise<void> | void,
41
+ directoryPath: AbsolutePath
42
+ ) {
43
+ if (!fs.existsSync(directoryPath.getValue())) {
44
+ return;
45
+ }
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('.')) {
51
+ continue;
52
+ } else if (dirOrFileName.startsWith('@')) {
53
+ await this.forEachDependencyInDirectory(
54
+ cb,
55
+ directoryPath.copyWithNewSegment(dirOrFileName)
56
+ );
57
+ } else {
58
+ if (
59
+ !fs.existsSync(
60
+ potentialDependencyRootPath
61
+ .copyWithNewSegment('package.json')
62
+ .getValue()
63
+ )
64
+ ) {
65
+ continue;
66
+ }
67
+ await cb(
68
+ new ProjectDependency(
69
+ potentialDependencyRootPath,
70
+ this.projectRootPath
71
+ )
72
+ );
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }