<commit_before>'use strict';
var git = require('gift');
var fs = require('fs');
var fstream = require('fstream');
var jsforce = require('jsforce');
var async = require('async');
var AdmZip = require('adm-zip');

//muts all logs
var MUTE = false;

/*
 * Creates the return object for the mainCallback
 */
function createReturnObject(err, msg){
    return {
        error: err,
        details: msg
    };
}

/*
 * Sync Deletes a folder recursively
 * @path: folder path
 * @exclude: exclude a certain folder's name
 * @doNotDeleteRoot: do not delete root folder
 */
var deleteFolderRecursive = function(path, excludeFolder, doNotDeleteRoot) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        if(!excludeFolder || file != exclude){
            deleteFolderRecursive(curPath, exclude);
        }
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    if(!doNotDeleteRoot) fs.rmdirSync(path);
  }
};

module.exports = {
    doAll : function(mainCallback){

        //status object
        var status = {
            tempPath : '/tmp/',
            zipPath : "zips/",
            repoPath: "repos/",
            zipFile : "_MyPackage"+Math.random()+".zip",
            sfConnection : (new jsforce.Connection()),
            sfLoginResult : null,
            types : {},
        };
        //polling timeout of the SF connection
        status.sfConnection.metadata.pollTimeout = process.env.SF_METADATA_POLL_TIMEOUT || 120000;

        //creates all the main folders (temp folder, zip folder and git clone folder)
        try{
            if(!fs.existsSync(status.tempPath)){
                fs.mkdirSync(status.tempPath);
            }
            if (!fs.existsSync(status.tempPath+status.zipPath)){
                fs.mkdirSync(status.tempPath+status.zipPath);
            }
            if (!fs.existsSync(status.tempPath+status.repoPath)){
                fs.mkdirSync(status.tempPath+status.repoPath);
            }
        }catch(ex){
            return mainCallback && mainCallback(ex);
        }

        //asyncs jobs called sequentially (all the tasks to be done)
        async.series({
            //login to SF
            sfLogin : function(callback){
                if(!MUTE) console.log('SF LOGIN');
                status.sfConnection.login(process.env.SF_USERNAME, process.env.SF_PASSWORD, function(err, lgnResult) {
                    status.sfLoginResult = lgnResult;
                    return callback((err)?createReturnObject(err, 'SF Login failed'):null);
                });
            },
            //Describes metadata items
            sfDescribeMetadata : function(callback){
                if(!MUTE) console.log('SF DESCRIBE METADATA');
                status.sfConnection.metadata.describe(process.env.SF_API_VERSION+'.0', function(err, describe){
                    status.sfDescribe = describe;
                    return callback((err)?createReturnObject(err, 'SF Describe failed'):null);
                });
            },
            //Lists of all metadata details
            sfListMetadata : function(callback){
                if(!MUTE) console.log('SF LIST DESCRIBE METADATA ALL');
                var iterations =  parseInt(Math.ceil(status.sfDescribe.metadataObjects.length/3.0));
                var excludeMetadata = process.env.EXCLUDE_METADATA || '';
                var excludeMetadataList = excludeMetadata.toLowerCase().split(',');

                var asyncObj = {};

                function listMetadataBatch(qr){
                    return function(cback){
                        if(!MUTE) console.log('SF LIST DESCRIBE METADATA: '+JSON.stringify(qr));
                        status.sfConnection.metadata.list(qr,process.env.SF_API_VERSION+'.0', function(err, fileProperties){
                            if(!err && fileProperties){
                                for(var ft = 0; ft < fileProperties.length; ft++){
                                    if(!status.types[fileProperties[ft].type]){
                                        status.types[fileProperties[ft].type] = [];
                                    }
                                    status.types[fileProperties[ft].type].push(fileProperties[ft].fullName);
                                }
                            }
                            return cback(err);
                        });
                    }
                }

                for(var it = 0; it < iterations; it++){
                    var query = [];
                    for(var i = 0; i < 3; i++){
                        var index = it*3+i;

                        if(status.sfDescribe.metadataObjects.length > index){
                            var metadata = status.sfDescribe.metadataObjects[index];
                            if(excludeMetadataList.indexOf((metadata.xmlName||'').toLowerCase()) <0){
                                query.push({type: metadata.xmlName, folder: metadata.folderName});
                            }
                        }
                    }
                    if(query.length>0){
                        asyncObj['fn'+it] = listMetadataBatch(query);
                    }
                }
                async.series(asyncObj, function(err, results){
                    return callback((err)?createReturnObject(err, 'SF Describe list metadata failed'):null);
                });

            },
            //Retrieving ZIP file of metadata
            sfRetrieveZip : function(callback){
                //should use describe
                //retrieve xml
                if(!MUTE) console.log('SF RETRIEVE ZIP');

                var _types = [];
                for(var t in status.types){
                    _types.push({
                        members: status.types[t],
                        name: t,
                    });
                }
                var stream = status.sfConnection.metadata.retrieve({
                unpackaged: {
                  types: _types,
                  version: process.env.SF_API_VERSION,
                }
                }).stream();
                stream.on('end', function() {
                   return callback(null);
                });
                stream.on('error', function(err){
                    return callback((err)?createReturnObject(err, 'SF Retrieving metadata ZIP file failed'):null);
                });
                stream.pipe(fs.createWriteStream(status.tempPath+status.zipPath+status.zipFile));
            },
            //Clones original repo
            gitClone : function(callback){
                if(!MUTE) console.log('GIT CLONE');
                var folderPath = status.tempPath+status.repoPath+status.zipFile;

                git.clone(process.env.REPO_URL, folderPath,
                function(err, _repo){
                    status.gitRepo = _repo;
                    //deletes all cloned files except the .git folder (the ZIP file will be the master)
                    deleteFolderRecursive(folderPath, '.git', true);
                    return callback((err)?createReturnObject(err, 'Git clone failed'):null);
                });
            },
            //Unzip metadata zip file
            unzipFile : function(callback){

                if(!MUTE) console.log('UNZIP FILE');

                //create .gitignore
                var fs = require('fs');

                var gitIgnoreBody = '#ignore files';
                if(process.env.GIT_IGNORE){
                    var spl = process.env.GIT_IGNORE.split(',');
                    for(var i in spl){
                        if(spl[i]){
                            gitIgnoreBody+='\n'+spl[i];
                        }
                    }
                }

                var readmeBody = process.env.REPO_README || "";
                fs.writeFile(status.tempPath+status.repoPath+status.zipFile+'/README.md', readmeBody, function(err) {
                    if(err){
                        return callback(createReturnObject(err, 'README.md file creation failed'));
                    }
                    fs.writeFile(status.tempPath+status.repoPath+status.zipFile+'/.gitignore', gitIgnoreBody, function(err) {
                        if(err){
                            return callback(createReturnObject(err, '.gitignore file creation failed'));
                        }
                        try{
                            var zip = new AdmZip(status.tempPath+status.zipPath+status.zipFile);
                            zip.extractAllTo(status.tempPath+status.repoPath+status.zipFile+'/', true);
                            return callback(null);
                        }catch(ex){
                            return callback(createReturnObject(ex, 'Unzip failed'));
                        }
                    });
                });

            },
            //Git add new resources
            gitAdd : function(callback){
                if(!MUTE) console.log('GIT ADD');

                status.gitRepo.add("-A",function(err){
                    return callback((err)?createReturnObject(err, 'git add failed'):null);
                });
            },
            //Git commit
            gitCommit : function(callback){
                if(!MUTE) console.log('GIT COMMIT');
                var userName = process.env.REPO_USER_NAME || "Heroku SFGit";
                var userEmail = process.env.REPO_USER_EMAIL || "sfgit@heroku.com";
                status.gitRepo.identify({"name":userName, "email":userEmail}, function(err, oth){
                    var commitMessage = process.env.REPO_COMMIT_MESSAGE || 'Automatic commit (sfgit)';
                    status.gitRepo.commit(commitMessage, function(err, oth){
                        if(err){
                            err.details = oth;
                        }
                        return callback((err)?createReturnObject(err, 'git commit failed'):null);
                    });
                });
            },
            //Git push
            gitPush : function(callback){
                if(!MUTE) console.log('GIT PUSH');

                status.gitRepo.remote_push("origin", "master", function(err, oth){
                    if(err){
                        err.details = oth;
                    }
                    return callback((err)?createReturnObject(err, 'git push failed'):null);
                });
            },
        },
        function(err, results){
            //deletes all temp files
            deleteFolderRecursive(status.tempPath+status.zipPath+'/');
            deleteFolderRecursive(status.tempPath+status.repoPath+'/');

            if(err
                && err.error.details
                && (err.error.details.indexOf("up-to-date")>=0 || err.error.details.indexOf("nothing to commit") >=0)){
                console.log('Success', err.error.details);
                return mainCallback && mainCallback(null, err.error.details);
            }

            var details = (err && err.error && err.details) || null;
            if(err){
                console.log("Error occurred",err);
            }else{
                console.log('Success');
                details = 'Success';
            }
            return mainCallback && mainCallback(err, details);
        })
    },
}<commit_msg>issue #1<commit_after>'use strict';
var git = require('gift');
var fs = require('fs');
var fstream = require('fstream');
var jsforce = require('jsforce');
var async = require('async');
var AdmZip = require('adm-zip');

//muts all logs
var MUTE = false;

/*
 * Creates the return object for the mainCallback
 */
function createReturnObject(err, msg){
    return {
        error: err,
        details: msg
    };
}

/*
 * Sync Deletes a folder recursively
 * @path: folder path
 * @exclude: exclude a certain folder's name
 * @doNotDeleteRoot: do not delete root folder
 */
var deleteFolderRecursive = function(path, exclude, doNotDeleteRoot) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        if(!exclude || file != exclude){
            deleteFolderRecursive(curPath, exclude);
        }
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    if(!doNotDeleteRoot) fs.rmdirSync(path);
  }
};

