diff --git a/package.json b/package.json index 19eea08437656a61232e82cdcb79e822ebb0018f..6c8e9979b113ff5c24bf6882292b8003910fd5de 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "dependencies": { "gh-got": "5.0.0", "got": "6.6.3", + "lodash": "4.17.4", "mkdirp": "0.5.1", "nodegit": "0.16.0", "rimraf": "2.5.4", diff --git a/src/helpers/packageJson.js b/src/helpers/packageJson.js new file mode 100644 index 0000000000000000000000000000000000000000..017fbde7c39600b1eba1be6f64dc01d902a50ccd --- /dev/null +++ b/src/helpers/packageJson.js @@ -0,0 +1,38 @@ +const _ = require('lodash'); + +module.exports = { + setNewValue: function(currentFileContent, depType, depName, newVersion) { + const parsedContents = JSON.parse(currentFileContent); + // Save the old version + const oldVersion = parsedContents[depType][depName]; + // Update the file = this is what we want + parsedContents[depType][depName] = newVersion; + // Look for the old version number + const versionHits = indexes(currentFileContent, `"${oldVersion}"`); + let newSource = null; + // Loop through all instances of string until one matches + versionHits.some(function(index) { + // Replace the string and parse the result + const testSource = replaceStringAtIndex(currentFileContent, oldVersion, newVersion, index+1); + if (_.isEqual(parsedContents, JSON.parse(testSource))) { + newSource = testSource; + return true; + } + }); + return newSource; + } +}; + +function indexes(currentFileContent, find) { + var result = []; + for (i = 0; i < currentFileContent.length; ++i) { + if (currentFileContent.substring(i, i + find.length) == find) { + result.push(i); + } + } + return result; +} + +function replaceStringAtIndex(currentFileContent, oldStr, newStr, index) { + return currentFileContent.substr(0, index) + newStr + currentFileContent.substr(index + oldStr.length); +} diff --git a/src/index.js b/src/index.js index 29e8eee3ece521b3d015eda128674172381e0e6b..498954c8123acab8de89ac57272b90794bcbacb0 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const stable = require('semver-stable'); const config = require('./config'); const github = require('./github'); const npm = require('./npm'); +const packageJson = require('./helpers/packageJson'); npm.init(config.verbose); @@ -115,14 +116,14 @@ function updateDependency(depType, depName, currentVersion, nextVersion) { // Retrieve the package.json from this renovate branch return github.getFile(packageFile, branchName).then(res => { const currentSHA = res.body.sha; - let currentFileContent = JSON.parse(new Buffer(res.body.content, 'base64').toString()); - if (currentFileContent[depType][depName] !== nextVersion) { + const currentFileContent = new Buffer(res.body.content, 'base64').toString(); + const currentFileContentJson = JSON.parse(currentFileContent); + if (currentFileContentJson[depType][depName] !== nextVersion) { // Branch is new, or needs version updated if (config.verbose) { console.log(`${depName}: Updating to ${nextVersion} in branch ${branchName}`); } - currentFileContent[depType][depName] = nextVersion; - const newPackageContents = JSON.stringify(currentFileContent, null, 2) + '\n'; + const newPackageContents = packageJson.setNewValue(currentFileContent, depType, depName, nextVersion); return github.writeFile(branchName, currentSHA, packageFile, newPackageContents, commitMessage); } else { if (config.verbose) {