/
index.ts
65 lines (56 loc) · 1.74 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
import * as fs from 'fs-extra';
import { join, parse } from 'upath';
import { RenovateConfig } from '../../../config/common';
import { logger } from '../../../logger';
let localDir = '';
export function setFsConfig(config: Partial<RenovateConfig>): void {
localDir = config.localDir;
}
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);
}