diff --git a/lib/manager/npm/update.js b/lib/manager/npm/update.js index fba5a64009eb147c4b37dff2f4db652d7fbc2e83..61e6212e2e258cf1d05bdce5cfb1446549c74cfc 100644 --- a/lib/manager/npm/update.js +++ b/lib/manager/npm/update.js @@ -51,6 +51,47 @@ function setNewValue(currentFileContent, depType, depName, newVersion) { ); return currentFileContent; } + if ( + parsedContents && + parsedContents.resolutions && + parsedContents.resolutions[depName] + ) { + if (parsedContents.resolutions[depName] === oldVersion) { + // Update the file = this is what we want + parsedContents.resolutions[depName] = newVersion; + // Look for the old version number + const oldResolution = `"${oldVersion}"`; + const newResolution = `"${newVersion}"`; + // Skip ahead to depType section + searchIndex = newFileContent.indexOf(`"resolutions"`); + logger.debug(`Starting search at index ${searchIndex}`); + // Iterate through the rest of the file + for (; searchIndex < newFileContent.length; searchIndex += 1) { + // First check if we have a hit for the old version + if (matchAt(newFileContent, searchIndex, oldResolution)) { + logger.debug(`Found match at index ${searchIndex}`); + // Now test if the result matches + const testContent = replaceAt( + newFileContent, + searchIndex, + oldResolution, + newResolution + ); + // Compare the parsed JSON structure of old and new + if (_.isEqual(parsedContents, JSON.parse(testContent))) { + newFileContent = testContent; + break; + } + } + } + } else { + // istanbul ignore next + logger.warn( + { parsedContents }, + 'Upgraded dependency exists in yarn resolutions but is different version' + ); + } + } return newFileContent; } catch (err) { logger.info({ err }, 'setNewValue error'); diff --git a/test/_fixtures/package-json/inputs/01.json b/test/_fixtures/package-json/inputs/01.json index 767788618747157faa70516e9ddac523f083e29d..6d8aab96b7d85395c0125e199e85cf8c52a5d721 100644 --- a/test/_fixtures/package-json/inputs/01.json +++ b/test/_fixtures/package-json/inputs/01.json @@ -23,6 +23,9 @@ "angular-sanitize": "1.5.8", "@angular/core": "4.0.0-beta.1" }, + "resolutions": { + "config": "1.21.0" + }, "homepage": "https://keylocation.sg", "keywords": [ "Key Location", diff --git a/test/_fixtures/package-json/outputs/011.json b/test/_fixtures/package-json/outputs/011.json index fe8a992cd4b064da951f44263d59b0c2c7e2589f..e833043686619045ef52e38c5314f1d3bf15735a 100644 --- a/test/_fixtures/package-json/outputs/011.json +++ b/test/_fixtures/package-json/outputs/011.json @@ -23,6 +23,9 @@ "angular-sanitize": "1.5.8", "@angular/core": "4.0.0-beta.1" }, + "resolutions": { + "config": "1.21.0" + }, "homepage": "https://keylocation.sg", "keywords": [ "Key Location", diff --git a/test/_fixtures/package-json/outputs/012.json b/test/_fixtures/package-json/outputs/012.json index 7778ef556c87a862ea2a51dc4910792c7e94bc1a..f4454edfddb409feaf2788257776ebc7e15efc64 100644 --- a/test/_fixtures/package-json/outputs/012.json +++ b/test/_fixtures/package-json/outputs/012.json @@ -23,6 +23,9 @@ "angular-sanitize": "1.5.8", "@angular/core": "4.0.0-beta.1" }, + "resolutions": { + "config": "1.21.0" + }, "homepage": "https://keylocation.sg", "keywords": [ "Key Location", diff --git a/test/_fixtures/package-json/outputs/013.json b/test/_fixtures/package-json/outputs/013.json index c1c87b270cd14808d71c44559a69f2338b2ed4c9..d332c094ab53dac7b68fc3d6ec837e685bad2645 100644 --- a/test/_fixtures/package-json/outputs/013.json +++ b/test/_fixtures/package-json/outputs/013.json @@ -23,6 +23,9 @@ "angular-sanitize": "1.6.1", "@angular/core": "4.0.0-beta.1" }, + "resolutions": { + "config": "1.21.0" + }, "homepage": "https://keylocation.sg", "keywords": [ "Key Location", diff --git a/test/manager/npm/update.spec.js b/test/manager/npm/update.spec.js index db17856ef4dffc31540e3b7bff51b4a5f6f2106d..775248d0dcbb5e90805ff9fe5055646b8bd53fe3 100644 --- a/test/manager/npm/update.spec.js +++ b/test/manager/npm/update.spec.js @@ -23,6 +23,16 @@ describe('workers/branch/package-json', () => { ); testContent.should.equal(outputContent); }); + it('updates resolutions too', () => { + const testContent = npmUpdater.setNewValue( + input01Content, + 'dependencies', + 'config', + '1.22.0' + ); + expect(JSON.parse(testContent).dependencies.config).toEqual('1.22.0'); + expect(JSON.parse(testContent).resolutions.config).toEqual('1.22.0'); + }); it('replaces only the first instance of a value', () => { const outputContent = readFixture('outputs/012.json'); const testContent = npmUpdater.setNewValue(