// new.js
//

'use strict';

var createError  = require('../createError');
var colliderfile = require('../colliderfile');
var logErrorExit = require('../logErrorExit');
var pkg = require('../../package.json');

var fs   = require('fs');
var path = require('path');
var tar  = require('tar-fs');
var zlib = require('zlib');

var docopt = require('docopt').docopt;
var ProgressBar = require('progress');
var request = require('request');

var cwd = process.cwd();

// NEW
//

var doc = `
usage:
  collider new [--author <author-name>] <project-name>

options:
  --author <author-name>  Set an author name [default: unknown].
`;

module.exports = function (argv) {

  var args = docopt(doc, {
    argv: argv,
    version: `Collider CLI ${pkg.version}`,
  });

  var projectName = args['<project-name>'];
  var author      = args['--author'];

  var projectPath = path.join(cwd, projectName);

  // Create a new directory at 'projectPath'.
  fs.mkdir(projectPath, function (err) {

    // Set a default error message.
    var errMsg = `there was a problem creating "${projectName}" inside of "${cwd}".`;

    if (err) {

      // Using the error code, make the default error message more useful.
      switch (err.code) {
        case 'EEXIST':
          errMsg = `a file or directory named "${projectName}" already exists in "${cwd}".`;
          break;

        default:
      }

      var err = createError(errMsg);
      logErrorExit(err, true);

    } else {

      downloadCollider(projectPath, function () {
        colliderfile.update({
          name: projectName,
          createdTime: Date.now(),
          author: author,
          matterLibs: [],
        }, projectPath);
      });

    }

  });

};

function downloadCollider(destination, cb) {

  // Stream the request for the latest Collider archive.
  request('http://getcollider.com/latest.tar.gz', {
    headers: { 'User-Agent': `collider-cli/${pkg.version}`, },
    timeout: 10000,
  })
    .on('response', function (response) {

      // Only create and bind events to a progress bar if we're within a TTY context.
      // - Prevents "TypeError: this.stream.clearLine is not a function" etc. in testing.
      if (process.stdout.isTTY) {
        var contentLength = Number(response.headers['content-length']);
        var progressBar = new ProgressBar('Downloading [:bar] :percent ETA :etas ', {
          total: contentLength,
          incomplete: ' ',
          clear: true,
        });

        // Response events.
        //

        response.on('data', function (chunk) {
          progressBar.tick(chunk.length);
        });
      }

      response.on('error', function (err) {
        throw err;
      });
    })

    // request 'error'
    .on('error', function (err) {
      throw err;
    })

  // Gunzip.
  .pipe(zlib.createGunzip())
    .on('error', function (err) {
      throw err;
    })

  // Untar to 'destination'.
  .pipe(tar.extract(destination))
    .on('error', function (err) {
      throw err;
    })
    .on('finish', function () {
      console.log(`Download complete. Your new project is located at "${destination}".`);
      if (typeof cb === 'function') {
        cb();
      }
    });
}

@@ -434,34 +434,8 @@
 );%0A%0A
-var cwd = process.cwd();%0A%0A
 // N
@@ -625,16 +625,25 @@
 on (argv
+, dir, cb
 ) %7B%0A%0A  v
@@ -846,19 +846,19 @@
 th.join(
-cwd
+dir
 , projec
@@ -1063,27 +1063,27 @@
 nside of %22$%7B
-cwd
+dir
 %7D%22.%60;%0A%0A    i
@@ -1305,11 +1305,11 @@
  %22$%7B
-cwd
+dir
 %7D%22.%60
@@ -1633,16 +1633,16 @@
 bs: %5B%5D,%0A
-
         
@@ -1655,16 +1655,113 @@
 jectPath
+, function () %7B%0A          if (typeof cb === 'function') %7B%0A            cb();%0A          %7D%0A        %7D
 );%0A     

Replace 'cwd' with 'dir' arg and add a callback