compat.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const { compare, has, intersection } = require('./helpers');
  3. const data = require('./data');
  4. const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
  5. const modules = require('./modules');
  6. const targetsParser = require('./targets-parser');
  7. function checkModule(name, targets) {
  8. if (!has(data, name)) throw new TypeError(`Incorrect module: ${ name }`);
  9. const requirements = data[name];
  10. const result = {
  11. required: false,
  12. targets: {},
  13. };
  14. for (const [engine, version] of targets) {
  15. if (!has(requirements, engine) || compare(version, '<', requirements[engine])) {
  16. result.required = true;
  17. result.targets[engine] = version;
  18. }
  19. }
  20. return result;
  21. }
  22. module.exports = function ({ targets, filter, version }) {
  23. const parsedTargets = targetsParser(targets);
  24. const result = {
  25. list: [],
  26. targets: {},
  27. };
  28. let $modules = Array.isArray(filter) ? filter : modules;
  29. if (filter instanceof RegExp) {
  30. $modules = $modules.filter(it => filter.test(it));
  31. } else if (typeof filter == 'string') {
  32. $modules = $modules.filter(it => it.startsWith(filter));
  33. }
  34. if (version) {
  35. $modules = intersection($modules, getModulesListForTargetVersion(version));
  36. }
  37. for (const key of $modules) {
  38. const check = checkModule(key, parsedTargets);
  39. if (check.required) {
  40. result.list.push(key);
  41. result.targets[key] = check.targets;
  42. }
  43. }
  44. return result;
  45. };