<commit_before>#!/usr/bin/env node

var program = require('commander'),
    crypto = require('crypto'),
    fs = require('fs'),
    git = require('git-rev'),
    path = require('path'),
    rimraf = require('rimraf'),
    spawn = require('child_process').spawn;

function list(val) {
      return val.split(',');
}

program
    .option('-n, --no-minify', 'Do no minify package')
    .option('--no-symlink', 'Do not symlink new bundle')
    .option('-t, --tag [version]', 'Build specific version')
    .option('-m, --modules [module1,module2,...]',
            'Include only these modules in bundle', list)
    .parse(process.argv);

var build = function(tag, cleanup) {
    var filename;
    if (program.modules) {
        // User wants a custom bundle
        var patterns = program.modules.sort().join('&'),
            shasum = crypto.createHash('sha1');
        shasum.update(tag + '?' + patterns);

        var hex = shasum.digest('hex').substr(0,8);
        filename = 'patterns-' + tag+ '-' + hex + (program.minify?'.min':'') + '.js';
    } else {
        filename = 'patterns-' + tag + (program.minify?'.min':'') + '.js';
    }

    var modules = program.modules || ['main'];
    modules = modules.sort();

    // Generate args that are passed to jam
    var fullname = path.join(__dirname, 'bundles', filename);

    // The CLI interface is:
    // jam compile -i pat1 [-i pat2 [-i pat3]] -o outfile
    var args = [
            'build',
            '--almond',
            '--verbose',
            '-i', modules.join(','),
            '-o', fullname
            ];

    if (!program.minify)
        args.push('--no-minify');

    var p = spawn(path.join(__dirname, 'node_modules/.bin/bungle'), args);
    p.stdout.on('data', function(data){
        console.log(data.toString());
    });
    p.stderr.on('data', function(data){
        console.log('stderr ' + data);
    });
    p.on('exit', function(code) {
        if (code === 0) {
            var header = "/**\n" +
                " * Patterns bundle " + tag + (hex?"-"+hex:"") + "\n" +
                " *\n" +
                " * See http://patternslib.com/ for more information.\n" +
                " *\n";
            if (modules.indexOf('main') === -1) {
                header += " * Included patterns:\n * - ";
                header += program.modules.sort().join("\n * - ") + "\n";
            }
            header += " */";

            var js = fs.readFileSync(fullname);

            var init = '';
            if (modules.indexOf('main') === -1) {
                var deps = modules
                    .map(function(e){ return '"'+e+'"';  })
                    .join(', ');

                init = "require(['registry', " + deps +
                    "], function(r){r.init();});";
            }

            var stream = fs.createWriteStream(fullname);
            stream.write(header + js + init);
            stream.end();
            if (program.symlink) {
                var link = path.join(__dirname, 'bundles',<commit_msg>more linebreaks, beautify and fix #258<commit_after>#!/usr/bin/env node

var program = require('commander'),
    crypto = require('crypto'),
    fs = require('fs'),
    git = require('git-rev'),
    path = require('path'),
    rimraf = require('rimraf'),
    spawn = require('child_process').spawn;

function list(val) {
      return val.split(',');
}

program
    .option('-n, --no-minify', 'Do no minify package')
    .option('--no-symlink', 'Do not symlink new bundle')
    .option('-t, --tag [version]', 'Build specific version')
    .option('-m, --modules [module1,module2,...]',
            'Include only these modules in bundle', list)
    .parse(process.argv);

var build = function(tag, cleanup) {
    var filename;
    if (program.modules) {
        // User wants a custom bundle
        var patterns = program.modules.sort().join('&'),
            shasum = crypto.createHash('sha1');
        shasum.update(tag + '?' + patterns);

        var hex = shasum.digest('hex').substr(0,8);
        filename = 'patterns-' + tag+ '-' + hex + (program.minify?'.min':'') + '.js';
    } else {
        filename = 'patterns-' + tag + (program.minify?'.min':'') + '.js';
    }

    var modules = program.modules || ['main'];
    modules = modules.sort();

    // Generate args that are passed to jam
    var fullname = path.join(__dirname, 'bundles', filename);

    // The CLI interface is:
    // jam compile -i pat1 [-i pat2 [-i pat3]] -o outfile
    var args = [
            'build',
            '--almond',
            '--verbose',
            '-i', modules.join(','),
            '-o', fullname
            ];

    if (!program.minify)
        args.push('--no-minify');

    var p = spawn(path.join(__dirname, 'node_modules/.bin/bungle'), args);
    p.stdout.on('data', function(data){
        console.log(data.toString());
    });
    p.stderr.on('data', function(data){
        console.log('stderr ' + data);
    });
    p.on('exit', function(code) {
        if (code === 0) {
            var header = "/**\n" +
                " * Patterns bundle " + tag + (hex?"-"+hex:"") + "\n" +
                " *\n" +
                " * See http://patternslib.com/ for more information.\n" +
                " *\n";
            if (modules.indexOf('main') === -1) {
                header += " * Included patterns:\n * - ";
                header += program.modules.sort().join("\n * - ") + "\n";
            }
            header += " */\n\n";

            var js = fs.readFileSync(fullname);

            var init = '';
            if (modules.indexOf('main') === -1) {
                var deps = modules
                    .map(function(e){ return '"'+e+'"';  })
                    .join(', ');

                init = "require(['registry', " + deps +
                    "], function(r){r.init();});";
            }

            var stream = fs.createWriteStream(fullname);
            stream.write(header + js + init);
            stream.end();
            if (program.symlink) {
                var link = path.join(__dirname, 'bundles',
{'fraction_non_alphanumeric': 0.1893901834407536, 'fraction_numerical': 0.0041315485043794415, 'mean_word_length': 1.7838086476540937, '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': '1802196', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1808, 'n_words': 413}