
var fs = require('fs');
var path = require('path');

function isFile(filename)
{
    try {
        var stats = fs.lstatSync(filename);
        return stats.isFile();
    }
    catch (err)
    {
        return false;
    }
}

function isDirectory(filename)
{
    try {
        var stats = fs.lstatSync(filename);
        return stats.isDirectory();
    }
    catch (err)
    {
        return false;
    }
}

function createDirectory()
{
	var dirpath;
    
    for (var k = 0; k < arguments.length; k++) {
        if (k)
            dirpath = path.join(dirpath, arguments[k]);
        else
            dirpath = arguments[k];
            
        if (!isDirectory(dirpath))
            fs.mkdirSync(dirpath);
    }
}

function copyDirectory(source, target, cb) {
    createDirectory(target);
    
    fs.readdir(source, function (err, filenames) {
        if (err) {
            cb(err);
            return;
        }
        
        processFile();
        
        function processFile() {
            if (!filenames.length) {
                cb();
                return;
            }
            
            var filename = filenames.shift();
            var sourcefilename = path.join(source, filename);
            var targetfilename = path.join(target, filename);
            
            if (isFile(sourcefilename))
                copyFile(sourcefilename, targetfilename, function (err, result) {
                    if (err)
                        cb(err);
                    else
                        processFile();
                });
            else
                copyDirectory(sourcefilename, targetfilename, function (err, result) {
                    if (err)
                        cb(err);
                    else
                        processFile();
                });
        }            
    });
}

// from http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js

function copyFile(source, target, cb) {
    var cbCalled = false;

    var rd = fs.createReadStream(source);
    
    rd.on("error", function(err) {
        done(err);
    });
    
    var wr = fs.createWriteStream(target);
    
    wr.on("error", function(err) {
        done(err);
    });
    
    wr.on("close", function(ex) {
        done();
    });
    
    rd.pipe(wr);

    function done(err) {
        if (!cbCalled) {
            cb(err);
            cbCalled = true;
        }
    }
}

module.exports = {
    isFile: isFile,
    isDirectory: isDirectory,
    createDirectory: createDirectory,
    copyDirectory: copyDirectory,
    copyFile: copyFile,
    exists: function (target) { return fs.existsSync(target); }
};


@@ -745,29 +745,173 @@
 ce, target, 
-cb) %7B
+options, cb) %7B%0A    if (!cb && typeof options === 'function') %7B%0A        cb = options;%0A        options = null;%0A    %7D%0A    %0A    options = options %7C%7C %7B%7D;%0A
 %0A    createD
@@ -1509,32 +1509,41 @@
  targetfilename,
+ options,
  function (err, 
@@ -1776,16 +1776,25 @@
 ilename,
+ options,
  functio
@@ -2113,21 +2113,250 @@
 target, 
-cb) %7B
+options, cb) %7B%0A    if (!cb && typeof options === 'function') %7B%0A        cb = options;%0A        options = null;%0A    %7D%0A    %0A    options = options %7C%7C %7B%7D;%0A    %0A    if (options.noreplace && exists(target))%0A        return cb(null, null);%0A    
 %0A    var

Copy file with noreplace option