<commit_before>'use strict';

var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
var utils = require('../../helpers/utils');
var lss = require('less');
var jde = require('jade');
var UglifyJS = require('uglify-js');
var brwserify = require('browserify');
var Migrations = require('../migrations');
var Q = require('q');

exports = module.exports = BuildSystem;

function BuildSystem(stageMethods, $q) {
    var basePath = process.cwd();

    var _mkdir = function(directoryPath) {
        var defer = $q.defer();
        mkdirp(directoryPath, function(error) {
            if(error) {
                defer.reject(error);
            }
            else {
                defer.resolve(directoryPath);
            }
        });
        return defer.promise;
    };

    /**
     * fire build:scripts
     */
    stageMethods.addMethod('build', function scripts(app) {
        var scriptsPath = '';

        if(app.container.numberOfApps() > 1) {
            scriptsPath = path.join(basePath, '.fire', '.build', 'public', app.name, 'scripts');
        }
        else {
            scriptsPath = path.join(basePath, '.fire', '.build', 'public', 'scripts');
        }

        return _mkdir(scriptsPath)
            .then(function() {
                var savePath = path.join(scriptsPath, 'fire.js');
                var writeStream = fs.createWriteStream(savePath);

                return app.bridge.generate(writeStream);
            });
    });

    /*
     * fire build:api
     */
    stageMethods.addMethod('build', function api(app) {
        var apiPath = '';

        if(app.container.numberOfApps() > 1) {
            apiPath = path.join(basePath, '.fire', '.build', 'api', app.name);
        }
        else {
            apiPath = path.join(basePath, '.fire', '.build', 'api');
        }

        return _mkdir(apiPath)
            .then(function() {
                var result = $q.when(true);

                app.models.forEach(function(model) {
                    result = result.then(function() {
                        if(model.isShared() && !app.settings('isMaster') && !app.settings('includeAPI')) {
                            //
                        }
                        else {
                            var savePath = path.join(apiPath, model.getFileName() + '.js');
                            var writeStream = fs.createWriteStream(savePath);
                            return app.APIBuild.generateModelController(model, writeStream);
                        }
                    });
                });

                return result;
            });
    });

    /*
     * fire build:migrations
     */
    stageMethods.addMethod('build', function migrations(app) {
        var migrationsPath = '';
        if(app.container.numberOfApps() > 1) {
            migrationsPath = path.join(basePath, '.fire', 'migrations', app.name);
        }
        else {
            migrationsPath = path.join(basePath, '.fire', 'migrations');
        }

        var defer = $q.defer();
        _mkdir(migrationsPath)
            .then(function() {
                var Generate = require('../migrations/generate');
                var generate = new Generate(app, basePath);
                generate.delegate = {
                    addMigration: function(fileName, stream) {
                        var savePath = path.join(migrationsPath, fileName);

                        var writeStream = fs.createWriteStream(savePath);
                        stream.pipe(writeStream);

                        writeStream.once('finish', function() {
                            defer.resolve();
                        });
                    }
                };

                return generate.createMigrations(true);
            })
            .then(function(result) {
                if(!result) {
                    // If the result is false, there are no migrations so we'll call done.
                    defer.resolve();
                }
            })
            .done();
        return defer.promise;
    });

    /*
     * fire build:procfile
     */
    stageMethods.addMethod('build', function procfile(app) {
        var processes = [];
        processes.push('web: node index.js --web');

        if(app.workers.numberOfWorkers() > 0) {
            processes.push('workers: node index.js --workers');
        }

        if(app.schedulers.numberOfSchedulers() > 0) {
            processes.push('schedulers: node index.js --schedulers');
        }

        if(app.triggers.numberOfTriggers() > 0) {
            processes.push('triggers: node index.js --triggers');
        }

        if(app.triggers.numberOfTriggers() > 0 || app.schedulers.numberOfSchedulers() > 0) {
            processes.push('clock: node index.js --clock');
        }

        var savePath = path.join(basePath, 'Procfile');

        var contents = fs.readFileSync(savePath, {encoding: 'utf8'});
        var newContents = processes.join('\n');
        if(contents != newContents) {
            var defer = $q.defer();
            fs.writeFile(savePath, newContents, defer.makeNodeResolver());
            return defer.promise;
        }
    });

    /*
     * fire build:version
     */
    stageMethods.addMethod('build', function version() {
        return _mkdir(path.join('.fire'))
            .then(function(savePath) {<commit_msg>fixes procfile error<commit_after>'use strict';

var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
var utils = require('../../helpers/utils');
var lss = require('less');
var jde = require('jade');
var UglifyJS = require('uglify-js');
var brwserify = require('browserify');
var Migrations = require('../migrations');
var Q = require('q');

exports = module.exports = BuildSystem;

function BuildSystem(stageMethods, $q) {
    var basePath = process.cwd();

    var _mkdir = function(directoryPath) {
        var defer = $q.defer();
        mkdirp(directoryPath, function(error) {
            if(error) {
                defer.reject(error);
            }
            else {
                defer.resolve(directoryPath);
            }
        });
        return defer.promise;
    };

    /**
     * fire build:scripts
     */
    stageMethods.addMethod('build', function scripts(app) {
        var scriptsPath = '';

        if(app.container.numberOfApps() > 1) {
            scriptsPath = path.join(basePath, '.fire', '.build', 'public', app.name, 'scripts');
        }
        else {
            scriptsPath = path.join(basePath, '.fire', '.build', 'public', 'scripts');
        }

        return _mkdir(scriptsPath)
            .then(function() {
                var savePath = path.join(scriptsPath, 'fire.js');
                var writeStream = fs.createWriteStream(savePath);

                return app.bridge.generate(writeStream);
            });
    });

    /*
     * fire build:api
     */
    stageMethods.addMethod('build', function api(app) {
        var apiPath = '';

        if(app.container.numberOfApps() > 1) {
            apiPath = path.join(basePath, '.fire', '.build', 'api', app.name);
        }
        else {
            apiPath = path.join(basePath, '.fire', '.build', 'api');
        }

        return _mkdir(apiPath)
            .then(function() {
                var result = $q.when(true);

                app.models.forEach(function(model) {
                    result = result.then(function() {
                        if(model.isShared() && !app.settings('isMaster') && !app.settings('includeAPI')) {
                            //
                        }
                        else {
                            var savePath = path.join(apiPath, model.getFileName() + '.js');
                            var writeStream = fs.createWriteStream(savePath);
                            return app.APIBuild.generateModelController(model, writeStream);
                        }
                    });
                });

                return result;
            });
    });

    /*
     * fire build:migrations
     */
    stageMethods.addMethod('build', function migrations(app) {
        var migrationsPath = '';
        if(app.container.numberOfApps() > 1) {
            migrationsPath = path.join(basePath, '.fire', 'migrations', app.name);
        }
        else {
            migrationsPath = path.join(basePath, '.fire', 'migrations');
        }

        var defer = $q.defer();
        _mkdir(migrationsPath)
            .then(function() {
                var Generate = require('../migrations/generate');
                var generate = new Generate(app, basePath);
                generate.delegate = {
                    addMigration: function(fileName, stream) {
                        var savePath = path.join(migrationsPath, fileName);

                        var writeStream = fs.createWriteStream(savePath);
                        stream.pipe(writeStream);

                        writeStream.once('finish', function() {
                            defer.resolve();
                        });
                    }
                };

                return generate.createMigrations(true);
            })
            .then(function(result) {
                if(!result) {
                    // If the result is false, there are no migrations so we'll call done.
                    defer.resolve();
                }
            })
            .done();
        return defer.promise;
    });

    /*
     * fire build:procfile
     */
    stageMethods.addMethod('build', function procfile(app) {
        var processes = [];
        processes.push('web: node index.js --web');

        if(app.workers.numberOfWorkers() > 0) {
            processes.push('workers: node index.js --workers');
        }

        if(app.schedulers.numberOfSchedulers() > 0) {
            processes.push('schedulers: node index.js --schedulers');
        }

        if(app.triggers.numberOfTriggers() > 0) {
            processes.push('triggers: node index.js --triggers');
        }

        if(app.triggers.numberOfTriggers() > 0 || app.schedulers.numberOfSchedulers() > 0) {
            processes.push('clock: node index.js --clock');
        }

        var savePath = path.join(basePath, 'Procfile');

        var contents = null;
        try {
            contents = fs.readFileSync(savePath, {encoding: 'utf8'});
        }
        catch(e) {
            //
        }

        var newContents = processes.join('\n');
        if(contents != newContents) {
            var defer = $q.defer();
            fs.writeFile(savePath, newContents, defer.makeNodeResolver());
            return defer.promise;
        }
    });

    /*
     * fire build:version
     */
    stageMethods.addMethod('build', function version() {
        return _mkdir(path.join('.fire'))
            .then(function(savePath) {
{'fraction_non_alphanumeric': 0.1359402379415291, 'fraction_numerical': 0.0016600571797473025, 'mean_word_length': 1.573327005220693, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '3290184', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2870, 'n_words': 550}