# Code coverage report for lib/config/exec.js

## Statements: 85.42% (41 / 48) Branches: 78.33% (47 / 60) Functions: 100% (3 / 3) Lines: 85.42% (41 / 48)

All files » lib/config/ » exec.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
    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
    1271
    1

    1

    1
    43
    33

    43

    43

    43
    3
    3

    43
    34

    9
    3
    3

    43

    43
    7
    7
    7

    43
    3

    43
    7

    7

    7
    3

    43

    7
    7

    7

    43

    12

    36

    43
    43

    43
    5
    38

    43
     'use strict';
    var path = require('path'),
        utils = require('../utils');

    module.exports = exec;

    /**
     * Discovers all the options required to run the script
     * and if a custom exec has been passed in, then it will
     * also try to work out what extensions to monitor and
     * whether there's a special way of running that script.
     *
     * @param  {Object} nodemonOptions
     * @param  {Object} execMap
     * @return {Object} new and updated version of nodemonOptions
     */
    function exec(nodemonOptions, execMap) {
      if (!execMap) {
        execMap = {};
      }

      var options = utils.clone(nodemonOptions || {}),
          script = path.basename(options.script || ''),
          scriptExt = path.extname(script).slice(1),
          extension = options.ext || scriptExt || 'js',
          execDefined = !!options.exec;

      // strip any leading periods int he extension
      Iif (extension.indexOf('.') === 0) {
        extension = extension.slice(1);
      }

      // allows the user to simplify cli usage: https://github.com/remy/nodemon/issues/195
      // but always give preference to the user defined argument
      if (!options.exec && execMap[scriptExt] !== undefined) {
        options.exec = execMap[scriptExt];
        execDefined = true;
      }

      if (options.exec === undefined) {
        options.exec = 'node';
      } else {
        // allow variable substitution for {{filename}} and {{pwd}}
        options.exec = (options.exec || '').replace(/\{\{(filename|pwd)\}\}/, function (all, m) {
          Eif (m === 'filename') {
            return script || '';
          } else if (m === 'pwd') {
            return process.cwd() || '';
          }
          return all;
        });
      }

      options.execArgs = [];

      if (options.exec.indexOf(' ') !== -1) {
        var execOptions = options.exec.split(' ');
        options.exec = execOptions.splice(0, 1)[0];
        options.execArgs = execOptions;
      }

      if (options.exec === 'node' && options.nodeArgs && options.nodeArgs.length) {
        options.execArgs = options.execArgs.concat(options.nodeArgs);
      }

      // note: indexOf('coffee') handles both .coffee and .litcoffee
      if (!execDefined && options.exec === 'node' && scriptExt.indexOf('coffee') !== -1) {
        options.exec = 'coffee';
        // ensure that we call: `coffee --nodejs ...`
        Iif (options.execArgs === undefined) {
          options.execArgs = [];
        }

        // if there's a leading argument to the exec that starts with `--` then
        // it could be --debug or --debug-brk or something else intended for node
        // so we'll add the --nodejs switch.
        if ((options.args || []).join(' ').indexOf('--') === 0) {
          options.execArgs.unshift('--nodejs');
        }
      }

      if (options.exec === 'coffee') {
        // don't override user specified extension tracking
        Eif (!options.ext) {
          extension = 'coffee litcoffee js';
        }

        // because windows can't find 'coffee', it needs the real file 'coffee.cmd'
        Iif (utils.isWindows) {
          options.exec += '.cmd';
        }
      }

      // allow users to make a mistake on the extension to monitor
      // converts js,jade => .js$|.jade$
      // and 'js jade' => .js$|.jade$
      // BIG NOTE: user can't do this: nodemon -e *.js
      // because the terminal will automatically expand the glob against
      // the file system :(
      if (extension.indexOf(' ') !== -1 ||
          extension.indexOf(',') !== -1 ||
          extension.indexOf('*.') !== -1) {

        extension = extension.replace(/\s+/g, '|') // convert spaces to pipes
          .replace(/,/g, '|') // convert commas to pipes
          .split('|') // split on those pipes
          .map(function (item) {
            return item.replace(/^[\*\.]+/, ''); // remove "*."
          }).join(','); // return regexp string like: .js$|.jade$
      }

      // this final part ensures both multiple extension and
      // single extensions work
      // extension += '$';

      options.ext = extension;
      options.env = {};

      // make sure it's an object (and since we don't have )
      if (({}).toString.apply(nodemonOptions.env) === '[object Object]') {
        options.env = utils.clone(nodemonOptions.env);
      } else Iif (nodemonOptions.env !== undefined) {
        throw new Error('nodemon env values must be an object: { PORT: 8000 }');
      }

      return options;
    }