All files / if-env/util args.ts

100% Statements 29/29
100% Branches 4/4
100% Functions 2/2
100% Lines 28/28

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 581x   1x   1x 1x 1x   1x 1x       1x 1x 1x         1x 6x 6x   2x 1x   1x     1x             1x 6x   4x 3x 3x   3x 1x     2x 1x     1x     1x    
import {parse} from 'ts-command-line-args';
 
import {ERRORS} from '@grnsft/if-core/utils';
 
import {isFileExists} from '../../common/util/fs';
import {prependFullFilePath, runHelpCommand} from '../../common/util/helpers';
import {checkIfFileIsYaml} from '../../common/util/yaml';
 
import {CONFIG} from '../config';
import {STRINGS as COMMON_STRINGS} from '../../common/config';
 
import {IFEnvArgs} from '../types/process-args';
 
const {ParseCliParamsError, CliSourceFileError} = ERRORS;
const {ARGS, HELP} = CONFIG;
const {MANIFEST_NOT_FOUND, SOURCE_IS_NOT_YAML} = COMMON_STRINGS;
 
/**
 * Parses `if-env` process arguments.
 */
const validateAndParseIfEnvArgs = () => {
  try {
    return parse<IFEnvArgs>(ARGS, HELP);
  } catch (error) {
    if (error instanceof Error) {
      console.log(error.message);
 
      runHelpCommand('if-env');
    }
 
    throw error;
  }
};
 
/**
 * Checks if the `manifest` command is provided and it is valid manifest file.
 */
export const parseIfEnvArgs = async () => {
  const {manifest, install, cwd} = validateAndParseIfEnvArgs();
 
  if (manifest) {
    const response = prependFullFilePath(manifest);
    const isManifestFileExists = await isFileExists(response);
 
    if (!isManifestFileExists) {
      throw new ParseCliParamsError(MANIFEST_NOT_FOUND);
    }
 
    if (checkIfFileIsYaml(manifest)) {
      return {manifest: response, install, cwd};
    }
 
    throw new CliSourceFileError(SOURCE_IS_NOT_YAML);
  }
 
  return {install, cwd};
};