<commit_before>/* Copyright 2015, Yahoo Inc.
   Copyrights licensed under the MIT License.
   See the accompanying LICENSE file for terms. */

var path = require('path');
var xtend = require('xtend');
var pathToRegexp = require('path-to-regexp');
var browserify = require('browserify');
var watchify = require('watchify');
var treenherit = require('mendel-treenherit');

var parseConfig = require('mendel-config');
var validVariations = require('mendel-config/variations');
var resolveVariations = require('mendel-development/resolve-variations');
var Swatch = require('./swatch');
var CachedStreamCollection = require('./cached-stream-collection');
var MendelLoader = require('mendel-development-loader');

module.exports = MendelMiddleware;

function MendelMiddleware(opts) {
    var config = parseConfig(opts);
    var existingVariations = validVariations(config);
    var base = config.base || 'base';

    existingVariations = existingVariations.concat({
        id: base,
        chain: [config.basetree || 'base'],
    });

    // server side watch
    var swatch = new Swatch(opts).watch(); // eslint-disable-line no-unused-vars

    var route = config.variationsroute || '/mendel/:variations/:bundle\.js';
    var getPath = pathToRegexp.compile(route);
    var keys = [];
    var bundleRoute = pathToRegexp(route, keys);
    var bundles = config.bundles.reduce(function(acc, bundle) {
        acc[bundle.id] = bundle;
        return acc;
    }, {});

    var loader = new MendelLoader(existingVariations, config, module.parent);

    return function(req, res, next) {
        req.mendel = req.mendel || {};

        req.mendel.getURL = function(bundle, variations) {
            var vars = variations.join(',') || config.base;
            return getPath({bundle: bundle, variations: vars});
        };

        req.mendel.resolver = loader.resolver.bind(loader);

        // Match bundle route<commit_msg>Added `watch` config option to disable swatch in dev middleware.<commit_after>/* Copyright 2015, Yahoo Inc.
   Copyrights licensed under the MIT License.
   See the accompanying LICENSE file for terms. */

var path = require('path');
var xtend = require('xtend');
var pathToRegexp = require('path-to-regexp');
var browserify = require('browserify');
var watchify = require('watchify');
var treenherit = require('mendel-treenherit');

var parseConfig = require('mendel-config');
var validVariations = require('mendel-config/variations');
var resolveVariations = require('mendel-development/resolve-variations');
var Swatch = require('./swatch');
var CachedStreamCollection = require('./cached-stream-collection');
var MendelLoader = require('mendel-development-loader');

module.exports = MendelMiddleware;

function MendelMiddleware(opts) {
    var config = parseConfig(opts);
    var existingVariations = validVariations(config);
    var base = config.base || 'base';

    existingVariations = existingVariations.concat({
        id: base,
        chain: [config.basetree || 'base'],
    });

    // server side watch
    if (config.watch !== false) {
        var swatch = new Swatch(opts).watch(); // eslint-disable-line no-unused-vars
    }

    var route = config.variationsroute || '/mendel/:variations/:bundle\.js';
    var getPath = pathToRegexp.compile(route);
    var keys = [];
    var bundleRoute = pathToRegexp(route, keys);
    var bundles = config.bundles.reduce(function(acc, bundle) {
        acc[bundle.id] = bundle;
        return acc;
    }, {});

    var loader = new MendelLoader(existingVariations, config, module.parent);

    return function(req, res, next) {
        req.mendel = req.mendel || {};

        req.mendel.getURL = function(bundle, variations) {
            var vars = variations.join(',') || config.base;
            return getPath({bundle: bundle, variations: vars});
        };

        req.mendel.resolver = loader.resolver.bind(loader);

        // Match bundle route
{'fraction_non_alphanumeric': 0.13535559520774917, 'fraction_numerical': 0.002039255671679837, 'mean_word_length': 3.5841121495327104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5555741', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1129, 'n_words': 279}