all files / src/ parse-cli-args.js

0% Statements 0/26
0% Branches 0/12
0% Functions 0/3
0% Lines 0/24
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                                                                                                                   
import yargs from 'yargs';
import { version } from '../package.json';
import common from './commands/common-yargs';
import commands from './commands';
 
const help = {
  options: null,
  execute: yargs.showHelp
};
 
export default function parseCliArgs(argv) {
  if (argv === process.argv) {
    argv = argv.slice(2);
  }
 
  common(yargs);
 
  yargs
    .version(`v${version}`)
    .alias('version', 'v')
    .wrap(null);
 
  Object.keys(commands)
    .map(key => commands[key])
    .forEach(x => {
      if (x.yargsSetup) {
        x.yargsSetup();
      }
 
      if (x.examples) {
        x.examples();
      }
    });
 
  let commandOptions = yargs.parse(argv);
 
  let command = commandOptions._[0];
 
  if (!command) {
    if (commandOptions.help) {
      return help;
    }
 
    throw 'No command provided';
  }
 
  let options = commands[command].parseArgs(argv.slice(1));
 
  if (commandOptions.help) {
    return help;
  }
 
  return {
    options,
    execute: commands[command].execute
  };
}