-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
111 lines (88 loc) · 2.37 KB
/
utils.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
"use strict";
const IOS_PLATFORM = "iOS";
const ANDROID_PLATFORM = "Mobile";
const iOSFixedFirefoxVersion = "29.0";
const DESKTOP_NIGHTLY_KEY = "FIREFOX_NIGHTLY";
const MOBILE_NIGHTLY_KEY = "nightly_version";
const PRODUCT_ENDPOINT = "https://product-details.mozilla.org/1.0/";
const DESKTOP_ENDPOINT = `${PRODUCT_ENDPOINT}firefox_versions.json`;
const MOBILE_ENDPOINT = `${PRODUCT_ENDPOINT}mobile_versions.json`;
const WINDOWS = "Windows 10";
const iOS = "iOS 14.1";
const macOS = "Mac OS X 10.15";
const OS_REPLACE = {
All: WINDOWS,
Windows: WINDOWS,
macOS: macOS,
iOS: iOS
};
function getOS(os) {
if (!os) {
return WINDOWS;
}
if (os in OS_REPLACE) {
return OS_REPLACE[os];
}
return os;
}
function getPlatform(os) {
if (os.includes("Android")) return ANDROID_PLATFORM;
else if (os.includes("iOS")) return IOS_PLATFORM;
return "";
}
/**
* Returns a string containing version in NN.N format
*
* @param n
* @param os
* @returns {string}
*/
function getVersionNum(n, os) {
if (os.includes("iOS")) return iOSFixedFirefoxVersion;
return parseFloat(n).toFixed(1);
}
function combinePlatformVersion(platform, version) {
return `Firefox ${platform ? platform + " " : ""}${version}`;
}
async function getNightlyVersion(platform, os) {
const endpoint =
platform === "Mobile" || platform === "iOS"
? MOBILE_ENDPOINT
: DESKTOP_ENDPOINT;
return await fetch(endpoint)
.then(response => response.json())
.then(data => {
const key = !platform ? DESKTOP_NIGHTLY_KEY : MOBILE_NIGHTLY_KEY;
return combinePlatformVersion(platform, getVersionNum(data[key], os));
})
.catch(error => error);
}
/**
* If version is provided, returns platfor & version combination
* Otherwise makes a request to retrieve latest Nightly version
*
* @param version
* @param os
*/
async function getBrowser(version = "", os = "") {
const matches = version.match(/\d+/g);
const platform = getPlatform(os);
if (matches && matches.length) {
return new Promise(function(resolve) {
resolve(combinePlatformVersion(platform, getVersionNum(matches[0], os)));
});
}
return getNightlyVersion(platform, os);
}
function getSteps(comments = [], hint) {
if (comments.length && comments[0].text) {
return `${comments[0].text} \n ${hint}`;
}
return hint;
}
const utils = {
getOS,
getBrowser,
getSteps
};
module.exports = utils;