react-native-update-cli 2.22.6 → 2.22.7

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.
@@ -1,4 +1,4 @@
1
- import { spawnSync } from 'child_process';
1
+ import { spawn, spawnSync } from 'child_process';
2
2
  import { satisfies } from 'compare-versions';
3
3
  import * as fs from 'fs-extra';
4
4
  import os from 'os';
@@ -58,6 +58,16 @@ export interface HermesCompileResult {
58
58
  verified?: boolean;
59
59
  }
60
60
 
61
+ /** Outcome of picking a base: what to compile with, plus the log lines. */
62
+ export interface HermesBaseSelectionResult {
63
+ base: HermesBaseSelection | null;
64
+ bytecodeVersion: number | null;
65
+ /** messages to print when the compile phase starts (kept out of Metro's output) */
66
+ logs: string[];
67
+ /** hermesc could not be resolved; the compile phase will report it */
68
+ commandUnavailable?: boolean;
69
+ }
70
+
61
71
  interface GradleConfig {
62
72
  crunchPngs?: boolean;
63
73
  enableHermes?: boolean;
@@ -319,6 +329,18 @@ export async function runReactNativeBundleCommand({
319
329
  reactNativeBundleArgs.push('--config', config);
320
330
  }
321
331
 
332
+ // Decide about Hermes before Metro runs so the base lookup (server query,
333
+ // Range download, cache) overlaps the bundling instead of following it.
334
+ const hermesEnabled = await detectHermesEnabled(
335
+ platform,
336
+ forceHermes,
337
+ gradleConfig,
338
+ );
339
+ const pendingBase =
340
+ hermesEnabled && hermesBase
341
+ ? startHermesBaseSelection(hermesBase)
342
+ : undefined;
343
+
322
344
  const jsRuntime = getJavaScriptRuntime();
323
345
  const reactNativeBundleProcess = spawnJavaScript(reactNativeBundleArgs);
324
346
  console.log(
@@ -344,51 +366,15 @@ export async function runReactNativeBundleCommand({
344
366
  }
345
367
 
346
368
  try {
347
- let hermesEnabled: boolean | undefined = false;
348
-
349
- if (forceHermes) {
350
- hermesEnabled = true;
351
- console.log(t('forceHermes'));
352
- } else if (platform === 'android') {
353
- const gradleProperties = await new Promise<{
354
- hermesEnabled?: boolean;
355
- }>((resolve) => {
356
- properties.parse(
357
- './android/gradle.properties',
358
- { path: true },
359
- (
360
- error: Error | null,
361
- props: { hermesEnabled?: boolean } = {},
362
- ) => {
363
- if (error) {
364
- console.error(error);
365
- resolve({});
366
- return;
367
- }
368
- resolve(props);
369
- },
370
- );
371
- });
372
- hermesEnabled = gradleProperties.hermesEnabled;
373
-
374
- if (typeof hermesEnabled !== 'boolean') {
375
- hermesEnabled = gradleConfig.enableHermes;
376
- }
377
- } else if (
378
- platform === 'ios' &&
379
- fs.existsSync('ios/Pods/hermes-engine')
380
- ) {
381
- hermesEnabled = true;
382
- }
383
-
384
369
  if (hermesEnabled) {
385
- hermesResult = await compileHermesByteCode(
370
+ hermesResult = await compileHermesByteCode({
386
371
  bundleName,
387
372
  outputFolder,
388
373
  sourcemapOutput,
389
- !isSentry,
390
- hermesBase,
391
- );
374
+ shouldCleanSourcemap: !isSentry,
375
+ baseRequest: hermesBase,
376
+ pendingBase,
377
+ });
392
378
  }
393
379
 
394
380
  if (platform === 'harmony') {
@@ -416,6 +402,43 @@ export async function runReactNativeBundleCommand({
416
402
  return hermesResult;
417
403
  }
418
404
 
405
+ async function detectHermesEnabled(
406
+ platform: string,
407
+ forceHermes: boolean | undefined,
408
+ gradleConfig: GradleConfig,
409
+ ): Promise<boolean> {
410
+ if (forceHermes) {
411
+ console.log(t('forceHermes'));
412
+ return true;
413
+ }
414
+ if (platform === 'android') {
415
+ const gradleProperties = await new Promise<{ hermesEnabled?: boolean }>(
416
+ (resolve) => {
417
+ properties.parse(
418
+ './android/gradle.properties',
419
+ { path: true },
420
+ (error: Error | null, props: { hermesEnabled?: boolean } = {}) => {
421
+ if (error) {
422
+ console.error(error);
423
+ resolve({});
424
+ return;
425
+ }
426
+ resolve(props);
427
+ },
428
+ );
429
+ },
430
+ );
431
+ if (typeof gradleProperties.hermesEnabled === 'boolean') {
432
+ return gradleProperties.hermesEnabled;
433
+ }
434
+ return Boolean(gradleConfig.enableHermes);
435
+ }
436
+ if (platform === 'ios' && fs.existsSync('ios/Pods/hermes-engine')) {
437
+ return true;
438
+ }
439
+ return false;
440
+ }
441
+
419
442
  function getHermesOSBin() {
420
443
  if (os.platform() === 'win32') return 'win64-bin';
421
444
  if (os.platform() === 'darwin') return 'osx-bin';
@@ -594,15 +617,62 @@ export function buildHermescArgs(bundlePath: string): string[] {
594
617
  ];
595
618
  }
596
619
 
597
- async function compileHermesByteCode(
598
- bundleName: string,
599
- outputFolder: string,
600
- sourcemapOutput: string,
601
- shouldCleanSourcemap: boolean,
602
- baseRequest?: HermesBaseRequest,
603
- ): Promise<HermesCompileResult> {
620
+ interface ProcessOutcome {
621
+ status: number | null;
622
+ signal: NodeJS.Signals | null;
623
+ stderr: string;
624
+ error?: Error;
625
+ }
626
+
627
+ /** spawn without blocking the event loop, so two hermesc runs can overlap */
628
+ function runProcess(
629
+ command: string,
630
+ args: string[],
631
+ captureStderr: boolean,
632
+ ): Promise<ProcessOutcome> {
633
+ return new Promise((resolve) => {
634
+ const child = spawn(command, args, {
635
+ stdio: ['ignore', 'ignore', captureStderr ? 'pipe' : 'ignore'],
636
+ });
637
+ const chunks: Buffer[] = [];
638
+ child.stderr?.on('data', (chunk: Buffer) => chunks.push(chunk));
639
+ let settled = false;
640
+ const finish = (outcome: ProcessOutcome) => {
641
+ if (settled) return;
642
+ settled = true;
643
+ resolve(outcome);
644
+ };
645
+ child.once('error', (error) =>
646
+ finish({ status: null, signal: null, stderr: '', error }),
647
+ );
648
+ child.once('close', (status, signal) =>
649
+ finish({ status, signal, stderr: Buffer.concat(chunks).toString() }),
650
+ );
651
+ });
652
+ }
653
+
654
+ export interface CompileHermesOptions {
655
+ bundleName: string;
656
+ outputFolder: string;
657
+ sourcemapOutput: string;
658
+ shouldCleanSourcemap: boolean;
659
+ baseRequest?: HermesBaseRequest;
660
+ /** base selection started before Metro (see startHermesBaseSelection) */
661
+ pendingBase?: Promise<HermesBaseSelectionResult>;
662
+ /** defaults to the project's hermesc */
663
+ hermesCommand?: string;
664
+ }
665
+
666
+ export async function compileHermesByteCode({
667
+ bundleName,
668
+ outputFolder,
669
+ sourcemapOutput,
670
+ shouldCleanSourcemap,
671
+ baseRequest,
672
+ pendingBase,
673
+ hermesCommand = resolveHermesCommand(),
674
+ }: CompileHermesOptions): Promise<HermesCompileResult> {
604
675
  console.log(t('hermesEnabledCompiling'));
605
- const hermesCommand = resolveHermesCommand();
606
676
 
607
677
  const bundlePath = path.join(outputFolder, bundleName);
608
678
  const plainArgs = buildHermescArgs(bundlePath);
@@ -615,27 +685,48 @@ async function compileHermesByteCode(
615
685
  console.log(t('hermesSourcemapKept', { file: `${bundlePath}.map` }));
616
686
  }
617
687
 
618
- const result: HermesCompileResult = { bytecodeVersion: null, base: null };
619
- const base = await selectHermesBase(hermesCommand, baseRequest, result);
688
+ let selection = pendingBase ? await pendingBase : null;
689
+ if (!selection || selection.commandUnavailable) {
690
+ selection = await selectHermesBase(hermesCommand, baseRequest);
691
+ }
692
+ for (const line of selection.logs) console.log(line);
693
+ const result: HermesCompileResult = {
694
+ bytecodeVersion: selection.bytecodeVersion,
695
+ base: null,
696
+ };
697
+ const base = selection.base;
620
698
 
621
- // hermesc reads its input in full before writing, so compiling in place is
622
- // safe; keep the JS bundle aside while a base compile may still fail or be
623
- // rejected by the equivalence check.
624
- const jsBackup = base ? `${bundlePath}.js.bak` : '';
625
- if (base) fs.copyFileSync(bundlePath, jsBackup);
626
- let usedBase = false;
627
699
  if (base) {
700
+ // hermesc reads its input in full before writing, so compiling in place is
701
+ // safe; keep the JS bundle aside while a base compile may still fail or be
702
+ // rejected by the equivalence check. With verification on, the plain
703
+ // compile the check needs runs concurrently in a sibling directory (same
704
+ // file name, so its sourcemap looks identical) and simply becomes the
705
+ // result when the base is rejected — no third compile.
706
+ const jsBackup = `${bundlePath}.js.bak`;
707
+ fs.copyFileSync(bundlePath, jsBackup);
708
+ const plainDir = path.join(outputFolder, '.hermes-plain');
709
+ const plainPath = path.join(plainDir, bundleName);
710
+ const wantPlain = Boolean(baseRequest?.verify);
711
+ if (wantPlain) {
712
+ fs.ensureDirSync(plainDir);
713
+ fs.copyFileSync(bundlePath, plainPath);
714
+ }
628
715
  const args = hermescArgsWithBase(plainArgs, base.path);
629
716
  console.log(
630
717
  t('runningHermesc', { command: hermesCommand, args: args.join(' ') }),
631
718
  );
632
- const attempt = spawnSync(hermesCommand, args, {
633
- stdio: ['ignore', 'ignore', 'pipe'],
634
- });
635
- if (attempt.status === 0) {
636
- usedBase = true;
637
- } else {
638
- const fullStderr = attempt.stderr ? String(attempt.stderr) : '';
719
+ const [attempt, plain] = await Promise.all([
720
+ runProcess(hermesCommand, args, true),
721
+ wantPlain
722
+ ? runProcess(hermesCommand, buildHermescArgs(plainPath), false)
723
+ : Promise.resolve<ProcessOutcome | null>(null),
724
+ ]);
725
+ let usedBase = attempt.status === 0 && !attempt.error;
726
+ if (!usedBase) {
727
+ const fullStderr = attempt.error
728
+ ? String(attempt.error.message ?? attempt.error)
729
+ : attempt.stderr;
639
730
  const reason = summarizeHermescStderr(fullStderr);
640
731
  console.warn(
641
732
  t('hermesBaseCompileFailed', {
@@ -650,44 +741,56 @@ async function compileHermesByteCode(
650
741
  console.warn(t('hermesBaseCompileFailedLog', { file: logPath }));
651
742
  } catch {}
652
743
  }
653
- fs.copyFileSync(jsBackup, bundlePath);
654
744
  }
655
- }
656
- if (usedBase && baseRequest?.verify) {
657
- // plain compile next to it, compare disassembly, keep whichever is right
658
- const plainOut = `${bundlePath}.plain.hbc`;
659
- const plain = spawnSync(
660
- hermesCommand,
661
- // same flags as the real compile (only the base differs), otherwise
662
- // debug-info settings would make the two dumps legitimately differ
663
- ['-emit-binary', '-out', plainOut, jsBackup, '-O', '-output-source-map'],
664
- { stdio: 'ignore' },
665
- );
666
- let ok = plain.status === 0;
667
- if (ok) {
668
- try {
669
- ok = await verifyHermesBaseEquivalence(
745
+ const plainOk = plain !== null && plain.status === 0 && !plain.error;
746
+ if (usedBase && wantPlain) {
747
+ // compare disassembly, keep whichever is right
748
+ let ok = plainOk;
749
+ if (ok) {
750
+ try {
751
+ ok = await verifyHermesBaseEquivalence(
752
+ hermesCommand,
753
+ bundlePath,
754
+ plainPath,
755
+ );
756
+ } catch {
757
+ ok = false;
758
+ }
759
+ }
760
+ result.verified = ok;
761
+ if (ok) {
762
+ console.log(t('hermesBaseVerified'));
763
+ } else {
764
+ console.warn(t('hermesBaseVerifyFailed'));
765
+ usedBase = false;
766
+ }
767
+ }
768
+ if (!usedBase) {
769
+ if (plainOk) {
770
+ fs.moveSync(plainPath, bundlePath, { overwrite: true });
771
+ if (fs.existsSync(`${plainPath}.map`)) {
772
+ fs.moveSync(`${plainPath}.map`, `${bundlePath}.map`, {
773
+ overwrite: true,
774
+ });
775
+ }
776
+ } else {
777
+ fs.copyFileSync(jsBackup, bundlePath);
778
+ console.log(
779
+ t('runningHermesc', {
780
+ command: hermesCommand,
781
+ args: plainArgs.join(' '),
782
+ }),
783
+ );
784
+ assertSuccessfulSyncProcess(
785
+ spawnSync(hermesCommand, plainArgs, { stdio: 'ignore' }),
670
786
  hermesCommand,
671
- bundlePath,
672
- plainOut,
673
787
  );
674
- } catch {
675
- ok = false;
676
788
  }
677
789
  }
678
- fs.removeSync(plainOut);
679
- fs.removeSync(`${plainOut}.map`);
680
- result.verified = ok;
681
- if (ok) {
682
- console.log(t('hermesBaseVerified'));
683
- } else {
684
- console.warn(t('hermesBaseVerifyFailed'));
685
- fs.copyFileSync(jsBackup, bundlePath);
686
- usedBase = false;
687
- }
688
- }
689
- if (jsBackup) fs.removeSync(jsBackup);
690
- if (!usedBase) {
790
+ fs.removeSync(jsBackup);
791
+ fs.removeSync(plainDir);
792
+ result.base = usedBase ? base : null;
793
+ } else {
691
794
  console.log(
692
795
  t('runningHermesc', {
693
796
  command: hermesCommand,
@@ -700,9 +803,6 @@ async function compileHermesByteCode(
700
803
  }),
701
804
  hermesCommand,
702
805
  );
703
- result.base = null;
704
- } else {
705
- result.base = base;
706
806
  }
707
807
  if (sourcemapOutput) {
708
808
  let composerPath: string;
@@ -739,45 +839,81 @@ async function compileHermesByteCode(
739
839
  return result;
740
840
  }
741
841
 
842
+ /**
843
+ * Begin resolving the base right away (before Metro); the compile phase awaits
844
+ * the result. Never rejects: any failure is reported there instead.
845
+ */
846
+ export function startHermesBaseSelection(
847
+ request: HermesBaseRequest,
848
+ ): Promise<HermesBaseSelectionResult> {
849
+ return (async () => {
850
+ let hermesCommand: string;
851
+ try {
852
+ hermesCommand = resolveHermesCommand();
853
+ } catch {
854
+ return {
855
+ base: null,
856
+ bytecodeVersion: null,
857
+ logs: [],
858
+ commandUnavailable: true,
859
+ };
860
+ }
861
+ try {
862
+ return await selectHermesBase(hermesCommand, request);
863
+ } catch (error: any) {
864
+ return {
865
+ base: null,
866
+ bytecodeVersion: null,
867
+ logs: [
868
+ t('hermesBaseNone', { reason: error?.message ?? String(error) }),
869
+ ],
870
+ };
871
+ }
872
+ })();
873
+ }
874
+
742
875
  async function selectHermesBase(
743
876
  hermesCommand: string,
744
877
  request: HermesBaseRequest | undefined,
745
- result: HermesCompileResult,
746
- ): Promise<HermesBaseSelection | null> {
878
+ ): Promise<HermesBaseSelectionResult> {
879
+ const logs: string[] = [];
880
+ const none = (bytecodeVersion: number | null = null) => ({
881
+ base: null,
882
+ bytecodeVersion,
883
+ logs,
884
+ });
747
885
  if (!request || request.option === 'none') {
748
886
  if (request) {
749
- console.log(t('hermesBaseNone', { reason: 'disabled by option' }));
887
+ logs.push(t('hermesBaseNone', { reason: 'disabled by option' }));
750
888
  }
751
- return null;
889
+ return none();
752
890
  }
753
891
  const gate = classifyHermesCommand(hermesCommand);
754
892
  if (!gate.allowed) {
755
- console.log(
893
+ logs.push(
756
894
  t('hermesBaseNone', { reason: gate.reason ?? 'hermesc not eligible' }),
757
895
  );
758
- return null;
896
+ return none();
759
897
  }
760
898
  const bytecodeVersion = probeHbcVersion(hermesCommand);
761
- result.bytecodeVersion = bytecodeVersion;
762
899
  if (bytecodeVersion === null) {
763
- console.log(t('hermesBaseNone', { reason: 'could not probe HBC version' }));
764
- return null;
900
+ logs.push(t('hermesBaseNone', { reason: 'could not probe HBC version' }));
901
+ return none();
765
902
  }
766
903
  try {
767
- return await resolveHermesBase({
904
+ const base = await resolveHermesBase({
768
905
  option: request.option,
769
906
  hermesCommand,
770
907
  bytecodeVersion,
771
908
  appId: request.appId,
772
909
  cacheMaxMb: request.cacheMaxMb,
773
910
  fetchBase: getHermesBase,
774
- log: (message) => console.log(message),
911
+ log: (message) => logs.push(message),
775
912
  });
913
+ return { base, bytecodeVersion, logs };
776
914
  } catch (error: any) {
777
- console.log(
778
- t('hermesBaseNone', { reason: error?.message ?? String(error) }),
779
- );
780
- return null;
915
+ logs.push(t('hermesBaseNone', { reason: error?.message ?? String(error) }));
916
+ return none(bytecodeVersion);
781
917
  }
782
918
  }
783
919
 
package/src/package.ts CHANGED
@@ -16,9 +16,10 @@ import {
16
16
  import { AabParser } from './utils/app-info-parser/aab';
17
17
  import { depVersions } from './utils/dep-versions';
18
18
  import { getCommitInfo } from './utils/git';
19
- import { cachePut } from './utils/hermes-base';
19
+ import { bundleEntryMatcher, cachePut } from './utils/hermes-base';
20
20
  import { t } from './utils/i18n';
21
21
  import { getStringListOption } from './utils/options';
22
+ import { bundleLocationFields, locateZipEntry } from './utils/zip-range';
22
23
 
23
24
  type PackageCommandOptions = Record<string, unknown> & {
24
25
  appId?: string;
@@ -153,6 +154,19 @@ async function uploadNativePackage(
153
154
  try {
154
155
  await createSlimNativePackage(filePath, slimPackagePath, config.platform);
155
156
  const { hash } = await uploadFile(slimPackagePath, undefined, appId);
157
+ // where the bundle sits inside the uploaded (slim) archive, so a later
158
+ // `bundle` that falls back to this package as hermes base fetches only
159
+ // the bundle with one HTTP Range request. Harmony .app nests a second
160
+ // zip, so no location is reported for it.
161
+ const bundleLocation =
162
+ config.extension === '.app'
163
+ ? {}
164
+ : bundleLocationFields(
165
+ await locateZipEntry(
166
+ slimPackagePath,
167
+ bundleEntryMatcher(config.extension.slice(1) as 'apk' | 'ipa'),
168
+ ).catch(() => null),
169
+ );
156
170
  if (bundleFile) {
157
171
  // keep the embedded bundle locally so a later `bundle` can use it as
158
172
  // hermes base without downloading it back (see hermes-base.ts)
@@ -171,6 +185,7 @@ async function uploadNativePackage(
171
185
  // against the client-reported bundleHash to decide pdiff applicability.
172
186
  // Old servers strip unknown fields, so this is forward-compatible.
173
187
  ...(bundleHash ? { bundleHash } : {}),
188
+ ...bundleLocation,
174
189
  deps: depVersions,
175
190
  commit: await getCommitInfo(),
176
191
  });