/
index.ts
79 lines (68 loc) · 2.09 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as fs from 'fs-extra';
import { join, parse } from 'upath';
import { RenovateConfig } from '../../config/common';
import { logger } from '../../logger';
export * from './proxies';
let localDir = '';
let cacheDir = '';
export function setFsConfig(config: Partial<RenovateConfig>): void {
localDir = config.localDir;
cacheDir = config.cacheDir;
}
export function getSubDirectory(fileName: string): string {
return parse(fileName).dir;
}
export function getSiblingFileName(
existingFileNameWithPath: string,
otherFileName: string
): string {
const subDirectory = getSubDirectory(existingFileNameWithPath);
return join(subDirectory, otherFileName);
}
export async function readLocalFile(fileName: string): Promise<Buffer>;
export async function readLocalFile(
fileName: string,
encoding: 'utf8'
): Promise<string>;
export async function readLocalFile(
fileName: string,
encoding?: string
): Promise<string | Buffer> {
const localFileName = join(localDir, fileName);
try {
const fileContent = await fs.readFile(localFileName, encoding);
return fileContent;
} catch (err) {
logger.trace({ err }, 'Error reading local file');
return null;
}
}
export async function writeLocalFile(
fileName: string,
fileContent: string
): Promise<void> {
const localFileName = join(localDir, fileName);
await fs.outputFile(localFileName, fileContent);
}
export async function deleteLocalFile(fileName: string): Promise<void> {
const localFileName = join(localDir, fileName);
await fs.remove(localFileName);
}
// istanbul ignore next
export async function ensureDir(dirName): Promise<void> {
await fs.ensureDir(dirName);
}
// istanbul ignore next
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;
}