module.exports = {
    doAll : function(mainCallback){

        //status object
        var status = {
            tempPath : '/tmp/',
            zipPath : "zips/",
            repoPath: "repos/",
            zipFile : "_MyPackage"+Math.random()+".zip",
            sfConnection : (new jsforce.Connection()),
            sfLoginResult : null,
            types : {},
        };
        //polling timeout of the SF connection
        status.sfConnection.metadata.pollTimeout = process.env.SF_METADATA_POLL_TIMEOUT || 120000;

        //creates all the main folders (temp folder, zip folder and git clone folder)
        try{
            if(!fs.existsSync(status.tempPath)){
                fs.mkdirSync(status.tempPath);
            }
            if (!fs.existsSync(status.tempPath+status.zipPath)){
                fs.mkdirSync(status.tempPath+status.zipPath);
            }
            if (!fs.existsSync(status.tempPath+status.repoPath)){
                fs.mkdirSync(status.tempPath+status.repoPath);
            }
        }catch(ex){
            return mainCallback && mainCallback(ex);
        }

        //asyncs jobs called sequentially (all the tasks to be done)
        async.series({
            //login to SF
            sfLogin : function(callback){
                if(!MUTE) console.log('SF LOGIN');
                status.sfConnection.login(process.env.SF_USERNAME, process.env.SF_PASSWORD, function(err, lgnResult) {
                    status.sfLoginResult = lgnResult;
                    return callback((err)?createReturnObject(err, 'SF Login failed'):null);
                });
            },
            //Describes metadata items
            sfDescribeMetadata : function(callback){
                if(!MUTE) console.log('SF DESCRIBE METADATA');
                status.sfConnection.metadata.describe(process.env.SF_API_VERSION+'.0', function(err, describe){
                    status.sfDescribe = describe;
                    return callback((err)?createReturnObject(err, 'SF Describe failed'):null);
                });
            },
            //Lists of all metadata details
            sfListMetadata : function(callback){
                if(!MUTE) console.log('SF LIST DESCRIBE METADATA ALL');
                var iterations =  parseInt(Math.ceil(status.sfDescribe.metadataObjects.length/3.0));
                var excludeMetadata = process.env.EXCLUDE_METADATA || '';
                var excludeMetadataList = excludeMetadata.toLowerCase().split(',');

                var asyncObj = {};

                function listMetadataBatch(qr){
                    return function(cback){
                        if(!MUTE) console.log('SF LIST DESCRIBE METADATA: '+JSON.stringify(qr));
                        status.sfConnection.metadata.list(qr,process.env.SF_API_VERSION+'.0', function(err, fileProperties){
                            if(!err && fileProperties){
                                for(var ft = 0; ft < fileProperties.length; ft++){
                                    if(!status.types[fileProperties[ft].type]){
                                        status.types[fileProperties[ft].type] = [];
                                    }
                                    status.types[fileProperties[ft].type].push(fileProperties[ft].fullName);
                                }
                            }
                            return cback(err);
                        });
                    }
                }

                for(var it = 0; it < iterations; it++){
                    var query = [];
                    for(var i = 0; i < 3; i++){
                        var index = it*3+i;

                        if(status.sfDescribe.metadataObjects.length > index){
                            var metadata = status.sfDescribe.metadataObjects[index];
                            if(excludeMetadataList.indexOf((metadata.xmlName||'').toLowerCase()) <0){
                                query.push({type: metadata.xmlName, folder: metadata.folderName});
                            }
                        }
                    }
                    if(query.length>0){
                        asyncObj['fn'+it] = listMetadataBatch(query);
                    }
                }
                async.series(asyncObj, function(err, results){
                    return callback((err)?createReturnObject(err, 'SF Describe list metadata failed'):null);
                });

            },
            //Retrieving ZIP file of metadata
            sfRetrieveZip : function(callback){
                //should use describe
                //retrieve xml
                if(!MUTE) console.log('SF RETRIEVE ZIP');

                var _types = [];
                for(var t in status.types){
                    _types.push({
                        members: status.types[t],
                        name: t,
                    });
                }
                var stream = status.sfConnection.metadata.retrieve({
                unpackaged: {
                  types: _types,
                  version: process.env.SF_API_VERSION,
                }
                }).stream();
                stream.on('end', function() {
                   return callback(null);
                });
                stream.on('error', function(err){
                    return callback((err)?createReturnObject(err, 'SF Retrieving metadata ZIP file failed'):null);
                });
                stream.pipe(fs.createWriteStream(status.tempPath+status.zipPath+status.zipFile));
            },
            //Clones original repo
            gitClone : function(callback){
                if(!MUTE) console.log('GIT CLONE');
                var folderPath = status.tempPath+status.repoPath+status.zipFile;

                git.clone(process.env.REPO_URL, folderPath,
                function(err, _repo){
                    status.gitRepo = _repo;
                    //deletes all cloned files except the .git folder (the ZIP file will be the master)
                    deleteFolderRecursive(folderPath, '.git', true);
                    return callback((err)?createReturnObject(err, 'Git clone failed'):null);
                });
            },
            //Unzip metadata zip file
            unzipFile : function(callback){

                if(!MUTE) console.log('UNZIP FILE');

                //create .gitignore
                var fs = require('fs');

                var gitIgnoreBody = '#ignore files';
                if(process.env.GIT_IGNORE){
                    var spl = process.env.GIT_IGNORE.split(',');
                    for(var i in spl){
                        if(spl[i]){
                            gitIgnoreBody+='\n'+spl[i];
                        }
                    }
                }

                var readmeBody = process.env.REPO_README || "";
                fs.writeFile(status.tempPath+status.repoPath+status.zipFile+'/README.md', readmeBody, function(err) {
                    if(err){
                        return callback(createReturnObject(err, 'README.md file creation failed'));
                    }
                    fs.writeFile(status.tempPath+status.repoPath+status.zipFile+'/.gitignore', gitIgnoreBody, function(err) {
                        if(err){
                            return callback(createReturnObject(err, '.gitignore file creation failed'));
                        }
                        try{
                            var zip = new AdmZip(status.tempPath+status.zipPath+status.zipFile);
                            zip.extractAllTo(status.tempPath+status.repoPath+status.zipFile+'/', true);
                            return callback(null);
                        }catch(ex){
                            return callback(createReturnObject(ex, 'Unzip failed'));
                        }
                    });
                });

            },
            //Git add new resources
            gitAdd : function(callback){
                if(!MUTE) console.log('GIT ADD');

                status.gitRepo.add("-A",function(err){
                    return callback((err)?createReturnObject(err, 'git add failed'):null);
                });
            },
            //Git commit
            gitCommit : function(callback){
                if(!MUTE) console.log('GIT COMMIT');
                var userName = process.env.REPO_USER_NAME || "Heroku SFGit";
                var userEmail = process.env.REPO_USER_EMAIL || "sfgit@heroku.com";
                status.gitRepo.identify({"name":userName, "email":userEmail}, function(err, oth){
                    var commitMessage = process.env.REPO_COMMIT_MESSAGE || 'Automatic commit (sfgit)';
                    status.gitRepo.commit(commitMessage, function(err, oth){
                        if(err){
                            err.details = oth;
                        }
                        return callback((err)?createReturnObject(err, 'git commit failed'):null);
                    });
                });
            },
            //Git push
            gitPush : function(callback){
                if(!MUTE) console.log('GIT PUSH');

                status.gitRepo.remote_push("origin", "master", function(err, oth){
                    if(err){
                        err.details = oth;
                    }
                    return callback((err)?createReturnObject(err, 'git push failed'):null);
                });
            },
        },
        function(err, results){
            //deletes all temp files
            deleteFolderRecursive(status.tempPath+status.zipPath+'/');
            deleteFolderRecursive(status.tempPath+status.repoPath+'/');

            if(err
                && err.error.details
                && (err.error.details.indexOf("up-to-date")>=0 || err.error.details.indexOf("nothing to commit") >=0)){
                console.log('Success', err.error.details);
                return mainCallback && mainCallback(null, err.error.details);
            }

            var details = (err && err.error && err.details) || null;
            if(err){
                console.log("Error occurred",err);
            }else{
                console.log('Success');
                details = 'Success';
            }
            return mainCallback && mainCallback(err, details);
        })
    },
}<|endoftext|>


<commit_before>'use strict';

const Stats = require('fast-stats').Stats;

function percentileName(percentile) {
  if (percentile === 0) {
    return 'min';
  } else if (percentile === 100) {
    return 'max';
  } else if (percentile === 50) {
    return 'median';
  }
  return `p${String(percentile).replace('.', '_')}`;
}

class Statistics {
  constructor(name) {
    this.name = name;
    this.stats = new Stats();
  }

  add(value) {
    this.stats.push(value);
  }

  summarize(options) {
    options = options || {};
    const percentiles = options.percentiles || [0, 10, 50, 90, 99, 100];
    const decimals = options.decimals || 0;
    const data = {};
    const self = this;

    percentiles.forEach(p => {
      const name = percentileName(p);
      data[name] = self.stats.percentile(p).toFixed(decimals);
    });

    return data;
  }
}

module.exports = {
  Statistics,
};
<commit_msg>Remove unused name in Statistics constructor.<commit_after>'use strict';

const Stats = require('fast-stats').Stats;

function percentileName(percentile) {
  if (percentile === 0) {
    return 'min';
  } else if (percentile === 100) {
    return 'max';
  } else if (percentile === 50) {
    return 'median';
  }
  return `p${String(percentile).replace('.', '_')}`;
}

class Statistics {
  constructor() {
    this.stats = new Stats();
  }

  add(value) {
    this.stats.push(value);
  }

  summarize(options) {
    options = options || {};
    const percentiles = options.percentiles || [0, 10, 50, 90, 99, 100];
    const decimals = options.decimals || 0;
    const data = {};
    const self = this;

    percentiles.forEach(p => {
      const name = percentileName(p);
      data[name] = self.stats.percentile(p).toFixed(decimals);
    });

    return data;
  }
}

module.exports = {
  Statistics,
};
<|endoftext|>


<commit_before>var _ = require('underscore');
var $ = require('../bonzo-augmented');
var IconContainerView = require('./icon-container');

module.exports = HeaderView;

var validOptionsProperties = ['icon'];

/**
 * HeaderView
 *
 * @param {Object} widget
 * @param {Object} el
 * @param {Object} options
 *
 * @public
 */

function HeaderView(widget, el, options) {
  var self = this;

  this.el = el;
  this.widget = widget;
  this.options = _.filter(options, function (option) {
    /*jshint bitwise: false*/
    return !!~validOptionsProperties.indexOf(option);
    /*jshint bitwise: true*/
  });

  this.image = new IconContainerView(this, options);

  function redirectEvent(s) {
    return function () {
      widget.emit(s);
    };
  }

  var events = [ 'shown icon', 'hidden icon', 'shown avatar', 'hidden avatar' ];

  events.forEach(function (eventName) {
    self.image.on(eventName, redirectEvent(eventName));
  });

}

/**
 * Set widget's h1 to `title`
 *
 * @param {String} title
 * @private
 */

HeaderView.prototype.setTitle = function(title) {
  var h1 = this.query('h1');
  h1.html(title);
  h1.css('display', '');
};

/**
 * Query for elements at `this.el` context
 *
 * @param {String} selector
 * @return {BonzoAugmented}
 * @public
 */

HeaderView.prototype.query = function(selector) {
  if (!this.el) { throw new Error('Can\'t get element since no `el` is set to local context'); }
  return $(selector, this.el);
};

/**
 * Set image to display on header.
 *
 * @param {String} url Image to display
 * @public
 */

HeaderView.prototype.setImage = function(url) {
  this.image.set(url);
};

/**
 * Reset image to display on header to default.
 *
 * @public
 */

HeaderView.prototype.restoreImage = function () {
  this.image.reset();
};

