All files / modules/70-pipeline/components/AbstractSteps AbstractStepFactory.ts

55.88% Statements 19/34
84.21% Branches 32/38
56.25% Functions 9/16
59.38% Lines 19/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                175x                       175x               191x           191x 191x               4942x 4942x           4942x 4942x 1564x                                           1265x 1237x   28x       20x       19x       151x       18x       23x                     39x              
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { IconName } from '@wings-software/uicore'
import { isEmpty } from 'lodash-es'
import type { CompletionItemInterface } from '@common/interfaces/YAMLBuilderProps'
import type { StringsMap } from 'stringTypes'
import type { Step } from './Step'
 
export interface StepData {
  name: string
  icon: IconName
  type: string
  visible?: boolean
}
 
export abstract class AbstractStepFactory {
  /**
   * Couples the factory with the steps it generates
   */
  protected abstract type: string
 
  protected stepBank: Map<string, Step<unknown>>
  protected stepIconMap: Map<string, StepData>
  protected invocationMap: Map<
    RegExp,
    (path: string, yaml: string, params: Record<string, unknown>) => Promise<CompletionItemInterface[]>
  > = new Map()
 
  constructor() {
    this.stepBank = new Map()
    this.stepIconMap = new Map()
  }
 
  getType(): string {
    return this.type
  }
 
  registerStep<T>(step: Step<T>): void {
    this.stepBank.set(step.getType(), step as Step<unknown>)
    this.stepIconMap.set(step.getType(), {
      name: step.getStepName(),
      icon: step.getIconName(),
      type: step.getType(),
      visible: step.getStepPaletteVisibility()
    })
    const stepMap = step.getInvocationMap()
    if (stepMap) {
      this.invocationMap = new Map([...this.invocationMap, ...stepMap])
    }
  }
 
  deregisterStep(type: string): void {
    const deletedStep = this.stepBank.get(type)
    if (deletedStep) {
      this.stepBank.delete(type)
      this.stepIconMap.delete(type)
      if (deletedStep.getInvocationMap()) {
        this.invocationMap = new Map()
        this.stepBank.forEach(step => {
          const stepMap = step.getInvocationMap()
          if (stepMap) {
            this.invocationMap = new Map([...this.invocationMap, ...stepMap])
          }
        })
      }
    }
  }
 
  getStep<T>(type?: string): Step<T> | undefined {
    if (type && !isEmpty(type)) {
      return this.stepBank.get(type) as Step<T>
    }
    return
  }
 
  getStepDescription(type: string): keyof StringsMap | undefined {
    return this.stepBank.get(type)?.getDescription()
  }
 
  getStepName(type: string): string | undefined {
    return this.stepBank.get(type)?.getStepName()
  }
 
  getStepIcon(type: string): IconName {
    return this.stepBank.get(type)?.getIconName() || 'disable'
  }
 
  getStepIconColor(type: string): string | undefined {
    return this.stepBank.get(type)?.getIconColor() || undefined
  }
 
  getStepIsHarnessSpecific(type: string): boolean {
    return this.stepBank.get(type)?.getIsHarnessSpecific() || false
  }
 
  getStepData(type: string): StepData | undefined {
    return this.stepIconMap.get(type)
  }
 
  getInvocationMap(): Map<
    RegExp,
    (path: string, yaml: string, params: Record<string, unknown>) => Promise<CompletionItemInterface[]>
  > {
    return this.invocationMap
  }
 
  getAllStepsDataList(): Array<StepData> {
    return Array.from(this.stepIconMap, ([_key, value]) => value).filter(step => step.visible)
  }
}