| 1 |
|
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
const fs = require("fs"); |
| 6 |
const flatMap = require("lodash/flatMap"); |
| 7 |
const mapValues = require("lodash/mapValues"); |
| 8 |
const findLastIndex = require("lodash/findLastIndex"); |
| 9 |
const electronToChromiumVersions = require("electron-to-chromium").versions; |
| 10 |
|
| 11 |
const envs = require("../../build/compat-table/environments"); |
| 12 |
const parseEnvsVersions = require("../../build/compat-table/build-utils/parse-envs-versions"); |
| 13 |
const interpolateAllResults = require("../../build/compat-table/build-utils/interpolate-all-results"); |
| 14 |
const compareVersions = require("../../build/compat-table/build-utils/compare-versions"); |
| 15 |
|
| 16 |
|
| 17 |
Object.keys(electronToChromiumVersions).forEach(electron => { |
| 18 |
const chrome = electronToChromiumVersions[electron]; |
| 19 |
|
| 20 |
const electronId = `electron${electron.replace(".", "_")}`; |
| 21 |
let chromeId = `chrome${chrome}`; |
| 22 |
|
| 23 |
|
| 24 |
if (chromeId === "chrome82") chromeId = "chrome81"; |
| 25 |
if (!envs[chromeId]) { |
| 26 |
console.error( |
| 27 |
`Electron ${electron} inherits from Chrome ${chrome}, which is not defined.` |
| 28 |
); |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
envs[electronId] = { equals: chromeId }; |
| 33 |
}); |
| 34 |
|
| 35 |
const envsVersions = parseEnvsVersions(envs); |
| 36 |
|
| 37 |
const compatSources = ["es5", "es6", "es2016plus", "esnext"].map(source => { |
| 38 |
const data = require(`../../build/compat-table/data-${source}`); |
| 39 |
interpolateAllResults(data.tests, envs); |
| 40 |
return data; |
| 41 |
}); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
exports.environments = [ |
| 46 |
"chrome", |
| 47 |
"opera", |
| 48 |
"edge", |
| 49 |
"firefox", |
| 50 |
"safari", |
| 51 |
"node", |
| 52 |
"ie", |
| 53 |
"android", |
| 54 |
"ios", |
| 55 |
"phantom", |
| 56 |
"samsung", |
| 57 |
"electron", |
| 58 |
]; |
| 59 |
|
| 60 |
const compatibilityTests = flatMap(compatSources, data => |
| 61 |
flatMap(data.tests, test => { |
| 62 |
if (!test.subtests) return test; |
| 63 |
|
| 64 |
return test.subtests.map(subtest => |
| 65 |
Object.assign({}, subtest, { |
| 66 |
name: test.name + " / " + subtest.name, |
| 67 |
group: test.name, |
| 68 |
}) |
| 69 |
); |
| 70 |
}) |
| 71 |
); |
| 72 |
|
| 73 |
exports.getLowestImplementedVersion = ( |
| 74 |
{ features }, |
| 75 |
env, |
| 76 |
exclude = () => false |
| 77 |
) => { |
| 78 |
const tests = compatibilityTests.filter(test => { |
| 79 |
|
| 80 |
let ok = features.includes(test.name); |
| 81 |
ok = ok || (test.group && features.includes(test.group)); |
| 82 |
ok = ok || (features.length === 1 && test.name.startsWith(features[0])); |
| 83 |
ok = ok && !exclude(test.name); |
| 84 |
return ok; |
| 85 |
}); |
| 86 |
|
| 87 |
const envTests = tests.map(({ res }) => { |
| 88 |
const lastNotImplemented = findLastIndex( |
| 89 |
envsVersions[env], |
| 90 |
|
| 91 |
({ id }) => !(res[id] === true || res[id] === "strict") |
| 92 |
); |
| 93 |
|
| 94 |
return envsVersions[env][lastNotImplemented + 1]; |
| 95 |
}); |
| 96 |
|
| 97 |
if (envTests.length === 0 || envTests.some(t => !t)) return null; |
| 98 |
|
| 99 |
const result = envTests.reduce((a, b) => { |
| 100 |
return compareVersions(a, b) > 0 ? a : b; |
| 101 |
}); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
return result.version.join(".").replace(/\.0$/, ""); |
| 107 |
}; |
| 108 |
|
| 109 |
exports.generateData = (environments, features) => { |
| 110 |
return mapValues(features, options => { |
| 111 |
if (!options.features) { |
| 112 |
options = { |
| 113 |
features: [options], |
| 114 |
}; |
| 115 |
} |
| 116 |
|
| 117 |
const plugin = {}; |
| 118 |
|
| 119 |
environments.forEach(env => { |
| 120 |
const version = exports.getLowestImplementedVersion(options, env); |
| 121 |
if (version) plugin[env] = version; |
| 122 |
}); |
| 123 |
|
| 124 |
return plugin; |
| 125 |
}); |
| 126 |
}; |
| 127 |
|
| 128 |
exports.writeFile = function(data, dataPath, name) { |
| 129 |
const stringified = JSON.stringify(data, null, 2) + "\n"; |
| 130 |
if (process.env.CHECK_COMPAT_DATA) { |
| 131 |
const currentData = fs.readFileSync(dataPath, "utf8"); |
| 132 |
|
| 133 |
|
| 134 |
if (currentData !== stringified) { |
| 135 |
console.error( |
| 136 |
`The newly generated ${name} data does not match the current ` + |
| 137 |
"files. Re-run `yarn build-es-shims-data`." |
| 138 |
); |
| 139 |
|
| 140 |
return false; |
| 141 |
} |
| 142 |
} else { |
| 143 |
fs.writeFileSync(dataPath, stringified); |
| 144 |
} |
| 145 |
return true; |
| 146 |
}; |