From 3815d48c359f4652076b5e874ee9183ab9b9b1e6 Mon Sep 17 00:00:00 2001 From: Sergio Zharinov Date: Sun, 5 Jul 2020 16:21:29 +0400 Subject: [PATCH 1/2] refactor: Add 'ensureCacheDir` function --- lib/manager/gomod/artifacts.ts | 6 ++---- lib/manager/pipenv/artifacts.ts | 13 +++++++------ lib/util/fs/index.ts | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/manager/gomod/artifacts.ts b/lib/manager/gomod/artifacts.ts index 899627b176f07f..2d61d59b6fa07d 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 { 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 89e098f0f311e2..70c5997f5c5be6 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 { 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 602b810a1e5e75..7e8a8c66c4b97a 100644 --- a/lib/util/fs/index.ts +++ b/lib/util/fs/index.ts @@ -2,11 +2,14 @@ import * as fs from 'fs-extra'; import { join, parse } from 'upath'; import { RenovateConfig } from '../../config/common'; import { logger } from '../../logger'; +import { getChildProcessEnv } from '../exec/env'; let localDir = ''; +let cacheDir = ''; export function setFsConfig(config: Partial): void { localDir = config.localDir; + cacheDir = config.cacheDir; } export function getSubDirectory(fileName: string): string { @@ -63,3 +66,17 @@ export async function ensureLocalDir(dirName): Promise { const localDirName = join(localDir, dirName); await fs.ensureDir(localDirName); } + +export async function ensureCacheDir( + dirName, + envPathVar?: string +): Promise { + let envCacheDirName; + if (envPathVar) { + const env = getChildProcessEnv([envPathVar]); + envCacheDirName = env[envPathVar]; + } + const cacheDirName = envCacheDirName || join(cacheDir, dirName); + await fs.ensureDir(cacheDirName); + return cacheDirName; +} From 4fb06517744857aac70aaf412b132d201174acf4 Mon Sep 17 00:00:00 2001 From: Sergio Zharinov Date: Sun, 5 Jul 2020 18:21:17 +0400 Subject: [PATCH 2/2] Remove non-obvious 'getChildProcessEnv' call --- lib/util/fs/index.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/util/fs/index.ts b/lib/util/fs/index.ts index 7e8a8c66c4b97a..7f08141177e57a 100644 --- a/lib/util/fs/index.ts +++ b/lib/util/fs/index.ts @@ -2,7 +2,6 @@ import * as fs from 'fs-extra'; import { join, parse } from 'upath'; import { RenovateConfig } from '../../config/common'; import { logger } from '../../logger'; -import { getChildProcessEnv } from '../exec/env'; let localDir = ''; let cacheDir = ''; @@ -71,11 +70,7 @@ export async function ensureCacheDir( dirName, envPathVar?: string ): Promise { - let envCacheDirName; - if (envPathVar) { - const env = getChildProcessEnv([envPathVar]); - envCacheDirName = env[envPathVar]; - } + const envCacheDirName = envPathVar ? process.env[envPathVar] : null; const cacheDirName = envCacheDirName || join(cacheDir, dirName); await fs.ensureDir(cacheDirName); return cacheDirName;