const gulp = require('gulp');
const config = require('../config.js');

const babelify = require('babelify');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const eslint = require('gulp-eslint');
const source = require('vinyl-source-stream');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const runSequence = require('run-sequence');

const packageJSON = require('../../package.json');

const excludeVendorModules = [
  'babel-polyfill',
  'l20n'
];
const includeVendorModules = [
  'babel-polyfill/browser',
  'l20n/dist/compat/web/l20n'
];
const vendorModules = Object.keys(packageJSON.dependencies)
  .filter(name => excludeVendorModules.indexOf(name) < 0)
  .concat(includeVendorModules);

function shouldLint(opt, task) {
  return config[opt] ? [task] : [];
}

gulp.task('scripts-lint', () => {
  return gulp.src([config.SRC_PATH + '*.js', config.SRC_PATH + '{app,tests}/**/*.js'])
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(eslint.failOnError());
});

gulp.task('scripts-clean', () => {
});

gulp.task('scripts-watch', () => {
  gulp.watch([config.SRC_PATH + 'app/**/*.js'], ['scripts-app-main']);
  gulp.watch('../../package.json', ['scripts-app-vendor']);
  gulp.watch(config.SRC_PATH + 'scripts/**/*.js', ['scripts-misc']);
});

gulp.task('scripts-misc', () => {
  return gulp.src(config.SRC_PATH + 'scripts/**/*')
    .pipe(gulpif(config.IS_DEBUG, sourcemaps.init({loadMaps: true})))
    .pipe(gulpif(!config.IS_DEBUG, uglify()))
    .pipe(gulpif(config.IS_DEBUG, sourcemaps.write('./')))
    .pipe(gulp.dest(config.DEST_PATH + 'static/scripts'));
});

gulp.task('scripts-generate-static-html', () => {
  return commonBrowserify('generate-static-html.js', browserify({
    entries: [config.SRC_PATH + 'generate-static-html.js'],
    debug: config.IS_DEBUG,
    fullPaths: config.IS_DEBUG,
    transform: [babelify],
    standalone: 'generate-static-html'
  }).external(vendorModules));
});

gulp.task('scripts-app-main', () => {
  return commonBrowserify('app.js', browserify({
    entries: [config.SRC_PATH + 'app/index.js'],
    debug: config.IS_DEBUG,
    fullPaths: config.IS_DEBUG,
    transform: [babelify]
  }).external(vendorModules));
});

gulp.task('scripts-app-vendor', () => {
  return commonBrowserify('vendor.js', browserify({
    debug: config.IS_DEBUG
  }).require(vendorModules));
});

gulp.task('scripts-build', done => runSequence(
  'scripts-clean',
  'scripts-lint',
  'scripts-generate-static-html',
  'scripts-misc',
  'scripts-app-main',
  'scripts-app-vendor',
  done
));

function commonBrowserify(sourceName, b) {
  return b
    .bundle()
    .pipe(source(sourceName))
    .pipe(buffer())
    .pipe(gulpif(config.IS_DEBUG, sourcemaps.init({loadMaps: true})))
     // don't uglify in development. eases build chain debugging
    .pipe(gulpif(!config.IS_DEBUG, uglify()))
    .on('error', gutil.log)
    .pipe(gulpif(config.IS_DEBUG, sourcemaps.write('./')))
    .pipe(gulp.dest(config.DEST_PATH + 'static/app/'));
}

@@ -1490,32 +1490,8 @@
 ipe(
-gulpif(config.IS_DEBUG, 
 sour
@@ -1516,25 +1516,24 @@
 aps: true%7D))
-)
 %0A    .pipe(g
@@ -1581,32 +1581,8 @@
 ipe(
-gulpif(config.IS_DEBUG, 
 sour
@@ -1592,33 +1592,32 @@
 aps.write('./'))
-)
 %0A    .pipe(gulp.
@@ -2742,32 +2742,8 @@
 ipe(
-gulpif(config.IS_DEBUG, 
 sour
@@ -2772,17 +2772,16 @@
  true%7D))
-)
 %0A     //
@@ -2926,32 +2926,8 @@
 ipe(
-gulpif(config.IS_DEBUG, 
 sour
@@ -2945,17 +2945,16 @@
 e('./'))
-)
 %0A    .pi

add sourcemaps; fixes #1892