text: /* eslint-env node, es6 */
  /* global require */
  'use strict';
  
  /**
   * Configuration
   */
  
  // Load dependencies
  const
    {
      parallel,
      series,
      src,
      dest,
      task,
      watch,
    } = require('gulp'),
    shell = require('gulp-shell'),
    rename = require('gulp-rename'),
    // gulpIf = require('gulp-if'),
    sourceMaps = require('gulp-sourcemaps'),
    sass = require('gulp-sass'),
    // sassGlobbing = require('gulp-sass-globbing'),
    sassLint = require('gulp-sass-lint'), // @todo move to Stylelint since sass lint is deprected. Good example config in ux.redhat.com's code
    postCss = require('gulp-postcss'),
    autoprefixer = require('autoprefixer'),
    cssNano = require('cssnano'),
    pxToRem = require('postcss-pxtorem'),
    browserSync = require('browser-sync').create();
  
  // File locations
  const
    cssSource = 'scss/**/*.scss',
    cssOutput = '../../../src/main/resources/SLING-INF/content/static/',
    asciiDocTemplates = '../../../src/main/resources/apps/pantheon/templates/haml/html5',
    rhdocsCss = 'node_modules/@cpelements/cp-documentation/dist/rhdocs.min.css';
  
  task(
    'compileAsciiDocs',
    parallel(
      shell.task(`asciidoctor -T ${asciiDocTemplates} -a pantheonenv=localwebassets dev-preview/ascii-doc-styleguide.adoc`),
      shell.task(`asciidoctor -T ${asciiDocTemplates} -a pantheonenv=localwebassets 'dev-preview/assembly_access-control-list.adoc'`)
    )
  );
  
  // /home/wruvalca/dev/pantheon/pantheon-bundle/frontend/src/web-assets/dev-preview/rhdocs.css
  // /home/wruvalca/dev/pantheon/pantheon-bundle/src/main/resources/SLING-INF/content/static/rhdocs.css
  const copyRhdocsCss = () => {
    return src(rhdocsCss)
      .pipe(rename('rhdocs.css'))
      .pipe(dest(`${cssOutput}`))
      .pipe(dest('dev-preview'));
  };
  
  /**
   * CSS Compilation
   */
  const compileCSS = () => {
    return src(cssSource)
      // Lint first
      .pipe(sassLint())
      .pipe(sassLint.format())
      // Not sure this line is needed, leaving for now
      // .pipe(gulpIf(!isDev, sassLint.failOnError()))
      .pipe(sourceMaps.init())
      .pipe(sass())
      .pipe(
        postCss([
          pxToRem({
            'propList': ['*',],
          }),
          autoprefixer(),
        ])
      )
      // Write an unminified version with sourcemaps
      // to this directory for dev
      .pipe(sourceMaps.write())
      .pipe(dest('./dev-preview/'))
      // Make production CSS and put in prod location
      .pipe(postCss([cssNano(),]))
      .pipe(dest(cssOutput));
  };
  
  // const compileCSS = series(globScss, processScss);
  // const compileCSS = processScss;
  
  /**
   * Start Browsersync
   */
  const startBrowserSync = (done) => {
    browserSync.init({
      'server': './',
      'index': './dev-preview/ascii-doc-styleguide.html',
    });
    done();
  };
  
  const reloadBrowserSync = (done) => {
    browserSync.reload();
    done();
  };
  
  const watchTasks = () => {
    watch(
      cssSource,
      series(
        compileCSS,
        reloadBrowserSync
      )
    );
  
    watch(
      `${asciiDocTemplates}/**/*.haml`,
      series(
        'compileAsciiDocs',
        reloadBrowserSync
      )
    );
  
    watch(
      'dev-preview/**/*.adoc',
      series(
        'compileAsciiDocs',
        reloadBrowserSync
      )
    );
  };
  
  /**
   * Gulp tasks
   */
  // Builds dev assets in dev-preview and prod CSS in to the correct folder (see cssOutput variable)
  task('default',
    parallel(
      copyRhdocsCss,
      compileCSS,
      'compileAsciiDocs'
    )
  );
  
  task('build:dev',
    parallel(
      compileCSS,
      copyRhdocsCss,
      'compileAsciiDocs'
    )
  );
  
  // Starts browsersync, watches project for changes and reloads all browsers
  task('watch',
    series(
      parallel(
        compileCSS,
        copyRhdocsCss,
        'compileAsciiDocs'
      ),
      parallel(
        startBrowserSync,
        watchTasks
      )
    )
  );
  