@@ -22,16 +22,29 @@ try {
22
22
hdiff = require ( 'node-hdiffpatch' ) . diff ;
23
23
} catch ( e ) { }
24
24
25
-
26
- async function runReactNativeBundleCommand (
27
- bundleName : string ,
28
- development : string ,
29
- entryFile : string ,
30
- outputFolder : string ,
31
- platform : string ,
32
- sourcemapOutput : string ,
33
- config : string ,
34
- ) {
25
+ async function runReactNativeBundleCommand ( {
26
+ bundleName,
27
+ dev,
28
+ entryFile,
29
+ outputFolder,
30
+ platform,
31
+ sourcemapOutput,
32
+ config,
33
+ cli,
34
+ } : {
35
+ bundleName : string ;
36
+ dev : string ;
37
+ entryFile : string ;
38
+ outputFolder : string ;
39
+ platform : string ;
40
+ sourcemapOutput : string ;
41
+ config ?: string ;
42
+ cli : {
43
+ taro ?: boolean ;
44
+ expo ?: boolean ;
45
+ rncli ?: boolean ;
46
+ } ;
47
+ } ) {
35
48
let gradleConfig : {
36
49
crunchPngs ?: boolean ;
37
50
enableHermes ?: boolean ;
@@ -58,26 +71,31 @@ async function runReactNativeBundleCommand(
58
71
59
72
fs . emptyDirSync ( outputFolder ) ;
60
73
61
- let cliPath ;
62
-
74
+ let cliPath : string | undefined ;
63
75
let usingExpo = false ;
64
- try {
65
- cliPath = require . resolve ( '@expo/cli' , {
66
- paths : [ process . cwd ( ) ] ,
67
- } ) ;
68
- const expoCliVersion = JSON . parse (
69
- fs . readFileSync (
70
- require . resolve ( '@expo/cli/package.json' , {
71
- paths : [ process . cwd ( ) ] ,
72
- } ) ,
73
- ) ,
74
- ) . version ;
75
- // expo cli 0.10.17 (expo 49) 开始支持 bundle:embed
76
- if ( semverSatisfies ( expoCliVersion , '>= 0.10.17' ) ) {
77
- usingExpo = true ;
78
- }
79
- } catch ( e ) { }
80
- if ( ! usingExpo ) {
76
+
77
+ const getExpoCli = ( ) => {
78
+ try {
79
+ cliPath = require . resolve ( '@expo/cli' , {
80
+ paths : [ process . cwd ( ) ] ,
81
+ } ) ;
82
+ const expoCliVersion = JSON . parse (
83
+ fs . readFileSync (
84
+ require . resolve ( '@expo/cli/package.json' , {
85
+ paths : [ process . cwd ( ) ] ,
86
+ } ) ,
87
+ ) ,
88
+ ) . version ;
89
+ // expo cli 0.10.17 (expo 49) 开始支持 bundle:embed
90
+ if ( semverSatisfies ( expoCliVersion , '>= 0.10.17' ) ) {
91
+ usingExpo = true ;
92
+ } else {
93
+ cliPath = undefined ;
94
+ }
95
+ } catch ( e ) { }
96
+ } ;
97
+
98
+ const getRnCli = ( ) => {
81
99
try {
82
100
// rn >= 0.75
83
101
cliPath = require . resolve ( '@react-native-community/cli/build/bin.js' , {
@@ -89,20 +107,49 @@ async function runReactNativeBundleCommand(
89
107
paths : [ process . cwd ( ) ] ,
90
108
} ) ;
91
109
}
110
+ } ;
111
+
112
+ const getTaroCli = ( ) => {
113
+ try {
114
+ cliPath = require . resolve ( '@tarojs/cli/bin/taro.js' , {
115
+ paths : [ process . cwd ( ) ] ,
116
+ } ) ;
117
+ } catch ( e ) { }
118
+ } ;
119
+
120
+ if ( cli . expo ) {
121
+ getExpoCli ( ) ;
122
+ } else if ( cli . taro ) {
123
+ getTaroCli ( ) ;
124
+ } else if ( cli . rncli ) {
125
+ getRnCli ( ) ;
126
+ }
127
+
128
+ if ( ! cliPath ) {
129
+ getExpoCli ( ) ;
130
+ if ( ! usingExpo ) {
131
+ getRnCli ( ) ;
132
+ }
92
133
}
134
+
93
135
const bundleParams = await checkPlugins ( ) ;
94
136
const isSentry = bundleParams . sentry ;
95
- const bundleCommand = usingExpo
96
- ? 'export:embed'
97
- : platform === 'harmony'
98
- ? 'bundle-harmony'
99
- : 'bundle' ;
137
+
138
+ let bundleCommand = 'bundle' ;
139
+ if ( usingExpo ) {
140
+ bundleCommand = 'export:embed' ;
141
+ } else if ( platform === 'harmony' ) {
142
+ bundleCommand = 'bundle-harmony' ;
143
+ } else if ( cli . taro ) {
144
+ bundleCommand = 'build' ;
145
+ }
146
+
100
147
if ( platform === 'harmony' ) {
101
148
Array . prototype . push . apply ( reactNativeBundleArgs , [
102
149
cliPath ,
103
150
bundleCommand ,
104
151
'--dev' ,
105
- development ,
152
+ dev ,
106
153
'--entry-file' ,
107
154
entryFile ,
108
155
] ) ;
@@ -122,14 +169,24 @@ async function runReactNativeBundleCommand(
122
169
outputFolder ,
123
170
'--bundle-output' ,
124
171
path . join ( outputFolder , bundleName ) ,
125
- '--dev' ,
126
- development ,
127
- '--entry-file' ,
128
- entryFile ,
129
172
'--platform' ,
130
173
platform ,
131
174
'--reset-cache' ,
132
175
] ) ;
176
+
177
+ if ( cli . taro ) {
178
+ reactNativeBundleArgs . push ( ...[
179
+ '--type' ,
180
+ 'rn' ,
181
+ ] )
182
+ } else {
183
+ reactNativeBundleArgs . push ( ...[
184
+ '--dev' ,
185
+ dev ,
186
+ '--entry-file' ,
187
+ entryFile ,
188
+ ] )
189
+ }
133
190
134
191
if ( sourcemapOutput ) {
135
192
reactNativeBundleArgs . push ( '--sourcemap-output' , sourcemapOutput ) ;
@@ -165,7 +222,9 @@ async function runReactNativeBundleCommand(
165
222
let hermesEnabled : boolean | undefined = false ;
166
223
167
224
if ( platform === 'android' ) {
168
- const gradlePropeties = await new Promise < { hermesEnabled ?: boolean } > ( ( resolve ) => {
225
+ const gradlePropeties = await new Promise < {
226
+ hermesEnabled ?: boolean ;
227
+ } > ( ( resolve ) => {
169
228
properties . parse (
170
229
'./android/gradle.properties' ,
171
230
{ path : true } ,
@@ -322,7 +381,11 @@ async function compileHermesByteCode(
322
381
}
323
382
}
324
383
325
- async function copyDebugidForSentry ( bundleName : string , outputFolder : string , sourcemapOutput : string ) {
384
+ async function copyDebugidForSentry (
385
+ bundleName : string ,
386
+ outputFolder : string ,
387
+ sourcemapOutput : string ,
388
+ ) {
326
389
if ( sourcemapOutput ) {
327
390
let copyDebugidPath ;
328
391
try {
@@ -423,7 +486,10 @@ async function pack(dir: string, output: string) {
423
486
}
424
487
const childs = fs . readdirSync ( root ) ;
425
488
for ( const name of childs ) {
426
- if ( ignorePackingFileNames . includes ( name ) || ignorePackingExtensions . some ( ext => name . endsWith ( `.${ ext } ` ) ) ) {
489
+ if (
490
+ ignorePackingFileNames . includes ( name ) ||
491
+ ignorePackingExtensions . some ( ( ext ) => name . endsWith ( `.${ ext } ` ) )
492
+ ) {
427
493
continue ;
428
494
}
429
495
const fullPath = path . join ( root , name ) ;
@@ -723,55 +789,66 @@ async function diffFromPackage(
723
789
await writePromise ;
724
790
}
725
791
726
- export async function enumZipEntries ( zipFn : string , callback : ( entry : any , zipFile : any ) => void , nestedPath = '' ) {
792
+ export async function enumZipEntries (
793
+ zipFn : string ,
794
+ callback : ( entry : any , zipFile : any ) => void ,
795
+ nestedPath = '' ,
796
+ ) {
727
797
return new Promise ( ( resolve , reject ) => {
728
- openZipFile ( zipFn , { lazyEntries : true } , async ( err : any , zipfile : ZipFile ) => {
729
- if ( err ) {
730
- return reject ( err ) ;
731
- }
798
+ openZipFile (
799
+ zipFn ,
800
+ { lazyEntries : true } ,
801
+ async ( err : any , zipfile : ZipFile ) => {
802
+ if ( err ) {
803
+ return reject ( err ) ;
804
+ }
732
805
733
- zipfile . on ( 'end' , resolve ) ;
734
- zipfile . on ( 'error' , reject ) ;
735
- zipfile . on ( 'entry' , async ( entry ) => {
736
- const fullPath = nestedPath + entry . fileName ;
737
-
738
- try {
739
- if (
740
- ! entry . fileName . endsWith ( '/' ) &&
741
- entry . fileName . toLowerCase ( ) . endsWith ( '.hap' )
742
- ) {
743
- const tempDir = path . join ( os . tmpdir ( ) , `nested_zip_${ Date . now ( ) } ` ) ;
744
- await fs . ensureDir ( tempDir ) ;
745
- const tempZipPath = path . join ( tempDir , 'temp.zip' ) ;
746
-
747
- await new Promise ( ( res , rej ) => {
748
- zipfile . openReadStream ( entry , async ( err , readStream ) => {
749
- if ( err ) return rej ( err ) ;
750
- const writeStream = fs . createWriteStream ( tempZipPath ) ;
751
- readStream . pipe ( writeStream ) ;
752
- writeStream . on ( 'finish' , res ) ;
753
- writeStream . on ( 'error' , rej ) ;
806
+ zipfile . on ( 'end' , resolve ) ;
807
+ zipfile . on ( 'error' , reject ) ;
808
+ zipfile . on ( 'entry' , async ( entry ) => {
809
+ const fullPath = nestedPath + entry . fileName ;
810
+
811
+ try {
812
+ if (
813
+ ! entry . fileName . endsWith ( '/' ) &&
814
+ entry . fileName . toLowerCase ( ) . endsWith ( '.hap' )
815
+ ) {
816
+ const tempDir = path . join (
817
+ os . tmpdir ( ) ,
818
+ `nested_zip_${ Date . now ( ) } ` ,
819
+ ) ;
820
+ await fs . ensureDir ( tempDir ) ;
821
+ const tempZipPath = path . join ( tempDir , 'temp.zip' ) ;
822
+
823
+ await new Promise ( ( res , rej ) => {
824
+ zipfile . openReadStream ( entry , async ( err , readStream ) => {
825
+ if ( err ) return rej ( err ) ;
826
+ const writeStream = fs . createWriteStream ( tempZipPath ) ;
827
+ readStream . pipe ( writeStream ) ;
828
+ writeStream . on ( 'finish' , res ) ;
829
+ writeStream . on ( 'error' , rej ) ;
830
+ } ) ;
754
831
} ) ;
755
- } ) ;
756
832
757
- await enumZipEntries ( tempZipPath , callback , `${ fullPath } /` ) ;
833
+ await enumZipEntries ( tempZipPath , callback , `${ fullPath } /` ) ;
758
834
759
- await fs . remove ( tempDir ) ;
760
- }
835
+ await fs . remove ( tempDir ) ;
836
+ }
761
837
762
- const result = callback ( entry , zipfile , fullPath ) ;
763
- if ( result && typeof result . then === 'function' ) {
764
- await result ;
838
+ const result = callback ( entry , zipfile , fullPath ) ;
839
+ if ( result && typeof result . then === 'function' ) {
840
+ await result ;
841
+ }
842
+ } catch ( error ) {
843
+ console . error ( '处理文件时出错:' , error ) ;
765
844
}
766
- } catch ( error ) {
767
- console . error ( '处理文件时出错:' , error ) ;
768
- }
769
845
770
- zipfile . readEntry ( ) ;
771
- } ) ;
846
+ zipfile . readEntry ( ) ;
847
+ } ) ;
772
848
773
- zipfile . readEntry ( ) ;
774
- } ) ;
849
+ zipfile . readEntry ( ) ;
850
+ } ,
851
+ ) ;
775
852
} ) ;
776
853
}
777
854
@@ -817,11 +894,20 @@ export const commands = {
817
894
options . platform || ( await question ( '平台(ios/android/harmony):' ) ) ,
818
895
) ;
819
896
820
- const { bundleName, entryFile, intermediaDir, output, dev, sourcemap } =
821
- translateOptions ( {
822
- ...options ,
823
- platform,
824
- } ) ;
897
+ const {
898
+ bundleName,
899
+ entryFile,
900
+ intermediaDir,
901
+ output,
902
+ dev,
903
+ sourcemap,
904
+ taro,
905
+ expo,
906
+ rncli,
907
+ } = translateOptions ( {
908
+ ...options ,
909
+ platform,
910
+ } ) ;
825
911
826
912
const bundleParams = await checkPlugins ( ) ;
827
913
const sourcemapPlugin = bundleParams . sourcemap ;
@@ -839,20 +925,25 @@ export const commands = {
839
925
840
926
console . log ( `Bundling with react-native: ${ version } ` ) ;
841
927
842
- await runReactNativeBundleCommand (
928
+ await runReactNativeBundleCommand ( {
843
929
bundleName,
844
930
dev,
845
931
entryFile,
846
- intermediaDir ,
932
+ outputFolder : intermediaDir ,
847
933
platform,
848
- sourcemap || sourcemapPlugin ? sourcemapOutput : '' ,
849
- ) ;
934
+ sourcemapOutput : sourcemap || sourcemapPlugin ? sourcemapOutput : '' ,
935
+ cli : {
936
+ taro,
937
+ expo,
938
+ rncli,
939
+ } ,
940
+ } ) ;
850
941
851
942
await pack ( path . resolve ( intermediaDir ) , realOutput ) ;
852
943
853
944
const v = await question ( '是否现在上传此热更包?(Y/N)' ) ;
854
945
if ( v . toLowerCase ( ) === 'y' ) {
855
- const versionName = await this . publish ( {
946
+ const versionName = await this . publish ( {
856
947
args : [ realOutput ] ,
857
948
options : {
858
949
platform,
0 commit comments