lib/cli.js

Maintainability

64.80

Lines of code

71

Created with Raphaël 2.1.002550751002017-7-22

2017-7-22
Maintainability: 64.8

Created with Raphaël 2.1.00204060802017-7-22

2017-7-22
Lines of Code: 71

Difficulty

18.62

Estimated Errors

0.75

Function weight

By Complexity

Created with Raphaël 2.1.0_exec6

By SLOC

Created with Raphaël 2.1.0_exec43
1
const cli     = require('bagofcli');
2
const colors  = require('colors');
3
const fs      = require('fs');
4
const p       = require('path');
5
const SwaggyC = require('./swaggyc');
6
const util    = require('util');
7
 
8
const APP_DIR     = process.cwd();
9
const SWAGGYC_DIR = p.join(__dirname, '..');
10
 
11
function _exec() {
12
 
13
  var lastArg = arguments[arguments.length - 1];
14
  var tasks   = [lastArg._name].concat(lastArg.parent.args.slice(0, -1));
15
 
16
  var apiSpec  = lastArg.parent.apiSpec;
17
  var confFile = lastArg.parent.confFile || p.join(APP_DIR, '{lang}/conf.json');
18
  var outDir   = lastArg.parent.outDir || p.join(APP_DIR, '{lang}/generated/');
19
  var logDir   = lastArg.parent.logDir || p.join(APP_DIR, '{lang}/log/');
20
  var quiet    = lastArg.parent.quiet || false;
21
 
22
  var swaggerCodegen = lastArg.parent.jar ? util.format('java -jar %s', lastArg.parent.jar) : 'swagger-codegen';
23
 
24
  var params = {
25
    apiSpec: apiSpec,
26
    appDir: APP_DIR,
27
    confFile: confFile,
28
    outDir: outDir,
29
    swaggerCodegen: swaggerCodegen,
30
  };
31
 
32
  var opts = {
33
    logDir: logDir,
34
    swaggycDir: SWAGGYC_DIR,
35
    quiet: quiet
36
  };
37
 
38
  function errorCb(err) {
39
    if (err.code && !isNaN(err.code)) {
40
      console.error('%s | exit code %d', 'FAILURE'.red, err.code);
41
      process.exit(err.code);
42
    } else {
43
      console.error('%s | %s', 'ERROR'.red, err.message);
44
    }
45
  }
46
 
47
  function successCb() {
48
    console.log('%s | exit code 0', 'SUCCESS'.green);
49
  }
50
 
51
  var swaggyC = new SwaggyC(params, opts);
52
  swaggyC.run(tasks, cli.exitCb(errorCb, successCb));
53
}
54
 
55
/**
56
 * Execute Swaggy-C CLI.
57
 */
58
function exec() {
59
 
60
  var commandFile = p.join(SWAGGYC_DIR, 'conf', 'commands.json');
61
  var commands    = Object.keys(JSON.parse(fs.readFileSync(commandFile)).commands);
62
 
63
  var actions = { commands: {} };
64
  commands.forEach(function (command) {
65
    actions.commands[command] = { action: _exec };
66
  });
67
 
68
  cli.command(__dirname, actions);
69
}
70
 
71
exports.exec = exec;