Skip to content

Commit 3d41dd8

Browse files
committed
Only replace version once, do not fail on second replacement
1 parent 8dbc973 commit 3d41dd8

File tree

3 files changed

+3529
-6
lines changed

3 files changed

+3529
-6
lines changed

ReplaceVersion.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ module.exports = {
1616
const regexVariableDefinition = new RegExp(`(${variableName}(\\s+)?=(\\s+)?('|")${oldVersion}('|"))`, "ig")
1717
const regexVariableDefinitionMatches = regexVariableDefinition.exec(modifiedBody)
1818

19-
regexVariableDefinitionMatches.filter(it => it.includes(dependency.oldVersion)).forEach(match => {
20-
modifiedBody = modifiedBody.replace(match, match.replace(dependency.oldVersion, dependency.version))
21-
})
19+
if (regexVariableDefinitionMatches && regexVariableDefinitionMatches.length) {
20+
regexVariableDefinitionMatches.filter(it => it.includes(dependency.oldVersion)).forEach(match => {
21+
modifiedBody = modifiedBody.replace(match, match.replace(dependency.oldVersion, dependency.version))
22+
})
23+
}
2224
}
2325

2426
// compile 'de.kevcodez:pubg-api-wrapper:1.0.0'
@@ -30,16 +32,16 @@ module.exports = {
3032
// id 'com.github.ben-manes.versions' version "0.21.0"
3133
const regexPluginVersionWithPrefix = new RegExp(`${dependency.group}("|')(\\s+)?version(\\s+)?("|')${oldVersion}("|')`)
3234
const regexVersionWithPrefixMatches = regexPluginVersionWithPrefix.exec(modifiedBody)
33-
if (regexVersionWithPrefixMatches) {
35+
if (regexVersionWithPrefixMatches && regexVersionWithPrefixMatches.length) {
3436
regexVersionWithPrefixMatches.filter(it => it.includes(oldVersion)).forEach(match => {
3537
modifiedBody = modifiedBody.replace(match, match.replace(oldVersion, newVersion))
3638
})
3739
}
38-
40+
3941
// compile group: 'de.kevcodez.pubg', name: 'pubg-api-wrapper', version: '0.8.1'
4042
const regexDependencyWithVersionPrefix = new RegExp(`${dependency.name}('|"),(\\s+)?version:(\\s+)('|")${dependency.oldVersion}('|")`)
4143
const regexDependencyWithVersionPrefixMatches = regexDependencyWithVersionPrefix.exec(modifiedBody)
42-
if (regexDependencyWithVersionPrefixMatches) {
44+
if (regexDependencyWithVersionPrefixMatches && regexDependencyWithVersionPrefixMatches.length) {
4345
regexDependencyWithVersionPrefixMatches.filter(it => it.includes(oldVersion)).forEach(match => {
4446
modifiedBody = modifiedBody.replace(match, match.replace(oldVersion, newVersion))
4547
})

__tests__/ReplaceVersion.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ test('Replace version with variable with parenthensis and double quotation marks
3636

3737
expect(replacedVersion).toContain(`myVar = "1.0.0"`)
3838
})
39+
test('Replace leave variable if it is already updated', () => {
40+
const replacedVersion = ReplaceVersion.replace(`
41+
myVar = "1.0.0"
42+
compile 'de.kevcodez:pubg-api-wrapper:\$\{myVar\}'
43+
`, dependency)
3944

45+
expect(replacedVersion).toContain(`myVar = "1.0.0"`)
46+
})
4047
test('Replace plugin version with single quotation marks', () => {
4148
const pluginDependency = {
4249
group: 'com.github.ben-manes.versions',

0 commit comments

Comments
 (0)