-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher-generator.js
124 lines (112 loc) · 4.15 KB
/
launcher-generator.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
const fs = require('fs');
const https = require('https');
const configHelper = require('../../browserstack-helper.conf');
let browserstackCapability = '';
if (configHelper.username === undefined || configHelper.accessKey === undefined) {
let result = generateLaunchers();
writeJavaScriptFile(result);
return 0;
}
const httpsURL = 'https://'+ configHelper.username + ':' + configHelper.accessKey + '@api.browserstack.com/5/browsers?flat=true';
try {
https.get( httpsURL, (httpsResponse) => {
httpsResponse.setEncoding('utf8');
httpsResponse.on('data', (chunk) => {
browserstackCapability += chunk;
});
httpsResponse.on('end', () => {
let result = generateLaunchers();
writeJavaScriptFile(result);
});
});
} catch {
// Save as a JS file.
const JScontent = 'module.exports = ' + JSON.stringify(result);
fs.writeFile('browserstack-launchers.js', JScontent, (err) => {});
}
function writeJavaScriptFile(result) {
// Save as a JS file.
const JScontent = 'module.exports = ' + JSON.stringify(result) + ';';
fs.writeFile('browserstack-launchers.js', JScontent, (err) => {});
}
function generateLaunchers() {
let requiredLaunchers = getRequiredLaunchers();
// Add on additional launchers.
return Object.assign(requiredLaunchers, configHelper.additionalLaunchers);
}
function getRequiredLaunchers() {
let candidateContainer = {};
try {
browserstackCapability = JSON.parse(browserstackCapability);
for (let launcher of browserstackCapability) {
if ( validLauncher(launcher) ) {
const launcherIndexName = `BrowserStack${launcher.os}${launcher.os_version}${launcher.browser}`.replace(/ /g,'');
updateCandidateLauncher(launcherIndexName, launcher);
}
}
} catch {
// Do Nothing.
}
return formalizeCandidateLauncher();
function validLauncher(launcher) {
const isIncluded = configHelper.excludeList.reduce((result, eachExclude) => {
if (eachExclude.os == launcher.os && eachExclude.browser == launcher.browser) {
return false;
} else {
return result;
}
}, true);
return isIncluded
&& configHelper.osList[launcher.os]
&& configHelper.osList[launcher.os].includes(launcher.os_version)
&& configHelper.browserList.includes(launcher.browser);
}
function updateCandidateLauncher(launcherName, newLauncher) {
if (candidateContainer[launcherName] === undefined) {
candidateContainer[launcherName] = [ newLauncher ];
} else if (!newLauncher.browser_version.includes('beta')) {
selectNewestInRange(launcherName, newLauncher);
}
}
function selectNewestInRange(launcherName, comingLauncher) {
const sourceList = candidateContainer[launcherName];
if (sourceList.length < configHelper.browserVersionRange) {
sourceList.push(comingLauncher);
}
const oldestLauncher = getOldestLauncher(launcherName);
const comingVersion = parseFloat(comingLauncher.browser_version);
if (parseFloat(oldestLauncher.browser_version) < comingVersion) {
const targetIndex = sourceList.indexOf(oldestLauncher);
sourceList[targetIndex] = comingLauncher;
}
candidateContainer[launcherName] = sourceList;
}
function getOldestLauncher(launcherName) {
return candidateContainer[launcherName].reduce( (result, candidate) => {
const resultVer = parseFloat(result.browser_version);
const candidateVer = parseFloat(candidate.browser_version);
if (candidateVer < resultVer) {
return candidate;
} else {
return result;
}
}, { browser_version: Infinity });
}
function formalizeCandidateLauncher() {
let result = {};
for (let launchersName of Object.keys(candidateContainer)) {
for (let launcher of candidateContainer[launchersName]) {
const browserVersionName = parseInt(launcher.browser_version);
const uniqueName = `${launchersName}${browserVersionName}`;
result[uniqueName] = {
os: launcher.os,
os_version: launcher.os_version,
browser: launcher.browser,
browser_version: launcher.browser_version,
...configHelper.templateLauncher
};
}
}
return result;
}
}