text: /******************************************************
   * PATTERN LAB NODE
   * EDITION-NODE-GULP
   * The gulp wrapper around patternlab-node core, providing tasks to interact with the core library and move supporting frontend assets.
  ******************************************************/
  var gulp = require('gulp'),
    path = require('path'),
    browserSync = require('browser-sync').create(),
    argv = require('minimist')(process.argv.slice(2)),
    sass = require('gulp-sass'),
    sourcemaps = require('gulp-sourcemaps'),
    babelify = require('babelify'),
    concat = require('gulp-concat'),
    vinylPaths = require('vinyl-paths'),
    gutil = require('gulp-util'),
    tap = require('gulp-tap'),
    babelCore = require('babel-core'),
    plugins = require('gulp-load-plugins')(),
    flatten = require('gulp-flatten'),
    clean = require('gulp-clean')
  
  var reactBabelHelpers = [
    'inherits',
    'createClass',
    'classCallCheck',
    'possibleConstructorReturn',
    'interopRequireDefault',
    'interopRequireWildcard',
    'extends',
    'objectWithoutProperties'
  ];
  
  function string_src (filename, string) {
    var src = require('stream').Readable({ objectMode: true })
    src._read = function () {
      this.push(new gutil.File({
        cwd: '',
        base: '',
        path: filename,
        contents: new Buffer(string)
      }))
      this.push(null)
    }
    return src
  }
  
  gulp.task('gu-clean', function () {
    return gulp.src(path.resolve('./tmp/*'), {read: false})
      .pipe(clean())
  })
  
  gulp.task('gu-react', function () {
    // return new Promise(function (resolve, reject) {
      var vp = vinylPaths()
  
      return gulp.src(path.resolve(paths().source.patterns, '**/*.jsx'))
        .pipe(vp)
        .pipe(flatten())
        .pipe(gulp.dest('./tmp'))
        .pipe(tap(function () {}))
        .on('end', function () {
          var scripts = '(function(win) {\n' +
              'if (win._reactLoaded) return;\n' +
              'else win._reactLoaded = true;\n' +
              'var gu = win.gu = win.gu || [],\n' +
              'react = win,' +
              'lib;\n'
  
          vp.paths.forEach(function (path) {
            var splittedPath = path.split('/')
            var fileName = splittedPath[splittedPath.length - 1]
            fileName = fileName.split('.')[0]
  
            var fileNameFactors = fileName.split('-')
            if (!isNaN(fileNameFactors[0])) fileNameFactors.shift()
  
            var capFileNameFactors = fileNameFactors.map(function (factor) {
              return factor.charAt(0).toUpperCase() + factor.slice(1)
            })
  
            var requireName = capFileNameFactors.join('')
            scripts += 'react.' + requireName + ' = require("./' + fileName + '");\n'
          })
  
          scripts += '})(window);'
  
          string_src('root.js', scripts)
            .pipe(gulp.dest(path.resolve('./tmp')))
            .on('end', function () {
              var s = babelCore.buildExternalHelpers(reactBabelHelpers, 'global')
  
              return gulp.src('./tmp/root.js')
                .pipe(plugins.browserify({
                  transform: [babelify.configure({plugins: ['external-helpers']})],
                  paths: ['./', './node_modules'],
                  external: ['react'],
                  extensions: ['.jsx']
                }))
                .pipe(plugins.replace("require('react')", "window.React"))
                .pipe(plugins.injectString.prepend(s))
                .pipe(plugins.rename('gu-react.js'))
                .pipe(gulp.dest(path.resolve(paths().public.js)))
                // .pipe(plugins.uglify())
                // .pipe(plugins.rename('gu-react.min.js'))
                // .pipe(gulp.dest('./tmp'))
            })
        })
  })
  
  /******************************************************
   * COPY TASKS - stream assets from source to destination
  ******************************************************/
  // JS copy
  gulp.task('pl-copy:js', function(){
    return gulp.src('**/*.js', {cwd: path.resolve(paths().source.js)} )
      .pipe(gulp.dest(path.resolve(paths().public.js)));
  });
  
  // Images copy
  gulp.task('pl-copy:img', function(){
    return gulp.src('**/*.*',{cwd: path.resolve(paths().source.images)} )
      .pipe(gulp.dest(path.resolve(paths().public.images)));
  });
  
  // Favicon copy
  gulp.task('pl-copy:favicon', function(){
    return gulp.src('favicon.ico', {cwd: path.resolve(paths().source.root)} )
      .pipe(gulp.dest(path.resolve(paths().public.root)));
  });
  
  // Fonts copy
  gulp.task('pl-copy:font', function(){
    return gulp.src('*', {cwd: path.resolve(paths().source.fonts)})
      .pipe(gulp.dest(path.resolve(paths().public.fonts)));
  });
  
  // CSS Copy
  gulp.task('pl-copy:css', function(){
    return gulp.src(path.resolve(paths().source.css, '*.css'))
      .pipe(gulp.dest(path.resolve(paths().public.css)))
      .pipe(browserSync.stream());
  });
  
  // Styleguide Copy everything but css
  gulp.task('pl-copy:styleguide', function(){
    return gulp.src(path.resolve(paths().source.styleguide, '**/!(*.css)'))
      .pipe(gulp.dest(path.resolve(paths().public.root)))
      .pipe(browserSync.stream());
  });
  
  // Styleguide Copy and flatten css
  gulp.task('pl-copy:styleguide-css', function(){
    return gulp.src(path.resolve(paths().source.styleguide, '**/*.css'))
      .pipe(gulp.dest(function(file){
        //flatten anything inside the styleguide into a single output dir per http://stackoverflow.com/a/34317320/1790362
        file.path = path.join(file.base, path.basename(file.path));
        return path.resolve(path.join(paths().public.styleguide, 'css'));
      }))
      .pipe(browserSync.stream());
  });
  
  /******************************************************
   * SCSS BUILD CONFIGURATION
  ******************************************************/
  gulp.task('gu-build:scss', function() {
    return gulp.src(path.resolve(paths().source.patterns, '**/*.scss'))
      .pipe(concat('custom.scss'))
      .pipe(sourcemaps.init())
      .pipe(sass({
        outputStyle: 'compressed',
        includePaths: [
          path.join(__dirname, 'bower_components'), // bower
          path.join(__dirname, 'node_modules/admin-lte/dist/css') // npm
        ]
      }))
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest(path.resolve(paths().public.css)))
      .pipe(browserSync.stream());
  })
  
  /******************************************************
   * REACT BUILD CONFIGURATION
  ******************************************************/
  gulp.task('gu-build:react', gulp.series('gu-clean', 'gu-react'))
  
  /******************************************************
   * PATTERN LAB CONFIGURATION - API with core library
  ******************************************************/
  //read all paths from our namespaced config file
  var config = require('./patternlab-config.json'),
    patternlab = require('patternlab-node')(config);
  
  function paths() {
    return config.paths;
  }
  
  function getConfiguredCleanOption() {
    return config.cleanPublic;
  }
  
  function build(done) {
    patternlab.build(done, getConfiguredCleanOption());
  }
  
  gulp.task('pl-assets', gulp.series(
    gulp.parallel(
      'gu-build:scss',
      'gu-build:react',
      'pl-copy:js',
      'pl-copy:img',
      'pl-copy:favicon',
      'pl-copy:font',
      'pl-copy:css',
      'pl-copy:styleguide',
      'pl-copy:styleguide-css'
    ),
    function(done){
      done();
    })
  );
  
  gulp.task('patternlab:version', function (done) {
    patternlab.version();
    done();
  });
  
  gulp.task('patternlab:help', function (done) {
    patternlab.help();
    done();
  });
  
  gulp.task('patternlab:patternsonly', function (done) {
    patternlab.patternsonly(done, getConfiguredCleanOption());
  });
  
  gulp.task('patternlab:liststarterkits', function (done) {
    patternlab.liststarterkits();
    done();
  });
  
  gulp.task('patternlab:loadstarterkit', function (done) {
    patternlab.loadstarterkit(argv.kit, argv.clean);
    done();
  });
  
  gulp.task('patternlab:build', gulp.series('pl-assets', build, function(done){
    done();
  }));
  
  /******************************************************
   * SERVER AND WATCH TASKS
  ******************************************************/
  // watch task utility functions
  function getSupportedTemplateExtensions() {
    var engines = require('./node_modules/patternlab-node/core/lib/pattern_engines');
    return engines.getSupportedFileExtensions();
  }
  function getTemplateWatches() {
    return getSupportedTemplateExtensions().map(function (dotExtension) {
      return path.resolve(paths().source.patterns, '**/*' + dotExtension);
    });
  }
  
  function reload() {
    browserSync.reload();
  }
  
  function watch() {
    gulp.watch(path.resolve(paths().source.css, '**/*.css')).on('change', gulp.series('pl-copy:css', reload));
    gulp.watch(path.resolve(paths().source.patterns, '**/*.jsx')).on('change', gulp.series('gu-build:react'), reload);
    gulp.watch(path.resolve(paths().source.patterns, '**/*.scss')).on('change', gulp.series('gu-build:scss'), reload);
    gulp.watch(path.resolve(paths().source.styleguide, '**/*.*')).on('change', gulp.series('pl-copy:styleguide', 'pl-copy:styleguide-css', reload));
  
    var patternWatches = [
      path.resolve(paths().source.patterns, '**/*.json'),
      path.resolve(paths().source.patterns, '**/*.md'),
      path.resolve(paths().source.patterns, '**/*.scss'),
      path.resolve(paths().source.patterns, '**/*.jsx'),
      path.resolve(paths().source.data, '*.json'),
      path.resolve(paths().source.fonts + '/*'),
      path.resolve(paths().source.images + '/*'),
      path.resolve(paths().source.meta, '*'),
      path.resolve(paths().source.annotations + '/*')
    ].concat(getTemplateWatches());
  
    gulp.watch(patternWatches).on('change', gulp.series(build, reload));
  }
  
  gulp.task('patternlab:connect', gulp.series(function(done) {
    browserSync.init({
      server: {
        baseDir: path.resolve(paths().public.root)
      },
      snippetOptions: {
        // Ignore all HTML files within the templates folder
        blacklist: ['/index.html', '/', '/?*']
      },
      notify: {
        styles: [
          'display: none',
          'padding: 15px',
          'font-family: sans-serif',
          'position: fixed',
          'font-size: 1em',
          'z-index: 9999',
          'bottom: 0px',
          'right: 0px',
          'border-top-left-radius: 5px',
          'background-color: #1B2032',
          'opacity: 0.4',
          'margin: 0',
          'color: white',
          'text-align: center'
        ]
      }
    }, function(){
      console.log('PATTERN LAB NODE WATCHING FOR CHANGES');
    });
    done();
  }));
  
  /******************************************************
   * COMPOUND TASKS
  ******************************************************/
  gulp.task('default', gulp.series('patternlab:build'));
  gulp.task('patternlab:watch', gulp.series('patternlab:build', watch));
  gulp.task('patternlab:serve', gulp.series('patternlab:build', 'patternlab:connect', watch));
  