@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.
- package/.prettierrc +6 -6
- package/dist/assetResolver.js +23 -5
- package/dist/codegen/core/Codegen.js +35 -0
- package/dist/codegen/core/CodegenError.js +7 -0
- package/dist/codegen/core/EventUtils.js +19 -0
- package/dist/codegen/core/TypeAnnotationToTS.js +132 -0
- package/dist/codegen/core/UberSchema.js +74 -0
- package/dist/codegen/core/index.js +21 -0
- package/dist/codegen/generators/ComponentCodeGenerator.js +66 -0
- package/dist/codegen/generators/NativeModuleCodeGenerator.js +64 -0
- package/dist/codegen/generators/PackageCodeGenerator.js +52 -0
- package/dist/codegen/generators/index.js +19 -0
- package/dist/codegen/index.js +18 -0
- package/dist/codegen/templates/ComponentDescriptorHTemplate.js +55 -0
- package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +78 -0
- package/dist/codegen/templates/ComponentUtilsTSTemplate.js +181 -0
- package/dist/codegen/templates/IndexTSTemplate.js +31 -0
- package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +117 -0
- package/dist/codegen/templates/TurboModuleCppTemplate.js +45 -0
- package/dist/codegen/templates/TurboModuleHTemplate.js +32 -0
- package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +64 -0
- package/dist/codegen/templates/index.js +24 -0
- package/dist/commands/codegen-harmony.js +110 -0
- package/dist/commands/create-metro-host-constants.js +61 -0
- package/dist/commands/index.js +2 -0
- package/dist/commands/pack-harmony.js +4 -1
- package/dist/commands/unpack-harmony.js +86 -43
- package/dist/core/AbsolutePath.js +41 -0
- package/dist/core/CodegenConfig.js +53 -0
- package/dist/core/DescriptiveError.js +49 -0
- package/dist/core/PackageJSON.js +53 -0
- package/dist/core/ProjectDependenciesManager.js +70 -0
- package/dist/core/ValueObject.js +2 -0
- package/dist/core/fs.js +36 -0
- package/dist/core/index.js +7 -0
- package/dist/index.js +7 -1
- package/dist/io/Logger.js +71 -0
- package/dist/io/fsUtils.js +23 -0
- package/dist/io/index.js +18 -0
- package/jest.config.js +5 -5
- package/package.json +52 -47
- package/react-native.config.js +10 -10
- package/src/assetResolver.ts +44 -25
- package/src/codegen/core/Codegen.ts +44 -0
- package/src/codegen/core/CodegenError.ts +3 -0
- package/src/codegen/core/EventUtils.ts +18 -0
- package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -0
- package/src/codegen/core/TypeAnnotationToTS.ts +188 -0
- package/src/codegen/core/UberSchema.ts +99 -0
- package/src/codegen/core/index.ts +5 -0
- package/src/codegen/generators/ComponentCodeGenerator.ts +98 -0
- package/src/codegen/generators/NativeModuleCodeGenerator.ts +95 -0
- package/src/codegen/generators/PackageCodeGenerator.ts +73 -0
- package/src/codegen/generators/index.ts +3 -0
- package/src/codegen/index.ts +2 -0
- package/src/codegen/templates/ComponentDescriptorHTemplate.ts +49 -0
- package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +85 -0
- package/src/codegen/templates/ComponentUtilsTSTemplate.ts +202 -0
- package/src/codegen/templates/IndexTSTemplate.ts +33 -0
- package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +124 -0
- package/src/codegen/templates/TurboModuleCppTemplate.ts +47 -0
- package/src/codegen/templates/TurboModuleHTemplate.ts +26 -0
- package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +81 -0
- package/src/codegen/templates/index.ts +8 -0
- package/src/commands/bundle-harmony.ts +202 -202
- package/src/commands/codegen-harmony.ts +143 -0
- package/src/commands/create-metro-host-constants.ts +48 -0
- package/src/commands/index.ts +5 -3
- package/src/commands/pack-harmony.ts +121 -118
- package/src/commands/unpack-harmony.ts +231 -156
- package/src/core/AbsolutePath.ts +43 -0
- package/src/core/CodegenConfig.ts +54 -0
- package/src/core/DescriptiveError.ts +61 -0
- package/src/core/PackageJSON.ts +63 -0
- package/src/core/ProjectDependenciesManager.ts +77 -0
- package/src/core/ValidationError.ts +1 -1
- package/src/core/ValueObject.ts +2 -0
- package/src/core/fs.ts +31 -0
- package/src/core/index.ts +8 -1
- package/src/index.ts +18 -10
- package/src/io/Logger.ts +68 -0
- package/src/io/fsUtils.ts +24 -0
- package/src/io/index.ts +2 -0
- package/tsconfig.build.json +6 -5
- package/tsconfig.json +2 -2
|
@@ -1,202 +1,202 @@
|
|
|
1
|
-
import { Command } from '@react-native-community/cli-types';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import fse from 'fs-extra';
|
|
4
|
-
import Metro from 'metro';
|
|
5
|
-
import { RunBuildOptions as BuildOptions } from 'metro';
|
|
6
|
-
import MetroServer from 'metro/src/Server';
|
|
7
|
-
import pathUtils from 'path';
|
|
8
|
-
import { getAssetDestRelativePath } from '../assetResolver';
|
|
9
|
-
import { ConfigT as MetroConfig } from 'metro-config';
|
|
10
|
-
|
|
11
|
-
const ARK_RESOURCE_PATH = './harmony/entry/src/main/resources/rawfile';
|
|
12
|
-
const ASSETS_DEFAULT_DEST_PATH = './harmony/entry/src/main/resources/rawfile/assets';
|
|
13
|
-
|
|
14
|
-
type AssetData = Metro.AssetData;
|
|
15
|
-
type Bundle = { code: string; map: string };
|
|
16
|
-
type Path = string;
|
|
17
|
-
|
|
18
|
-
export const commandBundleHarmony: Command = {
|
|
19
|
-
name: 'bundle-harmony',
|
|
20
|
-
description:
|
|
21
|
-
'Creates JS bundle, creates a special cpp header containing the JS code, copies assets directory to the project.',
|
|
22
|
-
options: [
|
|
23
|
-
{
|
|
24
|
-
name: '--dev [boolean]',
|
|
25
|
-
description: 'If false, warnings are disabled and the bundle is minified',
|
|
26
|
-
parse: (val: string) => val !== 'false',
|
|
27
|
-
default: true,
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
name: '--entry-file <path>',
|
|
31
|
-
description:
|
|
32
|
-
'Path to the root JS file, either absolute or relative to JS root',
|
|
33
|
-
default: 'index.js',
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
name: '--config <path>',
|
|
37
|
-
description: 'Path to the Metro configuration file',
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
name: '--bundle-output <path>',
|
|
41
|
-
description:
|
|
42
|
-
'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
|
|
43
|
-
default: ARK_RESOURCE_PATH + '/bundle.harmony.js',
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
name: '--assets-dest <path>',
|
|
47
|
-
description:
|
|
48
|
-
'Directory name where to store assets referenced in the bundle',
|
|
49
|
-
default: ASSETS_DEFAULT_DEST_PATH,
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
name: '--sourcemap-output <path>',
|
|
53
|
-
description:
|
|
54
|
-
'File name where to store the resulting source map, ex. /tmp/groups.map',
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
name: '--minify [boolean]',
|
|
58
|
-
description: 'Allows overriding whether bundle is minified',
|
|
59
|
-
parse: (val: string) => val !== 'false',
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
func: async (argv, config, args: any) => {
|
|
63
|
-
const buildOptions: BuildOptions = {
|
|
64
|
-
entry: args.entryFile,
|
|
65
|
-
platform: 'harmony',
|
|
66
|
-
minify: args.minify !== undefined ? args.minify : !args.dev,
|
|
67
|
-
dev: args.dev,
|
|
68
|
-
sourceMap: args.sourcemapOutput,
|
|
69
|
-
sourceMapUrl: args.sourcemapOutput,
|
|
70
|
-
};
|
|
71
|
-
const metroConfig = await loadMetroConfig(args.config);
|
|
72
|
-
const bundle = await createBundle(metroConfig, buildOptions);
|
|
73
|
-
await saveBundle(bundle, args.bundleOutput, args.sourcemapOutput);
|
|
74
|
-
const assets = await retrieveAssetsData(metroConfig, buildOptions);
|
|
75
|
-
copyAssets(assets, args.assetsDest);
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
async function loadMetroConfig(
|
|
80
|
-
configPath: Path | undefined
|
|
81
|
-
): Promise<MetroConfig> {
|
|
82
|
-
return configPath
|
|
83
|
-
? await Metro.loadConfig({ config: configPath })
|
|
84
|
-
: await Metro.loadConfig();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function createBundle(
|
|
88
|
-
metroConfig: MetroConfig,
|
|
89
|
-
buildOptions: BuildOptions
|
|
90
|
-
): Promise<Bundle> {
|
|
91
|
-
// casting needed as Metro.runBuild returns Promise<{code: string, map: string}>
|
|
92
|
-
// despite being typed as Promise<void>
|
|
93
|
-
return (await Metro.runBuild(metroConfig, buildOptions)) as unknown as Bundle;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function saveBundle(
|
|
97
|
-
bundle: Bundle,
|
|
98
|
-
bundleOutput: Path,
|
|
99
|
-
sourceMapOutput: Path | undefined
|
|
100
|
-
) {
|
|
101
|
-
await fse.ensureDir(pathUtils.dirname(bundleOutput));
|
|
102
|
-
fs.writeFileSync(bundleOutput, bundle.code);
|
|
103
|
-
console.log(`[CREATED] ${bundleOutput}`);
|
|
104
|
-
if (sourceMapOutput) {
|
|
105
|
-
fs.writeFileSync(sourceMapOutput, bundle.map);
|
|
106
|
-
console.log(`[CREATED] ${sourceMapOutput}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function retrieveAssetsData(
|
|
111
|
-
metroConfig: MetroConfig,
|
|
112
|
-
buildOptions: BuildOptions
|
|
113
|
-
): Promise<readonly AssetData[]> {
|
|
114
|
-
const metroServer = new MetroServer(metroConfig);
|
|
115
|
-
try {
|
|
116
|
-
return await metroServer.getAssets({
|
|
117
|
-
...MetroServer.DEFAULT_BUNDLE_OPTIONS,
|
|
118
|
-
...buildOptions,
|
|
119
|
-
entryFile: buildOptions.entry,
|
|
120
|
-
bundleType: 'todo',
|
|
121
|
-
});
|
|
122
|
-
} finally {
|
|
123
|
-
metroServer.end();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function copyAssets(
|
|
128
|
-
assetsData: readonly AssetData[],
|
|
129
|
-
assetsDest: Path
|
|
130
|
-
): Promise<void> {
|
|
131
|
-
if (assetsDest == null) {
|
|
132
|
-
console.warn('Assets destination folder is not set, skipping...');
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
await fse.ensureDir(assetsDest);
|
|
136
|
-
const fileDestBySrc: Record<Path, Path> = {};
|
|
137
|
-
for (const asset of assetsData) {
|
|
138
|
-
const idx = getHighestQualityFileIdx(asset);
|
|
139
|
-
fileDestBySrc[asset.files[idx]] = pathUtils.join(
|
|
140
|
-
assetsDest,
|
|
141
|
-
getAssetDestRelativePath(asset)
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
return copyFiles(fileDestBySrc);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function getHighestQualityFileIdx(assetData: AssetData): number {
|
|
148
|
-
let result = 0;
|
|
149
|
-
let maxScale = -1;
|
|
150
|
-
for (let idx = 0; idx < assetData.scales.length; idx++) {
|
|
151
|
-
const scale = assetData.scales[idx];
|
|
152
|
-
if (scale > maxScale) {
|
|
153
|
-
maxScale = scale;
|
|
154
|
-
result = idx;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return result;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function copyFiles(fileDestBySrc: Record<Path, Path>) {
|
|
161
|
-
const fileSources = Object.keys(fileDestBySrc);
|
|
162
|
-
if (fileSources.length === 0) {
|
|
163
|
-
return Promise.resolve();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
console.info(`Copying ${fileSources.length} asset files`);
|
|
167
|
-
return new Promise<void>((resolve, reject) => {
|
|
168
|
-
const copyNext = (error?: Error) => {
|
|
169
|
-
if (error) {
|
|
170
|
-
reject(error);
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (fileSources.length === 0) {
|
|
174
|
-
console.info('Done copying assets');
|
|
175
|
-
resolve();
|
|
176
|
-
} else {
|
|
177
|
-
// fileSources.length === 0 is checked in previous branch, so this is string
|
|
178
|
-
const src = fileSources.shift()!;
|
|
179
|
-
const dest = fileDestBySrc[src];
|
|
180
|
-
copyFile(src, dest, copyNext);
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
copyNext();
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function copyFile(
|
|
188
|
-
src: string,
|
|
189
|
-
dest: string,
|
|
190
|
-
onFinished: (error: Error) => void
|
|
191
|
-
): void {
|
|
192
|
-
const destDir = pathUtils.dirname(dest);
|
|
193
|
-
fs.mkdir(destDir, { recursive: true }, (err?) => {
|
|
194
|
-
if (err) {
|
|
195
|
-
onFinished(err);
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
fs.createReadStream(src)
|
|
199
|
-
.pipe(fs.createWriteStream(dest))
|
|
200
|
-
.on('finish', onFinished);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
1
|
+
import { Command } from '@react-native-community/cli-types';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import fse from 'fs-extra';
|
|
4
|
+
import Metro from 'metro';
|
|
5
|
+
import { RunBuildOptions as BuildOptions } from 'metro';
|
|
6
|
+
import MetroServer from 'metro/src/Server';
|
|
7
|
+
import pathUtils from 'path';
|
|
8
|
+
import { getAssetDestRelativePath } from '../assetResolver';
|
|
9
|
+
import { ConfigT as MetroConfig } from 'metro-config';
|
|
10
|
+
|
|
11
|
+
const ARK_RESOURCE_PATH = './harmony/entry/src/main/resources/rawfile';
|
|
12
|
+
const ASSETS_DEFAULT_DEST_PATH = './harmony/entry/src/main/resources/rawfile/assets';
|
|
13
|
+
|
|
14
|
+
type AssetData = Metro.AssetData;
|
|
15
|
+
type Bundle = { code: string; map: string };
|
|
16
|
+
type Path = string;
|
|
17
|
+
|
|
18
|
+
export const commandBundleHarmony: Command = {
|
|
19
|
+
name: 'bundle-harmony',
|
|
20
|
+
description:
|
|
21
|
+
'Creates JS bundle, creates a special cpp header containing the JS code, copies assets directory to the project.',
|
|
22
|
+
options: [
|
|
23
|
+
{
|
|
24
|
+
name: '--dev [boolean]',
|
|
25
|
+
description: 'If false, warnings are disabled and the bundle is minified',
|
|
26
|
+
parse: (val: string) => val !== 'false',
|
|
27
|
+
default: true,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: '--entry-file <path>',
|
|
31
|
+
description:
|
|
32
|
+
'Path to the root JS file, either absolute or relative to JS root',
|
|
33
|
+
default: 'index.js',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: '--config <path>',
|
|
37
|
+
description: 'Path to the Metro configuration file',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: '--bundle-output <path>',
|
|
41
|
+
description:
|
|
42
|
+
'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
|
|
43
|
+
default: ARK_RESOURCE_PATH + '/bundle.harmony.js',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: '--assets-dest <path>',
|
|
47
|
+
description:
|
|
48
|
+
'Directory name where to store assets referenced in the bundle',
|
|
49
|
+
default: ASSETS_DEFAULT_DEST_PATH,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: '--sourcemap-output <path>',
|
|
53
|
+
description:
|
|
54
|
+
'File name where to store the resulting source map, ex. /tmp/groups.map',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: '--minify [boolean]',
|
|
58
|
+
description: 'Allows overriding whether bundle is minified',
|
|
59
|
+
parse: (val: string) => val !== 'false',
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
func: async (argv, config, args: any) => {
|
|
63
|
+
const buildOptions: BuildOptions = {
|
|
64
|
+
entry: args.entryFile,
|
|
65
|
+
platform: 'harmony',
|
|
66
|
+
minify: args.minify !== undefined ? args.minify : !args.dev,
|
|
67
|
+
dev: args.dev,
|
|
68
|
+
sourceMap: args.sourcemapOutput,
|
|
69
|
+
sourceMapUrl: args.sourcemapOutput,
|
|
70
|
+
};
|
|
71
|
+
const metroConfig = await loadMetroConfig(args.config);
|
|
72
|
+
const bundle = await createBundle(metroConfig, buildOptions);
|
|
73
|
+
await saveBundle(bundle, args.bundleOutput, args.sourcemapOutput);
|
|
74
|
+
const assets = await retrieveAssetsData(metroConfig, buildOptions);
|
|
75
|
+
copyAssets(assets, args.assetsDest);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
async function loadMetroConfig(
|
|
80
|
+
configPath: Path | undefined
|
|
81
|
+
): Promise<MetroConfig> {
|
|
82
|
+
return configPath
|
|
83
|
+
? await Metro.loadConfig({ config: configPath })
|
|
84
|
+
: await Metro.loadConfig();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function createBundle(
|
|
88
|
+
metroConfig: MetroConfig,
|
|
89
|
+
buildOptions: BuildOptions
|
|
90
|
+
): Promise<Bundle> {
|
|
91
|
+
// casting needed as Metro.runBuild returns Promise<{code: string, map: string}>
|
|
92
|
+
// despite being typed as Promise<void>
|
|
93
|
+
return (await Metro.runBuild(metroConfig, buildOptions)) as unknown as Bundle;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function saveBundle(
|
|
97
|
+
bundle: Bundle,
|
|
98
|
+
bundleOutput: Path,
|
|
99
|
+
sourceMapOutput: Path | undefined
|
|
100
|
+
) {
|
|
101
|
+
await fse.ensureDir(pathUtils.dirname(bundleOutput));
|
|
102
|
+
fs.writeFileSync(bundleOutput, bundle.code);
|
|
103
|
+
console.log(`[CREATED] ${bundleOutput}`);
|
|
104
|
+
if (sourceMapOutput) {
|
|
105
|
+
fs.writeFileSync(sourceMapOutput, bundle.map);
|
|
106
|
+
console.log(`[CREATED] ${sourceMapOutput}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function retrieveAssetsData(
|
|
111
|
+
metroConfig: MetroConfig,
|
|
112
|
+
buildOptions: BuildOptions
|
|
113
|
+
): Promise<readonly AssetData[]> {
|
|
114
|
+
const metroServer = new MetroServer(metroConfig);
|
|
115
|
+
try {
|
|
116
|
+
return await metroServer.getAssets({
|
|
117
|
+
...MetroServer.DEFAULT_BUNDLE_OPTIONS,
|
|
118
|
+
...buildOptions,
|
|
119
|
+
entryFile: buildOptions.entry,
|
|
120
|
+
bundleType: 'todo',
|
|
121
|
+
});
|
|
122
|
+
} finally {
|
|
123
|
+
metroServer.end();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function copyAssets(
|
|
128
|
+
assetsData: readonly AssetData[],
|
|
129
|
+
assetsDest: Path
|
|
130
|
+
): Promise<void> {
|
|
131
|
+
if (assetsDest == null) {
|
|
132
|
+
console.warn('Assets destination folder is not set, skipping...');
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
await fse.ensureDir(assetsDest);
|
|
136
|
+
const fileDestBySrc: Record<Path, Path> = {};
|
|
137
|
+
for (const asset of assetsData) {
|
|
138
|
+
const idx = getHighestQualityFileIdx(asset);
|
|
139
|
+
fileDestBySrc[asset.files[idx]] = pathUtils.join(
|
|
140
|
+
assetsDest,
|
|
141
|
+
getAssetDestRelativePath(asset)
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return copyFiles(fileDestBySrc);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function getHighestQualityFileIdx(assetData: AssetData): number {
|
|
148
|
+
let result = 0;
|
|
149
|
+
let maxScale = -1;
|
|
150
|
+
for (let idx = 0; idx < assetData.scales.length; idx++) {
|
|
151
|
+
const scale = assetData.scales[idx];
|
|
152
|
+
if (scale > maxScale) {
|
|
153
|
+
maxScale = scale;
|
|
154
|
+
result = idx;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function copyFiles(fileDestBySrc: Record<Path, Path>) {
|
|
161
|
+
const fileSources = Object.keys(fileDestBySrc);
|
|
162
|
+
if (fileSources.length === 0) {
|
|
163
|
+
return Promise.resolve();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.info(`Copying ${fileSources.length} asset files`);
|
|
167
|
+
return new Promise<void>((resolve, reject) => {
|
|
168
|
+
const copyNext = (error?: Error) => {
|
|
169
|
+
if (error) {
|
|
170
|
+
reject(error);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (fileSources.length === 0) {
|
|
174
|
+
console.info('Done copying assets');
|
|
175
|
+
resolve();
|
|
176
|
+
} else {
|
|
177
|
+
// fileSources.length === 0 is checked in previous branch, so this is string
|
|
178
|
+
const src = fileSources.shift()!;
|
|
179
|
+
const dest = fileDestBySrc[src];
|
|
180
|
+
copyFile(src, dest, copyNext);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
copyNext();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function copyFile(
|
|
188
|
+
src: string,
|
|
189
|
+
dest: string,
|
|
190
|
+
onFinished: (error: Error) => void
|
|
191
|
+
): void {
|
|
192
|
+
const destDir = pathUtils.dirname(dest);
|
|
193
|
+
fs.mkdir(destDir, { recursive: true }, (err?) => {
|
|
194
|
+
if (err) {
|
|
195
|
+
onFinished(err);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
fs.createReadStream(src)
|
|
199
|
+
.pipe(fs.createWriteStream(dest))
|
|
200
|
+
.on('finish', onFinished);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import pathUtils from 'node:path';
|
|
3
|
+
import { Command } from '@react-native-community/cli-types';
|
|
4
|
+
import {
|
|
5
|
+
AbsolutePath,
|
|
6
|
+
DescriptiveError,
|
|
7
|
+
maybeMakeDirectories,
|
|
8
|
+
maybeRemoveFilesInDirectory,
|
|
9
|
+
} from '../core';
|
|
10
|
+
import {
|
|
11
|
+
Codegen,
|
|
12
|
+
UberSchema,
|
|
13
|
+
ComponentCodeGenerator,
|
|
14
|
+
NativeModuleCodeGenerator,
|
|
15
|
+
PackageCodeGenerator,
|
|
16
|
+
} from '../codegen';
|
|
17
|
+
import { Logger } from '../io';
|
|
18
|
+
|
|
19
|
+
export const commandCodegenHarmony: Command = {
|
|
20
|
+
name: 'codegen-harmony',
|
|
21
|
+
description:
|
|
22
|
+
'Generates a glue layer and native utilities from Turbo Module or Fabric Components specifications',
|
|
23
|
+
options: [
|
|
24
|
+
{
|
|
25
|
+
name: '--rnoh-module-path <path>',
|
|
26
|
+
description: 'Specifies the relative path to the rnoh OHOS module',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: '--cpp-output-path <path>',
|
|
30
|
+
description:
|
|
31
|
+
'Specifies the relative path to the output directory intended for storing generated C++ files',
|
|
32
|
+
default: './harmony/entry/src/main/cpp/generated',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: '--project-root-path <path>',
|
|
36
|
+
description: 'Relative path to package root',
|
|
37
|
+
default: '.',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: '--debug [boolean]',
|
|
41
|
+
description: 'Enables logging details',
|
|
42
|
+
default: false,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
func: async (_argv, _config, args: any) => {
|
|
46
|
+
const logger = new Logger();
|
|
47
|
+
try {
|
|
48
|
+
validateArgs(args);
|
|
49
|
+
const etsOutputPath = new AbsolutePath(
|
|
50
|
+
args.rnohModulePath
|
|
51
|
+
).copyWithNewSegment('generated');
|
|
52
|
+
const cppOutputPath = new AbsolutePath(args.cppOutputPath);
|
|
53
|
+
const projectRootPath = new AbsolutePath(args.projectRootPath);
|
|
54
|
+
const codegen = createCodegen(cppOutputPath, etsOutputPath);
|
|
55
|
+
const uberSchema = await UberSchema.fromProjectRootPath(
|
|
56
|
+
new AbsolutePath(args.projectRootPath)
|
|
57
|
+
);
|
|
58
|
+
const fileContentByPath = codegen.generate(uberSchema);
|
|
59
|
+
if (args.debug) {
|
|
60
|
+
logger.debug((styles) =>
|
|
61
|
+
styles.gray(JSON.stringify(uberSchema.getValue(), null, 2))
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
saveCodegenResult(fileContentByPath, cppOutputPath, etsOutputPath);
|
|
65
|
+
logCodegenResult(logger, fileContentByPath, projectRootPath);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (err instanceof DescriptiveError) {
|
|
68
|
+
logger.descriptiveError(err);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
function validateArgs(args: any) {
|
|
77
|
+
if (!args.rnohModulePath) {
|
|
78
|
+
throw new DescriptiveError({
|
|
79
|
+
whatHappened: "--rnoh-module-path argument wasn't provided",
|
|
80
|
+
whatCanUserDo: {
|
|
81
|
+
default: [
|
|
82
|
+
'Please provide a path to the native React Native for Open Harmony module (rnoh) which is probably located somewhere in "<PROJECT_ROOT>/harmony" directory',
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function createCodegen(
|
|
90
|
+
cppOutputPath: AbsolutePath,
|
|
91
|
+
etsOutputPath: AbsolutePath
|
|
92
|
+
) {
|
|
93
|
+
return new Codegen(new PackageCodeGenerator(cppOutputPath, etsOutputPath), {
|
|
94
|
+
Component: new ComponentCodeGenerator(
|
|
95
|
+
cppOutputPath,
|
|
96
|
+
etsOutputPath.copyWithNewSegment('components')
|
|
97
|
+
),
|
|
98
|
+
NativeModule: new NativeModuleCodeGenerator(
|
|
99
|
+
cppOutputPath,
|
|
100
|
+
etsOutputPath.copyWithNewSegment('turboModules')
|
|
101
|
+
),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function saveCodegenResult(
|
|
106
|
+
fileContentByPath: Map<AbsolutePath, string>,
|
|
107
|
+
cppOutputPath: AbsolutePath,
|
|
108
|
+
etsOutputPath: AbsolutePath
|
|
109
|
+
) {
|
|
110
|
+
prepareDirectory(cppOutputPath);
|
|
111
|
+
prepareDirectory(etsOutputPath);
|
|
112
|
+
prepareDirectory(etsOutputPath.copyWithNewSegment('components'));
|
|
113
|
+
prepareDirectory(etsOutputPath.copyWithNewSegment('turboModules'));
|
|
114
|
+
fileContentByPath.forEach((fileContent, path) => {
|
|
115
|
+
fs.writeFileSync(path.getValue(), fileContent);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function prepareDirectory(path: AbsolutePath) {
|
|
120
|
+
maybeMakeDirectories(path);
|
|
121
|
+
maybeRemoveFilesInDirectory(path);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function logCodegenResult(
|
|
125
|
+
logger: Logger,
|
|
126
|
+
fileContentByPath: Map<AbsolutePath, string>,
|
|
127
|
+
projectRootPath: AbsolutePath
|
|
128
|
+
) {
|
|
129
|
+
const sortedRelativePathStrings = Array.from(fileContentByPath.keys()).map(
|
|
130
|
+
(path) => pathUtils.relative(projectRootPath.getValue(), path.getValue())
|
|
131
|
+
);
|
|
132
|
+
sortedRelativePathStrings.sort();
|
|
133
|
+
sortedRelativePathStrings.forEach((pathStr) => {
|
|
134
|
+
logger.info((styles) => styles.gray(`• ${pathStr}`));
|
|
135
|
+
});
|
|
136
|
+
logger.info(() => '');
|
|
137
|
+
logger.info(
|
|
138
|
+
(styles) =>
|
|
139
|
+
`Generated ${styles.green(styles.bold(fileContentByPath.size))} file(s)`,
|
|
140
|
+
{ prefix: true }
|
|
141
|
+
);
|
|
142
|
+
logger.info(() => '');
|
|
143
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Command } from '@react-native-community/cli-types';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import pathUtils from 'node:path';
|
|
5
|
+
|
|
6
|
+
export const commandCreateMetroConstantsHarmony: Command = {
|
|
7
|
+
name: 'create-metro-host-constants-harmony',
|
|
8
|
+
description:
|
|
9
|
+
'Creates a Typescript file that exports constant with IP of the host machine. That IP is later used to connect to the Metro server from a physical device.',
|
|
10
|
+
options: [
|
|
11
|
+
{
|
|
12
|
+
name: '--output <string>',
|
|
13
|
+
description: 'Output file',
|
|
14
|
+
default: './harmony/entry/src/main/ets/codegen/MetroHostConstants.ts',
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
func: async (argv, config, args: any) => {
|
|
18
|
+
createMetroHostConstants(pathUtils.resolve(args.output));
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function createMetroHostConstants(outputPath: string): void {
|
|
23
|
+
const networkInterfacesByName = os.networkInterfaces();
|
|
24
|
+
const getIP = () => {
|
|
25
|
+
for (let name of Object.keys(networkInterfacesByName)) {
|
|
26
|
+
for (let net of networkInterfacesByName[name] ?? []) {
|
|
27
|
+
// skip over non-ipv4 and internal (i.e. 127.0.0.1) addresses
|
|
28
|
+
if (net.family === 'IPv4' && !net.internal) {
|
|
29
|
+
return net.address;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
};
|
|
35
|
+
const ip = getIP();
|
|
36
|
+
const tsCode = [
|
|
37
|
+
'// This file is autogenerated. Do not modify it manually.',
|
|
38
|
+
'',
|
|
39
|
+
`export const METRO_SERVER_IP = '${ip}';\n`,
|
|
40
|
+
];
|
|
41
|
+
fs.writeFile(outputPath, tsCode.join('\n'), (err) => {
|
|
42
|
+
if (err) {
|
|
43
|
+
console.error(`Error writing to ${outputPath}:`, err);
|
|
44
|
+
} else {
|
|
45
|
+
console.log(`Created file at ${outputPath}`);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
package/src/commands/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export * from './bundle-harmony';
|
|
2
|
-
export * from './pack-harmony';
|
|
3
|
-
export * from './unpack-harmony';
|
|
1
|
+
export * from './bundle-harmony';
|
|
2
|
+
export * from './pack-harmony';
|
|
3
|
+
export * from './unpack-harmony';
|
|
4
|
+
export * from './create-metro-host-constants';
|
|
5
|
+
export * from './codegen-harmony';
|