Skip to content

Commit

Permalink
util: tune args parser to support input/output.
Browse files Browse the repository at this point in the history
  • Loading branch information
narekhovhannisyan committed Aug 30, 2023
1 parent f432144 commit ff56d42
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/util/args.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import path from 'path';
import {parse} from 'ts-command-line-args';

import {checkIfFileIsYaml} from './yaml';

import {STRINGS} from '../config';
import {CONFIG, STRINGS} from '../config';

import {ImplProcessArgs} from '../types/process-args';

const {VALIDATION} = CONFIG;
const {IMPL_CLI} = VALIDATION;

const {WRONG_CLI_ARGUMENT} = STRINGS;

/**
* Validates process arguments
* @private
*/
const validateAndParseProcessArgs = () => parse<ImplProcessArgs>(IMPL_CLI);

/**
* Prepends process path to fiven `filePath`.
* @private
*/
const prependFullFilePath = (filePath: string) => {
const processRunningPath = process.cwd();

return path.normalize(`${processRunningPath}/${filePath}`);
};

/**
* Parses process argument, if it's `yaml` file, then returns it.
* Otherwise throws error.
*/
export const parseProcessArgument = () => {
const lastArgIndex = process.argv.length - 1;
const path = process.argv[lastArgIndex];
const {impl, ompl} = validateAndParseProcessArgs();

if (checkIfFileIsYaml(path)) {
return path;
if (checkIfFileIsYaml(impl)) {
return {
inputPath: prependFullFilePath(impl),
...(ompl && {outputPath: prependFullFilePath(ompl)}),
};
}

throw Error(WRONG_CLI_ARGUMENT);
Expand Down

0 comments on commit ff56d42

Please sign in to comment.