'use strict';
const gutil = require('gulp-util');
const through = require('through2');
const jsforce = require('jsforce');
const path = require('path');

const PLUGIN_NAME = 'dataload';

module.exports = (options,mode) =>{
  return through.obj((file, enc, callback) => {
    const conn = new jsforce.Connection({
      loginUrl : options.loginUrl
    });

    let objecttype;
    try {
      objecttype = file.name.match(/(.*)\.csv/)[1];
    } catch(err) {return callback(new gutil.PluginError(PLUGIN_NAME + ' ' + mode,'File name not parsable'));}

    conn.login(options.username, options.password, (error, userInfo) => {
      if (error) { return callback(new gutil.PluginError(PLUGIN_NAME + ' ' + mode,error)); }
      conn.bulk.load(objecttype, mode, file, (err, rets) => {
        if (err) { return callback(new gutil.PluginError(PLUGIN_NAME + ' ' + mode,error)); }
        for (let res of rets) {
          if (res.success) {
            gutil.log(PLUGIN_NAME + ' ' + mode, res.id + ' loaded successfully')
          } else {
            gutil.log(PLUGIN_NAME + ' ' + mode, res.id + ' error occurred, message = ' + res.errors.join(', '))
          }
        }
      });
    });
  });
};
@@ -403,19 +403,23 @@
  = file.
-nam
+relativ
 e.match(
@@ -449,17 +449,16 @@
 catch(er
-r
 ) %7Bretur
@@ -538,17 +538,23 @@
 parsable
-'
+ ' + er
 ));%7D%0A%0A  
@@ -606,18 +606,16 @@
 rd, (err
-or
 , userIn
@@ -636,18 +636,16 @@
  if (err
-or
 ) %7B retu
@@ -698,34 +698,32 @@
 + ' ' + mode,err
-or
 )); %7D%0A      conn
@@ -750,16 +750,27 @@
 e, mode,
+ gutil.env,
  file, (
@@ -772,16 +772,18 @@
 le, (err
+or
 , rets) 
@@ -794,32 +794,34 @@
 %0A        if (err
+or
 ) %7B return callb

improve error handling