watchPluginsHelpers.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.getSortedUsageRows = exports.filterInteractivePlugins = void 0;
  6. /**
  7. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. const filterInteractivePlugins = (watchPlugins, globalConfig) => {
  13. const usageInfos = watchPlugins.map(
  14. p => p.getUsageInfo && p.getUsageInfo(globalConfig)
  15. );
  16. return watchPlugins.filter((_plugin, i) => {
  17. const usageInfo = usageInfos[i];
  18. if (usageInfo) {
  19. const {key} = usageInfo;
  20. return !usageInfos.slice(i + 1).some(u => !!u && key === u.key);
  21. }
  22. return false;
  23. });
  24. };
  25. exports.filterInteractivePlugins = filterInteractivePlugins;
  26. function notEmpty(value) {
  27. return value != null;
  28. }
  29. const getSortedUsageRows = (watchPlugins, globalConfig) =>
  30. filterInteractivePlugins(watchPlugins, globalConfig)
  31. .sort((a, b) => {
  32. if (a.isInternal && b.isInternal) {
  33. // internal plugins in the order we specify them
  34. return 0;
  35. }
  36. if (a.isInternal !== b.isInternal) {
  37. // external plugins afterwards
  38. return a.isInternal ? -1 : 1;
  39. }
  40. const usageInfoA = a.getUsageInfo && a.getUsageInfo(globalConfig);
  41. const usageInfoB = b.getUsageInfo && b.getUsageInfo(globalConfig);
  42. if (usageInfoA && usageInfoB) {
  43. // external plugins in alphabetical order
  44. return usageInfoA.key.localeCompare(usageInfoB.key);
  45. }
  46. return 0;
  47. })
  48. .map(p => p.getUsageInfo && p.getUsageInfo(globalConfig))
  49. .filter(notEmpty);
  50. exports.getSortedUsageRows = getSortedUsageRows;