#!/usr/bin/env node

// Check that the API key is set up
const KEY = process.env.THETVDB_API_KEY;
if (!KEY) {
  console.log("You should set up 'THETVDB_API_KEY' env variable. Go here to get it: http://thetvdb.com/?tab=apiregister");
  process.exit(1);
}

// Take arguments
const cmd = process.argv[2] || "remained";
const args = process.argv.slice(3);

// Available commands (array means aliases)
const commands = [
  ["lookup", "search"],
  "add",
  "list",
  ["view", "show"],
  "watch",
  ["watch-till", "wt"],
  ["rm", "remove"],
  "update",
  "remained",
  "help"
];

// Do we have this command?
let mod = commands.find((cmds) => {
  return Array.isArray(cmds) && cmds.indexOf(cmd) > -1 || cmds == cmd;
}) || "help";

// Let's figure out module
mod = Array.isArray(mod) ? mod[0] : mod;

// Require it and call the command
require(`./cmd/${mod}`)(...args);

@@ -21,53 +21,368 @@
 %0A// 
-Check that the API key is set up%0Aconst KEY = 
+Available commands (array for aliases)%0Aconst CMDS = %5B%0A  %5B%22lookup%22, %22search%22%5D,%0A  %22add%22,%0A  %22list%22,%0A  %5B%22view%22, %22show%22%5D,%0A  %22watch%22,%0A  %5B%22watch-till%22, %22wt%22%5D,%0A  %5B%22rm%22, %22remove%22%5D,%0A  %22update%22,%0A  %22remained%22,%0A  %22help%22%0A%5D;%0A%0AensureTvdbKey();%0Aconst %5Bcmd, args%5D = readArgs();%0Aconst mod = figureModule(cmd);%0Arequire(%60./cmd/$%7Bmod%7D%60)(...args);%0A%0Afunction ensureTvdbKey() %7B%0A  if (!
 proc
@@ -408,22 +408,14 @@
 _KEY
-;%0Aif (!KEY
 ) %7B%0A
+  
   co
@@ -429,25 +429,18 @@
 og(%22
-You should set up
+Please add
  'TH
@@ -472,25 +472,27 @@
 le. 
-Go here to
+You can
  get it
+ here
 : ht
@@ -528,16 +528,18 @@
 ster%22);%0A
+  
   proces
@@ -553,29 +553,39 @@
 1);%0A
-%7D%0A%0A// Take arguments%0A
+  %7D%0A%7D%0A%0Afunction readArgs() %7B%0A  
 cons
@@ -623,16 +623,18 @@
 ained%22;%0A
+  
 const ar
@@ -665,275 +665,79 @@
 3);%0A
-%0A// Available commands (array means aliases)%0Aconst commands = %5B%0A  %5B%22lookup%22, %22search%22%5D,%0A  %22add%22,%0A  %22list%22,%0A  %5B%22view%22, %22show%22%5D,%0A  %22watch%22,%0A  %5B%22watch-till%22, %22wt%22%5D,%0A  %5B%22rm%22, %22remove%22%5D,%0A  %22update%22,%0A  %22remained%22,%0A  %22help%22%0A%5D;%0A%0A// Do we have this command?%0Alet mod = commands
+  return %5B cmd, args %5D;%0A%7D%0A%0Afunction figureModule() %7B%0A  const mod = CMDS
 .fin
@@ -750,16 +750,18 @@
 s) =%3E %7B%0A
+  
   return
@@ -823,16 +823,18 @@
 == cmd;%0A
+  
 %7D) %7C%7C %22h
@@ -844,40 +844,16 @@
 %22;%0A%0A
-// Let's figure out module%0Amod =
+  return
  Arr
@@ -888,74 +888,6 @@
 od;%0A
-%0A// Require it and call the command%0Arequire(%60./cmd/$%7Bmod%7D%60)(...args);
+%7D
 %0A

Refactor main script