<commit_before>'use strict';

var gulp = require('gulp');
var runSequence = require('run-sequence');
var browserSync = require('browser-sync');

var eyeglass = require('eyeglass');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var prefix   = require('gulp-autoprefixer');
var plumber  = require('gulp-plumber');
var notify   = require('gulp-notify');
var rename   = require('gulp-rename');
var del      = require('del');
var clone    = require('gulp-clone');
var merge    = require('merge-stream');
var size     = require('gulp-size');

var postcss = require('gulp-postcss');
var cssnano = require('cssnano');
var flexibility = require('postcss-flexibility');
var uglify  = require('gulp-uglify');

var jade = require('gulp-jade');
var md = require('jstransformer')(require('jstransformer-markdown-it'));
var shorthash = require('shorthash');
var _ = require('lodash');

var affected = require('gulp-jade-find-affected');
var jadeInheritance = require('gulp-jade-inheritance');
var changed = require('gulp-changed');
var cached = require('gulp-cached');
var gulpif = require('gulp-if');

var vfs = require('vinyl-fs');
var symlink = require('gulp-symlink');
var path = require('path');
var exec = require('child_process').exec;

var argv = require('minimist')(process.argv.slice(2));
var buildPath = argv.release ? '.' : './docs';
var buildCss = buildPath + '/css';
var rbDir = './rocketbelt';
var siteDir = './site';

var nav = [];

gulp.task('default', function (done) {
  runSequence('build', ['server', 'watch'], done);
});

gulp.task('server', function () {
  return browserSync.init({
    server: {
      injectChanges: true,
      baseDir: buildPath
    }
  });
});

gulp.task('watch', function(){
  global.isWatching = true;

  gulp.watch(['./rocketbelt/**/*.scss', './templates/scss/**/*.scss'], ['styles']);
  gulp.watch('./templates/**/*', ['views']);
  gulp.watch(['./rocketbelt/**/*.js'], ['uglify']);
  gulp.watch(buildPath + '/**/*.html').on('change', debounce(browserSync.reload, 500));
  gulp.watch(buildPath + '/**/*.js').on('change', debounce(browserSync.reload, 500));
});

var sizeOptions = {
  showFiles: true,
  gzip: true
};

gulp.task('uglify', function () {
  return gulp.src(['./rocketbelt/**/*.js', '!./rocketbelt/**/*.min.js'])
    .pipe(changed(buildPath))
    .pipe(rename({ suffix: '.min' }))
    .pipe(sourcemaps.init())
    .pipe(uglify())
    .pipe(size(sizeOptions))
    .pipe(sourcemaps.write('.', { sourceRoot: '.' }))
    .pipe(gulp.dest(buildPath))
  ;
});

gulp.task('styles', function () {
  var source = gulp.src(['./rocketbelt/**/*.scss', './templates/scss/**/*.scss'])
    .pipe(plumber({ errorHandler: notify.onError('Error: <%= error.message %>') }))
    .pipe(sourcemaps.init())
    .pipe(sass(eyeglass()))
    .pipe(prefix({
      browsers: ['last 5 Chrome versions',
                 'last 5 Firefox versions',
                 'Safari >= 9',
                 'ie >= 9',
                 'Edge >= 1',
                 'iOS >= 8',
                 'Android >= 4.4']
    }))
    .pipe(postcss([flexibility()]))
  ;

  var max = source.pipe(clone())
    .pipe(sourcemaps.write('.', { sourceRoot: null }))
    .pipe(gulp.dest(buildCss))
  ;

  var min = source.pipe(clone())
    .pipe(sourcemaps.init())
    .pipe(rename({ suffix: '.min' }))
    .pipe(postcss([cssnano()]))
    .pipe(size(sizeOptions))
    .pipe(sourcemaps.write('.', { sourceRoot: null }))
    .pipe(gulp.dest(buildCss))
    .pipe(browserSync.stream({ match: '**/*.css' }))
  ;

  return merge(max, min);
});

gulp.task('clean', ['link:clean'], function () {
  del(['**/*/.DS_Store']);
  del([buildPath]);
});

gulp.task('build', function (done) {
  if (!argv.release) {
    runSequence('uglify', 'link', ['styles', 'views'], done);
  }
  else {
    runSequence('uglify', 'link', ['styles', 'views'], 'del-assets', done);
  }
});

gulp.task('del-assets', function () {
  if (argv.release) {
    del(['rocketbelt/', 'templates/', 'docs/'])
  }
});

gulp.task('js:site:copy', function () {
  vfs.src(['./templates/**/*.js'])
    .pipe(changed(buildPath))
    .pipe(vfs.dest(buildPath, { overwrite: true }));
});

gulp.task('link', ['link:partials', 'link:js']);