<commit_msg>Fixing broken event re-firing.<commit_after>var _ = require('underscore');
var $ = require('../bonzo-augmented');
var IconContainerView = require('./icon-container');

module.exports = HeaderView;

var validOptionsProperties = ['icon'];

/**
 * HeaderView
 *
 * @param {Object} widget
 * @param {Object} el
 * @param {Object} options
 *
 * @public
 */

function HeaderView(widget, el, options) {
  var self = this;

  this.el = el;
  this.widget = widget;
  this.options = _.filter(options, function (option) {
    /*jshint bitwise: false*/
    return !!~validOptionsProperties.indexOf(option);
    /*jshint bitwise: true*/
  });

  this.image = new IconContainerView(this, options);

  function redirectEvent(s) {
    return function () {
      widget.emit(s);
    };
  }

  var events = [ 'icon shown', 'icon hidden', 'avatar shown', 'avatar hidden' ];

  events.forEach(function (eventName) {
    self.image.on(eventName, redirectEvent(eventName));
  });

}

/**
 * Set widget's h1 to `title`
 *
 * @param {String} title
 * @private
 */

HeaderView.prototype.setTitle = function(title) {
  var h1 = this.query('h1');
  h1.html(title);
  h1.css('display', '');
};

/**
 * Query for elements at `this.el` context
 *
 * @param {String} selector
 * @return {BonzoAugmented}
 * @public
 */

HeaderView.prototype.query = function(selector) {
  if (!this.el) { throw new Error('Can\'t get element since no `el` is set to local context'); }
  return $(selector, this.el);
};

/**
 * Set image to display on header.
 *
 * @param {String} url Image to display
 * @public
 */

HeaderView.prototype.setImage = function(url) {
  this.image.set(url);
};

/**
 * Reset image to display on header to default.
 *
 * @public
 */

HeaderView.prototype.restoreImage = function () {
  this.image.reset();
};

<|endoftext|>


<commit_before>var json = require('../json').json;
var sections = [], key;

/**
 *  Abstract super class for help documents.
 */
var HelpDocument = function() {
  this.sections = sections;
}

/**
 *  Write the name section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.name = function(data, stream, doc) {}

/**
 *  Write the synopsis section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.synopsis = function(data, stream, doc) {}

/**
 *  Write the description section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.description = function(data, stream, doc) {}

/**
 *  Write the options section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.options = function(data, stream, doc) {
  doc.commands.call(this, data, stream, doc);
  doc.arguments.call(this, data, stream, doc);
}

/**
 *  Write the environment section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.environment = function(data, stream, doc) {}

/**
 *  Write the files section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.files = function(data, stream, doc) {}

/**
 *  Write the examples section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.examples = function(data, stream, doc) {}

/**
 *  Write the exit status section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.exit = function(data, stream, doc) {}

/**
 *  Write the history section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.history = function(data, stream, doc) {}

/**
 *  Write the author section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.author = function(data, stream, doc) {}

/**
 *  Write the bugs section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.bugs = function(data, stream, doc) {}

/**
 *  Write the copyright section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.copyright = function(data, stream, doc) {}

/**
 *  Write the see also section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.see = function(data, stream, doc) {}

for(key in HelpDocument.prototype) {
  sections.push(key);
}

/**
 *  Utility to remove a section by key from
 *  the list of sections that will be rendered.
 *
 *  @param ... List of keys to remove.
 */
HelpDocument.prototype.remove = function() {
  var ind, i, key;
  for(i = 0;i < arguments.length;i++) {
    key = arguments[i];
    ind = this.sections.indexOf(key);
    if(~ind) {
      this.sections.splice(ind, 1);
    }
  }
}

/**
 *  Write command options.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.commands = function(data, stream, doc) {
  var opts = Object.keys(this._commands);
  if(!opts.length) return;
  doc._commands.call(this, data, stream, doc);
}

/**
 *  Write command options if any commands are defined.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype._commands = function(data, stream, doc) {}

/**
 *  Write argument options.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.arguments = function(data, stream, doc) {
  var opts = Object.keys(this._options);
  if(!opts.length) return;
  doc._arguments.call(this, data, stream, doc);
}

/**
 *  Write argument options if any arguments are defined.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype._arguments = function(data, stream, doc) {}

/**
 *  Write the document to a stream.
 *
 *  @param program The program.
 *  @param data The program data.
 *  @param stream The output stream.
 */
HelpDocument.prototype.write = function(program, data, stream) {
  stream = stream || process.stdout;
  if(!data) data = json.call(program);
  var i, method, result;
  for(i = 0;i < this.sections.length;i++) {
    method = this[this.sections[i]];
    result = method.call(program, data, stream, this);
    // was written to the stream
    if(result === true) continue;
    if(typeof result === 'string') {
      stream.write(result);
    }
  }
}

module.exports = {};
module.exports.HelpDocument = HelpDocument;
module.exports.sections = sections;
for(key in sections) {
  module.exports[key.toUpperCase()] = key;
}
<commit_msg>Fix help section constants.<commit_after>var json = require('../json').json;
var sections = [
  'name',
  'synopsis',
  'description',
  'options',
  'environment',
  'files',
  'examples',
  'exit',
  'history',
  'author',
  'bugs',
  'copyright',
  'see'
];

/**
 *  Abstract super class for help documents.
 */
var HelpDocument = function() {
  this.sections = sections.slice(0);
}

/**
 *  Write the name section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.name = function(data, stream, doc) {}

/**
 *  Write the synopsis section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.synopsis = function(data, stream, doc) {}

/**
 *  Write the description section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.description = function(data, stream, doc) {}

/**
 *  Write the options section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.options = function(data, stream, doc) {
  doc.commands.call(this, data, stream, doc);
  doc.arguments.call(this, data, stream, doc);
}

/**
 *  Write the environment section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.environment = function(data, stream, doc) {}

/**
 *  Write the files section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.files = function(data, stream, doc) {}

/**
 *  Write the examples section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.examples = function(data, stream, doc) {}

/**
 *  Write the exit status section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.exit = function(data, stream, doc) {}

/**
 *  Write the history section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.history = function(data, stream, doc) {}

/**
 *  Write the author section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.author = function(data, stream, doc) {}

/**
 *  Write the bugs section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.bugs = function(data, stream, doc) {}

/**
 *  Write the copyright section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.copyright = function(data, stream, doc) {}

/**
 *  Write the see also section.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.see = function(data, stream, doc) {}

/**
 *  Utility to remove a section by key from
 *  the list of sections that will be rendered.
 *
 *  @param ... List of keys to remove.
 */
HelpDocument.prototype.remove = function() {
  //console.log('removing %j', arguments);
  //console.dir(arguments);
  var ind, i, key;
  for(i = 0;i < arguments.length;i++) {
    key = arguments[i];
    //console.dir(key);
    ind = this.sections.indexOf(key);
    if(~ind) {
      this.sections.splice(ind, 1);
    }
  }
}

/**
 *  Write command options.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.commands = function(data, stream, doc) {
  var opts = Object.keys(this._commands);
  if(!opts.length) return;
  doc._commands.call(this, data, stream, doc);
}

/**
 *  Write command options if any commands are defined.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype._commands = function(data, stream, doc) {}

/**
 *  Write argument options.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype.arguments = function(data, stream, doc) {
  var opts = Object.keys(this._options);
  if(!opts.length) return;
  doc._arguments.call(this, data, stream, doc);
}

/**
 *  Write argument options if any arguments are defined.
 *
 *  @param data The program data.
 *  @param stream The output stream.
 *  @param doc The help document.
 */
HelpDocument.prototype._arguments = function(data, stream, doc) {}

/**
 *  Write the document to a stream.
 *
 *  @param program The program.
 *  @param data The program data.
 *  @param stream The output stream.
 */
HelpDocument.prototype.write = function(program, data, stream) {
  stream = stream || process.stdout;
  if(!data) data = json.call(program);
  var i, method, result;
  for(i = 0;i < this.sections.length;i++) {
    method = this[this.sections[i]];
    result = method.call(program, data, stream, this);
    // was written to the stream
    if(result === true) continue;
    if(typeof result === 'string') {
      stream.write(result);
    }
  }
}

module.exports = {};
module.exports.HelpDocument = HelpDocument;
module.exports.sections = sections;
sections.forEach(function(key) {
  module.exports[key.toUpperCase()] = HelpDocument[key.toUpperCase()] = key;
});
<|endoftext|>


<commit_before>
var fs = require('fs');

// Read file content and stat data
module.exports = function readFile(path, callback) {
  var ret = { 'path': path };

	fs.open(path, 'r', function (error, fd) {
    if (error) return callback(error, null);

    fs.fstat(fd, function (error, stat) {
      if (error) return callback(error, null);
      ret.mtime = stat.mtime;
      ret.size = stat.size;
      ret.content = new Buffer(stat.size);

      fs.read(fd, ret.content, 0, stat.size, 0, function (error) {
        if (error) return callback(error, null);

        fs.close(fd, function () {
          callback(null, ret);
        });
      });
    });
	});
};
<commit_msg>[fix] support empty files<commit_after>
var fs = require('fs');

// Read file content and stat data
module.exports = function readFile(path, callback) {
  var ret = { 'path': path };

	fs.open(path, 'r', function (error, fd) {
    if (error) return callback(error, null);

    fs.fstat(fd, function (error, stat) {
      if (error) return callback(error, null);
      ret.mtime = stat.mtime;
      ret.size = stat.size;
      ret.content = new Buffer(stat.size);

      if (stat.size === 0) {
        return callback(null, ret);
      }

      fs.read(fd, ret.content, 0, stat.size, 0, function (error) {
        if (error) {
          console.log(stack);
          return callback(error, null);
        }

        fs.close(fd, function () {
          callback(null, ret);
        });
      });
    });
	});
};
<|endoftext|>


<commit_before>"use strict";

var config = require('config')
    , moment = require('moment')
    , _ = require('underscore')
    ;

module.exports = {

    /**
     * @returns {obj}
     */
    getDefault: function () {

        let start = config.has('start') ? config.get('start') : moment.utc().subtract(32, 'days').unix();
        let end   = config.has('end') ? config.get('end') : moment.utc().unix();

        return _.clone({
            'method': 'POST',
            'auth': {
                'user': process.env.AUTH_USER || (config.has('index.default.auth.username') ? config.get('index.default.auth.username') : ""),
                'pass': process.env.AUTH_KEY || (config.has('index.default.auth.api_key') ? config.get('index.default.auth.api_key') : ""),
            },
            'uri': process.env.ANALYZE_URI || (config.has('app.analyze_uri') ? config.get('app.analyze_uri') : "https://api.datasift.com/v1/pylon/analyze"),
            'json': {
                'hash': process.env.HASH || (config.has('index.default.hash') ? config.get('index.default.hash') : ""),
                'start': start,
                'end': end,
                'parameters': {
                    'analysis_type': 'freqDist',
                    'parameters': {}
                }
            }
        });
    },

    /**
     * isValidKey - check string is a valid target
     * @param str
     * @returns {boolean}
     */
    isValidKey: function(str){
        let arr = ["filter", "interval", "threshold", "target", "id", "name"];
        return (arr.indexOf(str) != -1);
    },

    /**
     * getIndexCreds - override the index auth and hash
     *
     * @param task
     * @param reqObj
     * @returns {*}
     */
    getIndexCreds: function(task, reqObj){
        if(task.index){
            let index = task.index.toString();
            reqObj.auth =  (config.has('index.'+index + '.auth') ? config.get('index.'+index + '.auth') : "");
            reqObj.json.hash =  (config.has('index.'+index + '.hash') ? config.get('index.'+index + '.hash') : "");
        }
        return reqObj;
    }

}<commit_msg>Fix auth cred overrides.<commit_after>"use strict";

