Skip to content

Commit

Permalink
feat: update yarn resolutions
Browse files Browse the repository at this point in the history
Detect if the upgraded dependency was already in “resolutions” and update it too if it was an exact match. Warn if it was not.

Closes #1318
  • Loading branch information
rarkins committed Dec 25, 2017
1 parent 3c4f46b commit 163ce43
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/manager/npm/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions test/_fixtures/package-json/inputs/01.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/_fixtures/package-json/outputs/011.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/_fixtures/package-json/outputs/012.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/_fixtures/package-json/outputs/013.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions test/manager/npm/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 163ce43

Please sign in to comment.