All files / lib cli.js

100% Statements 82/82
100% Branches 15/15
100% Functions 4/4
100% Lines 82/82

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 822x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 1x 1x 1x 1x 1x 2x 4x 4x 2x 2x 4x 4x 4x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 3x 3x 4x 4x 4x 4x 29x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x
"use strict";
import Bob from './bob.js';
import cli from 'bagofcli';
import fs from 'fs';
import os from 'os';
import p from 'path';
 
const APP_DIR = process.cwd(),
  DIRNAME = p.dirname(import.meta.url).replace('file://', ''),
  BOB_DIR = p.join(DIRNAME, '..');
 
function _exec() {
 
  const commandAndFirstTaskArg = arguments[0],
    remainingTasksArg = arguments[1] || [],
    quiet = commandAndFirstTaskArg.parent && commandAndFirstTaskArg.parent.quiet,
    tasks = [commandAndFirstTaskArg._name].concat(remainingTasksArg),
    pkgFile = p.join(APP_DIR, 'package.json'),
    pkg = JSON.parse(fs.readFileSync(pkgFile)),
    bob = new Bob({
      appDir: APP_DIR,
      appName: pkg.name,
      appVersion: pkg.version,
      bobDir: BOB_DIR,
      bobMode: process.env.BOB_MODE || 'human',
      maxBufferInKb: 1000,
      tmpDir: p.join(os.tmpdir(), '.bob', pkg.name),
      quiet: quiet
    });
 
  function errorCb(err) {
    if (err.code && !isNaN(err.code)) {
      console.error('%s | exit code %d', 'FAILURE'.red, err.code);
      process.exit(err.code);
    } else {
      console.error('%s | %s', 'ERROR'.red, err.message);
    }
  }
 
  function successCb() {
    console.log('%s | exit code 0', 'SUCCESS'.green);
  }
 
  bob.build(tasks, cli.exitCb(errorCb, successCb));
}
 
/**
 * Execute Bob CLI.
 */
function exec() {
 
  const commandFile = p.join(BOB_DIR, 'conf', 'commands.json'),
    commands = Object.keys(JSON.parse(fs.readFileSync(commandFile)).commands),
    configFile = p.join(APP_DIR, '.bob.json'),
    config = fs.existsSync(configFile) ? JSON.parse(fs.readFileSync(configFile)) : {},
    actions = { commands: {} };
 
  // replace aliases with tasks
  let argv = process.argv.slice(0, 2);
  process.argv.slice(2, process.argv.length).forEach((arg) => {
    if (Object.keys(config).indexOf(arg) !== -1 && commands.indexOf(arg) === -1) {
      argv = argv.concat(config[arg].split(' '));
    } else {
      argv.push(arg);
    }
  });
  process.argv = argv;
 
  commands.forEach((command) => {
    actions.commands[command] = { action: _exec };
  });
 
  cli.command(DIRNAME, actions);
}
 
const exports = {
  exec: exec
};
 
export {
  exports as default
};