var config = require('config')
    , moment = require('moment')
    , _ = require('underscore')
    ;

module.exports = {

    /**
     * @returns {obj}
     */
    getDefault: function () {

        let start = config.has('start') ? config.get('start') : moment.utc().subtract(32, 'days').unix();
        let end   = config.has('end') ? config.get('end') : moment.utc().unix();

        return _.clone({
            'method': 'POST',
            'auth': {
                'user': process.env.AUTH_USER || (config.has('index.default.auth.username') ? config.get('index.default.auth.username') : ""),
                'pass': process.env.AUTH_KEY || (config.has('index.default.auth.api_key') ? config.get('index.default.auth.api_key') : ""),
            },
            'uri': process.env.ANALYZE_URI || (config.has('app.analyze_uri') ? config.get('app.analyze_uri') : "https://api.datasift.com/v1/pylon/analyze"),
            'json': {
                'hash': process.env.HASH || (config.has('index.default.hash') ? config.get('index.default.hash') : ""),
                'start': start,
                'end': end,
                'parameters': {
                    'analysis_type': 'freqDist',
                    'parameters': {}
                }
            }
        });
    },

    /**
     * isValidKey - check string is a valid target
     * @param str
     * @returns {boolean}
     */
    isValidKey: function(str){
        let arr = ["filter", "interval", "threshold", "target", "id", "name"];
        return (arr.indexOf(str) != -1);
    },

    /**
     * getIndexCreds - set index auth and hash if
     * specified within the task config
     *
     * @param task
     * @param reqObj
     * @returns {*}
     */
    getIndexCreds: function(task, reqObj){
        if(task.index){
            let index = task.index.toString();
            reqObj.auth.user =  (config.has('index.' + index + '.auth.username') ? config.get('index.' + index + '.auth.username') : "");
            reqObj.auth.pass =  (config.has('index.' + index + '.auth.api_key') ? config.get('index.' + index + '.auth.api_key') : "");
            reqObj.json.hash =  (config.has('index.' + index + '.hash') ? config.get('index.' + index + '.hash') : "");
            delete (reqObj.json.parameters.parameters.index);
        }
        return reqObj;
    }

}<|endoftext|>


<commit_before>
process.on('uncaughtException', function (err) {
  Kadira.models.error.trackError(err, 'uncaught');
  Kadira.sendPayload(function () {
    clearTimeout(timer);
    throw err;
  });

  var timer = setTimeout(function () {
    throw err;
  }, 1000*10);
});
<commit_msg>Create a trace with error info to show on the UI<commit_after>
process.on('uncaughtException', function (err) {
  Kadira.models.error.trackError(err, getTrace(err));
  Kadira.sendPayload(function () {
    clearTimeout(timer);
    throw err;
  });

  var timer = setTimeout(function () {
    throw err;
  }, 1000*10);
});

function getTrace(err) {
  return {
    type: 'uncaught',
    name: err.message,
    errored: true,
    at: Kadira.syncedDate.getTime(),
    events: [
      ['start', 0, {}],
      ['error', 0, {error: {message: err.message, stack: err.stack}}]
    ],
    metrics: {
      total: 0
    }
  };
}
<|endoftext|>


<commit_before>'use strict';

var React = require('react');
var utils = require('./utils');

module.exports = function browser(document) {
  function getMountNode(appName) {
    var id = utils.getContainerId(appName);
    return document.getElementById(id);
  }

  function getInitData(appName) {
    var id = utils.getScriptId(appName);
    var scriptNode = document.getElementById(id);
    return JSON.parse(scriptNode.innerHTML);
  }

  function render(appName, Component) {
    var data = getInitData(appName);
    var app = React.createElement(Component, data);
    var mountNode = getMountNode(appName);
    React.render(app, mountNode);
  }

  return {
    getMountNode: getMountNode,
    getInitData: getInitData,
    render: render
  };
};
<commit_msg>react.render is deprecated<commit_after>'use strict';

var React = require('react');
var ReactDOM = require('react-dom');
var utils = require('./utils');

module.exports = function browser(document) {
  function getMountNode(appName) {
    var id = utils.getContainerId(appName);
    return document.getElementById(id);
  }

  function getInitData(appName) {
    var id = utils.getScriptId(appName);
    var scriptNode = document.getElementById(id);
    return JSON.parse(scriptNode.innerHTML);
  }

  function render(appName, Component) {
    var data = getInitData(appName);
    var app = React.createElement(Component, data);
    var mountNode = getMountNode(appName);
    ReactDOM.render(app, mountNode);
  }

  return {
    getMountNode: getMountNode,
    getInitData: getInitData,
    render: render
  };
};
<|endoftext|>


<commit_before>var assert = require('assert')
var fs = require('fs')
var path = require('path')
var level = require('level')
var hyperdrive = require('hyperdrive')
var raf = require('random-access-file')
var encoding = require('dat-encoding')
var debug = require('debug')('dat-node')

