#!/usr/bin/env node
'use strict';
const base = require('./lib/index');
const prompt = require('prompt');

function catchInputHandlerErr(err) {
  console.error('Input Handler Error: ', err);
}

const savePrompts = {
  desktopFilePath: (error, win, stdout) => {
    return new Promise((fulfill, reject) => {
      function askForVal(displayEntries) {
        // autosave first entry for now
        if (displayEntries && displayEntries[0]) {
          fulfill(displayEntries[0]);
        } else {
          reject('No input for desktop file path');
        }
      }

      if (error) {
        if (stdout && stdout.split) {
          askForVal(stdout.split('\n'));
        } else {
          askForVal();
        }
      } else {
        const displayEntries = stdout.split('\n');
        let displayStr = '';
        for (let i = 0; i < displayEntries.length; i++) {
          if (displayEntries[i] !== '') {
            displayStr += `${i + 1}. ${displayEntries[i]} \n`;
          }
        }

        askForVal(displayEntries);
      }
    }).catch(catchInputHandlerErr);
  }
};

const isCloseAllWinBefore = process.argv.indexOf('--closeAllOpenWindows') > -1;
const action = process.argv[2];
let sessionName;
if (process.argv[3] && !process.argv[3].match(/^--/)) {
  sessionName = process.argv[3];
}

if (action === 'save') {
  base.saveSession(sessionName, savePrompts);
} else if (action === 'restore') {
  base.restoreSession(sessionName, isCloseAllWinBefore);
} else if (action === 'remove') {
  base.removeSession(sessionName);
} else if (action === 'resetCfg') {
  base.resetCfg();
} else {
  console.log(`
  Usage:\n
  Saving your current windows:
  lwsm save [OPTIONAL_SESSION_ID] 
  
  Restoring a session:
  lwsm restore [OPTIONAL_SESSION_ID] [--closeAllOpenWindows]
  `);
}
@@ -711,32 +711,60 @@
   %7D%0A      %7D else
+ if (stdout && stdout.split)
  %7B%0A        const
@@ -1051,16 +1051,52 @@
 tries);%0A
+      %7D else %7B%0A        askForVal();%0A
       %7D%0A

make cmd input handler for desktop file path more robust 2