diff --git a/src/helpers/packageJson.js b/src/helpers/packageJson.js index 60d95ac2ebb58d33ca317822d6c7cd3f98d84107..2eb3c91db736c902f8bae7140bdc0304057b0279 100644 --- a/src/helpers/packageJson.js +++ b/src/helpers/packageJson.js @@ -1,38 +1,47 @@ const _ = require('lodash'); -module.exports = { - setNewValue(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 searchString = `"${oldVersion}"`; - const newString = `"${newVersion}"`; - let newFileContent = null; - // Skip ahead to depType section - let searchIndex = currentFileContent.indexOf(`"${depType}"`) + - depType.length; - // Iterate through the rest of the file - for (; searchIndex < currentFileContent.length; searchIndex += 1) { - // First check if we have a hit for the old version - if (matchAt(currentFileContent, searchIndex, searchString)) { - // Now test if the result matches - const testContent = replaceAt(currentFileContent, searchIndex, searchString, newString); - // Compare the parsed JSON structure of old and new - if (_.isEqual(parsedContents, JSON.parse(testContent))) { - newFileContent = testContent; - break; - } +let logger = null; + +module.exports = function packageJson(config) { + logger = config.logger; + this.setNewValue = setNewValue; + return this; +}; + +function setNewValue(currentFileContent, depType, depName, newVersion) { + logger.debug(`setNewValue: ${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 searchString = `"${oldVersion}"`; + const newString = `"${newVersion}"`; + let newFileContent = null; + // Skip ahead to depType section + let searchIndex = currentFileContent.indexOf(`"${depType}"`) + depType.length; + logger.debug(`Starting search at index ${searchIndex}`); + // Iterate through the rest of the file + for (; searchIndex < currentFileContent.length; searchIndex += 1) { + // First check if we have a hit for the old version + if (matchAt(currentFileContent, searchIndex, searchString)) { + logger.debug(`Found match at index ${searchIndex}`); + // Now test if the result matches + const testContent = replaceAt(currentFileContent, searchIndex, searchString, newString); + logger.debug(`testContent = ${testContent}`); + // Compare the parsed JSON structure of old and new + if (_.isEqual(parsedContents, JSON.parse(testContent))) { + newFileContent = testContent; + break; } } - if (!newFileContent) { - throw new Error('Could not find old version'); - } - return newFileContent; - }, -}; + } + if (!newFileContent) { + throw new Error('Could not find old version'); + } + return newFileContent; +} // Return true if the match string is found at index in content function matchAt(content, index, match) { @@ -41,5 +50,6 @@ function matchAt(content, index, match) { // Replace oldString with newString at location index of content function replaceAt(content, index, oldString, newString) { + logger.debug(`Replacing ${oldString} with ${newString} at index ${index}`); return content.substr(0, index) + newString + content.substr(index + oldString.length); } diff --git a/src/index.js b/src/index.js index 99ef969b43fb4e7be9496afece20f573de2a6174..520f9d5a16ae759a4ed46a048db29b609f3075fa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,11 @@ const semver = require('semver'); +const configurator = require('./helpers/configurator'); +const config = configurator.init(process.argv); const github = require('./helpers/github'); -const configurator = require('./helpers/configurator'); const npm = require('./helpers/npm'); -const packageJson = require('./helpers/packageJson'); +const packageJson = require('./helpers/packageJson')(config); -const config = configurator.init(process.argv); const logger = config.logger; // Initialize npm