module.exports = function (opts, cb) {
  assert.ok(opts, 'lib/init-archive opts required')
  assert.ok(opts.dir, 'dir option required')
  assert.ok(typeof cb === 'function', 'lib/init-archive callback function required')

  var archive
  var drive
  var db
  var dir = opts.dir
  var dbPath = opts.dbPath || path.join(dir, '.dat')
  var createIfMissing = opts.createIfMissing === false ? opts.createIfMissing : true
  var errorIfExists = opts.errorIfExists || false

  // opts.resume backwards compat. TODO: Remove in v2
  if (opts.resume) createIfMissing = false
  if (opts.resume === false) errorIfExists = true

  if (!opts.file) {
    // TODO: do we always want this set?
    opts.file = function (name, opts) {
      return raf(path.join(dir, name), opts && typeof opts.length === 'number' && {length: opts.length})
    }
  }

  if (opts.db || opts.drive) {
    drive = opts.drive || hyperdrive(opts.db)
    db = opts.db ? opts.db : drive.core._db
    return checkDriveKeys()
  }

  if (createIfMissing !== false) return createDb()

  try {
    // check if existing dbPath
    if (!fs.statSync(dbPath).isDirectory()) throw new Error('.dat folder not a dir')
    return createDb()
  } catch (e) {
    return cb('No existing .dat folder')
  }

  function createDb () {
    debug('Making/Reading archive database')
    level(dbPath, function (err, _db) {
      if (err) return cb(err)
      db = _db
      drive = hyperdrive(db)
      checkDriveKeys()
    })
  }

  function checkDriveKeys () {
    // Try to resume archives if there are keys in the drive
    debug('Reading existing keys in drive')
    drive.core.list(function (err, vals) {
      if (err) return cb(err)
      if (vals.length > 2 && !opts.key) return cb(new Error('Drive has multiple archives. Must specify key.'))
      else if (errorIfExists && vals.length) {
        return db.close(function () {
          cb(new Error('Existing feeds in database.'))
        })
      } else if (!vals.length || opts.key) {
        if (!vals.length) debug('No existing feeds in drive')
        if (opts.key) debug('Resuming using key option', opts.key)
        if (opts.key && typeof opts.key === 'string') opts.key = encoding.toBuf(opts.key)
        archive = drive.createArchive(opts.key, opts)
        archive.resumed = false
        return doneArchive(!vals.length && opts.key) // do not open if new drive and external key (e.g. downloading)
      }
      debug(`Drive has ${vals.length} existing keys. Getting archive key.`)

      // Try to guess metadata and maybe fail
      debug('Making archive with key', vals[vals.length - 1].toString('hex'))
      archive = drive.createArchive(vals[vals.length - 1], opts)
      var openTimeout = setTimeout(function () {
        debug('Timeout Open')
        tryOtherKey()
      }, 150)
      archive.open(function (err) {
        clearTimeout(openTimeout)
        if (!err) return doneArchive()
        var badKey = err && (err.message.indexOf('Unknown message type') > -1 || err.message.indexOf('Key not found in database') > -1)
        if (err & !badKey) return cb(err)
        tryOtherKey()
      })

      function tryOtherKey () {
        debug('Bad Key. Trying other key.')
        archive = drive.createArchive(vals[0], opts)
        doneArchive()
      }
    })
  }

  function doneArchive (doNotOpen) {
    debug('Archive created')
    // doNotOpen = true if no existing feeds in drive
    // Opening would have to wait for swarm connection
    if (doNotOpen) return cb(null, archive, db)

    if (archive.resumed !== false) archive.resumed = true

    if (archive.key) debug('Opening archive', archive.key.toString('hex'))
    else debug('Opening snapshot archive')
    archive.open(function (err) {
      if (err) return cb(err)
      debug('Archive opened')
      cb(null, archive, db)
    })
  }
}
<commit_msg>callback with new error object (#89)<commit_after>var assert = require('assert')
var fs = require('fs')
var path = require('path')
var level = require('level')
var hyperdrive = require('hyperdrive')
var raf = require('random-access-file')
var encoding = require('dat-encoding')
var debug = require('debug')('dat-node')

module.exports = function (opts, cb) {
  assert.ok(opts, 'lib/init-archive opts required')
  assert.ok(opts.dir, 'dir option required')
  assert.ok(typeof cb === 'function', 'lib/init-archive callback function required')

  var archive
  var drive
  var db
  var dir = opts.dir
  var dbPath = opts.dbPath || path.join(dir, '.dat')
  var createIfMissing = opts.createIfMissing === false ? opts.createIfMissing : true
  var errorIfExists = opts.errorIfExists || false

  // opts.resume backwards compat. TODO: Remove in v2
  if (opts.resume) createIfMissing = false
  if (opts.resume === false) errorIfExists = true

  if (!opts.file) {
    // TODO: do we always want this set?
    opts.file = function (name, opts) {
      return raf(path.join(dir, name), opts && typeof opts.length === 'number' && {length: opts.length})
    }
  }

  if (opts.db || opts.drive) {
    drive = opts.drive || hyperdrive(opts.db)
    db = opts.db ? opts.db : drive.core._db
    return checkDriveKeys()
  }

  if (createIfMissing !== false) return createDb()

  try {
    // check if existing dbPath
    if (!fs.statSync(dbPath).isDirectory()) throw new Error('.dat folder not a dir')
    return createDb()
  } catch (e) {
    return cb(new Error('No existing .dat folder'))
  }

  function createDb () {
    debug('Making/Reading archive database')
    level(dbPath, function (err, _db) {
      if (err) return cb(err)
      db = _db
      drive = hyperdrive(db)
      checkDriveKeys()
    })
  }

  function checkDriveKeys () {
    // Try to resume archives if there are keys in the drive
    debug('Reading existing keys in drive')
    drive.core.list(function (err, vals) {
      if (err) return cb(err)
      if (vals.length > 2 && !opts.key) return cb(new Error('Drive has multiple archives. Must specify key.'))
      else if (errorIfExists && vals.length) {
        return db.close(function () {
          cb(new Error('Existing feeds in database.'))
        })
      } else if (!vals.length || opts.key) {
        if (!vals.length) debug('No existing feeds in drive')
        if (opts.key) debug('Resuming using key option', opts.key)
        if (opts.key && typeof opts.key === 'string') opts.key = encoding.toBuf(opts.key)
        archive = drive.createArchive(opts.key, opts)
        archive.resumed = false
        return doneArchive(!vals.length && opts.key) // do not open if new drive and external key (e.g. downloading)
      }
      debug(`Drive has ${vals.length} existing keys. Getting archive key.`)

      // Try to guess metadata and maybe fail
      debug('Making archive with key', vals[vals.length - 1].toString('hex'))
      archive = drive.createArchive(vals[vals.length - 1], opts)
      var openTimeout = setTimeout(function () {
        debug('Timeout Open')
        tryOtherKey()
      }, 150)
      archive.open(function (err) {
        clearTimeout(openTimeout)
        if (!err) return doneArchive()
        var badKey = err && (err.message.indexOf('Unknown message type') > -1 || err.message.indexOf('Key not found in database') > -1)
        if (err & !badKey) return cb(err)
        tryOtherKey()
      })

      function tryOtherKey () {
        debug('Bad Key. Trying other key.')
        archive = drive.createArchive(vals[0], opts)
        doneArchive()
      }
    })
  }

  function doneArchive (doNotOpen) {
    debug('Archive created')
    // doNotOpen = true if no existing feeds in drive
    // Opening would have to wait for swarm connection
    if (doNotOpen) return cb(null, archive, db)

    if (archive.resumed !== false) archive.resumed = true

    if (archive.key) debug('Opening archive', archive.key.toString('hex'))
    else debug('Opening snapshot archive')
    archive.open(function (err) {
      if (err) return cb(err)
      debug('Archive opened')
      cb(null, archive, db)
    })
  }
}
<|endoftext|>


<commit_before>// Woodlot log generator module

'use strict';

var getLogFormat = require('./utils').getLogFormat,
    padSingleDigit = require('./utils').padSingleDigit;

// Get required parameters from request and response object to generate log entry
function getLogParameters(req, res, startTime, options) {
    var params = {
        responseTime: +new Date() - startTime + 'ms',
        method: req.method,
        url : req.originalUrl,
        ip: req.ip,
        body: req.body,
        params: req.params,
        query: req.query,
        httpVersion: req.httpVersion,
        statusCode: res.statusCode,
        timeStamp: new Date(),
        contentType: res.get('Content-Type'),
        contentLength: res.get('Content-Length') ? res.get('Content-Length') : null,
        userAgent: req.get('user-agent') ? req.get('user-agent') : '-',
        referrer: req.get('referer') ? req.get('referer') : null
    };

    options.headers ? (params.headers = req.headers) : null;
    options.cookies ? (params.cookies = req.cookies) : null;

    return params;
};

// Function to generate apache-specific timestamp format
function generateCLFTimestamp(timeStamp) {
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        date = new Date(timeStamp);

    return padSingleDigit(date.getUTCDate()) + '/' + months[date.getUTCMonth()] + '/' + date.getUTCFullYear() + ':' + padSingleDigit(date.getUTCHours()) + ':' + padSingleDigit(date.getUTCMinutes()) + ':' + padSingleDigit(date.getUTCSeconds()) + ' +0000';
};

// Function to generate log entry for stdout and specified file streams
function generateLog(req, res, startTime, options) {
    var params = getLogParameters(req, res, startTime, options),
        format = getLogFormat(options),
        contentLength = params.contentLength ? params.contentLength : '-',
        referrer = params.referrer ? '"' + params.referrer + '"' : '"-"',
        clfTimeStamp = generateCLFTimestamp(params.timeStamp);

    // Generate log entry based on type
    switch(format.toLowerCase()) {
        case 'combined':
            /*
                Format example : 127.0.0.1 [Thu Apr 06 2017 00:05:11 GMT+0530 (IST)] "GET /user HTTP1.1" 200 4 "http://www.example.com/" "Mozilla/4.08 [en] (Win98)"
                More info : http://httpd.apache.org/docs/current/logs.html#combined
            */

            return params.ip + " - - [" + clfTimeStamp + "] \"" + params.method + " " + params.url + " HTTP/" + params.httpVersion + "\" " + params.statusCode + " " + contentLength + " " + referrer + " \"" + params.userAgent + "\"\n";

        case 'common':
            /*
                Format example : 127.0.0.1 [Thu Apr 06 2017 00:05:11 GMT+0530 (IST)] "GET /user HTTP1.1" 200 4
                More info : http://httpd.apache.org/docs/current/logs.html#common
            */

            return params.ip + " [" + params.timeStamp + "] \"" + params.method + " " + params.url + " HTTP" + params.httpVersion + "\" " + params.statusCode + " " + contentLength + "\n";

        case 'json':
        default:

            return params;
    }
};

module.exports = generateLog;

<commit_msg>CLF timestamp fixes for apache 'common' format<commit_after>// Woodlot log generator module

'use strict';

var getLogFormat = require('./utils').getLogFormat,
    padSingleDigit = require('./utils').padSingleDigit;

// Get required parameters from request and response object to generate log entry
function getLogParameters(req, res, startTime, options) {
    var params = {
        responseTime: +new Date() - startTime + 'ms',
        method: req.method,
        url : req.originalUrl,
        ip: req.ip,
        body: req.body,
        params: req.params,
        query: req.query,
        httpVersion: req.httpVersion,
        statusCode: res.statusCode,
        timeStamp: new Date(),
        contentType: res.get('Content-Type'),
        contentLength: res.get('Content-Length') ? res.get('Content-Length') : null,
        userAgent: req.get('user-agent') ? req.get('user-agent') : '-',
        referrer: req.get('referer') ? req.get('referer') : null
    };

    options.headers ? (params.headers = req.headers) : null;
    options.cookies ? (params.cookies = req.cookies) : null;

    return params;
};

// Function to generate apache-specific timestamp format
function generateCLFTimestamp(timeStamp) {
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        date = new Date(timeStamp);

    return padSingleDigit(date.getUTCDate()) + '/' + months[date.getUTCMonth()] + '/' + date.getUTCFullYear() + ':' + padSingleDigit(date.getUTCHours()) + ':' + padSingleDigit(date.getUTCMinutes()) + ':' + padSingleDigit(date.getUTCSeconds()) + ' +0000';
};

// Function to generate log entry for stdout and specified file streams
function generateLog(req, res, startTime, options) {
    var params = getLogParameters(req, res, startTime, options),
        format = getLogFormat(options),
        contentLength = params.contentLength ? params.contentLength : '-',
        referrer = params.referrer ? '"' + params.referrer + '"' : '"-"',
        clfTimeStamp = generateCLFTimestamp(params.timeStamp);

    // Generate log entry based on type
    switch(format.toLowerCase()) {
        case 'combined':
            /*
                Format example : 127.0.0.1 [Thu Apr 06 2017 00:05:11 GMT+0530 (IST)] "GET /user HTTP1.1" 200 4 "http://www.example.com/" "Mozilla/4.08 [en] (Win98)"
                More info : http://httpd.apache.org/docs/current/logs.html#combined
            */

            return params.ip + " - - [" + clfTimeStamp + "] \"" + params.method + " " + params.url + " HTTP/" + params.httpVersion + "\" " + params.statusCode + " " + contentLength + " " + referrer + " \"" + params.userAgent + "\"\n";

        case 'common':
            /*
                Format example : 127.0.0.1 [Thu Apr 06 2017 00:05:11 GMT+0530 (IST)] "GET /user HTTP1.1" 200 4
                More info : http://httpd.apache.org/docs/current/logs.html#common
            */

            return params.ip + " - - [" + clfTimeStamp + "] \"" + params.method + " " + params.url + " HTTP/" + params.httpVersion + "\" " + params.statusCode + " " + contentLength + "\n";

        case 'json':
        default:

            return params;
    }
};

module.exports = generateLog;

<|endoftext|>


<commit_before>var path = require('path');
var vow = require('vow');
var EventEmitter = require('events').EventEmitter;
var MagicConfig = require('./magic-config');

/**
 * Хэлпер предназначен для решения задач, связанных с созданием новых нод и таргетов в процессе выполнения сборки.
 *
 * В процессе создания конфигуратора будет зарегистрирован таск c указанным именем, который предназначается для:
 * 1. Создания директорий и файлов, формирующих уровни-сеты.
 * 2. Регистрации нод и таргетов по созданным директориям и файлам.
 * 3. Запуска сборки зарегистрированных таргетов, а также всех таргетов в рамках зарегистрированных нод.
 *
 * Общение между хэлперами происходит через каналы событий. По умолчанию каждый из хэлперов создаёт следующие события:
 *
 * 1. **start** `function () { }`
 *
 * Срабатывает, когда происходит запуск таска.
 *
 * 2. **prebuild** `function (nodes, targets) { }`
 *
 * Срабатывает после создания всех уровней-сетов на файловой системе. Возвращает список всех зарегистрированных `nodes`,
 * а также список всех зарегистрированных таргетов `targets` ноды, которых не были зарегистрированны.
 *
 * 3. **build** `function (targets) { }`
 *
 * Срабатывает в случае успешного завершения сборки, возвращая список собранных таргетов.
 *
 * 4. **error** `function (err) { }`
 *
 * Срабатывает при возникновении ошибки в процессе построения или сборки уровней-сетов.
 *
 * @name MagicHelper
 * @extends EventEmitter
 * @param {String} taskName
 * @param {ProjectConfig} projectConfig
 * @constructor
 */
var MagicHelper = function (taskName, projectConfig) {
    this._chains = [];

    this._taskName = taskName;
    this._projectConfig = projectConfig;
    this._eventChannel = new EventEmitter();

    this._configureTask();
};

/**
 * Возвращает название таска, в котором происходит создание и нод и таргетов.
 *
 * @returns {String}
 */
MagicHelper.prototype.getTaskName = function () {
    return this._taskName;
};

/**
 * Возвращает конфиг, с помощью которого производится настройка сборки нод и таргетов.
 *
 * @returns {ProjectConfig}
 */
MagicHelper.prototype.getProjectConfig = function () {
    return this._projectConfig;
};

/**
 * Возвращает канал событий данного хэлпера.
 *
 * @returns {EventEmitter}
 */
MagicHelper.prototype.getEventChannel = function () {
    return this._eventChannel;
};

/**
 * Позволяет декларировать создание нод и таргетов на файловой системе для последующей их сборки.
 * Функции `callback` первым аргументом будет передан инстанс `MagicConfig`.
 *
 * Предполагается, что в функции `callback` будут производиться некие действия по созданию нод и таргетов
 * на файловой системе, которые будут зарегистрированы с помощью `MagicConfig`.
 *
 * Если несколько раз задекларировать создание уровней-сетов, то каждая такая `callback` функция будет выполняться
 * асинхронно, т.е. независимо от остальных.
 *
 * @param {Function} callback
 */
MagicHelper.prototype.prebuild = function (callback) {
    this._chains.push(callback);
};

MagicHelper.prototype._configureTask = function () {
    var _this = this;
    var projectConfig = this._projectConfig;
    var taskName = this._taskName;
    var channel = this._eventChannel;

    projectConfig.task(taskName, function (task) {
        var args = [].slice.call(arguments, 1);
        var magicConfig = new MagicConfig(args);

        _this._startTime = new Date();
        task.log('building started');
        channel.emit('start');

        // Запускаем сборку задекларированных `callback`-функций
        return vow.all(_this._chains.map(function (callback) {
                return callback.apply(this, [magicConfig]);
            }))
            .then(function () {
                var registeredNodes = magicConfig.getRegisteredNodes();
                var targets = magicConfig.getRegisteredTargets();
                var nodes = magicConfig.getNodes();
                var toBuild = {};

                channel.emit('prebuild', registeredNodes, targets);

                if (args.length) {
                    // Ничего не делаем, если нет нод и таргетов для сборки
                    if (nodes.length === 0 && targets.length === 0) {
                        return vow.resolve([]);
                    }

                    // Собираем все ноды, если пользователь не указал ожидаемых целей
                    return _this._buildTargets(nodes);
                }

                // Раскрываем цели в список нод и таргетов для сборки
                args.forEach(function (arg) {
                    // Добавляем цель в сборку, если она является была зарегистрированна
                    // или входит в явно или косвенно зарегистрированную ноду
                    if (magicConfig.isRegisteredTarget(arg) || nodes.indexOf(path.dirname(arg))) {
                        toBuild[arg] = true;
                    }

                    if (magicConfig.isRequiredNode(arg)) {
                        // Раскрываем цель в список нод
                        nodes.forEach(function (node) {
                            if (node.indexOf(arg) === 0) {
                                toBuild[node] = true;
                            }
                        });
                    }
                });

                return _this._buildTargets(Object.keys(toBuild));
            });
    });

    this._taskConfig = projectConfig.getTaskConfig(taskName);
};

MagicHelper.prototype._buildTargets = function (targets) {
    var startTime = this._startTime;
    var task = this._taskConfig;
    var makePlatform = task.getMakePlatform();
    var root = makePlatform.getDir();
    var channel = this._eventChannel;

    return makePlatform.init(root)
        .then(function () {
            makePlatform.loadCache();

            return makePlatform.buildTargets(targets);
        })
        .then(function () {
            task.log('building finished - ' + (new Date() - startTime) + 'ms');

            channel.emit('build', targets);

            return targets;
        })
        .fail(function (err) {
            task.log('building failed');

            channel.emit('error', err);

            throw err;
        });
};

module.exports = MagicHelper;
<commit_msg>Fixed bug with prepare targets to build<commit_after>var path = require('path');
var vow = require('vow');
var EventEmitter = require('events').EventEmitter;
var MagicConfig = require('./magic-config');

/**
 * Хэлпер предназначен для решения задач, связанных с созданием новых нод и таргетов в процессе выполнения сборки.
 *
 * В процессе создания конфигуратора будет зарегистрирован таск c указанным именем, который предназначается для:
 * 1. Создания директорий и файлов, формирующих уровни-сеты.
 * 2. Регистрации нод и таргетов по созданным директориям и файлам.
 * 3. Запуска сборки зарегистрированных таргетов, а также всех таргетов в рамках зарегистрированных нод.
 *
 * Общение между хэлперами происходит через каналы событий. По умолчанию каждый из хэлперов создаёт следующие события:
 *
 * 1. **start** `function () { }`
 *
 * Срабатывает, когда происходит запуск таска.
 *
 * 2. **prebuild** `function (nodes, targets) { }`
 *
 * Срабатывает после создания всех уровней-сетов на файловой системе. Возвращает список всех зарегистрированных `nodes`,
 * а также список всех зарегистрированных таргетов `targets` ноды, которых не были зарегистрированны.
 *
 * 3. **build** `function (targets) { }`
 *
 * Срабатывает в случае успешного завершения сборки, возвращая список собранных таргетов.
 *
 * 4. **error** `function (err) { }`
 *
 * Срабатывает при возникновении ошибки в процессе построения или сборки уровней-сетов.
 *
 * @name MagicHelper
 * @extends EventEmitter
 * @param {String} taskName
 * @param {ProjectConfig} projectConfig
 * @constructor
 */
var MagicHelper = function (taskName, projectConfig) {
    this._chains = [];

    this._taskName = taskName;
    this._projectConfig = projectConfig;
    this._eventChannel = new EventEmitter();

    this._configureTask();
};

/**
 * Возвращает название таска, в котором происходит создание и нод и таргетов.
 *
 * @returns {String}
 */
MagicHelper.prototype.getTaskName = function () {
    return this._taskName;
};

/**
 * Возвращает конфиг, с помощью которого производится настройка сборки нод и таргетов.
 *
 * @returns {ProjectConfig}
 */
MagicHelper.prototype.getProjectConfig = function () {
    return this._projectConfig;
};

/**
 * Возвращает канал событий данного хэлпера.
 *
 * @returns {EventEmitter}
 */
MagicHelper.prototype.getEventChannel = function () {
    return this._eventChannel;
};

/**
 * Позволяет декларировать создание нод и таргетов на файловой системе для последующей их сборки.
 * Функции `callback` первым аргументом будет передан инстанс `MagicConfig`.
 *
 * Предполагается, что в функции `callback` будут производиться некие действия по созданию нод и таргетов
 * на файловой системе, которые будут зарегистрированы с помощью `MagicConfig`.
 *
 * Если несколько раз задекларировать создание уровней-сетов, то каждая такая `callback` функция будет выполняться
 * асинхронно, т.е. независимо от остальных.
 *
 * @param {Function} callback
 */
MagicHelper.prototype.prebuild = function (callback) {
    this._chains.push(callback);
};

MagicHelper.prototype._configureTask = function () {
    var _this = this;
    var projectConfig = this._projectConfig;
    var taskName = this._taskName;
    var channel = this._eventChannel;

    projectConfig.task(taskName, function (task) {
        var args = [].slice.call(arguments, 1);
        var magicConfig = new MagicConfig(args);

        _this._startTime = new Date();
        task.log('building started');
        channel.emit('start');

        // Запускаем сборку задекларированных `callback`-функций
        return vow.all(_this._chains.map(function (callback) {
                return callback.apply(this, [magicConfig]);
            }))
            .then(function () {
                var registeredNodes = magicConfig.getRegisteredNodes();
                var targets = magicConfig.getRegisteredTargets();
                var nodes = magicConfig.getNodes();
                var toBuild = {};

                channel.emit('prebuild', registeredNodes, targets);

                // Ничего не делаем, если нет зарегистрированных нод и таргетов
                if (nodes.length === 0 && targets.length === 0) {
                    return vow.resolve([]);
                }

                // Собираем все ноды, если пользователь не указал ожидаемых целей
                if (!args.length) {
                    return _this._buildTargets(nodes);
                }

                // Раскрываем цели в список нод и таргетов для сборки
                args.forEach(function (arg) {
                    // Добавляем цель в сборку, если она была зарегистрированна
                    // или входит в явно или косвенно зарегистрированную ноду
                    if (magicConfig.isRegisteredTarget(arg) || nodes.indexOf(path.dirname(arg)) !== -1) {
                        toBuild[arg] = true;
                    }

                    if (magicConfig.isRequiredNode(arg)) {
                        // Раскрываем цель в список нод
                        nodes.forEach(function (node) {
                            if (node.indexOf(arg) === 0) {
                                toBuild[node] = true;
                            }
                        });
                    }
                });

                return _this._buildTargets(Object.keys(toBuild));
            });
    });

    this._taskConfig = projectConfig.getTaskConfig(taskName);
};

MagicHelper.prototype._buildTargets = function (targets) {
    var startTime = this._startTime;
    var task = this._taskConfig;
    var makePlatform = task.getMakePlatform();
    var root = makePlatform.getDir();
    var channel = this._eventChannel;

    return makePlatform.init(root)
        .then(function () {
            makePlatform.loadCache();

            return makePlatform.buildTargets(targets);
        })
        .then(function () {
            task.log('building finished - ' + (new Date() - startTime) + 'ms');

            channel.emit('build', targets);

            return targets;
        })
        .fail(function (err) {
            task.log('building failed');

            channel.emit('error', err);

            throw err;
        });
};

module.exports = MagicHelper;
<|endoftext|>


<commit_before>var fs = require("fs")
  , async = require("async")
  , marked = require("marked")
  , tmp = require('tmp')
  , childProcess = require('child_process')
  , path = require("path")

tmp.setGracefulCleanup()

/**
 * Converts the passed Markdown files to PDF files.
 *
 * @param {String|Array} filePaths Paths of markdown files to convert
 * @param {Object} [opts] Rendering options
 * @param {String} [opts.phantomPath] Path to phantom binary
 * @param {String} [opts.cssPath] Path to custom CSS file
 * @param {String} [opts.paperFormat] 'A3', 'A4', 'A5', 'Legal', 'Letter' or 'Tabloid'
 * @param {String} [opts.paperOrientation] 'portrait' or 'landscape'
 * @param {String} [opts.paperBorder] Supported dimension units are: 'mm', 'cm', 'in', 'px'
 * @param {Number} [opts.renderDelay] Delay before rendering the PDF (give HTML and CSS a chance to load)
 * @param {Function} cb Callback function invoked when the files have been generated. Passed Error and an Array of
 * file paths - a list of PDF files containing your PDF content. These are stored in a temp folder so if you want to
 * keep them, you MUST move or copy them or they will be deleted when the process exits.
 */
module.exports = function (filePaths, opts, cb) {

  if (Object.prototype.toString.call(filePaths) == '[object String]') {
    filePaths = [filePaths]
  }

  if (!cb) {
    cb = opts
    opts = {}
  }

  opts.phantomPath = opts.phantomPath || require("phantomjs").path
  opts.cssPath = opts.cssPath || "../pdf.css"
  opts.paperFormat = opts.paperFormat || "A4"
  opts.paperOrientation = opts.paperOrientation || "portrait"
  opts.paperBorder = opts.paperBorder || "1cm"
  opts.renderDelay = opts.renderDelay || 500
  opts.concatFiles = opts.concatFiles || false

  if (opts.concatFiles) {
    concatFiles(filePaths, function(er, concatenatedFile) {
      if (er) return cb(er)

      run([ concatenatedFile ], opts, cb)
    })
  }
  else {
    run(filePaths, opts, cb)
  }
}

/**
 * Run the coversion tasks.
 *
 * @param  {Array} filePaths Paths of files to convert
 * @param  {Object} opts Render options
 * @param {Function} cb Callback function invoked when the files have been generated
 */
function run (filePaths, opts, cb) {
  var tasks = filePaths.map(function (filename) {
    return markdownToPdfTask(path.resolve(filename), opts)
  })

  async.parallel(tasks, cb)
}

/**
 * Concatenate files into a single file.
 *
 * @param  {Array} filePaths Paths to files to concatenate together
 * @param  {Function} cb Callback function invoked when the files have been concatenated. Passed Error and a String
 * path to the new file.
 */
function concatFiles (filePaths, cb) {
  tmp.file(function (er, tmpMdPath, tmpMdFd) {
    if (er) return cb(er)
    fs.close(tmpMdFd)

    var writeStream = fs.createWriteStream(tmpMdPath)

    function iterate () {
      if (filePaths.length === 0) {
        writeStream.end();
        return cb(null, tmpMdPath)
      }

      var path = filePaths.shift()
      var readStream = fs.createReadStream(path)
      readStream.on('end', function () {
        iterate()
      })
      readStream.pipe(writeStream, { end: false })
    }

    iterate()
  })
}

/**
 * Create a function that can be passed to async.parallel that performs the conversion.
 *
 * @param {String} filePath File to convert
 * @param {Object} opts Render options
 * @returns {Function}
 */
function markdownToPdfTask (filePath, opts) {
  return function (cb) {

    fs.stat(filePath, function (er, stats) {
      if (er) return cb(er)

      if (!stats.isFile()) return cb(new Error(filePath + " is not a file"))

      // Read the markdown
      fs.readFile(filePath, {encoding: 'utf-8'}, function (er, data) {

        data = opts.preProcessMd ? opts.preProcessMd(data) : data

        // Convert to HTML
        var html = marked(data)

        html = opts.preProcessHtml ? opts.preProcessHtml(html) : html

        // Save HTML to tmp file
        tmp.file(function (er, tmpHtmlPath, tmpHtmlFd) {
          if (er) return cb(er)
          fs.close(tmpHtmlFd)

          fs.writeFile(tmpHtmlPath, html, function (er) {
            if (er) return cb(er)

            // Create tmp file to save PDF to
            tmp.file({postfix: '.pdf'}, function (er, tmpPdfPath, tmpPdfFd) {
              if (er) return cb(er)
              fs.close(tmpPdfFd)

              // Invoke phantom and to generate the PDF
              var childArgs = [
                  path.join(__dirname, "..", "lib-phantom", "markdown-pdf.js")
                , tmpHtmlPath
                , tmpPdfPath
                , opts.cssPath
                , opts.paperFormat
                , opts.paperOrientation
                , opts.paperBorder
                , opts.renderDelay
              ]

              childProcess.execFile(opts.phantomPath, childArgs, function(er, stdout, stderr) {
                //if (stdout) console.log(stdout)
                //if (stderr) console.error(stderr)
                if (er) return cb(er)
                cb(null, tmpPdfPath)
              })
            })
          })
        })
      })
    })
  }
}<commit_msg>Use copied source array before concatenating files<commit_after>var fs = require("fs")
  , async = require("async")
  , marked = require("marked")
  , tmp = require('tmp')
  , childProcess = require('child_process')
  , path = require("path")

tmp.setGracefulCleanup()

/**
 * Converts the passed Markdown files to PDF files.
 *
 * @param {String|Array} filePaths Paths of markdown files to convert
 * @param {Object} [opts] Rendering options
 * @param {String} [opts.phantomPath] Path to phantom binary
 * @param {String} [opts.cssPath] Path to custom CSS file
 * @param {String} [opts.paperFormat] 'A3', 'A4', 'A5', 'Legal', 'Letter' or 'Tabloid'
 * @param {String} [opts.paperOrientation] 'portrait' or 'landscape'
 * @param {String} [opts.paperBorder] Supported dimension units are: 'mm', 'cm', 'in', 'px'
 * @param {Number} [opts.renderDelay] Delay before rendering the PDF (give HTML and CSS a chance to load)
 * @param {Function} cb Callback function invoked when the files have been generated. Passed Error and an Array of
 * file paths - a list of PDF files containing your PDF content. These are stored in a temp folder so if you want to
 * keep them, you MUST move or copy them or they will be deleted when the process exits.
 */
module.exports = function (filePaths, opts, cb) {

  if (Object.prototype.toString.call(filePaths) == '[object String]') {
    filePaths = [filePaths]
  }

  if (!cb) {
    cb = opts
    opts = {}
  }

  opts.phantomPath = opts.phantomPath || require("phantomjs").path
  opts.cssPath = opts.cssPath || "../pdf.css"
  opts.paperFormat = opts.paperFormat || "A4"
  opts.paperOrientation = opts.paperOrientation || "portrait"
  opts.paperBorder = opts.paperBorder || "1cm"
  opts.renderDelay = opts.renderDelay || 500
  opts.concatFiles = opts.concatFiles || false

  if (opts.concatFiles) {
    concatFiles(filePaths, function(er, concatenatedFile) {
      if (er) return cb(er)

      run([ concatenatedFile ], opts, cb)
    })
  }
  else {
    run(filePaths, opts, cb)
  }
}

/**
 * Run the coversion tasks.
 *
 * @param  {Array} filePaths Paths of files to convert
 * @param  {Object} opts Render options
 * @param {Function} cb Callback function invoked when the files have been generated
 */
function run (filePaths, opts, cb) {
  var tasks = filePaths.map(function (filename) {
    return markdownToPdfTask(path.resolve(filename), opts)
  })

  async.parallel(tasks, cb)
}

/**
 * Concatenate files into a single file.
 *
 * @param  {Array} filePaths Paths to files to concatenate together
 * @param  {Function} cb Callback function invoked when the files have been concatenated. Passed Error and a String
 * path to the new file.
 */
function concatFiles (filePaths, cb) {
  filePaths = filePaths.slice(0);
  tmp.file(function (er, tmpMdPath, tmpMdFd) {
    if (er) return cb(er)
    fs.close(tmpMdFd)

    var writeStream = fs.createWriteStream(tmpMdPath)

    function iterate () {
      if (filePaths.length === 0) {
        writeStream.end();
        return cb(null, tmpMdPath)
      }

      var path = filePaths.shift()
      var readStream = fs.createReadStream(path)
      readStream.on('end', function () {
        iterate()
      })
      readStream.pipe(writeStream, { end: false })
    }

    iterate()
  })
}

/**
 * Create a function that can be passed to async.parallel that performs the conversion.
 *
 * @param {String} filePath File to convert
 * @param {Object} opts Render options
 * @returns {Function}
 */
function markdownToPdfTask (filePath, opts) {
  return function (cb) {

    fs.stat(filePath, function (er, stats) {
      if (er) return cb(er)

      if (!stats.isFile()) return cb(new Error(filePath + " is not a file"))

      // Read the markdown
      fs.readFile(filePath, {encoding: 'utf-8'}, function (er, data) {

        data = opts.preProcessMd ? opts.preProcessMd(data) : data

        // Convert to HTML
        var html = marked(data)

        html = opts.preProcessHtml ? opts.preProcessHtml(html) : html

        // Save HTML to tmp file
        tmp.file(function (er, tmpHtmlPath, tmpHtmlFd) {
          if (er) return cb(er)
          fs.close(tmpHtmlFd)

          fs.writeFile(tmpHtmlPath, html, function (er) {
            if (er) return cb(er)

            // Create tmp file to save PDF to
            tmp.file({postfix: '.pdf'}, function (er, tmpPdfPath, tmpPdfFd) {
              if (er) return cb(er)
              fs.close(tmpPdfFd)

              // Invoke phantom and to generate the PDF
              var childArgs = [
                  path.join(__dirname, "..", "lib-phantom", "markdown-pdf.js")
                , tmpHtmlPath
                , tmpPdfPath
                , opts.cssPath
                , opts.paperFormat
                , opts.paperOrientation
                , opts.paperBorder
                , opts.renderDelay
              ]

              childProcess.execFile(opts.phantomPath, childArgs, function(er, stdout, stderr) {
                //if (stdout) console.log(stdout)
                //if (stderr) console.error(stderr)
                if (er) return cb(er)
                cb(null, tmpPdfPath)
              })
            })
          })
        })
      })
    })
  }
}<|endoftext|>


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

var path = require('path')
var log = require('npmlog')
var fs = require('fs')
var extend = require('xtend')

var pkg = require(path.resolve('package.json'))
var rc = require('./rc')(pkg)
var download = require('./download')
var util = require('./util')

var prebuildClientVersion = require('./package.json').version
if (rc.version) {
  console.log(prebuildClientVersion)
  process.exit(0)
}

if (rc.path) process.chdir(rc.path)

log.heading = 'prebuild-install'
if (rc.verbose) {
  log.level = 'verbose'
}

if (!fs.existsSync('package.json')) {
  log.error('setup', 'No package.json found. Aborting...')
  process.exit(1)
}

if (rc.help) {
  console.error(fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf-8'))
  process.exit(0)
}

log.info('begin', 'Prebuild-install version', prebuildClientVersion)

var opts = extend(rc, {pkg: pkg, log: log})

var execPath = process.env.npm_execpath || process.env.NPM_CLI_JS

if (util.isYarnPath(execPath) && /node_modules/.test(process.cwd())) {
  // From yarn repository
} else if (!(typeof pkg._from === 'string')) {
  log.info('install', 'installing inside prebuild-install directory, skipping download.')
  process.exit(1)
} else if (pkg._from.length > 4 && pkg._from.substr(0, 4) === 'git+') {
  log.info('install', 'installing from git repository, skipping download.')
  process.exit(1)
} else if (opts.compile === true || opts.prebuild === false) {
  log.info('install', '--build-from-source specified, not attempting download.')
  process.exit(1)
}

download(opts, function (err) {
  if (err) {
    log.warn('install', err.message)
    return process.exit(1)
  }
  log.info('install', 'Prebuild successfully installed!')
})
<commit_msg>:sparkles: tweak installation messages<commit_after>#!/usr/bin/env node

var path = require('path')
var log = require('npmlog')
var fs = require('fs')
var extend = require('xtend')

var pkg = require(path.resolve('package.json'))
var rc = require('./rc')(pkg)
var download = require('./download')
var util = require('./util')

var prebuildClientVersion = require('./package.json').version
if (rc.version) {
  console.log(prebuildClientVersion)
  process.exit(0)
}

if (rc.path) process.chdir(rc.path)

log.heading = 'prebuild-install'
if (rc.verbose) {
  log.level = 'verbose'
}

if (!fs.existsSync('package.json')) {
  log.error('setup', 'No package.json found. Aborting...')
  process.exit(1)
}

if (rc.help) {
  console.error(fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf-8'))
  process.exit(0)
}

log.info('begin', 'Prebuild-install version', prebuildClientVersion)

var opts = extend(rc, {pkg: pkg, log: log})

var execPath = process.env.npm_execpath || process.env.NPM_CLI_JS

if (util.isYarnPath(execPath) && /node_modules/.test(process.cwd())) {
  // From yarn repository
} else if (!(typeof pkg._from === 'string')) {
  log.info('install', 'installing standalone, skipping download.')
  process.exit(1)
} else if (pkg._from.length > 4 && pkg._from.substr(0, 4) === 'git+') {
  log.info('install', 'installing from git repository, skipping download.')
  process.exit(1)
} else if (opts.compile === true || opts.prebuild === false) {
  log.info('install', '--build-from-source specified, not attempting download.')
  process.exit(1)
}

download(opts, function (err) {
  if (err) {
    log.warn('install', err.message)
    return process.exit(1)
  }
  log.info('install', 'Successfully installed prebuilt binary!')
})
<|endoftext|>


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

function usage () {/*

Usage

  http-file-store --config=/path/to/config.json

Options

  --config  | -c   Path to the JSON configuration file
  --verbose | -v   Enable verbosity pass in the module list to debug.

Config

  The configuration is a plain json object describing several options to
  apply to your instance of http-file-store.

  {
    "base": "/path/to/the/directory/to/read/write/files",
    "url_base": "/base/url/to/serve/files",
    "upload_path": "/base/to/temp/uploaded/files",
    "ssl": {
      "port": "a number, or null for a random port",
      "host": "a host value to listen for https requests",
      "key": "a path to an SSL key",
      "ca": "a path to the SSL CA file",
      "cert": "a path to the SSL cert file",
    },
    "clear": {
      "port": "a number, or null for a random port",
      "host": "a host value to listen for http requests",
    }
  }

*/}
var pkg   = require('./package.json')
var argv  = require('minimist')(process.argv.slice(2));
var help  = require('@maboiteaspam/show-help')(usage, argv.h||argv.help, pkg)
var debug = require('@maboiteaspam/set-verbosity')(pkg.name, argv.v || argv.verbose);
var fs    = require('fs')

const configPath  = argv.config || argv.c || false;

(!configPath) && help.print(usage, pkg) && help.die(
  "Wrong invokation"
);

var config = {}
try{
  config = require(configPath)
}catch(ex){
  help.die(
    "Config path must exist and be a valid JSON file.\n" + ex
  );
}

(!config) && help.print(usage, pkg)
&& help.die(
  "The configuration could not be loaded, please double check the file"
);

(!config.base) && help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : base is missing"
);

(!config.url_base)
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : url_base is missing"
);

(!config.upload_path)
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : upload_path is missing"
);

