Skip to content
Snippets Groups Projects
Commit 84d69a4c authored by Rhys Arkins's avatar Rhys Arkins
Browse files

Surgically insert new versions

Closes #9
parent 2c3fcf08
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"dependencies": { "dependencies": {
"gh-got": "5.0.0", "gh-got": "5.0.0",
"got": "6.6.3", "got": "6.6.3",
"lodash": "4.17.4",
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"nodegit": "0.16.0", "nodegit": "0.16.0",
"rimraf": "2.5.4", "rimraf": "2.5.4",
......
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);
}
...@@ -4,6 +4,7 @@ const stable = require('semver-stable'); ...@@ -4,6 +4,7 @@ const stable = require('semver-stable');
const config = require('./config'); const config = require('./config');
const github = require('./github'); const github = require('./github');
const npm = require('./npm'); const npm = require('./npm');
const packageJson = require('./helpers/packageJson');
npm.init(config.verbose); npm.init(config.verbose);
...@@ -115,14 +116,14 @@ function updateDependency(depType, depName, currentVersion, nextVersion) { ...@@ -115,14 +116,14 @@ function updateDependency(depType, depName, currentVersion, nextVersion) {
// Retrieve the package.json from this renovate branch // Retrieve the package.json from this renovate branch
return github.getFile(packageFile, branchName).then(res => { return github.getFile(packageFile, branchName).then(res => {
const currentSHA = res.body.sha; const currentSHA = res.body.sha;
let currentFileContent = JSON.parse(new Buffer(res.body.content, 'base64').toString()); const currentFileContent = new Buffer(res.body.content, 'base64').toString();
if (currentFileContent[depType][depName] !== nextVersion) { const currentFileContentJson = JSON.parse(currentFileContent);
if (currentFileContentJson[depType][depName] !== nextVersion) {
// Branch is new, or needs version updated // Branch is new, or needs version updated
if (config.verbose) { if (config.verbose) {
console.log(`${depName}: Updating to ${nextVersion} in branch ${branchName}`); console.log(`${depName}: Updating to ${nextVersion} in branch ${branchName}`);
} }
currentFileContent[depType][depName] = nextVersion; const newPackageContents = packageJson.setNewValue(currentFileContent, depType, depName, nextVersion);
const newPackageContents = JSON.stringify(currentFileContent, null, 2) + '\n';
return github.writeFile(branchName, currentSHA, packageFile, newPackageContents, commitMessage); return github.writeFile(branchName, currentSHA, packageFile, newPackageContents, commitMessage);
} else { } else {
if (config.verbose) { if (config.verbose) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment