All files / if-run/util args.ts

100% Statements 28/28
100% Branches 12/12
100% Functions 2/2
100% Lines 27/27

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 811x 1x   1x 1x   1x   1x 1x       1x   1x 1x 1x         1x 10x 10x   2x 1x   1x     1x                     1x                   10x   8x 1x     8x 7x 6x                           1x     1x    
import {parse} from 'ts-command-line-args';
import {ERRORS} from '@grnsft/if-core/utils';
 
import {checkIfFileIsYaml} from '../../common/util/yaml';
import {prependFullFilePath, runHelpCommand} from '../../common/util/helpers';
 
import {logger} from '../../common/util/logger';
 
import {CONFIG, STRINGS} from '../config';
import {STRINGS as COMMON_STRINGS} from '../../common/config';
 
import {IfRunArgs, ProcessArgsOutputs} from '../types/process-args';
 
const {CliSourceFileError} = ERRORS;
 
const {ARGS, HELP} = CONFIG;
const {NO_OUTPUT} = STRINGS;
const {SOURCE_IS_NOT_YAML, MANIFEST_IS_MISSING} = COMMON_STRINGS;
 
/**
 * Validates `if-run` process arguments.
 */
const validateAndParseProcessArgs = () => {
  try {
    return parse<IfRunArgs>(ARGS, HELP);
  } catch (error) {
    if (error instanceof Error) {
      console.log(error.message);
 
      runHelpCommand('if-run');
    }
 
    throw error;
  }
};
 
/**
 * 1. Parses process arguments for `if-run`.
 * 2. If output params are missing, warns user about it.
 * 3. Otherwise checks if `manifest` param is there, then processes with checking if it's a yaml file.
 *    If it is, then returns object containing full path.
 * 4. If params are missing or invalid, runs `--help` command.
 */
export const parseIfRunProcessArgs = (): ProcessArgsOutputs => {
  const {
    manifest,
    output,
    'no-output': noOutput,
    debug,
    observe,
    regroup,
    compute,
    append,
  } = validateAndParseProcessArgs();
 
  if (!output && noOutput) {
    logger.warn(NO_OUTPUT);
  }
 
  if (manifest) {
    if (checkIfFileIsYaml(manifest)) {
      return {
        inputPath: prependFullFilePath(manifest),
        outputOptions: {
          ...(output && {outputPath: prependFullFilePath(output)}),
          ...(noOutput && {noOutput}),
        },
        debug,
        observe,
        regroup,
        compute,
        ...(append && {append}),
      };
    }
 
    throw new CliSourceFileError(SOURCE_IS_NOT_YAML);
  }
 
  throw new CliSourceFileError(MANIFEST_IS_MISSING);
};