forked from NikitaDudin/react-native-networking-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.js
109 lines (91 loc) · 2.91 KB
/
patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const rootPath = path.join(__dirname, '.');
// ============================================================================
// Get RN version
// ============================================================================
const RNRootPath = path.join(__dirname, '..', 'react-native');
const RNPackageFile = `${RNRootPath}/package.json`;
if (!fs.existsSync(RNPackageFile)) {
console.log('[!] Not exists react-native');
process.exit(1);
}
const packageFile = fs.readFileSync(RNPackageFile);
let packageJSON = null;
try {
packageJSON = JSON.parse(packageFile);
} catch (e) {
console.error(e);
}
if (!packageJSON) {
console.log('[!] Failed to get version of react-native');
process.exit(1);
}
const version = packageJSON.version;
const versions = version.split('.');
const minorVersion = parseInt(versions[1], 10);
const patchVersion = parseInt(version[2], 10);
let patchDir = '';
switch (minorVersion) {
case 64:
case 63:
patchDir = version;
break;
case 62:
patchDir = '0.62.2';
break;
case 61:
patchDir = '0.61.5';
break;
case 60:
patchDir = '0.60.6';
break;
case 59:
patchDir = '0.59.10';
break;
default:
break;
}
const isPatchExists = patchDir !== '' && fs.existsSync(`${rootPath}/patches/${patchDir}`);
if (!isPatchExists) {
console.log(`[!] Unsupported react-native version! (${version})`);
process.exit(1);
}
if (patchDir !== version) {
console.log(`[!] React Native is not the latest version. Please upgrade to ${patchDir} first.`);
process.exit(1);
}
function getAllFiles(dirPath, arrayOfFiles) {
const files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function(file) {
if (file === '.DS_Store') {
return;
}
const filePath = `${dirPath}/${file}`;
if (fs.statSync(filePath).isDirectory()) {
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
} else {
arrayOfFiles.push(filePath);
}
});
return arrayOfFiles;
}
// ============================================================================
// Copy files
// ============================================================================
const targetFilesDir = `${rootPath}/patches/${patchDir}/files`;
getAllFiles(targetFilesDir).forEach(function(sourceFile) {
const destFile = `${RNRootPath}${sourceFile.replace(targetFilesDir, '')}`;
fs.copyFileSync(sourceFile, destFile);
});
// ============================================================================
// Copy Android library
// ============================================================================
const targetLibraryDir = `${rootPath}/patches/${patchDir}/library`;
getAllFiles(targetLibraryDir).forEach(function(sourceFile) {
const destFile = `${RNRootPath}/android/com/facebook/react/react-native${sourceFile.replace(targetLibraryDir, '')}`;
fs.copyFileSync(sourceFile, destFile);
});
console.log('[!] RN Netwokring module was patched!');