2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6 var util = require('util'),
7 path = require('path'),
10 mkdirp = require('mkdirp'),
11 Store = require('./index');
13 function makeTempDir() {
14 var dir = path.join(os.tmpDir ? os.tmpDir() : /* istanbul ignore next */ (process.env.TMPDIR || '/tmp'), 'ts' + new Date().getTime());
19 * a `Store` implementation using temporary files.
24 * var store = require('istanbul').Store.create('tmp');
30 * @param {Object} opts Optional.
31 * @param {String} [opts.tmp] a pre-existing directory to use as the `tmp` directory. When not specified, a random directory
32 * is created under `os.tmpDir()`
35 function TmpStore(opts) {
37 this.tmp = opts.tmp || makeTempDir();
40 this.prefix = 't' + new Date().getTime() + '-';
43 TmpStore.TYPE = 'tmp';
44 util.inherits(TmpStore, Store);
47 generateTmpFileName: function () {
49 return this.prefix + this.seq + '.tmp';
52 set: function (key, contents) {
53 var tmpFile = this.generateTmpFileName();
54 fs.writeFileSync(tmpFile, contents, 'utf8');
55 this.map[key] = tmpFile;
59 var tmpFile = this.map[key];
60 if (!tmpFile) { throw new Error('Unable to find tmp entry for [' + tmpFile + ']'); }
61 return fs.readFileSync(tmpFile, 'utf8');
64 hasKey: function (key) {
65 return !!this.map[key];
69 return Object.keys(this.map);
72 dispose: function () {
74 Object.keys(map).forEach(function (key) {
75 fs.unlinkSync(map[key]);
81 module.exports = TmpStore;