gulp.task('link:partials', function () {
  return gulp.src('./rocketbelt/**/_*.jade')
    .pipe(symlink(function (file) {
      return path.join('./templates', file.relative);
    }, { force: true, log: false }));
  // TODO: Using gulp-symlink because relative symlinks are broken in vfs.
  // Should be fixed in vfs 3.0 and the above should be replaced with the following:
  // return vfs.src('./rocketbelt/**/_*.jade')
  //   .pipe(vfs.symlink('./templates', { relative: true }));
});

gulp.task('link:js', function () {
  return gulp.src(['./rocketbelt/**/*.js', './rocketbelt/**/*.json', '!./**/slipsum-cache.json'])
    .pipe(symlink(function (file) {
      return path.join('./templates', file.relative);
    }, { force: true, log: false }));
  // TODO: Using gulp-symlink because relative symlinks are broken in vfs.
  // Should be fixed in vfs 3.0 and the above should be replaced with the following:
  // return vfs.src('./rocketbelt/**/*.js', './rocketbelt/**/*.json')
  //   .pipe(vfs.symlink('./templates', { relative: true }));
});

gulp.task('link:clean', function () {
  exec('find ./templates -type l -delete', function (err, stdout, stderr) { })
});

gulp.task('views', ['js:site:copy'], function () {
  var dir = './templates';
  directoryTreeToObj(dir, function (err, res) {
    if (err)
      console.error(err);

    var colorFamilies = require('./templates/base/color/_color-families.json');

    return gulp.src(['./templates/**/*.jade', '!./templates/**/_*.jade'])
      .pipe(plumber({ errorHandler: notify.onError('Error: <%= error.message %>') }))
      .pipe(affected())
      .pipe(jadeInheritance({ basedir: dir }))
      .pipe(jade({
        basedir: __dirname + '/templates',
        pretty: true,
        md: md,
        locals: {
          buildPath: argv.release ? '/rocketbelt' : '',
          nav: res,
          colorFamilies: colorFamilies,
          shorthash: shorthash,
          _: _
        }
      }))
      .pipe(gulp.dest(buildPath))
    ;
  });
});