(!config.clear || !config.ssl)
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : you must provide one of clear or ssl options"
);

(!fs.existsSync(config.base))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : base directory must exist"
);

(config.ssl && !fs.existsSync(config.ssl.key))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : SSL key file must exist"
);

(config.ssl && config.ssl.ca && !fs.existsSync(config.ssl.ca))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : SSL ca file must exist"
);

(config.ssl && !fs.existsSync(config.ssl.cert))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : SSL cert file must exist"
);

var http        = require('http');
var https       = require('https');
var express     = require('express');
var bodyParser  = require('body-parser');
var multer      = require('multer');
var fileStore   = require('./index.js');

var upload = multer({ dest: config.upload_path });
var app = express();

app.get(config.base_url, fileStore.read(config.base));
app.post(config.base_url,
  upload.single('file'),
  fileStore.write(config.base, config.allowOverwrite));

if ( config.ssl && config.ssl.key && config.ssl.cert ) {
  var SSL = https.createServer( {
      key: fs.readFileSync( config.ssl.key ),
      cert: fs.readFileSync( config.ssl.cert ),
      ca: config.ssl.ca || []
  }, app );

  SSL.listen(config.ssl.port, config.ssl.host);
}

var CLEAR = http.createServer( app );

CLEAR.listen(config.lear.port, config.clear.host);
<commit_msg>Fix typos in binary<commit_after>#!/usr/bin/env node

