Skip to content

Commit 973cc2d

Browse files
authored
Merge pull request #6 from sweatco/feat/custom-bundle-command
feat: use custom bundle command
2 parents 2e42cb8 + a44c279 commit 973cc2d

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

src/bundle/bundle.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import { info, rmRf, execCommand, buildBundleConfig, mkdir } from './utils'
66
import type { BundleArgs, BundlerConfig, Hashes } from './types'
77
import { checkout, gitRestore } from './git'
88
import { hashes, removeUnchangedAssets } from './diff'
9+
import { metroBundle } from './metroBundle'
910

10-
const {
11-
runHermesEmitBinaryCommand,
12-
runReactNativeBundleCommand,
13-
} = require('appcenter-cli/dist/commands/codepush/lib/react-native-utils')
11+
const { runHermesEmitBinaryCommand } = require('appcenter-cli/dist/commands/codepush/lib/react-native-utils')
1412

1513
export async function bundle(args: BundleArgs) {
1614
const { base } = args
@@ -40,6 +38,7 @@ export async function bundle(args: BundleArgs) {
4038

4139
const bundleReactNative = async (config: BundlerConfig, shouldBuildSourceMaps?: boolean) => {
4240
const {
41+
bundleCommand,
4342
bundleName,
4443
entryFile,
4544
os,
@@ -57,15 +56,17 @@ const bundleReactNative = async (config: BundlerConfig, shouldBuildSourceMaps?:
5756
info(`Using '${config.reinstallNodeModulesCommand}' to install node modules`)
5857
await execCommand(config.reinstallNodeModulesCommand)
5958

60-
await runReactNativeBundleCommand(
59+
metroBundle({
60+
bundleCommand,
6161
bundleName,
62-
false, // development
62+
development: false,
6363
entryFile,
6464
outputDir,
65-
os, // platform
65+
platform: os,
6666
sourcemapOutput,
67-
['--reset-cache', ...extraBundlerOptions]
68-
)
67+
extraBundlerOptions,
68+
})
69+
6970
if (shouldBuildSourceMaps && useHermes) {
7071
await runHermesEmitBinaryCommand(bundleName, outputDir, sourcemapOutput, extraHermesFlags)
7172
}

src/bundle/metroBundle.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const path = require('path')
2+
const { spawnSync } = require('child_process')
3+
4+
interface MetroBundleOptions {
5+
platform: string
6+
entryFile: string
7+
outputDir?: string
8+
development?: boolean
9+
resetCache?: boolean
10+
bundleName?: string
11+
bundleOutput?: string
12+
assetsDest?: string
13+
sourcemapOutputDir?: string
14+
sourcemapOutput?: string
15+
bundleCommand?: string
16+
extraBundlerOptions?: string[]
17+
}
18+
19+
export const metroBundle = ({
20+
platform,
21+
entryFile,
22+
outputDir = '',
23+
development = false,
24+
resetCache = true,
25+
bundleCommand = 'bundle',
26+
bundleName,
27+
bundleOutput = path.join(outputDir, bundleName),
28+
assetsDest = outputDir,
29+
sourcemapOutputDir = '',
30+
sourcemapOutput = sourcemapOutputDir ? path.join(sourcemapOutputDir, `${bundleName}.map`) : null,
31+
extraBundlerOptions = [],
32+
}: MetroBundleOptions) => {
33+
const args = [
34+
bundleCommand,
35+
`--platform=${platform}`,
36+
`--entry-file=${entryFile}`,
37+
`--dev=${development}`,
38+
`--bundle-output=${bundleOutput}`,
39+
sourcemapOutput && `--sourcemap-output=${sourcemapOutput}`,
40+
assetsDest && `--assets-dest=${assetsDest}`,
41+
resetCache && '--reset-cache',
42+
...extraBundlerOptions,
43+
].filter(Boolean)
44+
45+
spawnSync(getCliPath(), args, { stdio: 'inherit' })
46+
}
47+
48+
function getCliPath() {
49+
return path.join('node_modules', '.bin', 'react-native')
50+
}

src/bundle/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface BundlerConfig {
2222
sourcemapOutputDir: string
2323
useHermes?: boolean
2424
extraHermesFlags?: string[]
25+
bundleCommand?: string
2526
}
2627

2728
type Config = Partial<BundlerConfig> & VersionSearchParams

src/cli/bundleArgs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export const bundleArgs = <T = {}>(yargs: Argv<T>) =>
55
.option('base', { type: 'string', demandOption: true })
66
.option('reinstallNodeModulesCommand', { type: 'string', alias: ['npm'] })
77
.option('rest', { type: 'string', default: '' })
8+
.option('bundle-command', { type: 'string' })

0 commit comments

Comments
 (0)