1
1
/*
2
2
* This script generates a hash of all the React Native bundled assets and writes it into
3
- * into the APK. The hash in "updateCheck" requests to prevent downloading an identical
3
+ * into the APK. The hash in "updateCheck" requests to prevent downloading an identical
4
4
* update to the one already present in the binary.
5
- *
6
- * It first creates a snapshot of the contents in the resource directory by creating
7
- * a map with the modified time of all the files in the directory. It then compares this
5
+ *
6
+ * It first creates a snapshot of the contents in the resource directory by creating
7
+ * a map with the modified time of all the files in the directory. It then compares this
8
8
* snapshot with the one saved earlier in "recordFilesBeforeBundleCommand.js" to figure
9
9
* out which files were generated by the "react-native bundle" command. It then computes
10
10
* the hash for each file to generate a manifest, and then computes a hash over the entire
@@ -49,44 +49,53 @@ var manifest = [];
49
49
if ( bundleGeneratedAssetFiles . length ) {
50
50
bundleGeneratedAssetFiles . forEach ( function ( assetFile ) {
51
51
// Generate hash for each asset file
52
- var readStream = fs . createReadStream ( resourcesDir + assetFile ) ;
53
- var hashStream = crypto . createHash ( HASH_ALGORITHM ) ;
54
-
55
- readStream . pipe ( hashStream )
56
- . on ( "error" , function ( error ) {
57
- throw error ;
58
- } )
59
- . on ( "finish" , function ( ) {
60
- hashStream . end ( ) ;
61
- var buffer = hashStream . read ( ) ;
62
- var fileHash = buffer . toString ( "hex" ) ;
63
- manifest . push ( CODE_PUSH_FOLDER_PREFIX + assetFile . replace ( / \\ / g, "/" ) + ":" + fileHash ) ;
64
-
65
- if ( manifest . length === bundleGeneratedAssetFiles . length ) {
66
- // Generate hash for JS bundle
67
- readStream = fs . createReadStream ( jsBundleFilePath ) ;
68
- hashStream = crypto . createHash ( HASH_ALGORITHM ) ;
69
- readStream . pipe ( hashStream )
70
- . on ( "error" , function ( error ) {
71
- throw error ;
72
- } )
73
- . on ( "finish" , function ( ) {
74
- hashStream . end ( ) ;
75
- var buffer = hashStream . read ( ) ;
76
- var fileHash = buffer . toString ( "hex" ) ;
77
- manifest . push ( CODE_PUSH_FOLDER_PREFIX + "/" + path . basename ( jsBundleFilePath ) + ":" + fileHash ) ;
78
- manifest = manifest . sort ( ) ;
79
-
80
- var finalHash = crypto . createHash ( HASH_ALGORITHM )
81
- . update ( JSON . stringify ( manifest ) )
82
- . digest ( "hex" ) ;
83
-
84
- var savedResourcesManifestPath = assetsDir + "/" + CODE_PUSH_HASH_FILE_NAME ;
85
- fs . writeFileSync ( savedResourcesManifestPath , finalHash ) ;
86
- } ) ;
87
- }
88
- } ) ;
52
+ addFileToManifest ( resourcesDir , assetFile , manifest , function ( ) {
53
+ if ( manifest . length === bundleGeneratedAssetFiles . length ) {
54
+ // Generate hash for JS bundle
55
+ addFileToManifest ( path . dirname ( jsBundleFilePath ) , path . basename ( jsBundleFilePath ) , manifest , function ( ) {
56
+ // ...and the JS bundle "meta"
57
+ var jsBundleMetaFilePath = jsBundleFilePath + ".meta" ;
58
+ addFileToManifest ( path . dirname ( jsBundleMetaFilePath ) , path . basename ( jsBundleMetaFilePath ) , manifest , function ( ) {
59
+ manifest = manifest . sort ( ) ;
60
+ var finalHash = crypto . createHash ( HASH_ALGORITHM )
61
+ . update ( JSON . stringify ( manifest ) )
62
+ . digest ( "hex" ) ;
63
+
64
+ var savedResourcesManifestPath = assetsDir + "/" + CODE_PUSH_HASH_FILE_NAME ;
65
+ fs . writeFileSync ( savedResourcesManifestPath , finalHash ) ;
66
+ } ) ;
67
+ } ) ;
68
+ }
69
+ } ) ;
89
70
} ) ;
90
71
}
91
72
73
+ function addFileToManifest ( folder , assetFile , manifest , done ) {
74
+ var fullFilePath = path . join ( folder , assetFile ) ;
75
+ if ( ! fileExists ( fullFilePath ) ) {
76
+ done ( ) ;
77
+ return ;
78
+ }
79
+
80
+ var readStream = fs . createReadStream ( path . join ( folder , assetFile ) ) ;
81
+ var hashStream = crypto . createHash ( HASH_ALGORITHM ) ;
82
+
83
+ readStream . pipe ( hashStream )
84
+ . on ( "error" , function ( error ) {
85
+ throw error ;
86
+ } )
87
+ . on ( "finish" , function ( ) {
88
+ hashStream . end ( ) ;
89
+ var buffer = hashStream . read ( ) ;
90
+ var fileHash = buffer . toString ( "hex" ) ;
91
+ manifest . push ( path . join ( CODE_PUSH_FOLDER_PREFIX , assetFile ) . replace ( / \\ / g, "/" ) + ":" + fileHash ) ;
92
+ done ( ) ;
93
+ } ) ;
94
+ }
95
+
96
+ function fileExists ( file ) {
97
+ try { return fs . statSync ( file ) . isFile ( ) ; }
98
+ catch ( e ) { return false ; }
99
+ }
100
+
92
101
fs . unlinkSync ( TEMP_FILE_PATH ) ;
0 commit comments