Skip to content

Commit ca43e77

Browse files
committed
feat(CLI): add output-metro-dir option for pre-hermes bundle copies
1 parent 0c3d007 commit ca43e77

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

cli/commands/bundleCommand/bundleCodePush.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs";
2+
import path from "path";
23
import { prepareToBundleJS } from "../../functions/prepareToBundleJS.js";
34
import { runReactNativeBundleCommand } from "../../functions/runReactNativeBundleCommand.js";
45
import { runExpoBundleCommand } from "../../functions/runExpoBundleCommand.js";
@@ -17,6 +18,7 @@ export async function bundleCodePush(
1718
entryFile: string = ENTRY_FILE,
1819
jsBundleName: string, // JS bundle file name (not CodePush bundle file)
1920
bundleDirectory: string, // CodePush bundle output directory
21+
outputMetroDir?: string,
2022
): Promise<string> {
2123
if (fs.existsSync(outputRootPath)) {
2224
fs.rmSync(outputRootPath, { recursive: true });
@@ -49,6 +51,8 @@ export async function bundleCodePush(
4951

5052
console.log('log: JS bundling complete');
5153

54+
copyMetroOutputsIfNeeded(outputRootPath, outputMetroDir, OUTPUT_CONTENT_PATH, _jsBundleName, SOURCEMAP_OUTPUT);
55+
5256
await runHermesEmitBinaryCommand(
5357
_jsBundleName,
5458
OUTPUT_CONTENT_PATH,
@@ -61,3 +65,28 @@ export async function bundleCodePush(
6165

6266
return codePushBundleFileName;
6367
}
68+
69+
function copyMetroOutputsIfNeeded(
70+
outputRootPath: string,
71+
outputMetroDir: string | undefined,
72+
outputContentPath: string,
73+
jsBundleName: string,
74+
sourceMapOutputPath: string,
75+
) {
76+
if (!outputMetroDir) {
77+
return;
78+
}
79+
80+
const resolvedOutputMetroDir = path.join(outputRootPath, outputMetroDir);
81+
82+
fs.mkdirSync(resolvedOutputMetroDir, { recursive: true });
83+
fs.copyFileSync(
84+
path.join(outputContentPath, jsBundleName),
85+
path.join(resolvedOutputMetroDir, jsBundleName),
86+
);
87+
fs.copyFileSync(
88+
sourceMapOutputPath,
89+
path.join(resolvedOutputMetroDir, path.basename(sourceMapOutputPath)),
90+
);
91+
console.log(`log: Metro outputs copied to: ${resolvedOutputMetroDir}`);
92+
}

cli/commands/bundleCommand/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type Options = {
99
entryFile: string;
1010
bundleName: string;
1111
outputBundleDir: string;
12+
outputMetroDir?: string;
1213
}
1314

1415
program.command('bundle')
@@ -18,6 +19,7 @@ program.command('bundle')
1819
.option('-o, --output-path <string>', 'path to output root directory', ROOT_OUTPUT_DIR)
1920
.option('-e, --entry-file <string>', 'path to JS/TS entry file', ENTRY_FILE)
2021
.option('-b, --bundle-name <string>', 'bundle file name (default-ios: "main.jsbundle" / default-android: "index.android.bundle")')
22+
.option('--output-metro-dir <string>', 'name of directory to copy the Metro JS bundle and sourcemap before Hermes compilation')
2123
.option('--output-bundle-dir <string>', 'name of directory containing the bundle file created by the "bundle" command', OUTPUT_BUNDLE_DIR)
2224
.action((options: Options) => {
2325
bundleCodePush(
@@ -27,5 +29,6 @@ program.command('bundle')
2729
options.entryFile,
2830
options.bundleName,
2931
`${options.outputPath}/${options.outputBundleDir}`,
32+
options.outputMetroDir,
3033
)
3134
});

cli/commands/releaseCommand/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Options = {
1919
skipBundle: boolean;
2020
skipCleanup: boolean;
2121
outputBundleDir: string;
22+
outputMetroDir?: string;
2223
hashCalc?: boolean;
2324
}
2425

@@ -39,6 +40,7 @@ program.command('release')
3940
.option('--skip-bundle <bool>', 'skip bundle process', parseBoolean, false)
4041
.option('--hash-calc <bool>', 'calculates the bundle file hash used for packageHash in the release history (Requires setting --skip-bundle to true)', parseBoolean)
4142
.option('--skip-cleanup <bool>', 'skip cleanup process', parseBoolean, false)
43+
.option('--output-metro-dir <string>', 'name of directory to copy the Metro JS bundle and sourcemap before Hermes compilation')
4244
.option('--output-bundle-dir <string>', 'name of directory containing the bundle file created by the "bundle" command', OUTPUT_BUNDLE_DIR)
4345
.action(async (options: Options) => {
4446
const config = findAndReadConfigFile(process.cwd(), options.config);
@@ -71,6 +73,7 @@ program.command('release')
7173
options.skipBundle,
7274
options.skipCleanup,
7375
`${options.outputPath}/${options.outputBundleDir}`,
76+
options.outputMetroDir,
7477
options.hashCalc,
7578
)
7679

cli/commands/releaseCommand/release.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export async function release(
2424
skipBundle: boolean,
2525
skipCleanup: boolean,
2626
bundleDirectory: string,
27+
outputMetroDir?: string,
2728
hashCalc?: boolean,
2829
): Promise<void> {
2930
const bundleFileName = skipBundle
3031
? readBundleFileNameFrom(bundleDirectory)
31-
: await bundleCodePush(framework, platform, outputPath, entryFile, jsBundleName, bundleDirectory);
32+
: await bundleCodePush(framework, platform, outputPath, entryFile, jsBundleName, bundleDirectory, outputMetroDir);
3233
const bundleFilePath = `${bundleDirectory}/${bundleFileName}`;
3334

3435
const packageHash = await (() => {

0 commit comments

Comments
 (0)