<commit_before>/* global require: false, exports: false */

var gulp = require('gulp');

var replace = require('gulp-replace');
var _ = require('lodash');

var defaults = require('./defaults');

exports.debug = function(libReferences, libraryPath, target, index) {
	if (!target) {
		target = defaults.debugFolder;
	}

	var scripts = buildScripts(exports.getReferences(libReferences, libraryPath));
	return insertScripts(scripts, target, index);
}

exports.release = function(libReferences, libraryPath, target, index) {
	if (!target) {
		target = defaults.releaseFolder;
	}

	var scripts = buildScripts(exports.getCDNReferences(libReferences, libraryPath));
	return insertScripts(scripts, target, index);
};

function insertScripts(scripts, target, index) {
	if (!index) {
		index = 'index.html';
	}
	
    index = target + '/' + index;
	
    // Find <!-- build:js scripts/vendor --> ... <!-- endbuild -->
    // Replace internal block <!-- bower:js --> ... <!-- endbower --> with the script tags
    //                  Find build tag start for vendor scripts....match any content..find bower ref area..match previous bower refs to end..match any content..find end build tag
    //                                  |                                   |                  |               |               |                  |                   |
    //                                  V                                   V                  V               V               V                  V                   V
    var findBowerScriptArea = /<!--\s*build:js\s*scripts\/vendor\s*-->((?:\n|\r|.)*?)<!--\s*bower:js\s*-->(?:\n|\r|.)*<!--\s*endbower\s*-->((?:\n|\r|.)*?)<!--\s*endbuild\s*-->/gim;
    var bowerReferences = '<!--build:js scripts\/vendor-->' +
                          '$1' +
                          '<!--bower:js-->\n' + 
                          scripts +
                          '\t<!--endbower-->' +
                          '$2' +
                          '<!--endbuild-->';

    return gulp.src(index)
        .pipe(replace(findBowerScriptArea, bowerReferences))
        .pipe(gulp.dest(target));
}

exports.getReferences = function (libReferences, libraryPath) {
	if (!libraryPath) {
		libraryPath = defaults.librariesFolder;
	}
	
    return _.map(libReferences, function(ref) { return libraryPath + ref.js; });
};

exports.getMinifiedReferences = function (libReferences, libraryPath) {
	if (!libraryPath) {
		libraryPath = defaults.librariesFolder;
	}
	
    return _.map(libReferences, function(ref) { return libraryPath + ref.min; });
};

exports.getCDNReferences = function (libReferences, libraryPath) {
	if (!libraryPath) {
		libraryPath = defaults.librariesFolder;
	}
	
    return _.map(libReferences, function(ref) {
		if (!ref.cdn) {
			return libraryPath + ref.min;
		} else {
			return libraryPath + ref.cdn;
		}
	});
};

function buildScripts(references) {
    var scriptTemplate = '\t<script type="text/javascript" src="{0}"></script>';
    return _.reduce(references, function (scripts, reference) {
        return scripts +
            scriptTemplate.replace('{0}', reference) + '\n';
    }, '');        
}<commit_msg>Add a path separator between the library path and the reference path.<commit_after>/* global require: false, exports: false */

var gulp = require('gulp');

var replace = require('gulp-replace');
var _ = require('lodash');

var defaults = require('./defaults');

exports.debug = function(libReferences, libraryPath, target, index) {
	if (!target) {
		target = defaults.debugFolder;
	}

	var scripts = buildScripts(exports.getReferences(libReferences, libraryPath));
	return insertScripts(scripts, target, index);
}

exports.release = function(libReferences, libraryPath, target, index) {
	if (!target) {
		target = defaults.releaseFolder;
	}

	var scripts = buildScripts(exports.getCDNReferences(libReferences, libraryPath));
	return insertScripts(scripts, target, index);
};

function insertScripts(scripts, target, index) {
	if (!index) {
		index = 'index.html';
	}
	
    index = target + '/' + index;
	
    // Find <!-- build:js scripts/vendor --> ... <!-- endbuild -->
    // Replace internal block <!-- bower:js --> ... <!-- endbower --> with the script tags
    //                  Find build tag start for vendor scripts....match any content..find bower ref area..match previous bower refs to end..match any content..find end build tag
    //                                  |                                   |                  |               |               |                  |                   |
    //                                  V                                   V                  V               V               V                  V                   V
    var findBowerScriptArea = /<!--\s*build:js\s*scripts\/vendor\s*-->((?:\n|\r|.)*?)<!--\s*bower:js\s*-->(?:\n|\r|.)*<!--\s*endbower\s*-->((?:\n|\r|.)*?)<!--\s*endbuild\s*-->/gim;
    var bowerReferences = '<!--build:js scripts\/vendor-->' +
                          '$1' +
                          '<!--bower:js-->\n' + 
                          scripts +
                          '\t<!--endbower-->' +
                          '$2' +
                          '<!--endbuild-->';

    return gulp.src(index)
        .pipe(replace(findBowerScriptArea, bowerReferences))
        .pipe(gulp.dest(target));
}

exports.getReferences = function (libReferences, libraryPath) {
	if (!libraryPath) {
		libraryPath = defaults.librariesFolder;
	}
	
    return _.map(libReferences, function(ref) { return libraryPath + '/' + ref.js; });
};

exports.getMinifiedReferences = function (libReferences, libraryPath) {
	if (!libraryPath) {
		libraryPath = defaults.librariesFolder;
	}
	
    return _.map(libReferences, function(ref) { return libraryPath + '/' + ref.min; });
};

exports.getCDNReferences = function (libReferences, libraryPath) {
	if (!libraryPath) {
		libraryPath = defaults.librariesFolder;
	}
	
    return _.map(libReferences, function(ref) {
		if (!ref.cdn) {
			return libraryPath + '/' + ref.min;
		} else {
			return libraryPath + '/' + ref.cdn;
		}
	});
};

function buildScripts(references) {
    var scriptTemplate = '\t<script type="text/javascript" src="{0}"></script>';
    return _.reduce(references, function (scripts, reference) {
        return scripts +
            scriptTemplate.replace('{0}', reference) + '\n';
    }, '');        
}
{'fraction_non_alphanumeric': 0.1571294559099437, 'fraction_numerical': 0.0012507817385866166, 'mean_word_length': 1.9795062878435026, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1291360', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1776, 'n_words': 385}