<commit_before>var fs = require('fs'),
    URL = require('url'),
    fileUtils = require('../fileUtils'),
    step = require('step'),
    _ = require('underscore'),
    error = require('../error');

var SenchaJSBuilder = module.exports = function (config) {
    // Expects: config.url, config.body
    _.extend(this, config);
    if (this.version !== 2 && this.version !== 3) {
        throw new Error("SenchaJSBuilder: Unsupported version: " + this.version);
    }

    this.pkgIndex = {};
    if (this.version === 2) {
        this.body.packages = this.body.pkgs;
        delete this.body.pkgs;
    }

    this.body.builds = this.body.builds || [];

    ['packages', 'builds'].forEach(function (sectionName) {
        if (sectionName in this.body) {
            this.body[sectionName].forEach(function (pkg) {
                if (this.version === 2) {
                    pkg.packages = pkg.pkgDeps;
                    delete pkg.pkgDeps;
                    pkg.target = pkg.file;
                    delete pkg.file;
                    pkg.files = pkg.fileIncludes.map(function (fileInclude) {
                        return {
                            path: fileInclude.path.replace(/^src\/ext-core/, "../ext-core"), // FIXME
                            name: fileInclude.text
                        };
                    });
                    delete pkg.fileIncludes;
                }
                this.pkgIndex[pkg.name] = this.pkgIndex[pkg.target] = pkg;
                if ('id' in pkg) {
                    this.pkgIndex[pkg.id] = pkg;
                }
            }, this);
       }
    }, this);
};

SenchaJSBuilder.prototype = {
    resolvePkg: function (pkg, cb) {
        var that = this,
            assetConfigs = [];
        step(
            function () {
                if (pkg.packages && pkg.packages.length > 0) {
                    pkg.packages.forEach(function (pkgTargetFileName) {
                        var callback = this.parallel();
                        that.resolvePkg(that.pkgIndex[pkgTargetFileName], error.passToFunction(cb, function (pkgAssetConfigs) {
                            [].push.apply(assetConfigs, pkgAssetConfigs);
                            callback();
                        }));
                    }, this);
                } else {
                    process.nextTick(this);
                }
            },
            function () {
                var urls = (pkg.files || []).map(function (fileDef) {
                    return URL.parse(URL.resolve(that.url, fileDef.path + fileDef.name));
                });
                if (!urls.length) {
                    return cb(null, assetConfigs);
                }
                if (pkg.name === 'Ext Base') {
                    step(
                        function () {
                            var innerGroup = this.group();
                            urls.forEach(function (url) {
                                fs.readFile(fileUtils.fileUrlToFsPath(url), 'utf8', innerGroup());
                            });
                        },
                        error.passToFunction(cb, function (fileBodies, i) {
                            assetConfigs.push({
                                type: 'JavaScript',
                                isDirty: true,
                                originalSrc: fileBodies.join("\n"),
                                originalRelations: [],
                                url: urls[i]
                            });
                            cb(null, assetConfigs);
                        })
                    );
                } else {
                    var cssUrls = [];
                    urls.forEach(function (url) {
                        if (/\.css$/.test(url.pathname)) {
                            cssUrls.push(url);
                        } else {
                            assetConfigs.push({
                                url: url,
                                originalRelations: []
                            });
                        }
                    });
                    if (cssUrls.length && /^Ext JS Library [23].\d.\d/.test(that.body.licenseText)) {
                        // Stupid ExtJS 3 has CSS url()s relative to the target paths of their
                        // bundles, NOT the source files!
                        // Issue reported here: http://www.extjs.com/forum/showthread.php?p=330222<commit_msg>SenchaJSBuilder resolver: Fixed url of ext-base.js<commit_after>var fs = require('fs'),
    URL = require('url'),
    fileUtils = require('../fileUtils'),
    step = require('step'),
    _ = require('underscore'),
    error = require('../error');

var SenchaJSBuilder = module.exports = function (config) {
    // Expects: config.url, config.body
    _.extend(this, config);
    if (this.version !== 2 && this.version !== 3) {
        throw new Error("SenchaJSBuilder: Unsupported version: " + this.version);
    }

    this.pkgIndex = {};
    if (this.version === 2) {
        this.body.packages = this.body.pkgs;
        delete this.body.pkgs;
    }

    this.body.builds = this.body.builds || [];

    ['packages', 'builds'].forEach(function (sectionName) {
        if (sectionName in this.body) {
            this.body[sectionName].forEach(function (pkg) {
                if (this.version === 2) {
                    pkg.packages = pkg.pkgDeps;
                    delete pkg.pkgDeps;
                    pkg.target = pkg.file;
                    delete pkg.file;
                    pkg.files = pkg.fileIncludes.map(function (fileInclude) {
                        return {
                            path: fileInclude.path.replace(/^src\/ext-core/, "../ext-core"), // FIXME
                            name: fileInclude.text
                        };
                    });
                    delete pkg.fileIncludes;
                }
                this.pkgIndex[pkg.name] = this.pkgIndex[pkg.target] = pkg;
                if ('id' in pkg) {
                    this.pkgIndex[pkg.id] = pkg;
                }
            }, this);
       }
    }, this);
};

SenchaJSBuilder.prototype = {
    resolvePkg: function (pkg, cb) {
        var that = this,
            assetConfigs = [];
        step(
            function () {
                if (pkg.packages && pkg.packages.length > 0) {
                    pkg.packages.forEach(function (pkgTargetFileName) {
                        var callback = this.parallel();
                        that.resolvePkg(that.pkgIndex[pkgTargetFileName], error.passToFunction(cb, function (pkgAssetConfigs) {
                            [].push.apply(assetConfigs, pkgAssetConfigs);
                            callback();
                        }));
                    }, this);
                } else {
                    process.nextTick(this);
                }
            },
            function () {
                var urls = (pkg.files || []).map(function (fileDef) {
                    return URL.parse(URL.resolve(that.url, fileDef.path + fileDef.name));
                });
                if (!urls.length) {
                    return cb(null, assetConfigs);
                }
                if (pkg.name === 'Ext Base') {
                    step(
                        function () {
                            var innerGroup = this.group();
                            urls.forEach(function (url) {
                                fs.readFile(fileUtils.fileUrlToFsPath(url), 'utf8', innerGroup());
                            });
                        },
                        error.passToFunction(cb, function (fileBodies, i) {
                            assetConfigs.push({
                                type: 'JavaScript',
                                isDirty: true,
                                originalSrc: fileBodies.join("\n"),
                                originalRelations: [], // Save the trouble of parsing it to find zero relations
                                url: URL.parse(URL.resolve(that.url, pkg.target))
                            });
                            cb(null, assetConfigs);
                        })
                    );
                } else {
                    var cssUrls = [];
                    urls.forEach(function (url) {
                        if (/\.css$/.test(url.pathname)) {
                            cssUrls.push(url);
                        } else {
                            assetConfigs.push({
                                url: url,
                                originalRelations: []
                            });
                        }
                    });
                    if (cssUrls.length && /^Ext JS Library [23].\d.\d/.test(that.body.licenseText)) {
                        // Stupid ExtJS 3 has CSS url()s relative to the target paths of their
                        // bundles, NOT the source files!
                        // Issue reported here: http://www.extjs.com/forum/showthread.php?p=330222
{'fraction_non_alphanumeric': 0.1167608286252354, 'fraction_numerical': 0.003323363243602526, 'mean_word_length': 1.0269420745397395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7363799', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2132, 'n_words': 419}