'use strict';

const duplexer = require('plexer');
const svgicons2svgfont = require('gulp-svgicons2svgfont');
const svg2ttf = require('gulp-svg2ttf');
const ttf2eot = require('gulp-ttf2eot');
const ttf2woff = require('gulp-ttf2woff');
const ttf2woff2 = require('gulp-ttf2woff2');
const cond = require('gulp-cond');
const filter = require('streamfilter');
const spawn = require('gulp-spawn');

function gulpFontIcon(options) {
  let inStream = null;
  let outStream = null;
  let duplexStream = null;

  options = options || {};
  options.formats = options.formats || ['ttf', 'eot', 'woff'];
  // Generating SVG font and saving her
  inStream = svgicons2svgfont(options);
  // Generating TTF font and saving her
  outStream = inStream
    .pipe(svg2ttf({
      clone: -1 !== options.formats.indexOf('svg'),
      timestamp: options.timestamp,
    }).on('error', (err) => {
      outStream.emit('error', err);
    }))
    // TTFAutoHint
    .pipe(cond(!!options.autohint, () => {
      const hintPath = 'string' === typeof options.autohint ? options.autohint : 'ttfautohint';
      const nonTTFfilter = filter((file, unused, cb) => {
        cb(file.path.indexOf('.ttf') !== file.path.length - 4);
      }, {
        objectMode: true,
        restore: true,
        passthrough: true,
      });

      return duplexer(
        { objectMode: true },
        nonTTFfilter,
        nonTTFfilter.pipe(spawn({
          cmd: '/bin/sh',
          args: [
            '-c',
            `cat | "${hintPath}" --symbol --fallback-script=latn` +
              ' --windows-compatibility --no-info /dev/stdin /dev/stdout | cat',
          ],
        })).pipe(nonTTFfilter.restore)
      ).on('error', (err) => {
        outStream.emit('error', err);
      });
    }))
    // Generating EOT font
    .pipe(cond(
      -1 !== options.formats.indexOf('eot'),
      () => ttf2eot({ clone: true }).on('error', (err) => {
        outStream.emit('error', err);
      })
    ))
    // Generating WOFF font
    .pipe(cond(
      -1 !== options.formats.indexOf('woff'),
      () => ttf2woff({ clone: true }).on('error', (err) => {
        outStream.emit('error', err);
      })
    ))
    // Generating WOFF2 font
    .pipe(cond(
      -1 !== options.formats.indexOf('woff2'),
      () => ttf2woff2({ clone: true }).on('error', (err) => {
        outStream.emit('error', err);
      })
    ))
    // Filter TTF font if necessary
    .pipe(cond(
      -1 === options.formats.indexOf('ttf'),
      () => filter((file, unused, cb) => {
        cb(file.path.indexOf('.ttf') === file.path.length - 4);
      }, {
        objectMode: true,
        passthrough: true,
      })
    ));

  duplexStream = duplexer({ objectMode: true }, inStream, outStream);

  // Re-emit codepoint mapping event
  inStream.on('glyphs', (glyphs) => {
    duplexStream.emit('glyphs', glyphs, options);
  });

  return duplexStream;
}

module.exports = gulpFontIcon;

@@ -584,16 +584,128 @@
 woff'%5D;%0A
+  options.clone = -1 !== options.formats.indexOf('svg');%0A  options.timestamp = options.timestamp %7C%7C Date.now();%0A
   // Gen
@@ -861,103 +861,15 @@
 ttf(
-%7B%0A      clone: -1 !== options.formats.indexOf('svg'),%0A      timestamp: options.timestamp,%0A    %7D
+options
 ).on

move options to the top and pass the entire object to svg2ttf function