Parcourir la source

Fix rnpm script #53

Ben Hsieh il y a 8 ans
Parent
révision
1c8d085759
1 fichiers modifiés avec 42 ajouts et 0 suppressions
  1. 42
    0
      src/scripts/prelink.js

+ 42
- 0
src/scripts/prelink.js Voir le fichier

@@ -1,11 +1,47 @@
1 1
 var fs = require('fs');
2
+
2 3
 var MANIFEST_PATH = process.cwd() + '/android/app/src/main/AndroidManifest.xml';
4
+var PACKAGE_JSON = process.cwd() + '/package.json';
5
+
6
+var hasNecessaryFile = fs.existsSync(MANIFEST_PATH) && fs.existsSync(MANIFEST_PATH);
7
+
8
+if (!hasNecessaryFile) {
9
+  throw 'RNFetchBlob could not found link Android automatically, some files could not be found.'
10
+}
11
+
12
+var package = JSON.parse(fs.readFileSync(PACKAGE_JSON));
13
+var APP_NAME = package.name;
14
+var APPLICATION_MAIN = process.cwd() + '/android/app/src/main/java/com/' + APP_NAME.toLocaleLowerCase() + '/MainApplication.java';
15
+
16
+if(!fs.existsSync(APPLICATION_MAIN)) {
17
+  throw 'RNFetchBlob could not found link Android automatically, MainApplication.java not found in path : ' + APPLICATION_MAIN
18
+}
19
+
20
+var VERSION = checkVersion();
21
+console.log('RNFetchBlob detected app version .. ' + VERSION);
3 22
 
23
+if(VERSION >= 0.29) {
24
+  console.log('RNFetchBlob patching MainApplication.java .. ');
25
+  var main = fs.readFileSync(APPLICATION_MAIN);
26
+  if(String(main).match('new RNFetchBlobPackage()') !== null) {
27
+    console.log('skipped');
28
+    return
29
+  }
30
+  main = String(main).replace('new MainReactPackage()', 'new RNFetchBlobPackage(),\n           new MainReactPackage()');
31
+  main = String(main).replace('import com.facebook.react.ReactApplication;', 'import com.facebook.react.ReactApplication;\nimport com.RNFetchBlob.RNFetchBlobPackage;')
32
+  fs.writeFileSync(APPLICATION_MAIN, main);
33
+  console.log('RNFetchBlob patching MainApplication.java .. ok')
34
+
35
+}
36
+
37
+// set file access permission for Android < 6.0
4 38
 fs.readFile(MANIFEST_PATH, function(err, data) {
5 39
 
6 40
   if(err)
7 41
     console.log('failed to locate AndroidManifest.xml file, you may have to add file access permission manually.');
8 42
   else {
43
+
44
+    console.log('RNFetchBlob patching AndroidManifest.xml .. ');
9 45
     // append fs permission
10 46
     data = String(data).replace(
11 47
       '<uses-permission android:name="android.permission.INTERNET" />',
@@ -17,7 +53,13 @@ fs.readFile(MANIFEST_PATH, function(err, data) {
17 53
       '<category android:name="android.intent.category.LAUNCHER" />\n     <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>'
18 54
     )
19 55
     fs.writeFileSync(MANIFEST_PATH, data);
56
+    console.log('RNFetchBlob patching AndroidManifest.xml .. ok');
20 57
 
21 58
   }
22 59
 
23 60
 })
61
+
62
+function checkVersion() {
63
+  console.log('RNFetchBlob checking app version ..');
64
+  return parseFloat(/\d\.\d+(?=\.)/.exec(package.dependencies['react-native']));
65
+}