@react-native-oh/react-native-harmony-cli 0.0.15 → 0.0.23

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 (85) hide show
  1. package/.prettierrc +6 -6
  2. package/dist/assetResolver.js +23 -5
  3. package/dist/codegen/core/Codegen.js +35 -0
  4. package/dist/codegen/core/CodegenError.js +7 -0
  5. package/dist/codegen/core/EventUtils.js +19 -0
  6. package/dist/codegen/core/TypeAnnotationToTS.js +132 -0
  7. package/dist/codegen/core/UberSchema.js +74 -0
  8. package/dist/codegen/core/index.js +21 -0
  9. package/dist/codegen/generators/ComponentCodeGenerator.js +66 -0
  10. package/dist/codegen/generators/NativeModuleCodeGenerator.js +64 -0
  11. package/dist/codegen/generators/PackageCodeGenerator.js +52 -0
  12. package/dist/codegen/generators/index.js +19 -0
  13. package/dist/codegen/index.js +18 -0
  14. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +55 -0
  15. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +78 -0
  16. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +181 -0
  17. package/dist/codegen/templates/IndexTSTemplate.js +31 -0
  18. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +117 -0
  19. package/dist/codegen/templates/TurboModuleCppTemplate.js +45 -0
  20. package/dist/codegen/templates/TurboModuleHTemplate.js +32 -0
  21. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +64 -0
  22. package/dist/codegen/templates/index.js +24 -0
  23. package/dist/commands/codegen-harmony.js +110 -0
  24. package/dist/commands/create-metro-host-constants.js +61 -0
  25. package/dist/commands/index.js +2 -0
  26. package/dist/commands/pack-harmony.js +4 -1
  27. package/dist/commands/unpack-harmony.js +86 -43
  28. package/dist/core/AbsolutePath.js +41 -0
  29. package/dist/core/CodegenConfig.js +53 -0
  30. package/dist/core/DescriptiveError.js +49 -0
  31. package/dist/core/PackageJSON.js +53 -0
  32. package/dist/core/ProjectDependenciesManager.js +70 -0
  33. package/dist/core/ValueObject.js +2 -0
  34. package/dist/core/fs.js +36 -0
  35. package/dist/core/index.js +7 -0
  36. package/dist/index.js +7 -1
  37. package/dist/io/Logger.js +71 -0
  38. package/dist/io/fsUtils.js +23 -0
  39. package/dist/io/index.js +18 -0
  40. package/jest.config.js +5 -5
  41. package/package.json +52 -47
  42. package/react-native.config.js +10 -10
  43. package/src/assetResolver.ts +44 -25
  44. package/src/codegen/core/Codegen.ts +44 -0
  45. package/src/codegen/core/CodegenError.ts +3 -0
  46. package/src/codegen/core/EventUtils.ts +18 -0
  47. package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -0
  48. package/src/codegen/core/TypeAnnotationToTS.ts +188 -0
  49. package/src/codegen/core/UberSchema.ts +99 -0
  50. package/src/codegen/core/index.ts +5 -0
  51. package/src/codegen/generators/ComponentCodeGenerator.ts +98 -0
  52. package/src/codegen/generators/NativeModuleCodeGenerator.ts +95 -0
  53. package/src/codegen/generators/PackageCodeGenerator.ts +73 -0
  54. package/src/codegen/generators/index.ts +3 -0
  55. package/src/codegen/index.ts +2 -0
  56. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +49 -0
  57. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +85 -0
  58. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +202 -0
  59. package/src/codegen/templates/IndexTSTemplate.ts +33 -0
  60. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +124 -0
  61. package/src/codegen/templates/TurboModuleCppTemplate.ts +47 -0
  62. package/src/codegen/templates/TurboModuleHTemplate.ts +26 -0
  63. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +81 -0
  64. package/src/codegen/templates/index.ts +8 -0
  65. package/src/commands/bundle-harmony.ts +202 -202
  66. package/src/commands/codegen-harmony.ts +143 -0
  67. package/src/commands/create-metro-host-constants.ts +48 -0
  68. package/src/commands/index.ts +5 -3
  69. package/src/commands/pack-harmony.ts +121 -118
  70. package/src/commands/unpack-harmony.ts +231 -156
  71. package/src/core/AbsolutePath.ts +43 -0
  72. package/src/core/CodegenConfig.ts +54 -0
  73. package/src/core/DescriptiveError.ts +61 -0
  74. package/src/core/PackageJSON.ts +63 -0
  75. package/src/core/ProjectDependenciesManager.ts +77 -0
  76. package/src/core/ValidationError.ts +1 -1
  77. package/src/core/ValueObject.ts +2 -0
  78. package/src/core/fs.ts +31 -0
  79. package/src/core/index.ts +8 -1
  80. package/src/index.ts +18 -10
  81. package/src/io/Logger.ts +68 -0
  82. package/src/io/fsUtils.ts +24 -0
  83. package/src/io/index.ts +2 -0
  84. package/tsconfig.build.json +6 -5
  85. package/tsconfig.json +2 -2