var directoryTreeToObj = function(dir, done) {
  var fs = require('fs');
  var path = require('path');
  var results = [];

  fs.readdir(dir, function(err, files) {
    if (err)
      return done(err);

    files = files.filter(function (file) {
      if (fs.lstatSync(dir + '/' + file).isDirectory()) { if (file === 'js' || file === 'scss') return false; }
      return (file.indexOf('_') !== 0) && (file.indexOf('.js') == -1);
    });

    var pending = files.length;<commit_msg>Modify build to support TLD.<commit_after>'use strict';

var gulp = require('gulp');
var runSequence = require('run-sequence');
var browserSync = require('browser-sync');

var eyeglass = require('eyeglass');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var prefix   = require('gulp-autoprefixer');
var plumber  = require('gulp-plumber');
var notify   = require('gulp-notify');
var rename   = require('gulp-rename');
var del      = require('del');
var clone    = require('gulp-clone');
var merge    = require('merge-stream');
var size     = require('gulp-size');

var postcss = require('gulp-postcss');
var cssnano = require('cssnano');
var flexibility = require('postcss-flexibility');
var uglify  = require('gulp-uglify');

var jade = require('gulp-jade');
var md = require('jstransformer')(require('jstransformer-markdown-it'));
var shorthash = require('shorthash');
var _ = require('lodash');

var affected = require('gulp-jade-find-affected');
var jadeInheritance = require('gulp-jade-inheritance');
var changed = require('gulp-changed');
var cached = require('gulp-cached');
var gulpif = require('gulp-if');

var vfs = require('vinyl-fs');
var symlink = require('gulp-symlink');
var path = require('path');
var exec = require('child_process').exec;

var argv = require('minimist')(process.argv.slice(2));
var buildPath = argv.release ? '.' : './docs';
var buildCss = buildPath + '/css';
var rbDir = './rocketbelt';
var siteDir = './site';

var nav = [];

gulp.task('default', function (done) {
  runSequence('build', ['server', 'watch'], done);
});

gulp.task('server', function () {
  return browserSync.init({
    server: {
      injectChanges: true,
      baseDir: buildPath
    }
  });
});

gulp.task('watch', function(){
  global.isWatching = true;

  gulp.watch(['./rocketbelt/**/*.scss', './templates/scss/**/*.scss'], ['styles']);
  gulp.watch('./templates/**/*', ['views']);
  gulp.watch(['./rocketbelt/**/*.js'], ['uglify']);
  gulp.watch(buildPath + '/**/*.html').on('change', debounce(browserSync.reload, 500));
  gulp.watch(buildPath + '/**/*.js').on('change', debounce(browserSync.reload, 500));
});

var sizeOptions = {
  showFiles: true,
  gzip: true
};

gulp.task('uglify', function () {
  return gulp.src(['./rocketbelt/**/*.js', '!./rocketbelt/**/*.min.js'])
    .pipe(changed(buildPath))
    .pipe(rename({ suffix: '.min' }))
    .pipe(sourcemaps.init())
    .pipe(uglify())
    .pipe(size(sizeOptions))
    .pipe(sourcemaps.write('.', { sourceRoot: '.' }))
    .pipe(gulp.dest(buildPath))
  ;
});

gulp.task('styles', function () {
  var source = gulp.src(['./rocketbelt/**/*.scss', './templates/scss/**/*.scss'])
    .pipe(plumber({ errorHandler: notify.onError('Error: <%= error.message %>') }))
    .pipe(sourcemaps.init())
    .pipe(sass(eyeglass()))
    .pipe(prefix({
      browsers: ['last 5 Chrome versions',
                 'last 5 Firefox versions',
                 'Safari >= 9',
                 'ie >= 9',
                 'Edge >= 1',
                 'iOS >= 8',
                 'Android >= 4.4']
    }))
    .pipe(postcss([flexibility()]))
  ;

  var max = source.pipe(clone())
    .pipe(sourcemaps.write('.', { sourceRoot: null }))
    .pipe(gulp.dest(buildCss))
  ;

  var min = source.pipe(clone())
    .pipe(sourcemaps.init())
    .pipe(rename({ suffix: '.min' }))
    .pipe(postcss([cssnano()]))
    .pipe(size(sizeOptions))
    .pipe(sourcemaps.write('.', { sourceRoot: null }))
    .pipe(gulp.dest(buildCss))
    .pipe(browserSync.stream({ match: '**/*.css' }))
  ;

  return merge(max, min);
});

gulp.task('clean', ['link:clean'], function () {
  del(['**/*/.DS_Store']);
  del([buildPath]);
});

gulp.task('build', function (done) {
  if (!argv.release) {
    runSequence('uglify', 'link', ['styles', 'views'], done);
  }
  else {
    runSequence('uglify', 'link', ['styles', 'views'], 'del-assets', done);
  }
});

gulp.task('del-assets', function () {
  if (argv.release) {
    del(['rocketbelt/', 'templates/', 'docs/']);
  }
});

gulp.task('js:site:copy', function () {
  vfs.src(['./templates/**/*.js'])
    .pipe(changed(buildPath))
    .pipe(vfs.dest(buildPath, { overwrite: true }));
});

gulp.task('link', ['link:partials', 'link:js']);

gulp.task('link:partials', function () {
  return gulp.src('./rocketbelt/**/_*.jade')
    .pipe(symlink(function (file) {
      return path.join('./templates', file.relative);
    }, { force: true, log: false }));
  // TODO: Using gulp-symlink because relative symlinks are broken in vfs.
  // Should be fixed in vfs 3.0 and the above should be replaced with the following:
  // return vfs.src('./rocketbelt/**/_*.jade')
  //   .pipe(vfs.symlink('./templates', { relative: true }));
});

gulp.task('link:js', function () {
  return gulp.src(['./rocketbelt/**/*.js', './rocketbelt/**/*.json', '!./**/slipsum-cache.json'])
    .pipe(symlink(function (file) {
      return path.join('./templates', file.relative);
    }, { force: true, log: false }));
  // TODO: Using gulp-symlink because relative symlinks are broken in vfs.
  // Should be fixed in vfs 3.0 and the above should be replaced with the following:
  // return vfs.src('./rocketbelt/**/*.js', './rocketbelt/**/*.json')
  //   .pipe(vfs.symlink('./templates', { relative: true }));
});

gulp.task('link:clean', function () {
  exec('find ./templates -type l -delete', function (err, stdout, stderr) { });
});

gulp.task('views', ['js:site:copy'], function () {
  var dir = './templates';
  directoryTreeToObj(dir, function (err, res) {
    if (err)
      console.error(err);

    var colorFamilies = require('./templates/base/color/_color-families.json');

    return gulp.src(['./templates/**/*.jade', '!./templates/**/_*.jade'])
      .pipe(plumber({ errorHandler: notify.onError('Error: <%= error.message %>') }))
      .pipe(affected())
      .pipe(jadeInheritance({ basedir: dir }))
      .pipe(jade({
        basedir: __dirname + '/templates',
        pretty: true,
        md: md,
        locals: {
          buildPath: argv.release ? '' : '',
          nav: res,
          colorFamilies: colorFamilies,
          shorthash: shorthash,
          _: _
        }
      }))
      .pipe(gulp.dest(buildPath))
    ;
  });
});

var directoryTreeToObj = function(dir, done) {
  var fs = require('fs');
  var path = require('path');
  var results = [];

  fs.readdir(dir, function(err, files) {
    if (err)
      return done(err);

    files = files.filter(function (file) {
      if (fs.lstatSync(dir + '/' + file).isDirectory()) { if (file === 'js' || file === 'scss') return false; }
      return (file.indexOf('_') !== 0) && (file.indexOf('.js') == -1);
    });

    var pending = files.length;
{'fraction_non_alphanumeric': 0.21901629541037523, 'fraction_numerical': 0.0031394827328449695, 'mean_word_length': 3.345241961675869, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4061723', 'n_tokens_mistral': 4622, 'n_tokens_neox': 4511, 'n_words': 908}