From d35347da449a35bc68f4ce84c33809ffd284bd89 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Fri, 16 Jun 2017 12:14:38 +0200
Subject: [PATCH] Use .npmrc from repository for registry lookups (#309)

* Add ini

* Use repo npmrc

* Fix json

* Fix npmrc nesting

Closes #291
---
 lib/api/npm.js | 10 ++++++++--
 lib/index.js   | 19 +++++++++++++++++++
 package.json   |  1 +
 yarn.lock      |  2 +-
 4 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/lib/api/npm.js b/lib/api/npm.js
index 9e2e8e4d23..0f3d3c2318 100644
--- a/lib/api/npm.js
+++ b/lib/api/npm.js
@@ -7,25 +7,31 @@ const registryAuthToken = require('registry-auth-token');
 const logger = require('winston');
 
 module.exports = {
+  setNpmrc,
   getDependency,
   resetCache,
 };
 
 let npmCache = {};
+let npmrc = null;
 
 function resetCache() {
   npmCache = {};
 }
 
+async function setNpmrc(input) {
+  npmrc = input;
+}
+
 async function getDependency(name) {
   logger.debug(`getDependency(${name})`);
   const scope = name.split('/')[0];
-  const regUrl = registryUrl(scope);
+  const regUrl = registryUrl(scope, { npmrc });
   const pkgUrl = url.resolve(
     regUrl,
     encodeURIComponent(name).replace(/^%40/, '@')
   );
-  const authInfo = registryAuthToken(regUrl);
+  const authInfo = registryAuthToken(regUrl, { npmrc });
   const headers = {};
 
   // Reduce metadata https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md
diff --git a/lib/index.js b/lib/index.js
index 612d478fbb..5ff6f4899a 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -3,7 +3,9 @@ const logger = require('./logger');
 const configParser = require('./config');
 const githubApi = require('./api/github');
 const gitlabApi = require('./api/gitlab');
+const npmApi = require('./api/npm');
 const defaultsParser = require('./config/defaults');
+const ini = require('ini');
 
 // Require main source
 const worker = require('./worker');
@@ -11,6 +13,7 @@ const worker = require('./worker');
 module.exports = {
   start,
   processRepo,
+  setNpmrc,
 };
 
 // This will be github or others
@@ -53,6 +56,7 @@ async function processRepo(repo) {
     if (isConfigured === false) {
       return;
     }
+    await setNpmrc();
     await findPackageFiles(config);
     const upgrades = await getAllRepoUpgrades(config);
     await worker.processUpgrades(upgrades);
@@ -61,6 +65,21 @@ async function processRepo(repo) {
   }
 }
 
+// Check for config in `renovate.json`
+async function setNpmrc() {
+  try {
+    let npmrc = null;
+    const npmrcContent = await api.getFileContent('.npmrc');
+    if (npmrcContent) {
+      logger.debug('Found .npmrc file in repository');
+      npmrc = ini.parse(npmrcContent);
+    }
+    npmApi.setNpmrc(npmrc);
+  } catch (err) {
+    logger.error('Failed to set .npmrc');
+  }
+}
+
 // Check for config in `renovate.json`
 async function mergeRenovateJson(config) {
   const renovateJson = await api.getFileJson('renovate.json');
diff --git a/package.json b/package.json
index b5652ab43a..d83f9cbd39 100644
--- a/package.json
+++ b/package.json
@@ -48,6 +48,7 @@
     "gl-got": "6.0.2",
     "got": "7.0.0",
     "handlebars": "4.0.10",
+    "ini": "1.3.4",
     "json-stringify-pretty-compact": "1.0.4",
     "jsonwebtoken": "7.4.1",
     "lodash": "4.17.4",
diff --git a/yarn.lock b/yarn.lock
index 3e7e1aef8f..4bfa0e38b8 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1586,7 +1586,7 @@ inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1:
   version "2.0.3"
   resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
 
-ini@~1.3.0:
+ini@1.3.4, ini@~1.3.0:
   version "1.3.4"
   resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"
 
-- 
GitLab