import { isDevelopment } from '../lib/env';

import log from 'fancy-log';
import config from '../lib/config';
import pump from 'pump';
import rev from 'gulp-rev';
import revDeleteOriginal from 'gulp-rev-delete-original';
import revReplace from 'gulp-rev-replace';
import { src, dest, task, parallel, series } from 'gulp';

const MANIFEST_DIR = config.get('tasks.revision') && config.get('tasks.revision.manifest.directory')
  ? config.get('tasks.revision.manifest.directory')
  : config.get('paths.dist');

const MANIFEST_FILE = config.get('tasks.revision') && config.get('tasks.revision.manifest.file')
  ? config.get('tasks.revision.manifest.file')
  : 'assets.json';

const MANIFEST_FULL_PATH = `${MANIFEST_DIR}/${MANIFEST_FILE}`;

const DEFAULTS = [
  `${config.get('tasks.sass.dist')}/**/*.css`,
  `${config.get('tasks.javascript.dist')}/**/*.js`,
  `${config.get('tasks.images.dist')}/**/*.{gif,jpg,jpeg,svg,png}`,
];

/**
 * Add revision hash behind filename so we can cache assets forever.
 */
const hashFiles = done => {
  if (isDevelopment || !config.get('tasks.revision')) {
    log(`Skipping 'revision:hash' task for development`);
    return done();
  }
  return pump([
    src([
      ...DEFAULTS,
      ...config.get('tasks.revision.files') || [],
    ], { base: config.get('paths.dist') }),
    rev(),
    revDeleteOriginal(),
    dest(config.get('paths.dist')),
    rev.manifest(MANIFEST_FILE),
    dest(MANIFEST_DIR),
  ], done);
};

/*
 * Replace image and font urls in CSS files.
 */
const replaceCss = done => {
  if (isDevelopment || !config.get('tasks.revision')) {
    log(`Skipping 'revision:css' task for development`);
    return done();
  }
  return pump([
    src(`${config.get('tasks.sass.dist')}/**/*.css`),
    revReplace({ manifest: src(MANIFEST_FULL_PATH) }),
    dest(config.get('tasks.sass.dist')),
  ], done);
};

/**
 * Replace image and font urls in JavaScript files.
 */
const replaceJs = done => {
  if (isDevelopment || !config.get('tasks.revision')) {
    log(`Skipping 'revision:javascript' task for development`);
    return done();
  }
  return pump([
    src(`${config.get('tasks.javascript.dist')}/**/*.js`),
    revReplace({ manifest: src(MANIFEST_FULL_PATH) }),
    dest(config.get('tasks.javascript.dist')),
  ], done);
};

/**
 * Revision tasks wrapper.
 */
export const revision = series(
  hashFiles,
  parallel(
    replaceCss,
    replaceJs,
  ),
);

task('revision:hash', hashFiles);
task('revision:css', replaceCss);
task('revision:javascript', replaceJs);
task('revision', revision);

@@ -226,20 +226,20 @@
 rt revRe
-plac
+writ
 e from '
@@ -249,20 +249,20 @@
 p-rev-re
-plac
+writ
 e';%0Aimpo
@@ -1733,36 +1733,36 @@
 css%60),%0A    revRe
-plac
+writ
 e(%7B manifest: sr
@@ -2159,20 +2159,20 @@
    revRe
-plac
+writ
 e(%7B mani

Update revision task