All files cli.js

64.91% Statements 37/57
45.45% Branches 10/22
20% Functions 2/10
65.45% Lines 36/55
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 1801x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                                                                                                                                                     1x                   1x     1x 1x 1x 1x 1x   1x         1x 1x 1x 1x 1x 1x 1x           1x 1x 1x             1x 1x 1x           1x 1x          
var path = require('path')
var fs = require('fs')
var yargs = require('yargs')
var logger = require('./logger')
var Sync = require('./Sync')
var Config = require('./ConfigTask')
var Unpack = require('./Unpack')
var Fetch = require('./Fetch')
var Grab = require('./Grab')
var List = require('./List')
var GetApi = require('./GetApi')
var HistoricalRequests = require('./HistoricalRequests')
 
var cli = yargs
  .usage('canvasDataCli <command>')
  .demand(1, 'must provide a valid command')
  .option('level', {
    alias: 'l',
    default: 'info',
    describe: `logging level to use, valid levels are ${logger.levels.join(', ')}`,
    type: 'string'
  })
  .command('sync', 'download the latest files from the API', (yargs) => {
    yargs.option('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use',
      type: 'string'
    })
    .help('help')
  })
  .command('sampleConfig', 'write a sample config file to config.js.sample')
  .command('unpack', 'decompress and merge files into a single file', (yargs) => {
    yargs.option('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use',
      type: 'string'
    })
    .option('filter', {
      alias: 'f',
      describe: 'list of tables to unpack, ex: -f user_dim account_dim',
      demand: true,
      array: true,
      type: 'string'
    })
    .help('help')
  })
  .command('fetch', 'fetch a single table', (yargs) => {
    yargs.options('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use',
      type: 'string'
    })
    .option('table', {
      alias: 't',
      describe: 'the table to fetch',
      demand: true,
      type: 'string'
    })
  })
  .command('grab', 'grab one specific dump', (yargs) => {
    yargs.options('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use',
      type: 'string'
    })
    .option('dump', {
      alias: 'd',
      describe: 'the dump to fetch',
      demand: true,
      type: 'string'
    })
  })
  .command('list', 'list all dumps', (yargs) => {
    yargs.options('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use.',
      type: 'string'
    }).option('json', {
      alias: 'j',
      describe: 'output in json format',
      demand: false,
      type: 'boolean'
    })
  })
  .command('api', 'submit API GET request', (yargs) => {
    yargs.options('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use.',
      type: 'string'
    }).option('route', {
      alias: 'r',
      describe: 'route to request',
      demand: true,
      type: 'string'
    }).option('params', {
      alias: 'p',
      desscribe: 'params in JSON form',
      demand: false,
      type: 'string'
    })
  })
  .command('historical-requests', 'show historical requests by date ranges', (yargs) => {
    yargs.options('config', {
      alias: 'c',
      demand: true,
      describe: 'the configuration file to use.',
      type: 'string'
    })
  })
  .help('help')
  .alias('v', 'version')
  .version(() => require('../package').version)
  .describe('v', 'show version information')
 
var runnerMap = {
  sync: {requireConfig: true, class: Sync},
  sampleConfig: {class: Config},
  unpack: {requireConfig: true, class: Unpack},
  fetch: {requireConfig: true, class: Fetch},
  grab: {requireConfig: true, class: Grab},
  list: {requireConfig: true, class: List},
  api: {requireConfig: true, class: GetApi},
  'historical-requests': {requireConfig: true, class: HistoricalRequests}
}
module.exports = {
  cli: cli,
  run(argv) {
    var command = argv._[0]
    var runner = runnerMap[command]
    var logLevel = argv.l || argv.level
    Eif (logLevel) {
      logger.setLevel(logLevel)
    }
    Iif (!runner) {
      logger.error('invalid command')
      cli.showHelp()
      process.exit(1)
    }
    var RunnerClass = runner.class
    var config = {}
    Eif (runner.requireConfig) {
      var configFile = argv.config
      var configPath = path.resolve(process.cwd(), configFile)
      try {
        fs.statSync(configPath)
      } catch (e) {
        logger.error(`config file at ${configPath} does not exist`)
        process.exit(1)
      }
 
      config = require(configPath)
      var isInvalidConfig = Config.validate(config)
      Iif (isInvalidConfig) {
        logger.error(`config at ${configPath} is invalid`)
        logger.error(isInvalidConfig)
        process.exit(1)
      }
    }
 
    var runner = new RunnerClass(argv, config, logger)
    runner.run((err, showComplete = true) => {
      Iif (err) {
        logger.error('an error occured')
        logger.error(err)
        if (err.stack && !err.silence) logger.error(err.stack)
        process.exit(1)
      }
      Eif (showComplete) {
        logger.info(`${command} command completed successfully`)
      }
    })
  }
}