This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
forked from Instabug/Instabug-React-Native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlink_gradle.js
97 lines (86 loc) · 2.34 KB
/
unlink_gradle.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
'use strict';
var fs = require('fs');
const LOG_LEVEL_SUCCESS = 0;
const LOG_LEVEL_WARN = 1;
const CHAR_OPEN_PARAN = '{';
const CHAR_CLOSED_PARAN = '}';
const GRADLE_FILE_PATH = 'android/build.gradle';
const MAVEN_REPO_URL =
'https://sdks.instabug.com/nexus/repository/instabug-cp';
function readFile(filePath, success) {
fs.readFile(filePath, 'utf-8', function(err, data) {
if (err) {
finish(
LOG_LEVEL_WARN,
`Linking process could not be completed because of\n${err.message}`
);
}
success(data);
});
}
function findClosureStart(data, index) {
const alphRegex = /[a-z]/;
let maven = '';
let foundOpenParan = false;
let closureStart = -1;
for (let i = index; i >= 0; i--) {
if (!foundOpenParan) {
foundOpenParan = data.charAt(i) === CHAR_OPEN_PARAN;
}
if (alphRegex.test(data.charAt(i)) && foundOpenParan) {
maven = data.charAt(i) + maven;
}
if (maven === 'maven') {
closureStart = i;
break;
}
}
return closureStart;
}
function findClosureEnd(data, index) {
const startIndex = index + MAVEN_REPO_URL.length + 2;
let closureEnd = -1;
//after
for (let i = startIndex; i < data.length; i++) {
if (data.charAt(i) === CHAR_CLOSED_PARAN) {
closureEnd = i;
break;
}
}
return closureEnd;
}
function removeMavenRepo(data) {
const regex = /\"https:\/\/sdks.instabug.com\/nexus\/repository\/instabug-cp\"/;
if (!regex.test(data) || !data.match(regex)) {
finish(LOG_LEVEL_SUCCESS, 'Already Unlinked');
return data;
} else {
const start = findClosureStart(data, data.match(regex).index);
const end = findClosureEnd(data, data.match(regex).index);
let newGradle =
data.substring(0, start) + data.substring(end + 1, data.length);
return newGradle;
}
}
function writeFile(data) {
fs.writeFile(GRADLE_FILE_PATH, data, err => {
if (err) {
finish(
LOG_LEVEL_WARN,
`Unlinking process could not be completed because of\n${err.message}`
);
}
finish(LOG_LEVEL_SUCCESS, 'Unlinking process completed successfully');
});
}
function finish(logLevel, message) {
if (logLevel === LOG_LEVEL_SUCCESS) {
console.info(message);
} else {
console.warn(message);
}
}
readFile(GRADLE_FILE_PATH, function(data) {
const newGradle = removeMavenRepo(data);
writeFile(newGradle);
});