Skip to content

Commit

Permalink
refactor: Add 'ensureCacheDir` function (#6681)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 5, 2020
1 parent 66d0e03 commit 50e36a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
6 changes: 2 additions & 4 deletions 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';
Expand Down Expand Up @@ -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');
Expand Down
13 changes: 7 additions & 6 deletions 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,
Expand Down Expand Up @@ -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';
Expand Down
12 changes: 12 additions & 0 deletions lib/util/fs/index.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

0 comments on commit 50e36a1

Please sign in to comment.