Bez popisu

prelink.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var fs = require('fs');
  2. var MANIFEST_PATH = process.cwd() + '/android/app/src/main/AndroidManifest.xml';
  3. var PACKAGE_JSON = process.cwd() + '/package.json';
  4. var hasNecessaryFile = fs.existsSync(MANIFEST_PATH) && fs.existsSync(MANIFEST_PATH);
  5. if (!hasNecessaryFile) {
  6. throw 'RNFetchBlob could not found link Android automatically, some files could not be found.'
  7. }
  8. var package = JSON.parse(fs.readFileSync(PACKAGE_JSON));
  9. var APP_NAME = package.name;
  10. var APPLICATION_MAIN = process.cwd() + '/android/app/src/main/java/com/' + APP_NAME.toLocaleLowerCase() + '/MainApplication.java';
  11. var PACKAGE_GRADLE = process.cwd() + '/node_modules/react-native-fetch-blob/android/build.gradle'
  12. var VERSION = checkVersion();
  13. console.log('RNFetchBlob detected app version .. ' + VERSION);
  14. if(VERSION >= 0.29) {
  15. console.log('RNFetchBlob patching MainApplication.java .. ');
  16. if(!fs.existsSync(APPLICATION_MAIN)) {
  17. throw 'RNFetchBlob could not link Android automatically, MainApplication.java not found in path : ' + APPLICATION_MAIN
  18. }
  19. var main = fs.readFileSync(APPLICATION_MAIN);
  20. if(String(main).match('new RNFetchBlobPackage()') !== null) {
  21. console.log('skipped');
  22. return
  23. }
  24. main = String(main).replace('new MainReactPackage()', 'new RNFetchBlobPackage(),\n new MainReactPackage()');
  25. main = String(main).replace('import com.facebook.react.ReactApplication;', 'import com.facebook.react.ReactApplication;\nimport com.RNFetchBlob.RNFetchBlobPackage;')
  26. fs.writeFileSync(APPLICATION_MAIN, main);
  27. console.log('RNFetchBlob patching MainApplication.java .. ok')
  28. }
  29. if(VERSION < 0.28) {
  30. // console.log('You project version is '+ VERSION + 'which does not meet requirement of react-native-fetch-blob 7.0+, please upgrade your application template to react-native 0.27+, otherwise Android application will not working.')
  31. // add OkHttp3 dependency fo 0.28- project
  32. var main = fs.readFileSync(PACKAGE_GRADLE);
  33. console.log('adding OkHttp3 dependency to pre 0.28 project .. ')
  34. main = String(main).replace('//{RNFetchBlob_PRE_0.28_DEPDENDENCY}', "compile 'com.squareup.okhttp3:okhttp:3.4.1'");
  35. fs.writeFileSync(PACKAGE_GRADLE, main);
  36. console.log('adding OkHttp3 dependency to pre 0.28 project .. ok')
  37. }
  38. // set file access permission for Android < 6.0
  39. fs.readFile(MANIFEST_PATH, function(err, data) {
  40. if(err)
  41. console.log('failed to locate AndroidManifest.xml file, you may have to add file access permission manually.');
  42. else {
  43. console.log('RNFetchBlob patching AndroidManifest.xml .. ');
  44. // append fs permission
  45. data = String(data).replace(
  46. '<uses-permission android:name="android.permission.INTERNET" />',
  47. '<uses-permission android:name="android.permission.INTERNET" />\n <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> '
  48. )
  49. // append DOWNLOAD_COMPLETE intent permission
  50. data = String(data).replace(
  51. '<category android:name="android.intent.category.LAUNCHER" />',
  52. '<category android:name="android.intent.category.LAUNCHER" />\n <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>'
  53. )
  54. fs.writeFileSync(MANIFEST_PATH, data);
  55. console.log('RNFetchBlob patching AndroidManifest.xml .. ok');
  56. }
  57. })
  58. function checkVersion() {
  59. console.log('RNFetchBlob checking app version ..');
  60. return parseFloat(/\d\.\d+(?=\.)/.exec(package.dependencies['react-native']));
  61. }