# Code coverage report for slap/lib/cli.js

## Statements: 62.26% (33 / 53) Branches: 40% (8 / 20) Functions: 28.57% (2 / 7) Lines: 65.96% (31 / 47) Ignored: none

All files » slap/lib/ » cli.js

    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
    841
    1
    1
    1
    1
    1
    1

    1
    1
    1
    1
    1

    1
    1
    1
    1

    1

    1

    1

    1

    1

    1

    1
    1
    1

    1

    1
    1
    1

    1

    1

     var _ = require('lazy.js');
    var path = require('path');
    var iconv = require('iconv-lite');
    var rc = require('rc');
    var Promise = require('bluebird');
    var fork = require('child_process').fork;
    var fs = Promise.promisifyAll(require('fs'));

    var Slap = require('./ui/Slap');
    var Pane = require('./ui/Pane');
    var util = require('./util');
    var logger = require('./logger');
    var getHighlightClient = require('./highlight/client');

    var baseDir = path.join(__dirname, '..');
    var package = require('../package');
    var configFile = path.join(baseDir, package.name + '.ini');
    var opts = util.parseOpts(rc(package.name, configFile));

    // special invocation modes
    Iif (opts.h || opts.help) {
      var command = process.argv[1];
      if (!process.env.PATH.split(path.delimiter).some(function (binPath) {
        var newCommand = path.relative(binPath, command);
        if (path.dirname(newCommand) === '.') {
          command = newCommand;
          return true;
        }
      })) {
        command = path.relative(process.cwd(), command);
        if (path.dirname(command) === '.') command = '.' + path.sep + command;
      }

      console.error([
        "Usage: " + command + " [options...] [<file1> [<file2> [...]]]",
        "",
        package.description,
        "",
        "To see what options are available, run `" + command + " " + configFile + "`.",
        "Example: `" + command + " --logger.level debug file.c`."
      ].join("\n"));

      return process.exit(0);
    }

    Iif (opts.perf.profile && process.execArgv.indexOf('--prof') === -1) {
      var cp = fork(process.argv[1], process.argv.slice(2), {
        stdio: 'inherit',
        execArgv: ['--prof'].concat(process.execArgv)
      });
      cp.on('exit', function (code) { process.exit(code); });
      cp.on('error', function (err) { process.exit(8); });
      return;
    }

    module.exports = Promise.all([
      util.getUserDir().catch(function (err) {}),
      getHighlightClient
    ]).spread(function (userDir, highlightClient) {
      opts.logger = _(userDir ? {dir: userDir} : {}).merge(opts.logger).toObject();

      logger(opts.logger); highlightClient.send({type: 'logger', options: opts.logger});

      iconv.extendNodeEncodings();

      logger.info('starting...');
      logger.verbose('configuration:', opts);
      var slap = new Slap(opts);

      Promise.all(opts._.map(function (path, i) { return slap.open(path.toString(), !i); })).done();

      Eif (!opts._.length) { // if no files are passed
        new Pane().setCurrent(); // open a new empty file
        Iif (!opts.config) { // first run without a file passed
          slap.open(path.join(baseDir, 'README.md'), true).done();
          fs.openAsync(path.join(userDir, 'config'), 'a'); // touch user config file if it doesn't exist
        }
      }

      process.nextTick(function () { slap.update(); });

      return slap;
    });