diff --git a/src/bundle/bundle.ts b/src/bundle/bundle.ts index 8abb325..964e2f7 100644 --- a/src/bundle/bundle.ts +++ b/src/bundle/bundle.ts @@ -6,11 +6,9 @@ import { info, rmRf, execCommand, buildBundleConfig, mkdir } from './utils' import type { BundleArgs, BundlerConfig, Hashes } from './types' import { checkout, gitRestore } from './git' import { hashes, removeUnchangedAssets } from './diff' +import { metroBundle } from './metroBundle' -const { - runHermesEmitBinaryCommand, - runReactNativeBundleCommand, -} = require('appcenter-cli/dist/commands/codepush/lib/react-native-utils') +const { runHermesEmitBinaryCommand } = require('appcenter-cli/dist/commands/codepush/lib/react-native-utils') export async function bundle(args: BundleArgs) { const { base } = args @@ -40,6 +38,7 @@ export async function bundle(args: BundleArgs) { const bundleReactNative = async (config: BundlerConfig, shouldBuildSourceMaps?: boolean) => { const { + bundleCommand, bundleName, entryFile, os, @@ -57,15 +56,17 @@ const bundleReactNative = async (config: BundlerConfig, shouldBuildSourceMaps?: info(`Using '${config.reinstallNodeModulesCommand}' to install node modules`) await execCommand(config.reinstallNodeModulesCommand) - await runReactNativeBundleCommand( + metroBundle({ + bundleCommand, bundleName, - false, // development + development: false, entryFile, outputDir, - os, // platform + platform: os, sourcemapOutput, - ['--reset-cache', ...extraBundlerOptions] - ) + extraBundlerOptions, + }) + if (shouldBuildSourceMaps && useHermes) { await runHermesEmitBinaryCommand(bundleName, outputDir, sourcemapOutput, extraHermesFlags) } diff --git a/src/bundle/metroBundle.ts b/src/bundle/metroBundle.ts new file mode 100644 index 0000000..56e036f --- /dev/null +++ b/src/bundle/metroBundle.ts @@ -0,0 +1,50 @@ +const path = require('path') +const { spawnSync } = require('child_process') + +interface MetroBundleOptions { + platform: string + entryFile: string + outputDir?: string + development?: boolean + resetCache?: boolean + bundleName?: string + bundleOutput?: string + assetsDest?: string + sourcemapOutputDir?: string + sourcemapOutput?: string + bundleCommand?: string + extraBundlerOptions?: string[] +} + +export const metroBundle = ({ + platform, + entryFile, + outputDir = '', + development = false, + resetCache = true, + bundleCommand = 'bundle', + bundleName, + bundleOutput = path.join(outputDir, bundleName), + assetsDest = outputDir, + sourcemapOutputDir = '', + sourcemapOutput = sourcemapOutputDir ? path.join(sourcemapOutputDir, `${bundleName}.map`) : null, + extraBundlerOptions = [], +}: MetroBundleOptions) => { + const args = [ + bundleCommand, + `--platform=${platform}`, + `--entry-file=${entryFile}`, + `--dev=${development}`, + `--bundle-output=${bundleOutput}`, + sourcemapOutput && `--sourcemap-output=${sourcemapOutput}`, + assetsDest && `--assets-dest=${assetsDest}`, + resetCache && '--reset-cache', + ...extraBundlerOptions, + ].filter(Boolean) + + spawnSync(getCliPath(), args, { stdio: 'inherit' }) +} + +function getCliPath() { + return path.join('node_modules', '.bin', 'react-native') +} diff --git a/src/bundle/types.ts b/src/bundle/types.ts index e77357a..edef10e 100644 --- a/src/bundle/types.ts +++ b/src/bundle/types.ts @@ -22,6 +22,7 @@ export interface BundlerConfig { sourcemapOutputDir: string useHermes?: boolean extraHermesFlags?: string[] + bundleCommand?: string } type Config = Partial & VersionSearchParams diff --git a/src/cli/bundleArgs.ts b/src/cli/bundleArgs.ts index 80679e6..72b3433 100644 --- a/src/cli/bundleArgs.ts +++ b/src/cli/bundleArgs.ts @@ -5,3 +5,4 @@ export const bundleArgs = (yargs: Argv) => .option('base', { type: 'string', demandOption: true }) .option('reinstallNodeModulesCommand', { type: 'string', alias: ['npm'] }) .option('rest', { type: 'string', default: '' }) + .option('bundle-command', { type: 'string' })