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

Statements: 95.45% (63 / 66)      Branches: 83.33% (5 / 6)      Functions: 91.3% (21 / 23)      Lines: 100% (62 / 62)      Ignored: none     

All files » istanbul/lib/util/ » file-writer.js
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154          89                 89 445 1068       89 1181 1181   89   89   5744     1181       89 2055 2055   89   89   33180       89 49   89   89   1181 1181 1181 1181     49       89 20 20     89   89   2055 2055     2055           2055 2055 2055 2055 2055 2055   2055 2055 2055 2055     20 20     2075 2075 20                                                       89 69 69 69 69 69     89   89   50 50     3236     69       89
/*
 Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */
 
var path = require('path'),
    util = require('util'),
    fs = require('fs'),
    async = require('async'),
    mkdirp = require('mkdirp'),
    writer = require('./writer'),
    Writer = writer.Writer,
    ContentWriter = writer.ContentWriter;
 
function extend(cons, proto) {
    Object.keys(proto).forEach(function (k) {
        cons.prototype[k] = proto[k];
    });
}
 
function BufferedContentWriter() {
    ContentWriter.call(this);
    this.content = '';
}
util.inherits(BufferedContentWriter, ContentWriter);
 
extend(BufferedContentWriter, {
    write: function (str) {
        this.content += str;
    },
    getContent: function () {
        return this.content;
    }
});
 
function StreamContentWriter(stream) {
    ContentWriter.call(this);
    this.stream = stream;
}
util.inherits(StreamContentWriter, ContentWriter);
 
extend(StreamContentWriter, {
    write: function (str) {
        this.stream.write(str);
    }
});
 
function SyncFileWriter() {
    Writer.call(this);
}
util.inherits(SyncFileWriter, Writer);
 
extend(SyncFileWriter, {
    writeFile: function (file, callback) {
        mkdirp.sync(path.dirname(file));
        var cw = new BufferedContentWriter();
        callback(cw);
        fs.writeFileSync(file, cw.getContent(), 'utf8');
    },
    done: function () {
        this.emit('done'); //everything already done
    }
});
 
function AsyncFileWriter() {
    this.queue = async.queue(this.processFile.bind(this), 20);
    this.openFileMap = {};
}
 
util.inherits(AsyncFileWriter, Writer);
 
extend(AsyncFileWriter, {
    writeFile: function (file, callback) {
        this.openFileMap[file] = true;
        this.queue.push({ file: file, callback: callback });
    },
    processFile: function (task, cb) {
        var file = task.file,
            userCallback = task.callback,
            that = this,
            stream,
            contentWriter;
 
        mkdirp.sync(path.dirname(file));
        stream = fs.createWriteStream(file);
        stream.on('close', function () {
            delete that.openFileMap[file];
            cb();
            that.checkDone();
        });
        stream.on('error', function (err) { that.emit('error', err); });
        contentWriter = new StreamContentWriter(stream);
        userCallback(contentWriter);
        stream.end();
    },
    done: function () {
        this.doneCalled = true;
        this.checkDone();
    },
    checkDone: function () {
        Iif (!this.doneCalled) { return; }
        if (Object.keys(this.openFileMap).length === 0) {
            this.emit('done');
        }
    }
});
/**
 * a concrete writer implementation that can write files synchronously or
 * asynchronously based on the constructor argument passed to it.
 *
 * Usage
 * -----
 *
 *      var sync = true,
 *          fileWriter = new require('istanbul').FileWriter(sync);
 *
 *      fileWriter.on('done', function () { console.log('done'); });
 *      fileWriter.copyFile('/foo/bar.jpg', '/baz/bar.jpg');
 *      fileWriter.writeFile('/foo/index.html', function (contentWriter) {
 *          contentWriter.println('<html>');
 *          contentWriter.println('</html>');
 *      });
 *      fileWriter.done(); // will emit the `done` event when all files are written
 *
 * @class FileWriter
 * @extends Writer
 * @module io
 * @param sync
 * @constructor
 */
function FileWriter(sync) {
    Writer.call(this);
    var that = this;
    this.delegate = sync ? new SyncFileWriter() : new AsyncFileWriter();
    this.delegate.on('error', function (err) { that.emit('error', err); });
    this.delegate.on('done', function () { that.emit('done'); });
}
 
util.inherits(FileWriter, Writer);
 
extend(FileWriter, {
    copyFile: function (source, dest) {
        mkdirp.sync(path.dirname(dest));
        fs.writeFileSync(dest, fs.readFileSync(source));
    },
    writeFile: function (file, callback) {
        this.delegate.writeFile(file, callback);
    },
    done: function () {
        this.delegate.done();
    }
});
 
module.exports = FileWriter;