Refactor packageJson logging
This commit is contained in:
Родитель
9e4aac1d14
Коммит
a921ff265e
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
const semver = require('semver');
|
||||
|
||||
const github = require('./helpers/github');
|
||||
const configurator = require('./helpers/configurator');
|
||||
const npm = require('./helpers/npm');
|
||||
const packageJson = require('./helpers/packageJson');
|
||||
|
||||
const config = configurator.init(process.argv);
|
||||
const github = require('./helpers/github');
|
||||
const npm = require('./helpers/npm');
|
||||
const packageJson = require('./helpers/packageJson')(config);
|
||||
|
||||
const logger = config.logger;
|
||||
|
||||
// Initialize npm
|
||||
|
|
Загрузка…
Ссылка в новой задаче