|
@@ -1,83 +1,96 @@
|
1
|
|
-var fs = require('fs');
|
|
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 APPLICATION_MAIN = glob.sync(process.cwd() + '/android/app/src/main/**/MainApplication.java')[0];
|
|
10
|
+ var PACKAGE_GRADLE = process.cwd() + '/node_modules/react-native-fetch-blob/android/build.gradle'
|
|
11
|
+ var hasNecessaryFile = MANIFEST_PATH && APPLICATION_MAIN;
|
|
12
|
+ var VERSION = checkVersion();
|
2
|
13
|
|
3
|
|
-var MANIFEST_PATH = process.cwd() + '/android/app/src/main/AndroidManifest.xml';
|
4
|
|
-var PACKAGE_JSON = process.cwd() + '/package.json';
|
|
14
|
+ console.log('RNFetchBlob detected app version => ' + VERSION);
|
5
|
15
|
|
6
|
|
-var hasNecessaryFile = fs.existsSync(MANIFEST_PATH) && fs.existsSync(MANIFEST_PATH);
|
|
16
|
+ if(VERSION >= 0.29) {
|
|
17
|
+ console.log('RNFetchBlob patching MainApplication.java .. ');
|
|
18
|
+ if(!hasNecessaryFile) {
|
|
19
|
+ console.log(
|
|
20
|
+ '\033[95mreact-native-fetch-blob\033[97m link \033[91mFAILED \033[97m\nCould not automatically link Android package, '+
|
|
21
|
+ 'please follow the instructions to manually link the library : ' +
|
|
22
|
+ '\033[4mhttps://github.com/wkh237/react-native-fetch-blob#manually-link-the-package-android\n\n')
|
|
23
|
+ return
|
|
24
|
+ }
|
|
25
|
+ var main = fs.readFileSync(APPLICATION_MAIN);
|
|
26
|
+ if(String(main).match('new RNFetchBlobPackage()') === null) {
|
7
|
27
|
|
8
|
|
-if (!hasNecessaryFile) {
|
9
|
|
- console.log('\033[31m*** react-native-fetch-blob pre-link FAILED ***')
|
10
|
|
- console.log('\033[31mRNFetchBlob could not link Android automatically, some files could not be found.')
|
11
|
|
- console.log('\033[31mYou may have to manually link the library, see \033[36mhttps://github.com/wkh237/react-native-fetch-blob#manually-link-the-package-android \033[31mfor more information.')
|
12
|
|
- return
|
13
|
|
-}
|
14
|
|
-
|
15
|
|
-var package = JSON.parse(fs.readFileSync(PACKAGE_JSON));
|
16
|
|
-var APP_NAME = package.name;
|
17
|
|
-var APPLICATION_MAIN = process.cwd() + '/android/app/src/main/java/com/' + APP_NAME.toLocaleLowerCase() + '/MainApplication.java';
|
18
|
|
-var PACKAGE_GRADLE = process.cwd() + '/node_modules/react-native-fetch-blob/android/build.gradle'
|
19
|
|
-
|
20
|
|
-var VERSION = checkVersion();
|
21
|
|
-console.log('RNFetchBlob detected app version .. ' + VERSION);
|
|
28
|
+ main = String(main).replace('new MainReactPackage()', 'new RNFetchBlobPackage(),\n new MainReactPackage()');
|
|
29
|
+ main = String(main).replace('import com.facebook.react.ReactApplication;', 'import com.facebook.react.ReactApplication;\nimport com.RNFetchBlob.RNFetchBlobPackage;')
|
22
|
30
|
|
23
|
|
-if(VERSION >= 0.29) {
|
24
|
|
- console.log('RNFetchBlob patching MainApplication.java .. ');
|
25
|
|
- if(!fs.existsSync(APPLICATION_MAIN)) {
|
26
|
|
- console.log('\033[31m*** react-native-fetch-blob pre-link FAILED ***')
|
27
|
|
- console.log('\033[31mRNFetchBlob could not link Android automatically, MainApplication.java not found in path : ' + APPLICATION_MAIN)
|
28
|
|
- console.log('\033[31mRNFetchBlob could not link Android automatically, some files could not be found.')
|
29
|
|
- console.log('\033[31mTry manually link the library, see \033[36mhttps://github.com/wkh237/react-native-fetch-blob#manually-link-the-package-android \033[31mfor more information.')
|
30
|
|
- return
|
|
31
|
+ fs.writeFileSync(APPLICATION_MAIN, main);
|
|
32
|
+ console.log('RNFetchBlob patching MainApplication.java .. ok')
|
|
33
|
+ }
|
|
34
|
+ else {
|
|
35
|
+ console.log('Project already linked, process skipped.');
|
|
36
|
+ }
|
31
|
37
|
}
|
32
|
|
- var main = fs.readFileSync(APPLICATION_MAIN);
|
33
|
|
- if(String(main).match('new RNFetchBlobPackage()') !== null) {
|
34
|
|
- console.log('skipped');
|
35
|
|
- return
|
|
38
|
+
|
|
39
|
+ if(VERSION < 0.28) {
|
|
40
|
+ console.log('You project version is '+ VERSION + ' which may not compatible to react-native-fetch-blob 7.0+, please consider upgrade your application template to react-native 0.27+.')
|
|
41
|
+ // add OkHttp3 dependency fo pre 0.28 project
|
|
42
|
+ var main = fs.readFileSync(PACKAGE_GRADLE);
|
|
43
|
+ console.log('adding OkHttp3 dependency to pre 0.28 project .. ')
|
|
44
|
+ main = String(main).replace('//{RNFetchBlob_PRE_0.28_DEPDENDENCY}', "compile 'com.squareup.okhttp3:okhttp:3.4.1'");
|
|
45
|
+ fs.writeFileSync(PACKAGE_GRADLE, main);
|
|
46
|
+ console.log('adding OkHttp3 dependency to pre 0.28 project .. ok')
|
36
|
47
|
}
|
37
|
|
- main = String(main).replace('new MainReactPackage()', 'new RNFetchBlobPackage(),\n new MainReactPackage()');
|
38
|
|
- main = String(main).replace('import com.facebook.react.ReactApplication;', 'import com.facebook.react.ReactApplication;\nimport com.RNFetchBlob.RNFetchBlobPackage;')
|
39
|
48
|
|
40
|
|
- fs.writeFileSync(APPLICATION_MAIN, main);
|
41
|
|
- console.log('RNFetchBlob patching MainApplication.java .. ok')
|
|
49
|
+ console.log('Add Android permissions => ' + (addAndroidPermissions == "true"))
|
42
|
50
|
|
43
|
|
-}
|
|
51
|
+ if(addAndroidPermissions) {
|
44
|
52
|
|
45
|
|
-if(VERSION < 0.28) {
|
46
|
|
- 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.')
|
47
|
|
- // add OkHttp3 dependency fo 0.28- project
|
48
|
|
- var main = fs.readFileSync(PACKAGE_GRADLE);
|
49
|
|
- console.log('adding OkHttp3 dependency to pre 0.28 project .. ')
|
50
|
|
- main = String(main).replace('//{RNFetchBlob_PRE_0.28_DEPDENDENCY}', "compile 'com.squareup.okhttp3:okhttp:3.4.1'");
|
51
|
|
- fs.writeFileSync(PACKAGE_GRADLE, main);
|
52
|
|
- console.log('adding OkHttp3 dependency to pre 0.28 project .. ok')
|
53
|
|
-}
|
|
53
|
+ // set file access permission for Android < 6.0
|
|
54
|
+ fs.readFile(MANIFEST_PATH, function(err, data) {
|
54
|
55
|
|
55
|
|
-// set file access permission for Android < 6.0
|
56
|
|
-fs.readFile(MANIFEST_PATH, function(err, data) {
|
|
56
|
+ if(err)
|
|
57
|
+ console.log('failed to locate AndroidManifest.xml file, you may have to add file access permission manually.');
|
|
58
|
+ else {
|
57
|
59
|
|
58
|
|
- if(err)
|
59
|
|
- console.log('failed to locate AndroidManifest.xml file, you may have to add file access permission manually.');
|
60
|
|
- else {
|
|
60
|
+ console.log('RNFetchBlob patching AndroidManifest.xml .. ');
|
|
61
|
+ // append fs permission
|
|
62
|
+ data = String(data).replace(
|
|
63
|
+ '<uses-permission android:name="android.permission.INTERNET" />',
|
|
64
|
+ '<uses-permission android:name="android.permission.INTERNET" />\n <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> '
|
|
65
|
+ )
|
|
66
|
+ // append DOWNLOAD_COMPLETE intent permission
|
|
67
|
+ data = String(data).replace(
|
|
68
|
+ '<category android:name="android.intent.category.LAUNCHER" />',
|
|
69
|
+ '<category android:name="android.intent.category.LAUNCHER" />\n <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>'
|
|
70
|
+ )
|
|
71
|
+ fs.writeFileSync(MANIFEST_PATH, data);
|
|
72
|
+ console.log('RNFetchBlob patching AndroidManifest.xml .. ok');
|
61
|
73
|
|
62
|
|
- console.log('RNFetchBlob patching AndroidManifest.xml .. ');
|
63
|
|
- // append fs permission
|
64
|
|
- data = String(data).replace(
|
65
|
|
- '<uses-permission android:name="android.permission.INTERNET" />',
|
66
|
|
- '<uses-permission android:name="android.permission.INTERNET" />\n <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> '
|
67
|
|
- )
|
68
|
|
- // append DOWNLOAD_COMPLETE intent permission
|
69
|
|
- data = String(data).replace(
|
70
|
|
- '<category android:name="android.intent.category.LAUNCHER" />',
|
71
|
|
- '<category android:name="android.intent.category.LAUNCHER" />\n <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>'
|
72
|
|
- )
|
73
|
|
- fs.writeFileSync(MANIFEST_PATH, data);
|
74
|
|
- console.log('RNFetchBlob patching AndroidManifest.xml .. ok');
|
|
74
|
+ }
|
75
|
75
|
|
|
76
|
+ })
|
|
77
|
+ }
|
|
78
|
+ else {
|
|
79
|
+ console.log(
|
|
80
|
+ '\033[95mreact-native-fetch-blob \033[97mwill not automatically add Android permissions after \033[92m0.9.5 '+
|
|
81
|
+ '\033[97mplease run the following command if you want to add default permissions :\n\n' +
|
|
82
|
+ '\033[96m\tRNFB_ANDROID_PERMISSION=true rnpm link\n\n')
|
76
|
83
|
}
|
77
|
84
|
|
78
|
|
-})
|
|
85
|
+ function checkVersion() {
|
|
86
|
+ console.log('RNFetchBlob checking app version ..');
|
|
87
|
+ return parseFloat(/\d\.\d+(?=\.)/.exec(package.dependencies['react-native']));
|
|
88
|
+ }
|
79
|
89
|
|
80
|
|
-function checkVersion() {
|
81
|
|
- console.log('RNFetchBlob checking app version ..');
|
82
|
|
- return parseFloat(/\d\.\d+(?=\.)/.exec(package.dependencies['react-native']));
|
|
90
|
+} catch(err) {
|
|
91
|
+ console.log(
|
|
92
|
+ '\033[95mreact-native-fetch-blob\033[97m link \033[91mFAILED \033[97m\nCould not automatically link package :'+
|
|
93
|
+ err.stack +
|
|
94
|
+ 'please follow the instructions to manually link the library : ' +
|
|
95
|
+ '\033[4mhttps://github.com/wkh237/react-native-fetch-blob#manually-link-the-package-android\n\n')
|
83
|
96
|
}
|