forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpHelpers.js
197 lines (175 loc) · 6.88 KB
/
gulpHelpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// this will have all of a copy of the normal fs methods as well
const fs = require('fs.extra');
const path = require('path');
const argv = require('yargs').argv;
const MANIFEST = 'package.json';
const exec = require('child_process').exec;
const through = require('through2');
const _ = require('lodash');
const gutil = require('gulp-util');
const MODULE_PATH = './modules';
const BUILD_PATH = './build/dist';
const DEV_PATH = './build/dev';
// get only subdirectories that contain package.json with 'main' property
function isModuleDirectory(filePath) {
try {
const manifestPath = path.join(filePath, MANIFEST);
if (fs.statSync(manifestPath).isFile()) {
const module = require(manifestPath);
return module && module.main;
}
}
catch (error) {}
}
module.exports = {
parseBrowserArgs: function (argv) {
return (argv.browsers) ? argv.browsers.split(',') : [];
},
toCapitalCase: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
jsonifyHTML: function (str) {
console.log(arguments);
return str.replace(/\n/g, '')
.replace(/<\//g, '<\\/')
.replace(/\/>/g, '\\/>');
},
getArgModules() {
var modules = (argv.modules || '').split(',').filter(module => !!module);
try {
if (modules.length === 1 && path.extname(modules[0]).toLowerCase() === '.json') {
var moduleFile = modules[0];
modules = JSON.parse(
fs.readFileSync(moduleFile, 'utf8')
);
}
} catch(e) {
throw new gutil.PluginError({
plugin: 'modules',
message: 'failed reading: ' + argv.modules
});
}
return modules;
},
getModules: _.memoize(function(externalModules) {
externalModules = externalModules || [];
var internalModules;
try {
var absoluteModulePath = path.join(__dirname, MODULE_PATH);
internalModules = fs.readdirSync(absoluteModulePath)
.filter(file => !(/(^|\/)\.[^\/\.]/g).test(file))
.reduce((memo, file) => {
var moduleName = file.split(new RegExp('[.\\' + path.sep + ']'))[0];
var modulePath = path.join(absoluteModulePath, file);
if (fs.lstatSync(modulePath).isDirectory()) {
modulePath = path.join(modulePath, "index.js")
}
memo[modulePath] = moduleName;
return memo;
}, {});
} catch(err) {
internalModules = {};
}
return Object.assign(externalModules.reduce((memo, module) => {
try {
var modulePath = require.resolve(module);
memo[modulePath] = module;
} catch(err) {
// do something
}
return memo;
}, internalModules));
}),
getBuiltModules: function(dev, externalModules) {
var modules = this.getModuleNames(externalModules);
if(Array.isArray(externalModules)) {
modules = _.intersection(modules, externalModules);
}
return modules.map(name => path.join(__dirname, dev ? DEV_PATH : BUILD_PATH, name + '.js'));
},
getBuiltPrebidCoreFile: function(dev) {
return path.join(__dirname, dev ? DEV_PATH : BUILD_PATH, 'prebid-core' + '.js');
},
getModulePaths: function(externalModules) {
var modules = this.getModules(externalModules);
return Object.keys(modules);
},
getModuleNames: function(externalModules) {
return _.values(this.getModules(externalModules));
},
nameModules: function(externalModules) {
var modules = this.getModules(externalModules);
return through.obj(function(file, enc, done) {
file.named = modules[file.path] ? modules[file.path] : 'prebid';
this.push(file);
done();
})
},
/*
* Get source files for analytics subdirectories in top-level `analytics`
* directory adjacent to Prebid.js.
* Invoke with gulp <task> --analytics
* Returns an array of source files for inclusion in build process
*/
getAnalyticsSources: function(directory) {
if (!argv.analytics) {return [];} // empty arrays won't affect a standard build
const directoryContents = fs.readdirSync(directory);
return directoryContents
.filter(file => isModuleDirectory(path.join(directory, file)))
.map(moduleDirectory => {
const module = require(path.join(directory, moduleDirectory, MANIFEST));
return path.join(directory, moduleDirectory, module.main);
});
},
createEnd2EndTestReport : function(targetDestinationDir) {
var browsers = require('./browsers.json');
var env = [];
var input = 'bs';
for(var key in browsers) {
if(key.substring(0, input.length) === input && browsers[key].browser !== 'iphone') {
env.push(key);
}
}
//create new directory structure
fs.rmrfSync(targetDestinationDir);
env.forEach(item => {
fs.mkdirpSync(targetDestinationDir + '/' + item);
});
//move xml files to newly created directory
var walker = fs.walk('./build/coverage/e2e/reports');
walker.on("file", function (root, stat, next) {
env.forEach(item => {
if(stat.name.search(item) !== -1) {
var src = root + '/' + stat.name;
var dest = targetDestinationDir + '/' + item + '/' + stat.name;
fs.copy(src, dest, {replace: true}, function(err) {
if(err) {
throw err;
}
});
}
});
next();
});
//run junit-viewer to read xml and create html
env.forEach(item => {
//junit-viewer --results="./custom-reports/chrome51" --save="./chrome.html"
var cmd = 'junit-viewer --results="' + targetDestinationDir + '/' + item + '" --save="' + targetDestinationDir + '/' + item +'.html"';
exec(cmd);
});
//create e2e-results.html
var html = '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>End to End Testing Result</title><link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"><script src="https://code.jquery.com/jquery-1.12.4.js"></script><script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script><script>$( function() {$( "#tabs" ).tabs({heightStyle: "fill"});});</script></head><body><div style="font-weight: bold;">Note: Refresh in 2-3 seconds if it says "Cannot get ....."</div><div id="tabs" style="height:2000px;">';
var li = '';
var tabs = '';
env.forEach(function(item,i) {
i++;
li = li + '<li><a href="#tabs-'+i+'">'+item+'</a></li>';
tabs = tabs + '<div id="tabs-'+i+'"><iframe name="'+item+'" src="/' + targetDestinationDir.slice(2) + '/'+item+'.html" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;top:50px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe></div>';
});
html = html + '<ul>' + li + '</ul>' + tabs;
html = html + '</div></body></html>';
var filepath = targetDestinationDir + '/results.html';
fs.openSync(filepath, 'w+');
fs.writeFileSync(filepath, html);
}
};