#!/usr/bin/env node
var shell = require('shelljs');
var parseArgs = require('./parseArgs');
var printCmdRet = require('./printCmdRet');

var EXIT_CODES = {
  SHX_ERROR: 27, // https://xkcd.com/221/
  CMD_FAILED: 1, // TODO: Once shelljs/shelljs#269 lands, use `error()`
  SUCCESS: 0,
};

var CMD_BLACKLIST = [
  'cd',
  'pushd',
  'popd',
  'dirs',
  'set',
  'exit',
  'exec',
];

function shx(argv) {
  if (argv.length < 3) {
    console.error('Error: Missing ShellJS command name');
    return EXIT_CODES.SHX_ERROR;
  }

  var cmds = parseArgs(argv.slice(2));

  // If something fails, we want to execute all the commands, but still error at the end.
  var failed = false;

  for (var i = 0; i < cmds.length; ++i) {
    if (!cmds[i]) continue; // If you did foo;bar;baz;
    var cmd = shell[cmds[i][0]];
    if (!cmd || typeof cmd !== 'function') {
      console.error('Error: Invalid ShellJS command: ' + cmd);
      return EXIT_CODES.SHX_ERROR;
    } else if (CMD_BLACKLIST.indexOf(cmd) > -1) {
      console.error('Warning: shx ' + cmd + ' is not supported');
      return EXIT_CODES.SHX_ERROR;
    }

    var ret = cmd.apply(shell, cmds[i].slice(1));
    if (cmd.name !== '_echo') {
      printCmdRet(ret);
    }
    if (shell.error() || (ret && ret.code && ret.code !== 0)) failed = true;
  }

  return failed ? EXIT_CODES.CMD_FAILED : EXIT_CODES.SUCCESS;
}

module.exports = shx;

@@ -17,28 +17,26 @@
 ode%0A
-var
+import
  shell 
-= require(
+from 
 'she
@@ -44,54 +44,16 @@
 ljs'
-)
 ;%0A
-var parseArgs = require('./parseArgs');%0Avar
+import
  pri
@@ -65,18 +65,13 @@
 Ret 
-= require(
+from 
 './p
@@ -85,15 +85,16 @@
 Ret'
-)
 ;%0A%0A
-var
+const
  EXI
@@ -237,19 +237,21 @@
  0,%0A%7D;%0A%0A
-var
+const
  CMD_BLA
@@ -337,50 +337,88 @@
 %5D;%0A%0A
-function
+const
  shx
+ = 
 (argv)
+ =%3E
  %7B%0A  
-if (argv.length %3C 3
+const %5BfnName, ...args%5D = argv.slice(2);%0A  if (!fnName
 ) %7B%0A
@@ -519,315 +519,54 @@
 %0A%0A  
-var cmds = parseArgs(argv.slice(2));%0A%0A  // If something fails, we want to execute all the commands, but still error at the end.%0A  var failed = false;%0A%0A  for (var i = 0; i %3C cmds.length; ++i) %7B%0A    if (!cmds%5Bi%5D) continue; // If you did foo;bar;baz;%0A    var cmd = shell%5Bcmds%5Bi%5D%5B0%5D%5D;%0A    if (!cmd %7C%7C typeof cmd
+// validate command%0A  if (typeof shell%5BfnName%5D
  !==
@@ -576,26 +576,24 @@
 unction') %7B%0A
-  
     console.
@@ -594,25 +594,25 @@
 nsole.error(
-'
+%60
 Error: Inval
@@ -635,20 +635,22 @@
 nd: 
-' + cmd
+$%7BfnName%7D.%60
 );%0A
-  
     
@@ -678,18 +678,16 @@
 _ERROR;%0A
-  
   %7D else
@@ -711,29 +711,26 @@
 T.in
-dexOf(cmd) %3E -1
+cludes(fnName)
 ) %7B%0A
-  
     
@@ -743,17 +743,17 @@
 e.error(
-'
+%60
 Warning:
@@ -761,19 +761,17 @@
 shx 
-' + cmd + '
+$%7BfnName%7D
  is 
@@ -783,22 +783,20 @@
 upported
-'
+%60
 );%0A
-  
     retu
@@ -824,78 +824,141 @@
 OR;%0A
-  
   %7D%0A%0A  
-  var
+const
  ret = 
-cmd.apply(shell, cmds%5Bi%5D.slice(1));%0A  
+shell%5BfnName%5D(...args);%0A  const code = ret.hasOwnProperty('code') && ret.code;%0A%0A  // echo already prints%0A
   if (
-cmd.n
+fnN
 ame 
@@ -966,23 +966,14 @@
 == '
-_
 echo')
- %7B%0A     
  pri
@@ -991,113 +991,100 @@
 t);%0A
-    %7D%0A    if (shell.error() %7C%7C (ret && ret.code && ret.code !== 0)) failed = true;%0A  %7D%0A%0A  return failed ?
+%0A  if (typeof code === 'number') %7B%0A    return code;%0A  %7D else if (shell.error()) %7B%0A    return
  EXI
@@ -1105,10 +1105,23 @@
 ILED
- :
+;%0A  %7D%0A%0A  return
  EXI
@@ -1142,26 +1142,25 @@
 S;%0A%7D
+;
 %0A%0A
-module.exports =
+export default
  shx

update shx to es6