<commit_before>/*
Copyright (C) 2016 <NAME> <<EMAIL>>

This file is part of Pattrn - http://pattrn.co/.

Pattrn is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Pattrn is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Pattrn.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

var gulp = require('gulp');
var clean = require('gulp-clean');
var jade = require('gulp-jade');
var jsonlint = require('gulp-json-lint');
var sass = require('gulp-sass');
var util = require('gulp-util');
var webserver = require('gulp-webserver');

var browserify = require('browserify');
var babelify = require('babelify');
const execSync = require('child_process').execSync;
var source = require('vinyl-source-stream');

var config = require('./package.json').pattrn_configuration;

const source_data_packages_config_file = './source-data-packages.json';

/**
 * @technical-debt: pattrn-data-config.json is not mandatory, as data can be
 * pulled in in a number of different ways, so we should first check for its
 * existence here, and handle this scenario in the install_data_packages task
 * accordingly.
 */
try {
   const data_config = require(source_data_packages_config_file);
   config.source_data_packages = data_config.source_data_packages;
} catch (error) {
  util.log(
`Data package not configured: no data package will be installed via npm.
When not using an npm package for Pattrn source data, make sure source
data for this Pattrn instance is manually copied to the \`src\` folder and
that Pattrn is configured to use this data (in the \`src/config.json\` file).`
);
  config.source_data_packages = null;
}

/**
 * Copy over assets referenced from vendor stylesheets
 * This is currently configured manually through the assets member of the
 * config.vendor_stylesheets objects, from inspection of the vendor stylesheet
 * files for src references
 * @x-technical-debt: the destination is now hardcoded to /fonts within the
 * destination folder; this works but does not address the general scenario
 * where we may need to copy assets anywhere. we need to iterate over the
 * config.vendor_stylesheets elements, read source *and* destination
 * metadata for each, and pipe things accordingly to destinations.
 */
gulp.task('vendor-stylesheet-assets', function() {
  gulp.src(
    config.vendor_stylesheets
      .filter(item => { return item.assets && item.assets.length > 0; })
      .map(item => item.assets.map(group => group.src))
      .reduce((p, c, i) => { return p.concat(c); }, [])
    )
    .pipe(gulp.dest(config.dest + '/fonts'));
});

gulp.task('bundle', ['install_data_packages', 'vendor-stylesheet-assets'], function () {
    return browserify({entries: config.app_main, debug: true})
        .transform(babelify, {presets: ["es2015"]})
        .bundle()
        .pipe(source(config.bundle))
        .pipe(gulp.dest(config.dest));
});

gulp.task('watch', ['bundle'], function () {
    gulp.watch('*.js', ['build']);
});

gulp.task('default', ['watch']);

gulp.task('clean', [], () => {
  return gulp.src([config.src + '/config.json', config.src + '/data/**/*', config.dest + '/**/*'], {read: false})
    .pipe(clean());
});

gulp.task('jsonlint', function(){
      gulp.src('js/config.json')
        .pipe(jsonlint())
        .pipe(jsonlint.report('verbose'));
});

gulp.task('views', [], () => {
  gulp.src([`${config.src}/**/*.jade`])
    .pipe(jade({
      pretty: true
    }))
    .pipe(gulp.dest(config.dest))
});

gulp.task('sass', function () {
  return gulp.src(config.vendor_stylesheets.map(item => item.stylesheets).concat([ config.src + '/css/**/*.css', config.src + '/css/**/*.scss']))
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest(config.dest + '/css'));
});

/**
 * Install npm package with source data and copy its `pattrn-data` folder
 * to the `src` folder.
 * 
 * @x-technical-debt: config.source_data_packages is an array, but we only<commit_msg>correctly clean up project folders<commit_after>/*
Copyright (C) 2016 <NAME> <<EMAIL>>

This file is part of Pattrn - http://pattrn.co/.

Pattrn is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Pattrn is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Pattrn.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

var gulp = require('gulp');
var clean = require('gulp-clean');
var jade = require('gulp-jade');
var jsonlint = require('gulp-json-lint');
var sass = require('gulp-sass');
var util = require('gulp-util');
var webserver = require('gulp-webserver');

var browserify = require('browserify');
var babelify = require('babelify');
const execSync = require('child_process').execSync;
var source = require('vinyl-source-stream');

var config = require('./package.json').pattrn_configuration;

const source_data_packages_config_file = './source-data-packages.json';

/**
 * @technical-debt: pattrn-data-config.json is not mandatory, as data can be
 * pulled in in a number of different ways, so we should first check for its
 * existence here, and handle this scenario in the install_data_packages task
 * accordingly.
 */
try {
   const data_config = require(source_data_packages_config_file);
   config.source_data_packages = data_config.source_data_packages;
} catch (error) {
  util.log(
`Data package not configured: no data package will be installed via npm.
When not using an npm package for Pattrn source data, make sure source
data for this Pattrn instance is manually copied to the \`src\` folder and
that Pattrn is configured to use this data (in the \`src/config.json\` file).`
);
  config.source_data_packages = null;
}

/**
 * Copy over assets referenced from vendor stylesheets
 * This is currently configured manually through the assets member of the
 * config.vendor_stylesheets objects, from inspection of the vendor stylesheet
 * files for src references
 * @x-technical-debt: the destination is now hardcoded to /fonts within the
 * destination folder; this works but does not address the general scenario
 * where we may need to copy assets anywhere. we need to iterate over the
 * config.vendor_stylesheets elements, read source *and* destination
 * metadata for each, and pipe things accordingly to destinations.
 */
gulp.task('vendor-stylesheet-assets', function() {
  gulp.src(
    config.vendor_stylesheets
      .filter(item => { return item.assets && item.assets.length > 0; })
      .map(item => item.assets.map(group => group.src))
      .reduce((p, c, i) => { return p.concat(c); }, [])
    )
    .pipe(gulp.dest(config.dest + '/fonts'));
});

gulp.task('bundle', ['install_data_packages', 'vendor-stylesheet-assets'], function () {
    return browserify({entries: config.app_main, debug: true})
        .transform(babelify, {presets: ["es2015"]})
        .bundle()
        .pipe(source(config.bundle))
        .pipe(gulp.dest(config.dest));
});

gulp.task('watch', ['bundle'], function () {
    gulp.watch('*.js', ['build']);
});

gulp.task('default', ['watch']);

gulp.task('clean', [], () => {
  return gulp.src([config.src + '/data/*', config.dest], {read: false})
    .pipe(clean());
});

gulp.task('jsonlint', function(){
      gulp.src('js/config.json')
        .pipe(jsonlint())
        .pipe(jsonlint.report('verbose'));
});

gulp.task('views', [], () => {
  gulp.src([`${config.src}/**/*.jade`])
    .pipe(jade({
      pretty: true
    }))
    .pipe(gulp.dest(config.dest))
});

gulp.task('sass', function () {
  return gulp.src(config.vendor_stylesheets.map(item => item.stylesheets).concat([ config.src + '/css/**/*.css', config.src + '/css/**/*.scss']))
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest(config.dest + '/css'));
});

/**
 * Install npm package with source data and copy its `pattrn-data` folder
 * to the `src` folder.
 * 
 * @x-technical-debt: config.source_data_packages is an array, but we only
{'fraction_non_alphanumeric': 0.14293934860168028, 'fraction_numerical': 0.0023017608470479916, 'mean_word_length': 4.093786635404455, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6735755', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2587, 'n_words': 855}