#!/usr/bin/env node

import chalk from 'chalk';
import meow from 'meow';
import {ncp} from 'ncp';
import pify from 'pify';
import rejectGitignore from '../lib/rejectGitignore';
import formatPath from '../lib/formatPath';
import prepareProblem from '../lib/prepareProblem';
import prepareSolution from '../lib/prepareSolution';

const problemify = async function (srcDir, destDir, cb) {
  // match root directory name without slashes
  const regex = /.*?\/*([^<>:"\/\\|?*]+)\/*$/;
  const rootDirname = srcDir.replace(regex, '$1');
  let fileCount = 0;
  const ncpOptions = {
    stopOnErr: true,
    filter: rejectGitignore,
    transform(fileReadable, fileWriteable) {
      let file = '';
      fileReadable.on('data', chunk => {
        file += chunk;
      });

      fileReadable.on('end', () => {
        fileWriteable.write(cb(file));
        const srcName = fileReadable.path;
        const srcIndex = srcName.lastIndexOf(rootDirname);
        const destName = fileWriteable.path;
        const destIndex = destName.lastIndexOf(rootDirname);
        console.log(`${++fileCount}. ${srcName.slice(srcIndex)} → ${destName.slice(destIndex)}`);
        fileWriteable.end();
      });

      fileReadable.on('error', err => console.error(err));
      fileWriteable.on('error', err => console.error(err));
    }
  };

  await pify(ncp)(srcDir, destDir, ncpOptions);
};

const cli = meow(`
  Usage
    $ problemify <directory>

  Example
    $ problemify map-to-luke
    $ ls
    map-to-luke  map-to-luke-problem  map-to-luke-solution
`);

(async () => {
  try {
    const cliInput = formatPath(cli.input[0]);
    const problemDest = `${cliInput}-problem`;
    const solutionDest = `${cliInput}-solution`;
    await problemify(cliInput, problemDest, prepareProblem);
    await problemify(cliInput, solutionDest, prepareSolution);
  } catch (err) {
    console.log(chalk.yellow("Something went wrong. See 'problemify --help' for usage information."));
    console.error(chalk.red(err.stack || err));
    process.exit(1);
  }
})();

@@ -1219,37 +1219,48 @@
  err =%3E 
-console.error(err)
+%7B%0A        throw err;%0A      %7D
 );%0A
+%0A
       fi
@@ -1294,26 +1294,36 @@
  =%3E 
-console.error(err)
+%7B%0A        throw err;%0A      %7D
 );%0A 
@@ -1597,16 +1597,128 @@
 iInput =
+ cli.input%5B0%5D;%0A    if (!cliInput) %7B%0A      throw new Error('Invalid input');%0A    %7D%0A%0A    const formattedCliInput =
  formatP
@@ -1724,25 +1724,21 @@
 Path(cli
-.i
+I
 nput
-%5B0%5D
 );%0A    c
@@ -1751,33 +1751,42 @@
 roblemDest = %60$%7B
-c
+formattedC
 liInput%7D-problem
@@ -1816,17 +1816,26 @@
 st = %60$%7B
-c
+formattedC
 liInput%7D
@@ -1859,33 +1859,42 @@
 wait problemify(
-c
+formattedC
 liInput, problem
@@ -1937,17 +1937,26 @@
 blemify(
-c
+formattedC
 liInput,

Add error handling for no input arguments