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

Refactor packageJson logging

parent 9e4aac1d
No related branches found
No related tags found
No related merge requests found
const _ = require('lodash'); const _ = require('lodash');
module.exports = { let logger = null;
setNewValue(currentFileContent, depType, depName, newVersion) {
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); const parsedContents = JSON.parse(currentFileContent);
// Save the old version // Save the old version
const oldVersion = parsedContents[depType][depName]; const oldVersion = parsedContents[depType][depName];
...@@ -12,14 +20,16 @@ module.exports = { ...@@ -12,14 +20,16 @@ module.exports = {
const newString = `"${newVersion}"`; const newString = `"${newVersion}"`;
let newFileContent = null; let newFileContent = null;
// Skip ahead to depType section // Skip ahead to depType section
let searchIndex = currentFileContent.indexOf(`"${depType}"`) + let searchIndex = currentFileContent.indexOf(`"${depType}"`) + depType.length;
depType.length; logger.debug(`Starting search at index ${searchIndex}`);
// Iterate through the rest of the file // Iterate through the rest of the file
for (; searchIndex < currentFileContent.length; searchIndex += 1) { for (; searchIndex < currentFileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version // First check if we have a hit for the old version
if (matchAt(currentFileContent, searchIndex, searchString)) { if (matchAt(currentFileContent, searchIndex, searchString)) {
logger.debug(`Found match at index ${searchIndex}`);
// Now test if the result matches // Now test if the result matches
const testContent = replaceAt(currentFileContent, searchIndex, searchString, newString); const testContent = replaceAt(currentFileContent, searchIndex, searchString, newString);
logger.debug(`testContent = ${testContent}`);
// Compare the parsed JSON structure of old and new // Compare the parsed JSON structure of old and new
if (_.isEqual(parsedContents, JSON.parse(testContent))) { if (_.isEqual(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent; newFileContent = testContent;
...@@ -31,8 +41,7 @@ module.exports = { ...@@ -31,8 +41,7 @@ module.exports = {
throw new Error('Could not find old version'); throw new Error('Could not find old version');
} }
return newFileContent; return newFileContent;
}, }
};
// Return true if the match string is found at index in content // Return true if the match string is found at index in content
function matchAt(content, index, match) { function matchAt(content, index, match) {
...@@ -41,5 +50,6 @@ function matchAt(content, index, match) { ...@@ -41,5 +50,6 @@ function matchAt(content, index, match) {
// Replace oldString with newString at location index of content // Replace oldString with newString at location index of content
function replaceAt(content, index, oldString, newString) { 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); return content.substr(0, index) + newString + content.substr(index + oldString.length);
} }
const semver = require('semver'); const semver = require('semver');
const configurator = require('./helpers/configurator');
const config = configurator.init(process.argv);
const github = require('./helpers/github'); const github = require('./helpers/github');
const configurator = require('./helpers/configurator');
const npm = require('./helpers/npm'); 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; const logger = config.logger;
// Initialize npm // Initialize npm
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment