-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·220 lines (189 loc) · 6.31 KB
/
index.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env node
const fs = require('mz/fs');
const path = require('path');
const simpleGit = require('simple-git/promise');
const gitPath = path.join(__dirname, 'browser-compat-data/');
const util = require('util');
const glob = util.promisify(require('glob'));
const remote = 'https://github.com/SamsungInternet/browser-compat-data';
const Replacer = require('pattern-replace');
const samsungReplacer = new Replacer({
patterns: [{
match: /(chrome|version) (\d\d)/ig,
replacement: function (needle) {
const chromeVersion = Number(needle.slice(-2));
return 'Samsung Internet ' + getSamsungVersion(chromeVersion);
}
}]
})
const mappings = [
["1.0", 18],
["1.5", 28],
["2.0", 34],
["3.0", 38],
["4.0", 44],
["5.0", 51],
["6.0", 56],
["7.0", 59],
["8.0", 63],
["9.0", 67],
["10.0", 71],
];
function getSamsungVersion(chromeVersion) {
if (chromeVersion === null) return null;
if (chromeVersion === false) return false;
if (chromeVersion === true) return true;
if (chromeVersion < mappings[0][1]) return true;
let version = false;
for (const [samsung, chrome] of mappings) {
if (chromeVersion <= chrome) {
version = samsung;
break;
}
}
return version;
}
// Recurse through object finding __compat and running fn on it.
function compatWalker(inObject, parentName, fn) {
for (const [name, o] of Object.entries(inObject)) {
if (name === '__compat') {
fn(parentName, o);
} else {
if (typeof o === 'object') compatWalker (o, name, fn);
}
}
}
function getSamsungDataFromChromeData(propName, chromeData, samsungDataIn) {
const samsungData = samsungDataIn || {};
// We don't use flags.
if (chromeData['flags']) {
return {
version_added: false
}
}
// For eacho of the properties defined in the chrome data
for (const prop of Object.keys(chromeData).sort()) {
// Ignore notes they contain browser specific
// details we cannout garuntee.
if (prop === 'notes' && typeof chromeData[prop] === 'string' && chromeData[prop].match(/windows|linux|macos/ig)) continue;
// if that property is not defined in the existing Samsung data
// or if the data is falsy or true, it maybe updated to an actual version
if (!samsungData[prop] || samsungData[prop] === true || (prop.match(/^version/i) && chromeData[prop] <= 4)) {
console.log(`${propName} ${prop} in Chrome, ${chromeData.version_added} which is Samsung ${getSamsungVersion(chromeData.version_added)}`);
// Convert version numbers to the equivalent Samsung version
let value = chromeData[prop];
if (prop === 'notes' && typeof chromeData[prop] === 'string') {
value = samsungReplacer.replace(value) || value;
value = value.replace(/chrome/ig, "Samsung Internet");
} else if (prop === 'notes' && Array.isArray(value)) {
value = value.map(value => {
value = samsungReplacer.replace(value) || value;
value = value.replace(/chrome/ig, "Samsung Internet");
return value;
});
}
if (
prop === 'version_added' ||
prop === 'version_removed'
) {
value = getSamsungVersion(value);
}
// Update the samsung version with that value
samsungData[prop] = value;
}
}
// If a feature is added and removed in the same version then it was never added
if (samsungData.version_added && samsungData.version_added === samsungData.version_removed) {
delete samsungData.version_removed;
samsungData.version_added = false;
}
// if (data.version_added == false) {
// return {
// version_added: false
// }
// }
return samsungData;
}
(async function main() {
// Clone if it is not already downloaded
if (!await fs.exists(gitPath)) {
await fs.mkdir(gitPath);
const git = simpleGit(gitPath);
console.log('updating browser-compat-data');
await git
.silent(true)
.clone(remote, gitPath, {});
}
const args = process.argv.slice(2);
const files = args.length ? await glob(path.resolve(args[0])) : await glob(path.join(gitPath, '**/*.json'));
for (const filepath of files) {
let dirty = false;
console.log(filepath);
let file;
try {
file = JSON.parse(
await fs.readFile(filepath, 'utf8')
);
} catch (e) {
console.log('WARNING! Invalid JSON.');
continue;
}
for (const [type, api] of Object.entries(file)) {
// Ignore browsers doesn't contain compat info
if (type === "browsers") continue;
// Find __compat objects
compatWalker(api, type, function (parentName, compat) {
const support = compat.support;
console.log(`Found ${parentName}`)
if (!support) {
console.log('WARNING! Compat does not have support');
return;
}
const chromeData = (function () {
if (Array.isArray(support.chrome_android)) {
const tempArray = support.chrome_android
.filter(i => !i.flags)
.filter(i => !(i.version_removed && getSamsungVersion(i.version_added) === getSamsungVersion(i.version_removed)));
if (tempArray.length === 1) return tempArray[0];
return tempArray;
}
return support.chrome_android;
}());
if (!chromeData) {
console.log('Chrome Android Info is not defined cannot infer data');
} else if (Array.isArray(chromeData)) {
// Handle the case where it is an Array, t`his will overwrite if the number of entries is different
console.log(`${parentName} added in Chrome, data is Array so may be overwriting`);
if (
support.samsunginternet_android &&
Array.isArray(support.samsunginternet_android) &&
support.samsunginternet_android.length === chromeData.length
) {
support.samsunginternet_android = chromeData.map((data, i) => getSamsungDataFromChromeData(parentName, data, support.samsunginternet_android[i]));
dirty = true;
} else {
dirty = true;
support.samsunginternet_android = chromeData.map(data => getSamsungDataFromChromeData(parentName, data));
}
} else {
dirty = true;
support.samsunginternet_android = getSamsungDataFromChromeData(
parentName,
chromeData,
support.samsunginternet_android
)
}
// Sort support into alphabetical order
compat.support = {};
Object.keys(support)
.sort()
.forEach(key => {
compat.support[key] = support[key]
});
});
}
// console.log(`Writing out ${filepath}`)
// Write it back out, 2 spaces seperation with newline at end.
if (dirty) await fs.writeFile(filepath, JSON.stringify(file, null, 2) + '\n');
}
}());