/*!
 * Project: mp3web
 * File:    ./gulp-tasks/env/replvars.js
 * Author:  Baltrushaitis Tomas <tbaltrushaitis@gmail.com>
 * Created: 2017-12-19T13:28:00
 * Updated: 2017-12-19T13:28:00
 */

'use strict';

/**
 * DEPENDENCIES
 * @private
 */

const path = require('path');
const util = require('util');
const _ = require('lodash');

const buffer = require('vinyl-buffer');
const replace = require('gulp-replace');
const rename = require('gulp-rename');
const concat = require('gulp-concat');

const modName = path.basename(module.filename, '.js');
const modPath = path.relative(global.ME.WD, path.dirname(module.filename));

const modConfigFile = `config/${path.join(modPath, modName)}.json`;
const modConfig = require('read-config')(modConfigFile);

const utin = util.inspect;

//  ------------------------------------------------------------------------  //
//  -------------------------------  EXPOSES  ------------------------------  //
//  ------------------------------------------------------------------------  //

module.exports = function (gulp) {
  console.log(`[${new Date().toISOString()}][${modPath}/${modName}] INSTANCE ACTIVATED with modConfig = [${utin(modConfig)}]`);

  function replaceEnvironmentVariables (stream) {
    for (let cgv in modConfig) {
      let key = cgv.toUpperCase(),
          v = modConfig[cgv];
      v = _.isObject(v)
            ? JSON.stringify(v)
            : v;

      stream = stream.pipe(replace('{{' + key + '}}', v));
      console.log(`[${new Date().toISOString()}][${modPath}/${modName}] Replace ${key}:\t ${v}`);
    }
    stream = stream.pipe(replace('{{APP_VERSION}}', global.ME.pkg.version));
    return stream;
  };

  var process = gulp.src([
      path.join(global.ME.SRC, `.env.rc.test`)
    ])
    .pipe(buffer());

  process = replaceEnvironmentVariables(process);
  return process.pipe(concat('.env.rc.test.concat'))
          .pipe(gulp.dest(global.ME.BUILD));

};

@@ -1297,17 +1297,16 @@
 erCase()
-,
 %0A       
@@ -1306,20 +1306,22 @@
         
- 
+,
  v 
+  
 = modCon
@@ -1328,23 +1328,33 @@
 fig%5Bcgv%5D
+%0A      
 ;%0A     
+ //
  v = _.i
@@ -1367,22 +1367,14 @@
 t(v)
-%0A            ?
+ ? v =
  JSO
@@ -1391,24 +1391,67 @@
 y(v)
-%0A            : v
+ : true;%0A      _.isObject(v) ? v = JSON.stringify(v) : true
 ;%0A%0A 
@@ -1716,19 +1716,19 @@
   %7D;%0A%0A  
-var
+let
  process

Replace 'var' to 'let'