phases.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.mergePhaseConfiguration = exports.isInCurrentPhase = void 0;
  6. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  7. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  8. /**
  9. * Check if the current phase is in the phase config and so a plugin should get applied
  10. *
  11. * @param {string} currentPhase - current phase
  12. * @param {array|string} phaseConfig - phase config in an array ([PHASE1, PHASE2])
  13. * or string (PHASE1 + PHASE2)
  14. */
  15. const isInCurrentPhase = (currentPhase, phaseConfig) => {
  16. // phase config can be an array or string, so always convert it to a string
  17. const parsedPhaseConfig = phaseConfig instanceof Array ? phaseConfig.join('') : phaseConfig; // negate the check
  18. if (parsedPhaseConfig.substr(0, 1) === '!') {
  19. return parsedPhaseConfig.indexOf(currentPhase) < 0;
  20. }
  21. return parsedPhaseConfig.indexOf(currentPhase) >= 0;
  22. };
  23. /**
  24. * Merge the configuration of a plugin with specific values only applied on the current phase
  25. *
  26. * @param {string} currentPhase - current phase
  27. * @param {object} config - plugin configuration
  28. */
  29. exports.isInCurrentPhase = isInCurrentPhase;
  30. const mergePhaseConfiguration = (currentPhase, config) => {
  31. let mergedConfig = {};
  32. Object.keys(config).forEach(key => {
  33. if (key.startsWith('phase-') || key.startsWith('!phase-')) {
  34. if (isInCurrentPhase(currentPhase, key)) {
  35. mergedConfig = _objectSpread({}, mergedConfig, config[key]);
  36. }
  37. } else {
  38. mergedConfig[key] = config[key];
  39. }
  40. });
  41. return mergedConfig;
  42. };
  43. exports.mergePhaseConfiguration = mergePhaseConfiguration;