<commit_before>'use strict';

var uuid = require('node-uuid'),
    Q = require('q'),
    _ = require('lodash'),
    path = require('path'),
    config = require(path.join(__dirname,'../config')),
    platforms = config.platforms;

function Manifold(manifoldLib){
    this.lib = manifoldLib;
}

Manifold.prototype.createManifestFromUrl = function(url,client){
    var self = this;

    if(url.indexOf('http') === -1){
        url = 'http://'+url;
    }

    return Q.Promise(function(resolve,reject){
        self.lib.manifestTools.getManifestFromSite(url, function(err, manifestInfo) {
            if (err) {
                if(err.message !== 'Failed to retrieve manifest from site.'){
                    return reject(err);
                }else{
                    manifestInfo = {
                        content: {
                        },
                        format: 'w3c'
                    };
                }
            }

            var manifest = _.assign(manifestInfo,{ id: uuid.v4().slice(0,8) });

            self.validateManifest(manifest)
                .then(function(manifest){
                    client.set(manifest.id,JSON.stringify(manifest));
                    return resolve(manifest);
                })
                .fail(reject);
        });
    });
};

Manifold.prototype.createManifestFromFile = function(file,client){
    var self = this;

    return Q.Promise(function(resolve,reject){
        self.lib.manifestTools.getManifestFromFile(file.path, function (err, manifestInfo) {
            if (err) { return reject(err); }

            var manifest = _.assign(manifestInfo,{ id: uuid.v4().slice(0,8) });
            self.validateManifest(manifest)
                .then(function(manifest){
                    client.set(manifest.id,JSON.stringify(manifest));
                    return resolve(manifest);
                })
                .fail(reject);
        });
    });
};

Manifold.prototype.validateManifest = function(manifest){
    var self = this;

    return Q.Promise(function(resolve,reject){
        self.lib.manifestTools.validateManifest(manifest, platforms, function(err,results){
            if(err){ return reject(err); }

            var errors = _.filter(results,{level: 'error'}),
            suggestions = _.filter(results,{level: 'suggestion'}),
            warnings = _.filter(results,{level: 'warning'});

            self.assignValidationErrors(errors,manifest);
            self.assignSuggestions(suggestions,manifest);
            self.assignWarnings(warnings,manifest);

            return resolve(manifest);
        });
    });
};

Manifold.prototype.updateManifest = function(manifestId,updates,client) {
    var self = this;

    return Q.Promise(function(resolve,reject){
        client.get(manifestId,function(err,reply){
            if(err) return reject(err);
            if(!reply) return reject(new Error('Manifest not found'));

            var manifest = JSON.parse(reply);
            manifest.content = updates;

            return self.validateManifest(manifest)
                .then(function(manifest){
                    client.set(manifest.id,JSON.stringify(manifest));

                    resolve(manifest);
                });
        });
    });
};

Manifold.prototype.normalize = function(manifest){
    var self = this;

    return Q.Promise(function(resolve,reject){
        console.log('Validating start url...');
        self.lib.manifestTools.validateAndNormalizeStartUrl(manifest.content.start_url,manifest,function(err,normManifest){
            if(err){
                console.log('Normalizing Error',err);
                return reject(err);
            }

            manifest = _.assign(manifest,normManifest);

            resolve(manifest);
        });
    });
};

Manifold.prototype.createProject = function(manifest,outputDir,platforms,buildCordova){
    var self = this;

    return Q.Promise(function(resolve, reject){
        var cleanManifest = _.omit(manifest,'id');
        console.log('Building the project...',cleanManifest,outputDir,platforms,buildCordova);
        self.lib.projectBuilder.createApps(cleanManifest, outputDir, platforms, buildCordova, function (err) {

            if(err){
                console.log('Create Projects Errors!!!',err);
                return reject(err);
            }

            return resolve();
        });
    });
};

Manifold.prototype.assignValidationErrors = function(errors,manifest){
    var data = { errors: []};

    _.each(errors,function(e){
        if(_.any(data.errors,'member',e.member)){
            var error = _.find(data.errors,{ memeber: e.member });
            error.issues = error.issues || [];
            error.issues.push({ description: e.description, platform: e.platform, code: e.code });
        }else{
            data.errors.push({
                member: e.member,
                issues: [{
                    description: e.description,
                    platform: e.platform,
                    code: e.code
                }]
            });
        }
    });

    manifest = _.assign(manifest,data);
};