@@ -0,0 +1,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
+ 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
+ }
@@ -0,0 +1,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
+ };
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
+ }
@@ -0,0 +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
+ }
@@ -0,0 +1,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
+ 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
+ }
@@ -0,0 +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 +1 @@
1
- export class ValidationError extends Error {}
1
+ export class ValidationError extends Error {}
@@ -0,0 +1,2 @@
1
+ /** https://en.wikipedia.org/wiki/Value_object */
2
+ export interface ValueObject {}
package/src/core/fs.ts ADDED
@@ -0,0 +1,31 @@
1
+ import fs from 'node:fs';
2
+ import { AbsolutePath } from './AbsolutePath';
3
+ import { DescriptiveError } from './DescriptiveError';
4
+
5
+ export function maybeMakeDirectories(path: AbsolutePath): void {
6
+ if (!fs.existsSync(path.getValue())) {
7
+ fs.mkdirSync(path.getValue(), { recursive: true });
8
+ }
9
+ }
10
+
11
+ export function maybeRemoveFilesInDirectory(path: AbsolutePath): void {
12
+ if (!fs.existsSync(path.getValue())) {
13
+ return;
14
+ }
15
+ if (!fs.lstatSync(path.getValue()).isDirectory()) {
16
+ throw new DescriptiveError({
17
+ whatHappened:
18
+ "Tried to remove all files from a path that doesn't point to a directory",
19
+ whatCanUserDo: { default: [`Verify path: ${path.getValue()}`] },
20
+ });
21
+ }
22
+ const fileOrDirNames = fs.readdirSync(path.getValue());
23
+ fileOrDirNames.forEach((fileOrDirName) => {
24
+ const maybeFilePath = path.copyWithNewSegment(fileOrDirName);
25
+ if (fs.statSync(maybeFilePath.getValue()).isDirectory()) {
26
+ return;
27
+ } else {
28
+ fs.unlinkSync(maybeFilePath.getValue());
29
+ }
30
+ });
31
+ }
package/src/core/index.ts CHANGED
@@ -1 +1,8 @@
1
- export * from './ValidationError';
1
+ export * from './AbsolutePath';
2
+ export * from './ValidationError';
3
+ export * from './DescriptiveError';
4
+ export * from './ValueObject';
5
+ export * from './PackageJSON';
6
+ export * from './ProjectDependenciesManager';
7
+ export * from './CodegenConfig';
8
+ export * from './fs';
package/src/index.ts CHANGED
@@ -1,10 +1,18 @@
1
- import { Config } from '@react-native-community/cli-types';
2
- import {
3
- commandBundleHarmony,
4
- commandPackHarmony,
5
- commandUnpackHarmony,
6
- } from './commands';
7
-
8
- export const config = {
9
- commands: [commandBundleHarmony, commandPackHarmony, commandUnpackHarmony],
10
- } satisfies Partial<Config>;
1
+ import { Config } from '@react-native-community/cli-types';
2
+ import {
3
+ commandBundleHarmony,
4
+ commandPackHarmony,
5
+ commandUnpackHarmony,
6
+ commandCreateMetroConstantsHarmony,
7
+ commandCodegenHarmony,
8
+ } from './commands';
9
+
10
+ export const config = {
11
+ commands: [
12
+ commandBundleHarmony,
13
+ commandPackHarmony,
14
+ commandUnpackHarmony,
15
+ commandCreateMetroConstantsHarmony,
16
+ commandCodegenHarmony,
17
+ ],
18
+ } satisfies Partial<Config>;
@@ -0,0 +1,68 @@
1
+ import chalk from 'chalk';
2
+ import { DescriptiveError } from '../core';
3
+
4
+ export class Logger {
5
+ info(
6
+ prepareMsg: (styles: typeof chalk) => string,
7
+ options?: { prefix?: boolean }
8
+ ) {
9
+ const prefix = options?.prefix ? `[${chalk.bold(chalk.cyan('INFO'))}]` : '';
10
+ if (prefix) {
11
+ console.info(prefix, prepareMsg(chalk));
12
+ } else {
13
+ console.info(prepareMsg(chalk));
14
+ }
15
+ }
16
+
17
+ debug(prepareMsg: (styles: typeof chalk) => string) {
18
+ console.debug(prepareMsg(chalk));
19
+ }
20
+
21
+ descriptiveError(error: DescriptiveError) {
22
+ const lines: string[] = [];
23
+ if (error.isUnexpected() && error.stack) {
24
+ lines.push('');
25
+ lines.push(chalk.bold('Stack'));
26
+ error.stack.split('\n').forEach((line) => lines.push(chalk.gray(line)));
27
+ }
28
+ const details = error.getDetails();
29
+ if (details) {
30
+ lines.push('');
31
+ lines.push(chalk.bold('Details'));
32
+ details.split('\n').forEach((line) => lines.push(line));
33
+ }
34
+ lines.push('');
35
+ let prefix = '';
36
+ if (error.isUnexpected()) {
37
+ prefix = '[' + chalk.red(chalk.bold('UNEXPECTED_ERROR')) + ']';
38
+ } else {
39
+ prefix = '[' + chalk.red(chalk.bold('ERROR')) + ']';
40
+ }
41
+ lines.push(`${prefix} ${error.getMessage()}`);
42
+ const suggestionsByRole = error.getSuggestionsByRole();
43
+ if (suggestionsByRole) {
44
+ if ((suggestionsByRole.default?.length ?? 0) > 0) {
45
+ lines.push('');
46
+ lines.push(chalk.bold(`Suggestions`));
47
+ suggestionsByRole.default?.forEach((suggestion) => {
48
+ lines.push(suggestion);
49
+ });
50
+ }
51
+ if ((suggestionsByRole.rnAppDeveloper?.length ?? 0) > 0) {
52
+ lines.push('');
53
+ lines.push(chalk.bold(`Suggestions for RN App developers`));
54
+ suggestionsByRole.rnAppDeveloper?.forEach((suggestion) => {
55
+ lines.push(suggestion);
56
+ });
57
+ }
58
+ if (suggestionsByRole.rnPackageDeveloper?.length ?? 0) {
59
+ lines.push('');
60
+ lines.push(chalk.bold(`Suggestions for RN package developers`));
61
+ suggestionsByRole.rnPackageDeveloper?.forEach((suggestion) => {
62
+ lines.push(suggestion);
63
+ });
64
+ }
65
+ }
66
+ console.error(lines.join('\n'));
67
+ }
68
+ }
@@ -0,0 +1,24 @@
1
+ import pathUtils from 'node:path';
2
+ import fs from 'node:fs';
3
+ import { AbsolutePath } from '../core';
4
+
5
+ export function findFilePathsWithExtensions(
6
+ dirPath: AbsolutePath,
7
+ fileExtensions: readonly string[]
8
+ ): AbsolutePath[] {
9
+ let results: AbsolutePath[] = [];
10
+ const files = fs.readdirSync(dirPath.getValue(), { withFileTypes: true });
11
+ for (const file of files) {
12
+ const path = dirPath.copyWithNewSegment(file.name);
13
+ if (file.isDirectory()) {
14
+ results = results.concat(
15
+ findFilePathsWithExtensions(path, fileExtensions)
16
+ );
17
+ } else if (
18
+ fileExtensions.includes(pathUtils.extname(file.name).substring(1))
19
+ ) {
20
+ results.push(path);
21
+ }
22
+ }
23
+ return results;
24
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Logger';
2
+ export * from './fsUtils';
@@ -1,6 +1,7 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "exclude": [
4
- "tests"
5
- ]
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": [
4
+ "tests",
5
+ "**/*.test.ts",
6
+ ]
6
7
  }
package/tsconfig.json CHANGED
@@ -1,3 +1,3 @@
1
- {
2
- "extends": "@tsconfig/recommended/tsconfig.json",
1
+ {
2
+ "extends": "@tsconfig/recommended/tsconfig.json",
3
3
  }