Code coverage report for lib/tasks/fn2string.js

Statements: 83.33% (10 / 12)      Branches: 75% (3 / 4)      Functions: 66.67% (2 / 3)      Lines: 83.33% (10 / 12)     

All files » lib/tasks/ » fn2string.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            1                       1 3 3   5 3       3 3       3 3          
/*
Copyright (c) 2013, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
 
var vm = require('vm'),
    contextForRunInContext = vm.createContext({
        require: require,
        module: require('module'),
        console: {
            log: function () {}
        },
        window: {},
        document: {},
        YUI: null
    });
 
exports.fn2string = function (options, blob, done) {
    var moduleFn;
    contextForRunInContext.YUI = {
        add: function (name, fn) {
            if (name === options.name) {
                moduleFn = fn;
            }
        }
    };
    try {
        vm.runInContext(blob.result, contextForRunInContext, options.path);
    } catch (e) {
        return done(new Error('invalid yui module on ' + options.path + '.'));
    }
    Eif (moduleFn) {
        done(null, new blob.constructor(moduleFn.toString(), blob));
    } else {
        done(new Error('unable to locate module `' + options.name + '` on `' + options.path + '`.'));
    }
};