Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add 'ensureCacheDir` function #6681

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}