1
1
import path from 'node:path' ;
2
2
import { translateOptions } from './utils' ;
3
3
import * as fs from 'fs-extra' ;
4
- import { ZipFile } from 'yazl' ;
5
- import { open as openZipFile } from 'yauzl' ;
4
+ import { ZipFile as YazlZipFile } from 'yazl' ;
5
+ import {
6
+ type Entry ,
7
+ open as openZipFile ,
8
+ type ZipFile as YauzlZipFile ,
9
+ } from 'yauzl' ;
6
10
import { question , checkPlugins } from './utils' ;
7
11
import { checkPlatform } from './app' ;
8
12
import { spawn , spawnSync } from 'node:child_process' ;
@@ -16,9 +20,11 @@ import { tempDir } from './utils/constants';
16
20
import { checkLockFiles } from './utils/check-lockfile' ;
17
21
import { addGitIgnore } from './utils/add-gitignore' ;
18
22
19
- let bsdiff ;
20
- let hdiff ;
21
- let diff ;
23
+ type Diff = ( oldSource ?: Buffer , newSource ?: Buffer ) => Buffer ;
24
+
25
+ let bsdiff : Diff ;
26
+ let hdiff : Diff ;
27
+ let diff : Diff ;
22
28
try {
23
29
bsdiff = require ( 'node-bsdiff' ) . diff ;
24
30
} catch ( e ) { }
@@ -59,9 +65,7 @@ async function runReactNativeBundleCommand({
59
65
if ( platform === 'android' ) {
60
66
gradleConfig = await checkGradleConfig ( ) ;
61
67
if ( gradleConfig . crunchPngs !== false ) {
62
- console . warn (
63
- 'android 的 crunchPngs 选项似乎尚未禁用(如已禁用则请忽略此提示),这可能导致热更包体积异常增大,具体请参考 https://pushy.reactnative.cn/docs/getting-started.html#%E7%A6%81%E7%94%A8-android-%E7%9A%84-crunch-%E4%BC%98%E5%8C%96 \n' ,
64
- ) ;
68
+ console . warn ( t ( 'androidCrunchPngsWarning' ) ) ;
65
69
}
66
70
}
67
71
@@ -321,7 +325,7 @@ async function compileHermesByteCode(
321
325
sourcemapOutput : string ,
322
326
shouldCleanSourcemap : boolean ,
323
327
) {
324
- console . log ( 'Hermes enabled, now compiling to hermes bytecode:\n' ) ;
328
+ console . log ( t ( 'hermesEnabledCompiling' ) ) ;
325
329
// >= rn 0.69
326
330
const rnDir = path . dirname (
327
331
require . resolve ( 'react-native' , {
@@ -351,7 +355,9 @@ async function compileHermesByteCode(
351
355
) ;
352
356
args . push ( '-output-source-map' ) ;
353
357
}
354
- console . log ( t ( 'runningHermesc' , { command : hermesCommand , args : args . join ( ' ' ) } ) ) ;
358
+ console . log (
359
+ t ( 'runningHermesc' , { command : hermesCommand , args : args . join ( ' ' ) } ) ,
360
+ ) ;
355
361
spawnSync ( hermesCommand , args , {
356
362
stdio : 'ignore' ,
357
363
} ) ;
@@ -387,7 +393,7 @@ async function copyDebugidForSentry(
387
393
sourcemapOutput : string ,
388
394
) {
389
395
if ( sourcemapOutput ) {
390
- let copyDebugidPath ;
396
+ let copyDebugidPath : string | undefined ;
391
397
try {
392
398
copyDebugidPath = require . resolve (
393
399
'@sentry/react-native/scripts/copy-debugid.js' ,
@@ -426,13 +432,13 @@ async function uploadSourcemapForSentry(
426
432
version : string ,
427
433
) {
428
434
if ( sourcemapOutput ) {
429
- let sentryCliPath ;
435
+ let sentryCliPath : string | undefined ;
430
436
try {
431
437
sentryCliPath = require . resolve ( '@sentry/cli/bin/sentry-cli' , {
432
438
paths : [ process . cwd ( ) ] ,
433
439
} ) ;
434
440
} catch ( error ) {
435
- console . error ( '无法找到 Sentry CLI 工具,请确保已正确安装 @sentry/cli' ) ;
441
+ console . error ( t ( 'sentryCliNotFound' ) ) ;
436
442
return ;
437
443
}
438
444
@@ -471,12 +477,12 @@ async function uploadSourcemapForSentry(
471
477
}
472
478
473
479
const ignorePackingFileNames = [ '.' , '..' , 'index.bundlejs.map' ] ;
474
- const ignorePackingExtensions = [ 'DS_Store' , 'txt.map' ] ;
480
+ const ignorePackingExtensions = [ 'DS_Store' , 'txt.map' ] ;
475
481
async function pack ( dir : string , output : string ) {
476
482
console . log ( t ( 'packing' ) ) ;
477
483
fs . ensureDirSync ( path . dirname ( output ) ) ;
478
484
await new Promise < void > ( ( resolve , reject ) => {
479
- const zipfile = new ZipFile ( ) ;
485
+ const zipfile = new YazlZipFile ( ) ;
480
486
481
487
function addDirectory ( root : string , rel : string ) {
482
488
if ( rel ) {
@@ -513,10 +519,13 @@ async function pack(dir: string, output: string) {
513
519
console . log ( t ( 'fileGenerated' , { file : output } ) ) ;
514
520
}
515
521
516
- export function readEntire ( entry : string , zipFile : ZipFile ) {
522
+ export function readEntry (
523
+ entry : Entry ,
524
+ zipFile : YauzlZipFile ,
525
+ ) : Promise < Buffer > {
517
526
const buffers : Buffer [ ] = [ ] ;
518
527
return new Promise ( ( resolve , reject ) => {
519
- zipFile . openReadStream ( entry , ( err : any , stream : any ) => {
528
+ zipFile . openReadStream ( entry , ( err , stream ) => {
520
529
stream . pipe ( {
521
530
write ( chunk : Buffer ) {
522
531
buffers . push ( chunk ) ;
@@ -544,7 +553,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
544
553
const originEntries = { } ;
545
554
const originMap = { } ;
546
555
547
- let originSource ;
556
+ let originSource : Buffer | undefined ;
548
557
549
558
await enumZipEntries ( origin , ( entry , zipFile ) => {
550
559
originEntries [ entry . fileName ] = entry ;
@@ -557,7 +566,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
557
566
entry . fileName === 'bundle.harmony.js'
558
567
) {
559
568
// This is source.
560
- return readEntire ( entry , zipFile ) . then ( ( v ) => ( originSource = v ) ) ;
569
+ return readEntry ( entry , zipFile ) . then ( ( v ) => ( originSource = v ) ) ;
561
570
}
562
571
}
563
572
} ) ;
@@ -570,7 +579,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
570
579
571
580
const copies = { } ;
572
581
573
- const zipfile = new ZipFile ( ) ;
582
+ const zipfile = new YazlZipFile ( ) ;
574
583
575
584
const writePromise = new Promise ( ( resolve , reject ) => {
576
585
zipfile . outputStream . on ( 'error' , ( err ) => {
@@ -607,7 +616,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
607
616
}
608
617
} else if ( entry . fileName === 'index.bundlejs' ) {
609
618
//console.log('Found bundle');
610
- return readEntire ( entry , nextZipfile ) . then ( ( newSource ) => {
619
+ return readEntry ( entry , nextZipfile ) . then ( ( newSource ) => {
611
620
//console.log('Begin diff');
612
621
zipfile . addBuffer (
613
622
diff ( originSource , newSource ) ,
@@ -617,7 +626,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
617
626
} ) ;
618
627
} else if ( entry . fileName === 'bundle.harmony.js' ) {
619
628
//console.log('Found bundle');
620
- return readEntire ( entry , nextZipfile ) . then ( ( newSource ) => {
629
+ return readEntry ( entry , nextZipfile ) . then ( ( newSource ) => {
621
630
//console.log('Begin diff');
622
631
zipfile . addBuffer (
623
632
diff ( originSource , newSource ) ,
@@ -691,9 +700,9 @@ async function diffFromPackage(
691
700
const originEntries = { } ;
692
701
const originMap = { } ;
693
702
694
- let originSource ;
703
+ let originSource : Buffer | undefined ;
695
704
696
- await enumZipEntries ( origin , ( entry : any , zipFile : any ) => {
705
+ await enumZipEntries ( origin , ( entry , zipFile ) => {
697
706
if ( ! / \/ $ / . test ( entry . fileName ) ) {
698
707
const fn = transformPackagePath ( entry . fileName ) ;
699
708
if ( ! fn ) {
@@ -707,7 +716,7 @@ async function diffFromPackage(
707
716
708
717
if ( fn === originBundleName ) {
709
718
// This is source.
710
- return readEntire ( entry , zipFile ) . then ( ( v ) => ( originSource = v ) ) ;
719
+ return readEntry ( entry , zipFile ) . then ( ( v ) => ( originSource = v ) ) ;
711
720
}
712
721
}
713
722
} ) ;
@@ -720,7 +729,7 @@ async function diffFromPackage(
720
729
721
730
const copies = { } ;
722
731
723
- const zipfile = new ZipFile ( ) ;
732
+ const zipfile = new YazlZipFile ( ) ;
724
733
725
734
const writePromise = new Promise ( ( resolve , reject ) => {
726
735
zipfile . outputStream . on ( 'error' , ( err ) => {
@@ -737,7 +746,7 @@ async function diffFromPackage(
737
746
zipfile . addEmptyDirectory ( entry . fileName ) ;
738
747
} else if ( entry . fileName === 'index.bundlejs' ) {
739
748
//console.log('Found bundle');
740
- return readEntire ( entry , nextZipfile ) . then ( ( newSource ) => {
749
+ return readEntry ( entry , nextZipfile ) . then ( ( newSource ) => {
741
750
//console.log('Begin diff');
742
751
zipfile . addBuffer (
743
752
diff ( originSource , newSource ) ,
@@ -747,7 +756,7 @@ async function diffFromPackage(
747
756
} ) ;
748
757
} else if ( entry . fileName === 'bundle.harmony.js' ) {
749
758
//console.log('Found bundle');
750
- return readEntire ( entry , nextZipfile ) . then ( ( newSource ) => {
759
+ return readEntry ( entry , nextZipfile ) . then ( ( newSource ) => {
751
760
//console.log('Begin diff');
752
761
zipfile . addBuffer (
753
762
diff ( originSource , newSource ) ,
@@ -789,14 +798,18 @@ async function diffFromPackage(
789
798
790
799
export async function enumZipEntries (
791
800
zipFn : string ,
792
- callback : ( entry : any , zipFile : any ) => void ,
801
+ callback : (
802
+ entry : Entry ,
803
+ zipFile : YauzlZipFile ,
804
+ nestedPath ?: string ,
805
+ ) => Promise < any > ,
793
806
nestedPath = '' ,
794
807
) {
795
808
return new Promise ( ( resolve , reject ) => {
796
809
openZipFile (
797
810
zipFn ,
798
811
{ lazyEntries : true } ,
799
- async ( err : any , zipfile : ZipFile ) => {
812
+ async ( err : any , zipfile : YauzlZipFile ) => {
800
813
if ( err ) {
801
814
return reject ( err ) ;
802
815
}
@@ -850,7 +863,7 @@ export async function enumZipEntries(
850
863
} ) ;
851
864
}
852
865
853
- function diffArgsCheck ( args , options , diffFn ) {
866
+ function diffArgsCheck ( args : string [ ] , options : any , diffFn : string ) {
854
867
const [ origin , next ] = args ;
855
868
856
869
if ( ! origin || ! next ) {
@@ -889,7 +902,7 @@ function diffArgsCheck(args, options, diffFn) {
889
902
export const commands = {
890
903
bundle : async function ( { options } ) {
891
904
const platform = checkPlatform (
892
- options . platform || ( await question ( '平台(ios/android/harmony):' ) ) ,
905
+ options . platform || ( await question ( t ( 'platformPrompt' ) ) ) ,
893
906
) ;
894
907
895
908
const {
@@ -943,7 +956,7 @@ export const commands = {
943
956
944
957
await pack ( path . resolve ( intermediaDir ) , realOutput ) ;
945
958
946
- const v = await question ( '是否现在上传此热更包?(Y/N)' ) ;
959
+ const v = await question ( t ( 'uploadBundlePrompt' ) ) ;
947
960
if ( v . toLowerCase ( ) === 'y' ) {
948
961
const versionName = await this . publish ( {
949
962
args : [ realOutput ] ,
0 commit comments