Code coverage report for istanbul/lib/util/file-writer.js

Statements: 100% (30 / 30)      Branches: 100% (10 / 10)      Functions: 100% (11 / 11)      Lines: 100% (28 / 28)     

All files » istanbul/lib/util/ » file-writer.js
1 /*
2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4 */
5
6 1 var fs = require('fs');
7
8 1 function FileWriter(sync) {
9 16 this.writing = false;
10 16 this.start = sync ? this.startSync : this.startAsync;
11 16 this.write = sync ? this.writeSync : this.writeAsync;
12 16 this.end = sync ? this.endSync : this.endAsync;
13 }
14
15 1 FileWriter.prototype = {
16 writeFile: function (file, callback) {
17 34 this.start(file);
18 34 callback(this);
19 34 this.end();
20 },
21 println: function (str) {
22 174 this.write(str);
23 174 this.write('\n');
24 },
25 startSync: function (fileName) {
26 40 this.doStart();
27 39 this.contents = '';
28 39 this.filename = fileName;
29 },
30 writeSync: function (str) {
31 676 this.contents += str;
32 },
33 endSync: function () {
34 39 this.doEnd();
35 38 fs.writeFileSync(this.filename, this.contents, 'utf8');
36 },
37 startAsync: function (fileName) {
38 4 this.doStart();
39 3 this.stream = fs.createWriteStream(fileName);
40 },
41 writeAsync: function (str) {
42 12 this.stream.write(str);
43 },
44 endAsync: function () {
45 3 this.doEnd();
46 2 this.stream.end();
47 },
48 doStart: function () {
49 44 if (this.writing) { throw new Error('Attempt to start a new file before ending the previous one'); }
50 42 this.writing = true;
51 },
52 doEnd : function () {
53 42 if (!this.writing) { throw new Error('Attempt to end a file without starting it'); }
54 40 this.writing = false;
55 }
56 };
57
58 1 module.exports = FileWriter;