Manifold.prototype.assignSuggestions = function(suggestions,manifest){
    var data = { suggestions: []};

    _.each(suggestions,function(s){
        if(_.any(data.suggestions,'member',s.member)){<commit_msg>One more quick fix for errors<commit_after>'use strict';

var uuid = require('node-uuid'),
    Q = require('q'),
    _ = require('lodash'),
    path = require('path'),
    config = require(path.join(__dirname,'../config')),
    platforms = config.platforms;

function Manifold(manifoldLib){
    this.lib = manifoldLib;
}

Manifold.prototype.createManifestFromUrl = function(url,client){
    var self = this;

    if(url.indexOf('http') === -1){
        url = 'http://'+url;
    }

    return Q.Promise(function(resolve,reject){
        self.lib.manifestTools.getManifestFromSite(url, function(err, manifestInfo) {
            if (err) {
                if(err.message !== 'Failed to retrieve manifest from site.'){
                    return reject(err);
                }else{
                    manifestInfo = {
                        content: {
                        },
                        format: 'w3c'
                    };
                }
            }

            var manifest = _.assign(manifestInfo,{ id: uuid.v4().slice(0,8) });

            self.validateManifest(manifest)
                .then(function(manifest){
                    client.set(manifest.id,JSON.stringify(manifest));
                    return resolve(manifest);
                })
                .fail(reject);
        });
    });
};

Manifold.prototype.createManifestFromFile = function(file,client){
    var self = this;

    return Q.Promise(function(resolve,reject){
        self.lib.manifestTools.getManifestFromFile(file.path, function (err, manifestInfo) {
            if (err) { return reject(err); }

            var manifest = _.assign(manifestInfo,{ id: uuid.v4().slice(0,8) });
            self.validateManifest(manifest)
                .then(function(manifest){
                    client.set(manifest.id,JSON.stringify(manifest));
                    return resolve(manifest);
                })
                .fail(reject);
        });
    });
};

Manifold.prototype.validateManifest = function(manifest){
    var self = this;

    return Q.Promise(function(resolve,reject){
        self.lib.manifestTools.validateManifest(manifest, platforms, function(err,results){
            if(err){ return reject(err); }

            var errors = _.filter(results,{level: 'error'}),
            suggestions = _.filter(results,{level: 'suggestion'}),
            warnings = _.filter(results,{level: 'warning'});

            self.assignValidationErrors(errors,manifest);
            self.assignSuggestions(suggestions,manifest);
            self.assignWarnings(warnings,manifest);

            return resolve(manifest);
        });
    });
};

Manifold.prototype.updateManifest = function(manifestId,updates,client) {
    var self = this;

    return Q.Promise(function(resolve,reject){
        client.get(manifestId,function(err,reply){
            if(err) return reject(err);
            if(!reply) return reject(new Error('Manifest not found'));

            var manifest = JSON.parse(reply);
            manifest.content = updates;

            return self.validateManifest(manifest)
                .then(function(manifest){
                    client.set(manifest.id,JSON.stringify(manifest));

                    resolve(manifest);
                });
        });
    });
};

Manifold.prototype.normalize = function(manifest){
    var self = this;

    return Q.Promise(function(resolve,reject){
        console.log('Validating start url...');
        self.lib.manifestTools.validateAndNormalizeStartUrl(manifest.content.start_url,manifest,function(err,normManifest){
            if(err){
                console.log('Normalizing Error',err);
                return reject(err);
            }

            manifest = _.assign(manifest,normManifest);

            resolve(manifest);
        });
    });
};

Manifold.prototype.createProject = function(manifest,outputDir,platforms,buildCordova){
    var self = this;

    return Q.Promise(function(resolve, reject){
        var cleanManifest = _.omit(manifest,'id');
        console.log('Building the project...',cleanManifest,outputDir,platforms,buildCordova);
        self.lib.projectBuilder.createApps(cleanManifest, outputDir, platforms, buildCordova, function (err) {

            if(err){
                console.log('Create Projects Errors!!!',err);
                return reject(err);
            }

            return resolve();
        });
    });
};

Manifold.prototype.assignValidationErrors = function(errors,manifest){
    var data = { errors: []};

    _.each(errors,function(e){
        if(_.any(data.errors,'member',e.member)){
            var error = _.find(data.errors,'member',e.member);
            error.issues = error.issues || [];
            error.issues.push({ description: e.description, platform: e.platform, code: e.code });
        }else{
            data.errors.push({
                member: e.member,
                issues: [{
                    description: e.description,
                    platform: e.platform,
                    code: e.code
                }]
            });
        }
    });

    manifest = _.assign(manifest,data);
};

Manifold.prototype.assignSuggestions = function(suggestions,manifest){
    var data = { suggestions: []};

    _.each(suggestions,function(s){
        if(_.any(data.suggestions,'member',s.member)){
{'fraction_non_alphanumeric': 0.13269519519519518, 'fraction_numerical': 0.0015015015015015015, 'mean_word_length': 2.0439874321622393, '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': '7453608', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2747, 'n_words': 458}