All files / util args.ts

100% Statements 68/68
100% Branches 26/26
100% Functions 7/7
100% Lines 65/65

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 1891x   1x 1x   1x   1x   1x   1x                   1x   1x                 1x         1x 9x 9x   2x 1x     1x             1x 12x   12x 1x     11x                     1x             9x   7x 5x     7x 6x 5x                     1x     1x               1x 7x 7x   2x 1x     1x             1x 7x   5x 4x 1x     3x 2x       2x 1x     2x     1x     1x               1x 6x 6x   2x 1x     1x             1x 6x   4x 3x 3x   3x 1x     2x 1x     1x     1x    
import * as path from 'path';
 
import {parse} from 'ts-command-line-args';
import {ERRORS} from '@grnsft/if-core/utils';
 
import {checkIfFileIsYaml} from './yaml';
 
import {isFileExists} from './fs';
 
import {logger} from './logger';
 
import {CONFIG, STRINGS} from '../config';
 
import {
  IFDiffArgs,
  IEArgs,
  ProcessArgsOutputs,
  IFEnvArgs,
} from '../types/process-args';
import {LoadDiffParams} from '../types/util/args';
 
const {ParseCliParamsError, CliTargetFileError, CliSourceFileError} = ERRORS;
 
const {IE, IF_DIFF, IF_ENV} = CONFIG;
 
const {
  MANIFEST_IS_MISSING,
  MANIFEST_NOT_FOUND,
  NO_OUTPUT,
  SOURCE_IS_NOT_YAML,
  TARGET_IS_NOT_YAML,
  INVALID_TARGET,
} = STRINGS;
 
/**
 * Validates `ie` process arguments.
 */
const validateAndParseProcessArgs = () => {
  try {
    return parse<IEArgs>(IE.ARGS, IE.HELP);
  } catch (error) {
    if (error instanceof Error) {
      throw new ParseCliParamsError(error.message);
    }
 
    throw error;
  }
};
 
/**
 * Prepends process path to given `filePath`.
 */
const prependFullFilePath = (filePath: string) => {
  const processRunningPath = process.cwd();
 
  if (path.isAbsolute(filePath)) {
    return filePath;
  }
 
  return path.normalize(`${processRunningPath}/${filePath}`);
};
 
/**
 * 1. Parses process arguments like `manifest`, `output`, `override-params`, `help` and `debug`.
 * 2. Checks if `help` param is provided, then logs help message and exits.
 * 3. 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, then rejects with `ParseCliParamsError`.
 */
export const parseIEProcessArgs = (): ProcessArgsOutputs => {
  const {
    manifest,
    output,
    'override-params': overrideParams,
    stdout,
    debug,
  } = validateAndParseProcessArgs();
 
  if (!output && !stdout) {
    logger.warn(NO_OUTPUT);
  }
 
  if (manifest) {
    if (checkIfFileIsYaml(manifest)) {
      return {
        inputPath: prependFullFilePath(manifest),
        outputOptions: {
          ...(output && {outputPath: prependFullFilePath(output)}),
          ...(stdout && {stdout}),
        },
        ...(overrideParams && {paramPath: overrideParams}),
        debug,
      };
    }
 
    throw new CliSourceFileError(SOURCE_IS_NOT_YAML);
  }
 
  throw new CliSourceFileError(MANIFEST_IS_MISSING);
};
 
/** -- IF Diff -- */
 
/**
 * Parses `if-diff` process arguments.
 */
const validateAndParseIfDiffArgs = () => {
  try {
    return parse<IFDiffArgs>(IF_DIFF.ARGS, IF_DIFF.HELP);
  } catch (error) {
    if (error instanceof Error) {
      throw new ParseCliParamsError(error.message);
    }
 
    throw error;
  }
};
 
/**
 * Checks for `source` and `target` flags to be present.
 */
export const parseIfDiffArgs = () => {
  const {source, target} = validateAndParseIfDiffArgs();
 
  if (target) {
    if (source && !checkIfFileIsYaml(source)) {
      throw new CliSourceFileError(SOURCE_IS_NOT_YAML);
    }
 
    if (checkIfFileIsYaml(target)) {
      const response: LoadDiffParams = {
        targetPath: prependFullFilePath(target),
      };
 
      if (source && checkIfFileIsYaml(source)) {
        response.sourcePath = prependFullFilePath(source);
      }
 
      return response;
    }
 
    throw new CliTargetFileError(TARGET_IS_NOT_YAML);
  }
 
  throw new ParseCliParamsError(INVALID_TARGET);
};
 
/** -- IF Env -- */
 
/**
 * Parses `if-env` process arguments.
 */
const validateAndParseIfEnvArgs = () => {
  try {
    return parse<IFEnvArgs>(IF_ENV.ARGS, IF_ENV.HELP);
  } catch (error) {
    if (error instanceof Error) {
      throw new ParseCliParamsError(error.message);
    }
 
    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};
};