a1a1e9cb51b5b856ae35d531fd6e1962db12ca2e.svn-base
1.77 KB
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
80
/*
Copyright 2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const mkdirp = require('make-dir');
const rimraf = require('rimraf');
/* This exists for compatibility only to avoid changing the
* prototype chain. */
class SourceStore {}
class MemoryStore extends SourceStore {
constructor() {
super();
this.data = {};
}
registerSource(filePath, sourceText) {
this.data[filePath] = sourceText;
}
getSource(filePath) {
return this.data[filePath] || null;
}
dispose() {}
}
class FileStore extends SourceStore {
constructor(opts = {}) {
super();
const tmpDir = opts.tmpdir || os.tmpdir();
this.counter = 0;
this.mappings = [];
this.basePath = path.resolve(tmpDir, '.istanbul', 'cache_');
mkdirp.sync(path.dirname(this.basePath));
}
registerSource(filePath, sourceText) {
if (this.mappings[filePath]) {
return;
}
this.counter += 1;
const mapFile = this.basePath + this.counter;
this.mappings[filePath] = mapFile;
fs.writeFileSync(mapFile, sourceText, 'utf8');
}
getSource(filePath) {
const mapFile = this.mappings[filePath];
if (!mapFile) {
return null;
}
return fs.readFileSync(mapFile, 'utf8');
}
dispose() {
this.mappings = [];
rimraf.sync(path.dirname(this.basePath));
}
}
module.exports = {
create(type = 'memory', opts = {}) {
if (type.toLowerCase() === 'file') {
return new FileStore(opts);
}
return new MemoryStore(opts);
}
};