Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma-phantomjs-launcher / index.js
1 var fs = require('fs');
2 var path = require('path');
3
4 function serializeOption(value) {
5   if (typeof value === 'function') {
6     return value.toString();
7   }
8   return JSON.stringify(value);
9 }
10
11 var phantomJSExePath = function () {
12   // If the path we're given by phantomjs is to a .cmd, it is pointing to a global copy. 
13   // Using the cmd as the process to execute causes problems cleaning up the processes 
14   // so we walk from the cmd to the phantomjs.exe and use that instead.
15
16   var phantomSource = require('phantomjs').path;
17
18   if (path.extname(phantomSource).toLowerCase() === '.cmd') {
19     return path.join(path.dirname( phantomSource ), '//node_modules//phantomjs//lib//phantom//phantomjs.exe');
20   }
21
22   return phantomSource;
23 };
24
25 var PhantomJSBrowser = function(baseBrowserDecorator, config, args) {
26   baseBrowserDecorator(this);
27
28   var options = args && args.options || config && config.options || {};
29   var flags = args && args.flags || config && config.flags || [];
30
31   this._start = function(url) {
32     // create the js file that will open karma
33     var captureFile = this._tempDir + '/capture.js';
34     var optionsCode = Object.keys(options).map(function (key) {
35       if (key !== 'settings') { // settings cannot be overriden, it should be extended!
36         return 'page.' + key + ' = ' + serializeOption(options[key]) + ';';
37       }
38     });
39
40     if (options.settings) {
41       optionsCode = optionsCode.concat(Object.keys(options.settings).map(function (key) {
42         return 'page.settings.' + key + ' = ' + serializeOption(options.settings[key]) + ';';
43       }));
44     }
45
46     var captureCode = 'var page = require("webpage").create();\n' +
47         optionsCode.join('\n') + '\npage.open("' + url + '");\n';
48     fs.writeFileSync(captureFile, captureCode);
49
50     flags = flags.concat(captureFile);
51
52     // and start phantomjs
53     this._execCommand(this._getCommand(), flags);
54   };
55 };
56
57 PhantomJSBrowser.prototype = {
58   name: 'PhantomJS',
59
60   DEFAULT_CMD: {
61     linux: require('phantomjs').path,
62     darwin: require('phantomjs').path,
63     win32: phantomJSExePath()
64   },
65   ENV_CMD: 'PHANTOMJS_BIN'
66 };
67
68 PhantomJSBrowser.$inject = ['baseBrowserDecorator', 'config.phantomjsLauncher', 'args'];
69
70
71 // PUBLISH DI MODULE
72 module.exports = {
73   'launcher:PhantomJS': ['type', PhantomJSBrowser]
74 };