forked from airgap-it/airgap-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch-dependency-versions.js
77 lines (61 loc) · 2.21 KB
/
patch-dependency-versions.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
// a workaround for cordova-diagnostic-plugin: the plugin ignores Capacitor config setting and installs all its features
const fs = require('fs')
const path = require('path')
const rootdir = ''
const pluginConfig = path.join(rootdir, 'node_modules/cordova.plugins.diagnostic/plugin.xml')
const configFiles = [pluginConfig]
const LEGACY_SUPPORT_V4_ID = 'androidx.legacy:legacy-support-v4'
const APP_COMPAT_ID = 'androidx.appcompat:appcompat'
function getXMLDependencyRegex(versions, id) {
const targetVersion = versions[id].replace(/\./g, '\\.')
return RegExp(`(?<indent>.*)<framework src="(?<dependencyId>${id}):(?!${targetVersion}).*" \/>`, 'g')
}
function patchDependencyVersion(configFile, versions) {
fs.readFile(configFile, 'utf8', function(err, data) {
if (err) {
return console.log(err)
}
const regexes = [
getXMLDependencyRegex(versions, LEGACY_SUPPORT_V4_ID),
getXMLDependencyRegex(versions, APP_COMPAT_ID)
]
let result = data
regexes.forEach(regex => {
while ((match = regex.exec(result))) {
const indent = match.groups.indent || ''
const dependencyId = match.groups.dependencyId
const version = versions[dependencyId]
if (version !== undefined) {
result = result.replace(regex, `${indent}<framework src="${dependencyId}:${version}" />`)
}
}
})
fs.writeFile(configFile, result, 'utf8', function(err) {
if (err) {
console.log(err)
}
})
})
}
function getVersions(callback) {
const variablesGradle = path.join(rootdir, 'android/variables.gradle')
fs.readFile(variablesGradle, 'utf8', function(err, data) {
if (err) {
callback(err, undefined)
}
const appCompatVersionMatch = RegExp('.*androidxAppCompatVersion = \'(?<version>.+)\'', 'g').exec
(data)
const appCompatVersion = appCompatVersionMatch ? appCompatVersionMatch.groups.version : undefined
const versions = {
[LEGACY_SUPPORT_V4_ID]: '1.0.0',
[APP_COMPAT_ID]: appCompatVersion || '1.3.1'
}
callback(undefined, versions)
})
}
getVersions((err, versions) => {
if (err) {
return console.log(err)
}
configFiles.forEach(configFile => patchDependencyVersion(configFile, versions))
})