summaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/istanbul/lib/util/yui-load-hook.js
blob: 9b1365d7a942e8ca38240ccef90994d7097c69fa (plain)
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
/*
 Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */

//EXPERIMENTAL code: do not rely on this in anyway until the docs say it is allowed

var path = require('path'),
    yuiRegexp = /yui-nodejs\.js$/;

module.exports = function (matchFn, transformFn, verbose) {
    return function (file) {
        if (!file.match(yuiRegexp)) {
            return;
        }
        var YMain = require(file),
            YUI,
            loaderFn,
            origGet;

        if (YMain.YUI) {
            YUI = YMain.YUI;
            loaderFn = YUI.Env && YUI.Env.mods && YUI.Env.mods['loader-base'] ? YUI.Env.mods['loader-base'].fn : null;
            if (!loaderFn) { return; }
            if (verbose) { console.log('Applying YUI load post-hook'); }
            YUI.Env.mods['loader-base'].fn = function (Y) {
                loaderFn.call(null, Y);
                origGet = Y.Get._exec;
                Y.Get._exec = function (data, url, cb) {
                    if (matchFn(url) || matchFn(path.resolve(url))) { //allow for relative paths as well
                        if (verbose) {
                            console.log('Transforming [' + url + ']');
                        }
                        try {
                            data = transformFn(data, url);
                        } catch (ex) {
                            console.error('Error transforming: ' + url + ' return original code');
                            console.error(ex.message || ex);
                            if (ex.stack) { console.error(ex.stack); }
                        }
                    }
                    return origGet.call(Y, data, url, cb);
                };
                return Y;
            };
        }
    };
};