All files / lib load.ts

100% Statements 25/25
100% Branches 13/13
100% Functions 2/2
100% Lines 23/23

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 625x 5x   5x 5x   5x 5x           5x   5x           5x 2x   2x   2x     2x     2x                 5x 3x   3x 1x       2x   2x   2x 2x   2x          
import * as YAML from 'js-yaml';
import {ERRORS} from '@grnsft/if-core/utils';
 
import {openYamlFileAsObject} from '../util/yaml';
import {readAndParseJson} from '../util/json';
 
import {PARAMETERS} from '../config';
import {STRINGS} from '../config';
 
import {Parameters} from '../types/parameters';
import {LoadDiffParams} from '../types/util/args';
import {Manifest} from '../types/manifest';
 
const {CliSourceFileError} = ERRORS;
 
const {INVALID_SOURCE, LOADING_MANIFEST} = STRINGS;
 
/**
 * Parses manifest file as an object. Checks if parameter file is passed via CLI, then loads it too.
 * Returns context, tree and parameters (either the default one, or from CLI).
 */
export const load = async (inputPath: string, paramPath?: string) => {
  console.debug(LOADING_MANIFEST);
 
  const rawManifest = await openYamlFileAsObject<any>(inputPath);
  const parametersFromCli =
    paramPath &&
    (await readAndParseJson<Parameters>(paramPath)); /** @todo validate json */
  const parameters =
    parametersFromCli ||
    PARAMETERS; /** @todo PARAMETERS should be specified in parameterize only */
 
  return {
    rawManifest,
    parameters,
  };
};
 
/**
 * Loads files to compare. As a source file checks if data is piped and then decides which one to take.
 */
export const loadIfDiffFiles = async (params: LoadDiffParams) => {
  const {sourcePath, targetPath, pipedSourceManifest} = params;
 
  if (!sourcePath && !pipedSourceManifest) {
    throw new CliSourceFileError(INVALID_SOURCE);
  }
 
  const loadFromSource =
    sourcePath && (await openYamlFileAsObject<Manifest>(sourcePath!));
  const loadFromSTDIN =
    pipedSourceManifest && (await YAML.load(pipedSourceManifest!));
 
  const rawSourceManifest = loadFromSource || loadFromSTDIN;
  const rawTargetManifest = await openYamlFileAsObject<Manifest>(targetPath);
 
  return {
    rawSourceManifest,
    rawTargetManifest,
  };
};