-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
tscache.ts
42 lines (35 loc) 路 1.23 KB
/
tscache.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
import path from 'path';
import fs from 'fs';
/** Creates the folders needed given a path to a file to be saved*/
const createFileFolder = (filePath: string): void => {
const folderPath = path.dirname(filePath);
fs.mkdirSync(folderPath, { recursive: true });
};
export default class TSCache {
private _cacheFolder: string;
constructor(cacheFolder = '.rollup.cache') {
this._cacheFolder = cacheFolder;
}
/** Returns the path to the cached file */
cachedFilename(fileName: string): string {
return path.join(this._cacheFolder, fileName.replace(/^([a-zA-Z]+):/, '$1'));
}
/** Emits a file in the cache folder */
cacheCode(fileName: string, code: string): void {
const cachedPath = this.cachedFilename(fileName);
createFileFolder(cachedPath);
fs.writeFileSync(cachedPath, code);
}
/** Checks if a file is in the cache */
isCached(fileName: string): boolean {
return fs.existsSync(this.cachedFilename(fileName));
}
/** Read a file from the cache given the output name*/
getCached(fileName: string): string | undefined {
let code: string | undefined;
if (this.isCached(fileName)) {
code = fs.readFileSync(this.cachedFilename(fileName), { encoding: 'utf-8' });
}
return code;
}
}