function usage () {/*

Usage

  http-file-store --config=/path/to/config.json

Options

  --config  | -c   Path to the JSON configuration file
  --verbose | -v   Enable verbosity pass in the module list to debug.

Config

  The configuration is a plain json object describing several options to
  apply to your instance of http-file-store.

  {
    "base": "/path/to/the/directory/to/read/write/files",
    "url_base": "/base/url/to/serve/files",
    "upload_path": "/base/to/temp/uploaded/files",
    "ssl": {
      "port": "a number, or null for a random port",
      "host": "a host value to listen for https requests",
      "key": "a path to an SSL key",
      "ca": "a path to the SSL CA file",
      "cert": "a path to the SSL cert file",
    },
    "clear": {
      "port": "a number, or null for a random port",
      "host": "a host value to listen for http requests",
    }
  }

*/}
var pkg   = require('./package.json')
var argv  = require('minimist')(process.argv.slice(2));
var help  = require('@maboiteaspam/show-help')(usage, argv.h||argv.help, pkg)
var debug = require('@maboiteaspam/set-verbosity')(pkg.name, argv.v || argv.verbose);
var fs    = require('fs')

const configPath  = argv.config || argv.c || false;

(!configPath) && help.print(usage, pkg) && help.die(
  "Wrong invokation"
);

