This repository has been archived by the owner on May 11, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
build-data.js
287 lines (249 loc) · 7.57 KB
/
build-data.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"use strict";
const fs = require("fs");
const path = require("path");
const flattenDeep = require("lodash/flattenDeep");
const isEqual = require("lodash/isEqual");
const mapValues = require("lodash/mapValues");
const pickBy = require("lodash/pickBy");
const electronToChromiumVersions = require("electron-to-chromium").versions;
const electronToChromiumKeys = Object.keys(
electronToChromiumVersions
).reverse();
const chromiumToElectronMap = electronToChromiumKeys.reduce((all, electron) => {
all[electronToChromiumVersions[electron]] = +electron;
return all;
}, {});
const chromiumToElectronVersions = Object.keys(chromiumToElectronMap);
const findClosestElectronVersion = targetVersion => {
const chromiumVersionsLength = chromiumToElectronVersions.length;
const maxChromium = +chromiumToElectronVersions[chromiumVersionsLength - 1];
if (targetVersion > maxChromium) return null;
const closestChrome = chromiumToElectronVersions.find(
version => targetVersion <= version
);
return chromiumToElectronMap[closestChrome];
};
const chromiumToElectron = chromium =>
chromiumToElectronMap[chromium] || findClosestElectronVersion(chromium);
const renameTests = (tests, getName, category) =>
tests.map(test =>
Object.assign({}, test, { name: getName(test.name), category })
);
// The following is adapted from compat-table:
// https://github.com/kangax/compat-table/blob/gh-pages/build.js
//
// It parses and interpolates data so environments that "equal" other
// environments (node4 and chrome45), as well as familial relationships (edge
// and ie11) can be handled properly.
const envs = require("compat-table/environments");
const byTestSuite = suite => browser => {
return Array.isArray(browser.test_suites)
? browser.test_suites.indexOf(suite) > -1
: true;
};
const compatSources = [
"es6",
"es2016plus",
"esnext",
].reduce((result, source) => {
const data = require(`compat-table/data-${source}`);
data.browsers = pickBy(envs, byTestSuite(source));
result.push(data);
return result;
}, []);
const interpolateAllResults = (rawBrowsers, tests) => {
const interpolateResults = res => {
let browser;
let prevBrowser;
let result;
let prevResult;
let prevBid;
for (const bid in rawBrowsers) {
// For browsers that are essentially equal to other browsers,
// copy over the results.
browser = rawBrowsers[bid];
if (browser.equals && res[bid] === undefined) {
result = res[browser.equals];
res[bid] =
browser.ignore_flagged && result === "flagged" ? false : result;
// For each browser, check if the previous browser has the same
// browser full name (e.g. Firefox) or family name (e.g. Chakra) as this one.
} else if (
prevBrowser &&
(prevBrowser.full.replace(/,.+$/, "") ===
browser.full.replace(/,.+$/, "") ||
(browser.family !== undefined &&
prevBrowser.family === browser.family))
) {
// For each test, check if the previous browser has a result
// that this browser lacks.
result = res[bid];
prevResult = res[prevBid];
if (prevResult !== undefined && result === undefined) {
res[bid] = prevResult;
}
}
prevBrowser = browser;
prevBid = bid;
}
};
// Now print the results.
tests.forEach(function(t) {
// Calculate the result totals for tests which consist solely of subtests.
if ("subtests" in t) {
t.subtests.forEach(function(e) {
interpolateResults(e.res);
});
} else {
interpolateResults(t.res);
}
});
};
compatSources.forEach(({ browsers, tests }) =>
interpolateAllResults(browsers, tests)
);
// End of compat-table code adaptation
const environments = [
"chrome",
"opera",
"edge",
"firefox",
"safari",
"node",
"ie",
"android",
"ios",
"phantom",
];
const compatibilityTests = flattenDeep(
compatSources.map(data =>
data.tests.map(test => {
return test.subtests
? [
test,
renameTests(
test.subtests,
name => test.name + " / " + name,
test.category
),
]
: test;
})
)
);
const getLowestImplementedVersion = ({ features }, env) => {
const tests = compatibilityTests
.filter(test => {
return (
features.indexOf(test.name) >= 0 ||
// for features === ["DataView"]
// it covers "DataView (Int8)" and "DataView (UInt8)"
(features.length === 1 && test.name.indexOf(features[0]) === 0)
);
})
.reduce((result, test) => {
const isBuiltIn =
test.category === "built-ins" ||
test.category === "built-in extensions";
if (!test.subtests) {
result.push({
name: test.name,
res: test.res,
isBuiltIn,
});
} else {
test.subtests.forEach(subtest =>
result.push({
name: `${test.name}/${subtest.name}`,
res: subtest.res,
isBuiltIn,
})
);
}
return result;
}, []);
const envTests = tests.map(({ res: test, isBuiltIn }, i) => {
// Babel itself doesn't implement the feature correctly,
// don't count against it
// only doing this for built-ins atm
if (!test.babel && isBuiltIn) {
return "-1";
}
return (
Object.keys(test)
.filter(t => t.startsWith(env))
// Babel assumes strict mode
.filter(
test => tests[i].res[test] === true || tests[i].res[test] === "strict"
)
// normalize some keys
.map(test => test.replace("_", "."))
.filter(test => !isNaN(parseFloat(test.replace(env, ""))))
.shift()
);
});
const envFiltered = envTests.filter(t => t);
if (envTests.length > envFiltered.length || envTests.length === 0) {
// envTests.forEach((test, i) => {
// if (!test) {
// // print unsupported features
// if (env === 'node') {
// console.log(`ENV(${env}): ${tests[i].name}`);
// }
// }
// });
return null;
}
return envTests.map(str => Number(str.replace(env, ""))).reduce((a, b) => {
return a < b ? b : a;
});
};
const generateData = (environments, features) => {
return mapValues(features, options => {
if (!options.features) {
options = {
features: [options],
};
}
const plugin = {};
environments.forEach(env => {
const version = getLowestImplementedVersion(options, env);
if (version !== null) {
plugin[env] = version.toString();
}
});
if (plugin.chrome) {
// add opera
if (plugin.chrome >= 28) {
plugin.opera = (plugin.chrome - 13).toString();
} else if (plugin.chrome === 5) {
plugin.opera = "12";
}
// add electron
const electronVersion = chromiumToElectron(plugin.chrome);
if (electronVersion) {
plugin.electron = electronVersion.toString();
}
}
return plugin;
});
};
["plugin", "built-in"].forEach(target => {
const newData = generateData(
environments,
require(`../data/${target}-features`)
);
const dataPath = path.join(__dirname, `../data/${target}s.json`);
if (process.argv[2] === "--check") {
const currentData = require(dataPath);
if (!isEqual(currentData, newData)) {
console.error(
"The newly generated plugin/built-in data does not match the current " +
"files. Re-run `npm run build-data`."
);
process.exit(1);
}
process.exit(0);
}
fs.writeFileSync(dataPath, `${JSON.stringify(newData, null, 2)}\n`);
});