All files / src/commands local.js

30.43% Statements 7/23
0% Branches 0/2
0% Functions 0/2
30.43% Lines 7/23
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 481x           1x 1x   1x 1x   1x   1x                                                                  
module.exports.usage = `Run a service locally
 
Usage: api local <endpoint> [arg1=val1, arg2=val2...argn=valn]
 
Runs your api locally. Useful for testing changes before deploying`;
 
module.exports.category = 'using';
module.exports.weight = 2;
 
const utils = require('../utils/utils');
const handler = require('../utils/handler');
 
module.exports.aliases = ['invoke-local', 'dev'];
 
module.exports.run = (args) => {
  const data = {};
  const passedData = utils.parseArgs(args.splice(2));
  for (const arg of passedData) {
    const i = arg.indexOf('=');
    const parsedArg = [arg.slice(0, i), arg.slice(i + 1)];
    let value = parsedArg[1];
    try {
      value = JSON.parse(parsedArg[1]);
    } catch (e) {
      // Already in proper format
      // console.log(e);
    }
    data[parsedArg[0]] = value;
  }
 
  const errors = utils.buildErrors();
 
  const event = {
    name: args[1],
    data,
    errors,
  };
 
  handler.go(event, undefined, (err, response) => {
    if (err) {
      const parsedError = JSON.parse(err);
      console.log(parsedError);
    } else {
      console.log(response);
    }
  });
};