All files / modules/72-templates-library/components/TemplateStudio TemplateStudioUtils.ts

85.71% Statements 12/14
83.33% Branches 10/12
100% Functions 2/2
85.71% Lines 12/14

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              9x 9x         9x       28x           9x             3x 3x 3x   1x       1x   2x 2x            
/*
 * Copyright 2022 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 { defaultTo } from 'lodash-es'
import { parse } from 'yaml'
import type { StringKeys } from 'framework/strings'
import type { NGTemplateInfoConfig } from 'services/template-ng'
import type { YamlBuilderHandlerBinding } from '@common/interfaces/YAMLBuilderProps'
 
export function getContentAndTitleStringKeys(isYamlError: boolean): {
  navigationContentText: StringKeys
  navigationTitleText: StringKeys
} {
  return {
    navigationContentText: isYamlError ? 'navigationYamlError' : 'navigationCheckText',
    navigationTitleText: isYamlError ? 'navigationYamlErrorTitle' : 'navigationCheckTitle'
  }
}
 
export function isValidYaml(
  yamlHandler: YamlBuilderHandlerBinding | undefined,
  showInvalidYamlError: (error: string) => void,
  getString: (key: StringKeys, vars?: Record<string, any>) => string,
  updateTemplate: (template: NGTemplateInfoConfig) => Promise<void>
): boolean {
  // istanbul ignore else
  if (yamlHandler) {
    try {
      const parsedYaml = parse(yamlHandler.getLatestYaml())
      // istanbul ignore else
      Iif (!parsedYaml || yamlHandler.getYAMLValidationErrorMap()?.size > 0) {
        showInvalidYamlError(getString('invalidYamlText'))
        return false
      }
      updateTemplate(parsedYaml.template)
    } catch (e) {
      showInvalidYamlError(defaultTo(e.message, getString('invalidYamlText')))
      return false
    }
  }
  // istanbul ignore next - This is required just to match the return type and nothing more
  return true
}