Code coverage report for lib/installer.js

# All files / lib installer.js

100% Statements 116/116
58.82% Branches 50/85
0% Functions 0/1
100% Lines 116/116

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    1161x
    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
    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
    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
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1xconst debug = require('debug')('tabtab:installer');
    const fs = require('fs');
    const path = require('path');
    const untildify = require('untildify');
    const { promisify } = require('util');

    const readFile = promisify(fs.readFile);
    const writeFile = promisify(fs.writeFile);
    const { BASH_LOCATION, FISH_LOCATION, ZSH_LOCATION } = require('./constants');

    const shellExtension = location => {
      if (location === BASH_LOCATION) return 'bash';
      if (location === FISH_LOCATION) return 'fish';
      if (location === ZSH_LOCATION) return 'zsh';
    };

    const scriptFromLocation = location => {
      if (location === BASH_LOCATION) {
        return path.join(__dirname, '../scripts/bash.sh');
      }

      if (location === FISH_LOCATION) {
        return path.join(__dirname, '../scripts/fish.sh');
      }

      if (location === ZSH_LOCATION) {
        return path.join(__dirname, '../scripts/zsh.sh');
      }
    };

    const writeToShellConfig = ({ name, location }) => {
      debug(`Adding tabtab script to ${location}`);
      const filename = path.join(
        __dirname,
        '../.completions',
        `${name}.${shellExtension(location)}`
      );
      debug('Which filename', filename);

      return new Promise((resolve, reject) => {
        const stream = fs.createWriteStream(untildify(location), { flags: 'a' });
        stream.on('error', reject);
        stream.on('finish', () => resolve());

        debug('Writing to shell configuration file (%s)', location);
        stream.write(`\n# tabtab source for ${name} package`);
        stream.write('\n# uninstall by removing these lines');

        if (location === BASH_LOCATION) {
          stream.write(`\n[ -f ${filename} ] && . ${filename} || true`);
        } else if (location === FISH_LOCATION) {
          debug('Addding fish line');
          stream.write(`\n[ -f ${filename} ]; and . ${filename}; or true`);
        } else if (location === ZSH_LOCATION) {
          debug('Addding zsh line');
          stream.write(`\n[[ -f ${filename} ]] && . ${filename} || true`);
        }

        stream.end('\n');
      });
    };

    const writeToCompletionScript = ({ name, completer, location }) => {
      const filename = path.join(
        __dirname,
        '../.completions',
        `${name}.${shellExtension(location)}`
      );

      const script = scriptFromLocation(location);
      debug('Writing completion script to', filename);
      debug('with', script);

      return readFile(script, 'utf8')
        .then(filecontent =>
          filecontent
            .replace(/\{pkgname\}/g, name)
            .replace(/{completer}/g, completer)
            // on Bash on windows, we need to make sure to remove any \r
            .replace(/\r?\n/g, '\n')
        )
        .then(filecontent => writeFile(filename, filecontent));
    };

    const installer = {
      install(options = { name: '', completer: '', location: '' }) {
        debug('Install with options', options);
        if (!options.name) {
          throw new TypeError('options.name is required');
        }

        if (!options.completer) {
          throw new TypeError('options.completer is required');
        }

        if (!options.location) {
          throw new TypeError('options.location is required');
        }

        return Promise.all([
          writeToShellConfig(options),
          writeToCompletionScript(options)
        ]);
      },

      uninstall(options = { name: '' }) {
        debug('Uninstall with options', options);
        throw new Error('Not yet implemented');
      },

      writeToShellConfig,
      writeToCompletionScript
    };

    module.exports = installer;