<commit_before>module.exports = function (grunt) {
    var esperanto = require('esperanto');
    var path = require('path');
    var Promise = require('es6-promise').Promise;
    var TMP_DIR = 'build/tmp';

    function moveComments(code) {
        var comments = [], rest = [];
        code.split('\n').forEach(function (line) {
            if (line.trim().slice(0, 3) === '//!') {
                comments.push(line.trim());
            } else {
                rest.push(line);
            }
        });

        return comments.concat([''], rest).join('\n');
    }

    var headerCache = {};
    function getHeaderByFile(headerFile) {
        if (!(headerFile in headerCache)) {
            headerCache[headerFile] = grunt.file.read(headerFile);
        }
        return headerCache[headerFile];
    }

    function transpile(opts) {
        // base, entry, skip, headerFile, skipLines, target
        var umdName = opts.headerFile ? 'not_used' : opts.umdName,
            headerFile = opts.headerFile ? opts.headerFile : 'templates/default.js',
            header = getHeaderByFile(headerFile),
            skipLines = opts.skipLines ? opts.skipLines : 5;

        return esperanto.bundle({
            base: opts.base,
            entry: opts.entry,
            skip: opts.skip || []
        }).then(function (bundle) {
            var umd = bundle.toUmd({name: umdName}),
                fixed = header + umd.code.split('\n').slice(skipLines).join('\n');
            if (opts.moveComments) {
                fixed = moveComments(fixed);
            }
            grunt.file.write(opts.target, fixed);
        });
    }

    function transpileMany(opts) {
        var batchSize = 50,
            promise = Promise.resolve(null),
            files = grunt.file.expand({cwd: opts.base}, opts.pattern),
            i,
            transpileOne = function (i) {
                promise = promise.then(function () {
                    return Promise.all(files.slice(i, i + batchSize).map(function (file) {
                        return transpile({
                            base: opts.base,
                            entry: file,
                            headerFile: opts.headerFile,
                            skip: opts.skip,
                            skipLines: opts.skipLines,
                            moveComments: opts.moveComments,
                            target: path.join(opts.targetDir, file)
                        });
                    }));
                });
            };

        for (i = 0; i < files.length; i += batchSize) {
            transpileOne(i);
        }

        return promise;
    }

    function prepareTemp(base) {
        var files = grunt.file.expand({cwd: base}, '**/*.js'),
            tmpDir = TMP_DIR;
        if (grunt.file.exists(tmpDir)) {
            return;
        }
        files.forEach(function (file) {
            grunt.file.copy(path.join(base, file), path.join(tmpDir, file));
        });
    }

    function transpileCode(opts) {
        var entry = opts.entry || path.basename(opts.target);
        prepareTemp(opts.base);
        grunt.file.write(path.join(TMP_DIR, entry), opts.code);
        return transpile({
            base: TMP_DIR,
            entry: entry,
            umdName: opts.umdName || 'not_used',
            headerFile: opts.headerFile,
            skipLines: opts.skipLines,
            moveComments: opts.moveComments,
            target: opts.target,
            skip: opts.skip
        });
    }

    function generateLocales(target, localeFiles) {
        var files = localeFiles,
            code = files.map(function (file) {
                var identifier = path.basename(file, '.js').replace('-', '_');
                return 'import ' + identifier + ' from "./' + file + '";';
            }).concat([
                // Reset the language back to 'en', because every defineLocale
                // also sets it.
                'moment.locale(\'en\');'
            ]).join('\n');
        return transpileCode({
            base: 'src',
            code: code,
            target: target,
            skip: ['moment'],
            headerFile: 'templates/locale-header.js',
            skipLines: 5
        });
    }

    function generateMomentWithLocales(target, localeFiles) {
        var files = localeFiles,
            importCode = files.map(function (file) {
                var identifier = path.basename(file, '.js').replace('-', '_');
                var fileNoExt = file.replace('.js', '');
                return 'import ' + identifier + ' from "./' + fileNoExt + '";';
            }).join('\n'),
            code = 'import * as moment_export from "./moment";\n\n' +
                importCode + '\n\n' +
                'export default moment_export;';

        return transpileCode({
            base: 'src',
            code: code,
            umdName: 'moment',
            target: target
        }).then(function () {
            var code = grunt.file.read(target);
            var getDefaultRegExp = new RegExp('var ([a-z$_]+) =\\s+{[^]\\s+get default \\(\\) { return ([a-z$_]+); }[^]\\s+}', '');
            var crap = code.match(getDefaultRegExp);
            if (crap.length !== 3) {
                grunt.file.write('/tmp/crap.js', code);
                throw new Error('Failed to detect get default crap, check /tmp/crap.js');
            }
            code = code.replace(getDefaultRegExp, '');

            var buildExportVars = ['moment_with_locales', 'moment_with_locales_custom'];
            buildExportVars.forEach(function (buildExportVar) {
                var languageReset = buildExportVar  + '.locale(\'en\');';
                code = code.replace('var ' + buildExportVar + ' = ' + crap[1] + ';',
                                    'var ' + buildExportVar + ' = ' + crap[2] + ';\n' +
                                    '    ' + languageReset);
            });

            if (code.match('get default')) {
                grunt.file.write('/tmp/crap.js', code);
                throw new Error('Stupid shit es6 get default plaguing the code, check /tmp/crap.js');
            }<commit_msg>Add comment on top of moment-with-locales.js<commit_after>module.exports = function (grunt) {
    var esperanto = require('esperanto');
    var path = require('path');
    var Promise = require('es6-promise').Promise;
    var TMP_DIR = 'build/tmp';

    function moveComments(code, moveType) {
        var comments = [], rest = [], skipId = -1;
        code.split('\n').forEach(function (line, i) {
            var isComment = false;
            if (line.trim().slice(0, 3) === '//!') {
                isComment = true;
            }
            if (isComment && moveType === 'main-only') {
                if (i === skipId + 1 ||
                        line.trim() === '//! moment.js locale configuration') {
                    skipId = i;
                    // continue to next line
                    return;
                }
            }

            if (isComment) {
                comments.push(line.trim());
            } else {
                rest.push(line);
            }
        });

        return comments.concat([''], rest).join('\n');
    }

    var headerCache = {};
    function getHeaderByFile(headerFile) {
        if (!(headerFile in headerCache)) {
            headerCache[headerFile] = grunt.file.read(headerFile);
        }
        return headerCache[headerFile];
    }

    function transpile(opts) {
        // base, entry, skip, headerFile, skipLines, target
        var umdName = opts.headerFile ? 'not_used' : opts.umdName,
            headerFile = opts.headerFile ? opts.headerFile : 'templates/default.js',
            header = getHeaderByFile(headerFile),
            skipLines = opts.skipLines ? opts.skipLines : 5;

        return esperanto.bundle({
            base: opts.base,
            entry: opts.entry,
            skip: opts.skip || []
        }).then(function (bundle) {
            var umd = bundle.toUmd({name: umdName}),
                fixed = header + umd.code.split('\n').slice(skipLines).join('\n');
            if (opts.moveComments) {
                fixed = moveComments(fixed, opts.moveComments);
            }
            grunt.file.write(opts.target, fixed);
        });
    }

    function transpileMany(opts) {
        var batchSize = 50,
            promise = Promise.resolve(null),
            files = grunt.file.expand({cwd: opts.base}, opts.pattern),
            i,
            transpileOne = function (i) {
                promise = promise.then(function () {
                    return Promise.all(files.slice(i, i + batchSize).map(function (file) {
                        return transpile({
                            base: opts.base,
                            entry: file,
                            headerFile: opts.headerFile,
                            skip: opts.skip,
                            skipLines: opts.skipLines,
                            moveComments: opts.moveComments,
                            target: path.join(opts.targetDir, file)
                        });
                    }));
                });
            };

        for (i = 0; i < files.length; i += batchSize) {
            transpileOne(i);
        }

        return promise;
    }

    function prepareTemp(base) {
        var files = grunt.file.expand({cwd: base}, '**/*.js'),
            tmpDir = TMP_DIR;
        if (grunt.file.exists(tmpDir)) {
            return;
        }
        files.forEach(function (file) {
            grunt.file.copy(path.join(base, file), path.join(tmpDir, file));
        });
    }

    function transpileCode(opts) {
        var entry = opts.entry || path.basename(opts.target);
        prepareTemp(opts.base);
        grunt.file.write(path.join(TMP_DIR, entry), opts.code);
        return transpile({
            base: TMP_DIR,
            entry: entry,
            umdName: opts.umdName || 'not_used',
            headerFile: opts.headerFile,
            skipLines: opts.skipLines,
            moveComments: opts.moveComments,
            target: opts.target,
            skip: opts.skip
        });
    }

    function generateLocales(target, localeFiles) {
        var files = localeFiles,
            code = files.map(function (file) {
                var identifier = path.basename(file, '.js').replace('-', '_');
                return 'import ' + identifier + ' from "./' + file + '";';
            }).concat([
                // Reset the language back to 'en', because every defineLocale
                // also sets it.
                'moment.locale(\'en\');'
            ]).join('\n');
        return transpileCode({
            base: 'src',
            code: code,
            target: target,
            skip: ['moment'],
            headerFile: 'templates/locale-header.js',
            skipLines: 5
        });
    }

    function generateMomentWithLocales(target, localeFiles) {
        var files = localeFiles,
            importCode = files.map(function (file) {
                var identifier = path.basename(file, '.js').replace('-', '_');
                var fileNoExt = file.replace('.js', '');
                return 'import ' + identifier + ' from "./' + fileNoExt + '";';
            }).join('\n'),
            code = 'import * as moment_export from "./moment";\n\n' +
                importCode + '\n\n' +
                'export default moment_export;';

        return transpileCode({
            base: 'src',
            code: code,
            umdName: 'moment',
            target: target,
            moveComments: 'main-only'
        }).then(function () {
            var code = grunt.file.read(target);
            var getDefaultRegExp = new RegExp('var ([a-z$_]+) =\\s+{[^]\\s+get default \\(\\) { return ([a-z$_]+); }[^]\\s+}', '');
            var crap = code.match(getDefaultRegExp);
            if (crap.length !== 3) {
                grunt.file.write('/tmp/crap.js', code);
                throw new Error('Failed to detect get default crap, check /tmp/crap.js');
            }
            code = code.replace(getDefaultRegExp, '');

            var buildExportVars = ['moment_with_locales', 'moment_with_locales_custom'];
            buildExportVars.forEach(function (buildExportVar) {
                var languageReset = buildExportVar  + '.locale(\'en\');';
                code = code.replace('var ' + buildExportVar + ' = ' + crap[1] + ';',
                                    'var ' + buildExportVar + ' = ' + crap[2] + ';\n' +
                                    '    ' + languageReset);
            });

            if (code.match('get default')) {
                grunt.file.write('/tmp/crap.js', code);
                throw new Error('Stupid shit es6 get default plaguing the code, check /tmp/crap.js');
            }
{'fraction_non_alphanumeric': 0.13881948782011244, 'fraction_numerical': 0.0020299812617114305, 'mean_word_length': 1.4896015549076773, 'pattern_counts': {'":': 0, '<': 5, '<?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': 8, 'profanity_word_list.json': 4, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3993226', 'n_tokens_mistral': 3579, 'n_tokens_neox': 3443, 'n_words': 728}