From b29ea46f8b7b94bf6421bb442d7dc934fd4c4635 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 17 Sep 2018 12:45:05 +0200
Subject: [PATCH] feat: bumpVersion mirror

---
 lib/manager/npm/update.js                     | 17 ++++++++++++++++-
 .../npm/__snapshots__/update.spec.js.snap     |  6 ++++--
 test/manager/npm/update.spec.js               | 19 ++++++++++++++++++-
 website/docs/configuration-options.md         |  2 ++
 4 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/lib/manager/npm/update.js b/lib/manager/npm/update.js
index e518fd6733..05419bb6e4 100644
--- a/lib/manager/npm/update.js
+++ b/lib/manager/npm/update.js
@@ -152,8 +152,23 @@ function bumpPackageVersion(content, currentValue, bumpVersion) {
     { bumpVersion, currentValue },
     'Checking if we should bump package.json version'
   );
+  let newPjVersion;
   try {
-    const newPjVersion = semver.inc(currentValue, bumpVersion);
+    if (bumpVersion.startsWith('mirror:')) {
+      const mirrorPackage = bumpVersion.replace('mirror:', '');
+      const parsedContent = JSON.parse(content);
+      newPjVersion =
+        (parsedContent.dependencies || {})[mirrorPackage] ||
+        (parsedContent.devDependencies || {})[mirrorPackage] ||
+        (parsedContent.optionalDependencies || {})[mirrorPackage] ||
+        (parsedContent.peerDependencies || {})[mirrorPackage];
+      if (!newPjVersion) {
+        logger.warn('bumpVersion mirror package not found: ', mirrorPackage);
+        return content;
+      }
+    } else {
+      newPjVersion = semver.inc(currentValue, bumpVersion);
+    }
     logger.debug({ newPjVersion });
     const bumpedContent = content.replace(
       /("version":\s*")[^"]*/,
diff --git a/test/manager/npm/__snapshots__/update.spec.js.snap b/test/manager/npm/__snapshots__/update.spec.js.snap
index 95785e2f7e..21bae26179 100644
--- a/test/manager/npm/__snapshots__/update.spec.js.snap
+++ b/test/manager/npm/__snapshots__/update.spec.js.snap
@@ -1,8 +1,10 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
-exports[`workers/branch/package-json .bumpPackageVersion() increments 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.0.3\\"}"`;
+exports[`workers/branch/package-json .bumpPackageVersion() increments 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.0.3\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`;
 
-exports[`workers/branch/package-json .bumpPackageVersion() updates 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.1.0\\"}"`;
+exports[`workers/branch/package-json .bumpPackageVersion() mirrors 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"2.4.2\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`;
+
+exports[`workers/branch/package-json .bumpPackageVersion() updates 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.1.0\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`;
 
 exports[`workers/branch/package-json .updateDependency(fileContent, depType, depName, newValue) replaces a github dependency value 1`] = `"{\\"dependencies\\":{\\"gulp\\":\\"gulpjs/gulp#v4.0.0\\"}}"`;
 
diff --git a/test/manager/npm/update.spec.js b/test/manager/npm/update.spec.js
index b53582f58c..b95bba759c 100644
--- a/test/manager/npm/update.spec.js
+++ b/test/manager/npm/update.spec.js
@@ -123,7 +123,24 @@ describe('workers/branch/package-json', () => {
     });
   });
   describe('.bumpPackageVersion()', () => {
-    const content = JSON.stringify({ name: 'some-package', version: '0.0.2' });
+    const content = JSON.stringify({
+      name: 'some-package',
+      version: '0.0.2',
+      dependencies: { chalk: '2.4.2' },
+    });
+    it('mirrors', () => {
+      const res = npmUpdater.bumpPackageVersion(
+        content,
+        '0.0.2',
+        'mirror:chalk'
+      );
+      expect(res).toMatchSnapshot();
+      expect(res).not.toEqual(content);
+    });
+    it('aborts mirror', () => {
+      const res = npmUpdater.bumpPackageVersion(content, '0.0.2', 'mirror:a');
+      expect(res).toEqual(content);
+    });
     it('increments', () => {
       const res = npmUpdater.bumpPackageVersion(content, '0.0.2', 'patch');
       expect(res).toMatchSnapshot();
diff --git a/website/docs/configuration-options.md b/website/docs/configuration-options.md
index 7c13e6767d..ced14bafd6 100644
--- a/website/docs/configuration-options.md
+++ b/website/docs/configuration-options.md
@@ -90,6 +90,8 @@ This field is combined with `branchPrefix` and `managerBranchPrefix` to form the
 
 Set this value to 'patch', 'minor' or 'major' to have Renovate update the version in your edited `package.json`. e.g. if you wish Renovate to always increase the target `package.json` version with a patch update, set this to `patch`.
 
+You can also set this field to `"mirror:x"` where `x` is the name of a package in the `package.json`. Doing so means that the `package.json` `version` field will mirror whatever is the version for `x` dependened on. Make sure that version is a pinned version of course, as otherwise it won't be valid.
+
 ## circleci
 
 ## commitBody
-- 
GitLab