var config = {}
try{
  config = require(configPath)
}catch(ex){
  help.die(
    "Config path must exist and be a valid JSON file.\n" + ex
  );
}

(!config) && help.print(usage, pkg)
&& help.die(
  "The configuration could not be loaded, please double check the file"
);

(!config.base) && help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : base is missing"
);

(!config.url_base)
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : url_base is missing"
);

(!config.upload_path)
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : upload_path is missing"
);

(!config.clear && !config.ssl)
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : you must provide one of clear or ssl options"
);

(!fs.existsSync(config.base))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : base directory must exist"
);

(config.ssl && !fs.existsSync(config.ssl.key))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : SSL key file must exist"
);

(config.ssl && config.ssl.ca && !fs.existsSync(config.ssl.ca))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : SSL ca file must exist"
);

(config.ssl && !fs.existsSync(config.ssl.cert))
&& help.print(usage, pkg)
&& help.die(
  "Configuration options are wrong : SSL cert file must exist"
);

var http        = require('http');
var https       = require('https');
var express     = require('express');
var bodyParser  = require('body-parser');
var multer      = require('multer');
var fileStore   = require('./index.js');

var upload = multer({ dest: config.upload_path });
var app = express();

app.get(config.base_url, fileStore.read(config.base));
app.post(config.base_url,
  upload.single('file'),
  fileStore.write(config.base, config.allowOverwrite));

if ( config.ssl && config.ssl.key && config.ssl.cert ) {
  var SSL = https.createServer( {
      key: fs.readFileSync( config.ssl.key ),
      cert: fs.readFileSync( config.ssl.cert ),
      ca: config.ssl.ca || []
  }, app );

  SSL.listen(config.ssl.port, config.ssl.host);
}

var CLEAR = http.createServer( app );

CLEAR.listen(config.clear.port, config.clear.host);
<|endoftext|>


<commit_before>/**
* Pull in required modules
**/
const Constants     = require('../constants');
const async         = require('async');
const url           = require('url');
const dns           = require('dns');
const _             = require('lodash');
const os            = require('os');
const S             = require('string');
const querystring   = require('querystring');

/**
* Expose our creation class that will be called
* with properties.
**/
module.exports = exports = function(params) {

  /**
  * The Test object to return that we can use
  **/
  var Test = [];

  /**
  * Adds the array of tests if given
  **/
  if(Array.isArray(params.rules || []) === true) {

    // concat our list

  }

  /**
  * Get the sections of the key
  **/
  var sections = (params.key || '').split('.');

  /**
  * Defines the options we can use
  **/
  Test.getKey = function() {

    // returns the key
    return params.key;

  };

  /**
  * Returns the defined test key
  **/
  Test.getTest = function() {

    // returns the key
    return params.test || sections[1] || 'unknown';

  };

  /**
  * Returns the defined category key
  **/
  Test.getCategory = function() {

    // returns the key
    return params.category || sections[0] || 'unknown';

  };

  /**
  * Returns the current version of the test
  **/
  Test.getVersion = function() {

    // done
    return params.version || null;

  };

  /**
  * Runs on boot for the test
  **/
  Test.bootstrap = function(logger, fn) {

    // just finish for now
    fn(null);

  };

  // return the Test object to use
  return Test;

};
<commit_msg>Updated to sanity check the object existence before just using it<commit_after>/**
* Pull in required modules
**/
const Constants     = require('../constants');
const async         = require('async');
const url           = require('url');
const dns           = require('dns');
const _             = require('lodash');
const os            = require('os');
const S             = require('string');
const querystring   = require('querystring');

/**
* Expose our creation class that will be called
* with properties.
**/
module.exports = exports = function(params) {

  /**
  * The Test object to return that we can use
  **/
  var Test = [];

  /**
  * Adds the array of tests if given
  **/
  if(params.rules && Array.isArray(params.rules || []) === true) {

    // concat our list
    Test = Test.concat(params.rules || []);

  } else if(params.rules && typeof params.rules === 'function') {

    // add that
    Test = Test.concat([ params.rules ]);

  }

  /**
  * Get the sections of the key
  **/
  var sections = (params.key || '').split('.');

  /**
  * Defines the options we can use
  **/
  Test.getKey = function() {

    // returns the key
    return params.key;

  };

  /**
  * Returns the defined test key
  **/
  Test.getTest = function() {

    // returns the key
    return params.test || sections[1] || 'unknown';

  };

  /**
  * Returns the defined category key
  **/
  Test.getCategory = function() {

    // returns the key
    return params.category || sections[0] || 'unknown';

  };

  /**
  * Returns the current version of the test
  **/
  Test.getVersion = function() {

    // done
    return params.version || null;

  };

  /**
  * Runs on boot for the test
  **/
  Test.bootstrap = function(logger, fn) {

    // just finish for now
    fn(null);

  };

  // return the Test object to use
  return Test;

};
<|endoftext|>