From 50e36a1f59923ee2b2bc8214ba9e5811a8680117 Mon Sep 17 00:00:00 2001
From: Sergio Zharinov <zharinov@users.noreply.github.com>
Date: Sun, 5 Jul 2020 23:22:13 +0400
Subject: [PATCH] refactor: Add 'ensureCacheDir` function (#6681)

---
 lib/manager/gomod/artifacts.ts  |  6 ++----
 lib/manager/pipenv/artifacts.ts | 13 +++++++------
 lib/util/fs/index.ts            | 12 ++++++++++++
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/lib/manager/gomod/artifacts.ts b/lib/manager/gomod/artifacts.ts
index 899627b176..2d61d59b6f 100644
--- a/lib/manager/gomod/artifacts.ts
+++ b/lib/manager/gomod/artifacts.ts
@@ -1,11 +1,10 @@
-import { ensureDir } from 'fs-extra';
 import { quote } from 'shlex';
 import { dirname, join } from 'upath';
 import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
 import { logger } from '../../logger';
 import { ExecOptions, exec } from '../../util/exec';
 import { BinarySource } from '../../util/exec/common';
-import { readLocalFile, writeLocalFile } from '../../util/fs';
+import { ensureCacheDir, readLocalFile, writeLocalFile } from '../../util/fs';
 import { getRepoStatus } from '../../util/git';
 import { find } from '../../util/host-rules';
 import { UpdateArtifact, UpdateArtifactsResult } from '../common';
@@ -36,8 +35,7 @@ export async function updateArtifacts({
 }: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
   logger.debug(`gomod.updateArtifacts(${goModFileName})`);
 
-  const goPath = process.env.GOPATH || join(config.cacheDir, './others/go');
-  await ensureDir(goPath);
+  const goPath = await ensureCacheDir('./others/go', 'GOPATH');
   logger.debug(`Using GOPATH: ${goPath}`);
 
   const sumFileName = goModFileName.replace(/\.mod$/, '.sum');
diff --git a/lib/manager/pipenv/artifacts.ts b/lib/manager/pipenv/artifacts.ts
index 89e098f0f3..70c5997f5c 100644
--- a/lib/manager/pipenv/artifacts.ts
+++ b/lib/manager/pipenv/artifacts.ts
@@ -1,8 +1,11 @@
-import { ensureDir } from 'fs-extra';
-import { join } from 'upath';
 import { logger } from '../../logger';
 import { ExecOptions, exec } from '../../util/exec';
-import { deleteLocalFile, readLocalFile, writeLocalFile } from '../../util/fs';
+import {
+  deleteLocalFile,
+  ensureCacheDir,
+  readLocalFile,
+  writeLocalFile,
+} from '../../util/fs';
 import { getRepoStatus } from '../../util/git';
 import {
   UpdateArtifact,
@@ -42,9 +45,7 @@ export async function updateArtifacts({
 }: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
   logger.debug(`pipenv.updateArtifacts(${pipfileName})`);
 
-  const cacheDir =
-    process.env.PIPENV_CACHE_DIR || join(config.cacheDir, './others/pipenv');
-  await ensureDir(cacheDir);
+  const cacheDir = await ensureCacheDir('./others/pipenv', 'PIPENV_CACHE_DIR');
   logger.debug('Using pipenv cache ' + cacheDir);
 
   const lockFileName = pipfileName + '.lock';
diff --git a/lib/util/fs/index.ts b/lib/util/fs/index.ts
index 602b810a1e..7f08141177 100644
--- a/lib/util/fs/index.ts
+++ b/lib/util/fs/index.ts
@@ -4,9 +4,11 @@ import { RenovateConfig } from '../../config/common';
 import { logger } from '../../logger';
 
 let localDir = '';
+let cacheDir = '';
 
 export function setFsConfig(config: Partial<RenovateConfig>): void {
   localDir = config.localDir;
+  cacheDir = config.cacheDir;
 }
 
 export function getSubDirectory(fileName: string): string {
@@ -63,3 +65,13 @@ export async function ensureLocalDir(dirName): Promise<void> {
   const localDirName = join(localDir, dirName);
   await fs.ensureDir(localDirName);
 }
+
+export async function ensureCacheDir(
+  dirName,
+  envPathVar?: string
+): Promise<string> {
+  const envCacheDirName = envPathVar ? process.env[envPathVar] : null;
+  const cacheDirName = envCacheDirName || join(cacheDir, dirName);
+  await fs.ensureDir(cacheDirName);
+  return cacheDirName;
+}
-- 
GitLab