Nav apraksta

prelink.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. try {
  2. var fs = require('fs');
  3. var glob = require('glob');
  4. var addAndroidPermissions = process.env.RNFB_ANDROID_PERMISSIONS == 'true';
  5. var MANIFEST_PATH = glob.sync(process.cwd() + '/android/app/src/main/**/AndroidManifest.xml')[0];
  6. var PACKAGE_JSON = process.cwd() + '/package.json';
  7. var package = JSON.parse(fs.readFileSync(PACKAGE_JSON));
  8. var APP_NAME = package.name;
  9. var PACKAGE_GRADLE = process.cwd() + '/node_modules/rn-fetch-blob/android/build.gradle'
  10. var VERSION = checkVersion();
  11. console.log('RNFetchBlob detected app version => ' + VERSION);
  12. if(VERSION < 0.28) {
  13. console.log('You project version is '+ VERSION + ' which may not compatible to rn-fetch-blob 7.0+, please consider upgrade your application template to react-native 0.27+.')
  14. // add OkHttp3 dependency fo pre 0.28 project
  15. var main = fs.readFileSync(PACKAGE_GRADLE);
  16. console.log('adding OkHttp3 dependency to pre 0.28 project .. ')
  17. main = String(main).replace('//{RNFetchBlob_PRE_0.28_DEPDENDENCY}', "compile 'com.squareup.okhttp3:okhttp:3.4.1'");
  18. fs.writeFileSync(PACKAGE_GRADLE, main);
  19. console.log('adding OkHttp3 dependency to pre 0.28 project .. ok')
  20. }
  21. console.log('Add Android permissions => ' + (addAndroidPermissions == "true"))
  22. if(addAndroidPermissions) {
  23. // set file access permission for Android < 6.0
  24. fs.readFile(MANIFEST_PATH, function(err, data) {
  25. if(err)
  26. console.log('failed to locate AndroidManifest.xml file, you may have to add file access permission manually.');
  27. else {
  28. console.log('RNFetchBlob patching AndroidManifest.xml .. ');
  29. // append fs permission
  30. data = String(data).replace(
  31. '<uses-permission android:name="android.permission.INTERNET" />',
  32. '<uses-permission android:name="android.permission.INTERNET" />\n <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> '
  33. )
  34. // append DOWNLOAD_COMPLETE intent permission
  35. data = String(data).replace(
  36. '<category android:name="android.intent.category.LAUNCHER" />',
  37. '<category android:name="android.intent.category.LAUNCHER" />\n <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>'
  38. )
  39. fs.writeFileSync(MANIFEST_PATH, data);
  40. console.log('RNFetchBlob patching AndroidManifest.xml .. ok');
  41. }
  42. })
  43. }
  44. else {
  45. console.log(
  46. '\033[95mrn-fetch-blob \033[97mwill not automatically add Android permissions after \033[92m0.9.4 '+
  47. '\033[97mplease run the following command if you want to add default permissions :\n\n' +
  48. '\033[96m\tRNFB_ANDROID_PERMISSIONS=true react-native link \n')
  49. }
  50. function checkVersion() {
  51. console.log('RNFetchBlob checking app version ..');
  52. return parseFloat(/\d\.\d+(?=\.)/.exec(package.dependencies['react-native']));
  53. }
  54. } catch(err) {
  55. console.log(
  56. '\033[95mrn-fetch-blob\033[97m link \033[91mFAILED \033[97m\nCould not automatically link package :'+
  57. err.stack +
  58. 'please follow the instructions to manually link the library : ' +
  59. '\033[4mhttps://github.com/joltup/rn-fetch-blob/wiki/Manually-Link-Package\n')
  60. }