All files / src/commands help.js

90.2% Statements 46/51
80.77% Branches 21/26
100% Functions 8/8
91.11% Lines 41/45
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 991x 1x 1x   1x 1x   1x 1x   1x     17x 17x 17x                   1x 1x     1x 2x 2x   2x 1x 1x     1x                             15x   1x 17x 15x             1x 3x 3x 3x 26x 15x     1x 1x 1x     1x 1x         1x   1x         1x 1x 1x     1x      
module.exports.usage = 'Displays this usage information';
module.exports.category = 'utility';
module.exports.weight = 1;
 
const fs = require('fs');
const path = require('path');
 
const console = require('../utils/console');
const exit = require('../utils/exit');
 
const commands = fs.readdirSync(__dirname).map(getAliases).filter(Boolean);
 
function getAliases(file) {
  Iif (!file.match(/.js$/)) return undefined;
  const command = require(path.join(__dirname, file));
  return {
    cmd: file.replace('.js', ''),
    shortUsage: (command.usage || '').split('\n')[0],
    usage: (command.usage || ''),
    category: (command.category || ''),
    weight: (command.weight || 100),
  };
}
 
function singleUsage(command) {
  console.log(command.usage || `\`${command.cmd}\` Not yet documented`);
  return exit(0);
}
 
module.exports.run = (args) => {
  console.log('');
  console.log('Usage: api <command>');
 
  if (args && args[1]) {
    const command = commands.find(cmd => args[1] === cmd.cmd);
    Eif (command) return singleUsage(command);
  }
 
  const categories = {
    basic: {
      desc: 'Commands for getting started',
      commands: [],
    },
    using: {
      desc: 'Run APIs in the command line',
      commands: [],
    },
    utility: {
      desc: 'Utility functions',
      commands: [],
    },
  };
 
  const pad = text => `${text}               `.substr(0, 15);
 
  commands.forEach((cmd) => {
    if (cmd.category) {
      categories[cmd.category].commands.push({
        text: `  ${'$'.grey} ${pad(`api ${cmd.cmd}`)} ${cmd.shortUsage ? cmd.shortUsage.grey : ''}`,
        weight: cmd.weight,
      });
    }
  });
 
  for (const cat in categories) {
    const category = categories[cat];
    console.log('');
    console.log(category.desc);
    category.commands.sort((a, b) => a.weight - b.weight);
    category.commands.forEach(command => console.log(command.text));
  }
 
  console.log('');
  console.log(`Learn more with ${'api help <command>'.yellow}`.grey);
  console.log('');
 
  let existingPackageJson;
  try {
    existingPackageJson = require(path.join(process.cwd(), 'package.json'));
  } catch (e) {
    existingPackageJson = {};
  }
 
  const alreadySetup = existingPackageJson.dependencies && existingPackageJson.dependencies.api;
 
  Iif (alreadySetup) {
    console.log('Ready to deploy?'.green);
    console.log(`Run ${'api deploy'.yellow} to upload your API!`);
    console.log('');
  } else {
    console.log('Just getting started?'.green);
    console.log(`Run ${'api init'.yellow} to set up your new API!`);
    console.log('');
  }
 
  return exit(0);
};