# Code coverage report for lib/cli.js

## Statements: 63.16% (36 / 57) Branches: 40.91% (9 / 22) Functions: 37.5% (3 / 8) Lines: 66.04% (35 / 53) Ignored: none

All files » 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
    84
    85
    86
    87
    88
    89
    901
    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
    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 updateNotifier = require('update-notifier');
    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 highlightClient = 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 = function (options) {
      opts = _(opts).merge(options || {}).toObject();
      return util.getUserDir().catch(Promise.resolve()).then(function (userDir) {
        opts.logger = _(userDir ? {dir: userDir} : {}).merge(opts.logger).toObject();

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

        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)
              .tap(function (pane) { pane.editor.readOnly(true); })
              .done();
            fs.createReadStream(path.join(__dirname, '..', 'default-config.ini')).pipe(fs.createWriteStream(path.join(userDir, 'config')));
          }
        }

        updateNotifier({pkg: package}).notify();

        return slap.ready.disposer(function (slap) { slap.quit(); });
      });
    };