From f8ad10e69eb3a20af0b1e8dfb4cf2db35b92929b Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 2 Jun 2020 20:09:54 +0200
Subject: [PATCH] refactor: pass config to getNodeConstraint

---
 lib/manager/npm/post-update/lerna.ts             |  2 +-
 lib/manager/npm/post-update/node-version.spec.ts | 10 +++++-----
 lib/manager/npm/post-update/node-version.ts      | 10 ++++++----
 lib/manager/npm/post-update/npm.ts               |  2 +-
 lib/manager/npm/post-update/pnpm.ts              |  2 +-
 lib/manager/npm/post-update/yarn.ts              |  2 +-
 6 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/lib/manager/npm/post-update/lerna.ts b/lib/manager/npm/post-update/lerna.ts
index d449ac198f..8136af093b 100644
--- a/lib/manager/npm/post-update/lerna.ts
+++ b/lib/manager/npm/post-update/lerna.ts
@@ -48,7 +48,7 @@ export async function generateLockFiles(
     if (global.trustLevel === 'high' && config.ignoreScripts !== false) {
       cmdOptions = cmdOptions.replace('--ignore-scripts ', '');
     }
-    const tagConstraint = await getNodeConstraint(config.packageFile);
+    const tagConstraint = await getNodeConstraint(config);
     const execOptions: ExecOptions = {
       cwd,
       extraEnv: {
diff --git a/lib/manager/npm/post-update/node-version.spec.ts b/lib/manager/npm/post-update/node-version.spec.ts
index af76ab43ec..f287dbbccc 100644
--- a/lib/manager/npm/post-update/node-version.spec.ts
+++ b/lib/manager/npm/post-update/node-version.spec.ts
@@ -10,20 +10,20 @@ describe('getNodeConstraint', () => {
     fs.readLocalFile.mockResolvedValueOnce(null);
     fs.readLocalFile.mockResolvedValueOnce(null);
     fs.readLocalFile.mockResolvedValueOnce('{"engines":{"node":"^12.16.0"}}');
-    const res = await getNodeConstraint('package.json');
+    const res = await getNodeConstraint({ packageFile: 'package.json' });
     expect(res).toEqual('^12.16.0');
   });
   it('returns .node-version value', async () => {
     fs.readLocalFile = jest.fn();
     fs.readLocalFile.mockResolvedValueOnce(null);
     fs.readLocalFile.mockResolvedValueOnce('12.16.1\n');
-    const res = await getNodeConstraint('package.json');
+    const res = await getNodeConstraint({ packageFile: 'package.json' });
     expect(res).toEqual('12.16.1');
   });
   it('returns .nvmrc value', async () => {
     fs.readLocalFile = jest.fn();
     fs.readLocalFile.mockResolvedValueOnce('12.16.2\n');
-    const res = await getNodeConstraint('package.json');
+    const res = await getNodeConstraint({ packageFile: 'package.json' });
     expect(res).toEqual('12.16.2');
   });
   it('ignores unusable ranges in dotfiles', async () => {
@@ -31,7 +31,7 @@ describe('getNodeConstraint', () => {
     fs.readLocalFile.mockResolvedValueOnce('latest');
     fs.readLocalFile.mockResolvedValueOnce('lts');
     fs.readLocalFile.mockResolvedValueOnce('{"engines":{"node":"^12.16.0"}}');
-    const res = await getNodeConstraint('package.json');
+    const res = await getNodeConstraint({ packageFile: 'package.json' });
     expect(res).toEqual('^12.16.0');
   });
   it('returns no constraint', async () => {
@@ -39,7 +39,7 @@ describe('getNodeConstraint', () => {
     fs.readLocalFile.mockResolvedValueOnce(null);
     fs.readLocalFile.mockResolvedValueOnce(null);
     fs.readLocalFile.mockResolvedValueOnce('{}');
-    const res = await getNodeConstraint('package.json');
+    const res = await getNodeConstraint({ packageFile: 'package.json' });
     expect(res).toBeNull();
   });
 });
diff --git a/lib/manager/npm/post-update/node-version.ts b/lib/manager/npm/post-update/node-version.ts
index 28936276c9..2c664e2a8e 100644
--- a/lib/manager/npm/post-update/node-version.ts
+++ b/lib/manager/npm/post-update/node-version.ts
@@ -1,6 +1,7 @@
 import { validRange } from 'semver';
 import { logger } from '../../../logger';
 import { getSiblingFileName, readLocalFile } from '../../../util/fs';
+import { PostUpdateConfig } from '../../common';
 
 async function getNodeFile(filename: string): Promise<string> | null {
   try {
@@ -34,12 +35,13 @@ async function getPackageJsonConstraint(
 }
 
 export async function getNodeConstraint(
-  filename: string
+  config: PostUpdateConfig
 ): Promise<string> | null {
+  const { packageFile } = config;
   const constraint =
-    (await getNodeFile(getSiblingFileName(filename, '.nvmrc'))) ||
-    (await getNodeFile(getSiblingFileName(filename, '.node-version'))) ||
-    (await getPackageJsonConstraint(filename));
+    (await getNodeFile(getSiblingFileName(packageFile, '.nvmrc'))) ||
+    (await getNodeFile(getSiblingFileName(packageFile, '.node-version'))) ||
+    (await getPackageJsonConstraint(packageFile));
   if (!constraint) {
     logger.debug('No node constraint found - using latest');
   }
diff --git a/lib/manager/npm/post-update/npm.ts b/lib/manager/npm/post-update/npm.ts
index 18511f8180..26c3daf094 100644
--- a/lib/manager/npm/post-update/npm.ts
+++ b/lib/manager/npm/post-update/npm.ts
@@ -37,7 +37,7 @@ export async function generateLockFile(
       logger.debug('Updating lock file only');
       cmdOptions += '--package-lock-only --no-audit';
     }
-    const tagConstraint = await getNodeConstraint(config.packageFile);
+    const tagConstraint = await getNodeConstraint(config);
     const execOptions: ExecOptions = {
       cwd,
       extraEnv: {
diff --git a/lib/manager/npm/post-update/pnpm.ts b/lib/manager/npm/post-update/pnpm.ts
index aba81ac352..bec674a9b4 100644
--- a/lib/manager/npm/post-update/pnpm.ts
+++ b/lib/manager/npm/post-update/pnpm.ts
@@ -24,7 +24,7 @@ export async function generateLockFile(
   let cmd = 'pnpm';
   try {
     const preCommands = ['npm i -g pnpm'];
-    const tagConstraint = await getNodeConstraint(config.packageFile);
+    const tagConstraint = await getNodeConstraint(config);
     const execOptions: ExecOptions = {
       cwd,
       extraEnv: {
diff --git a/lib/manager/npm/post-update/yarn.ts b/lib/manager/npm/post-update/yarn.ts
index 44f19d48b1..8833651c55 100644
--- a/lib/manager/npm/post-update/yarn.ts
+++ b/lib/manager/npm/post-update/yarn.ts
@@ -57,7 +57,7 @@ export async function generateLockFile(
     if (global.trustLevel !== 'high' || config.ignoreScripts) {
       cmdOptions += ' --ignore-scripts --ignore-engines --ignore-platform';
     }
-    const tagConstraint = await getNodeConstraint(config.packageFile);
+    const tagConstraint = await getNodeConstraint(config);
     const execOptions: ExecOptions = {
       cwd,
       extraEnv: {
-- 
GitLab