Code coverage report for lib/tasks/wrap.js

Statements: 100% (25 / 25)      Branches: 100% (10 / 10)      Functions: 100% (9 / 9)      Lines: 100% (25 / 25)     

All files » lib/tasks/ » wrap.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          1         1 3     3 1     3 4 4 3 3     1         3 3           1 21   21       21 2 2       21 1 1       21 21   21      
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var Stack = require('../stack').Stack,
    fs = require('fs'),
    log = require('../log'),
    exists = require('../util').exists;
 
var read = function (files, callback) {
    var stack = new Stack(),
        str = [];
 
    if (typeof files === 'string') {
        files = [files];
    }
 
    files.forEach(function (file, id) {
        exists(file, stack.add(function (y) {
            if (y) {
                fs.readFile(file, 'utf8', stack.add(function (err, data) {
                    str[id] = data;
                }));
            } else {
                log.warn('failed to locate: ' + file);
            }
        }));
    });
 
    stack.done(function () {
        callback(str.join('\n'));
    });
 
};
 
 
exports.wrap = function (options, blob, done) {
    options = options || {}; //Not needed here??
 
    var prefix = '',
        postfix = '',
        stack = new Stack();
 
    if (options.prepend) {
        read(options.prepend, stack.add(function (str) {
            prefix = str;
        }));
    }
 
    if (options.append) {
        read(options.append, stack.add(function (str) {
            postfix = str;
        }));
    }
 
    stack.done(function () {
        var data = prefix + blob.result + postfix;
 
        done(null, new blob.constructor(data, blob));
    });
};