Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / istanbul / lib / util / yui-load-hook.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 //EXPERIMENTAL code: do not rely on this in anyway until the docs say it is allowed
7
8 var path = require('path'),
9     yuiRegexp = /yui-nodejs\.js$/;
10
11 module.exports = function (matchFn, transformFn, verbose) {
12     return function (file) {
13         if (!file.match(yuiRegexp)) {
14             return;
15         }
16         var YMain = require(file),
17             YUI,
18             loaderFn,
19             origGet;
20
21         if (YMain.YUI) {
22             YUI = YMain.YUI;
23             loaderFn = YUI.Env && YUI.Env.mods && YUI.Env.mods['loader-base'] ? YUI.Env.mods['loader-base'].fn : null;
24             if (!loaderFn) { return; }
25             if (verbose) { console.log('Applying YUI load post-hook'); }
26             YUI.Env.mods['loader-base'].fn = function (Y) {
27                 loaderFn.call(null, Y);
28                 origGet = Y.Get._exec;
29                 Y.Get._exec = function (data, url, cb) {
30                     if (matchFn(url) || matchFn(path.resolve(url))) { //allow for relative paths as well
31                         if (verbose) {
32                             console.log('Transforming [' + url + ']');
33                         }
34                         try {
35                             data = transformFn(data, url);
36                         } catch (ex) {
37                             console.error('Error transforming: ' + url + ' return original code');
38                             console.error(ex.message || ex);
39                             if (ex.stack) { console.error(ex.stack); }
40                         }
41                     }
42                     return origGet.call(Y, data, url, cb);
43                 };
44                 return Y;
45             };
46         }
47     };
48 };
49