<commit_before>"use strict";

const BloggifyCore = require("bloggify-core")
    , ul = require("ul")
    , defaults = require("./defaults")
    , Lien = require("lien")
    ;

/**
 * bloggifyServer
 * The Bloggify server.
 *
 * @name bloggifyServer
 * @function
 * @param {Number} a Param descrpition.
 * @param {Number} b Param descrpition.
 * @return {Number} Return description.
 */
module.exports = class BloggifyServer extends BloggifyCore {
    constructor (options) {
        if (typeof options === "string") {
            options = { root: options };
        }

        options = ul.deepMerge(options, defaults);
        super(options.root, options);

        options = this.options;
        options.server.public = this.paths.getPublicPaths();

        this.server = new Lien(options.server);
        this._serverLoaded = false;
        this._serverPort = options.server.port;
        this.server.on("load", (err, data) => {
            this._serverLoaded = [err, data];
        });

        if (options.loadPlugins !== false) {
            this.loadPlugins();
        }

        let firstRequests = this._firstRequests = [];
        this.on("plugins-loaded", () => {
            firstRequests._called = true;
            firstRequests.forEach(c => c[2]());
        });

        this.server.router.use(function (req, res, next) {
            if (firstRequests._called) {
                return next();
            }
            firstRequests.push([req, res, next]);
        });
    }

    onLoad (cb) {
        if (this._serverLoaded) {
            return cb(this._serverLoaded[0], this._serverLoaded[1]);
        }
        this.server.on("load", cb);
    }
};
<commit_msg>Rename the bundler<commit_after>"use strict";

const BloggifyCore = require("bloggify-core")
    , ul = require("ul")
    , defaults = require("./defaults")
    , Lien = require("lien")
    ;

/**
 * bloggifyServer
 * The Bloggify server.
 *
 * @name bloggifyServer
 * @function
 * @param {Number} a Param descrpition.
 * @param {Number} b Param descrpition.
 * @return {Number} Return description.
 */
module.exports = class BloggifyServer extends BloggifyCore {
    constructor (options) {
        if (typeof options === "string") {
            options = { root: options };
        }

        options = ul.deepMerge(options, defaults);
        super(options.root, options);

        options = this.options;
        options.server.public = this.paths.getPublicPaths();

        this.server = new Lien(options.server);
        this._serverLoaded = false;
        this._serverPort = options.server.port;
        this.server.on("load", (err, data) => {
            this._serverLoaded = [err, data];
        });

        options.bundler = options.bundler || require("rucksack");

        this.assets = new options.bundler({
            jsOutput: this.paths.bundlePaths.js
          , cssOutput: this.paths.bundlePaths.css
          , jsUrl: this.paths.bundleUrls.js
          , cssUrl: this.paths.bundleUrls.css
          , development: !!~process.argv.indexOf("--production-bundle") ? false : !this.production
        });

        if (options.loadPlugins !== false) {
            this.loadPlugins();
        }

        let firstRequests = this._firstRequests = [];
        this.on("plugins-loaded", () => {
            this.assets.bundle();
            firstRequests._called = true;
            firstRequests.forEach(c => c[2]());
        });

        this.server.router.use(function (req, res, next) {
            if (firstRequests._called) {
                return next();
            }
            firstRequests.push([req, res, next]);
        });
    }

    onLoad (cb) {
        if (this._serverLoaded) {
            return cb(this._serverLoaded[0], this._serverLoaded[1]);
        }
        this.server.on("load", cb);
    }
};
<|endoftext|>


<commit_before>#!/usr/bin/env node
var moment = require('moment');
var sync = require('synchronize');
var scraperjs = require('scraperjs');

function TideCrawler(argv) {

  this.location = 'falmouth';  //Default location
  this.date = '';

  // if the location argument starts with @ remove it
  this.formatLocation = function(location) {
    if (location[0] === '@') {
      return location.substring(1)
    } else {
      return location
    }
  }

  // takes tomorrow or a day of the week and converts it to a date url fragment
  this.formatDate = function(day) {
    // Vaid Day arguments
    var dayArgs = ['tomorrow', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

    var index = dayArgs.indexOf(day);
    if (index > -1 ){
      if (index === 0) {
        var date = moment().add('days', 1).format('-YYYYMMDD');
      } else {
        var date = moment().day(index).format('-YYYYMMDD');
      }
      return date;
    }
  }

  // Format the scaped tide times by removing extra spaces, adding a line break after m, and removing the leading spaces
  function formatTideTimes (times) {
    return times.replace(/\s+/g, " ").replace(/m /g, "m\n").substring(1)
  }

  //Set location and date via values passed from args
  if (argv._.length == 1) {
    // is the argument a date or a location?
    var d = this.formatDate(argv._[0])
    if (typeof(d) != "undefined"){
      this.date = d
    } else {
      location = this.formatLocation(argv._[0])
    }
    } else if (argv._.length == 2) {
    // two arguments provided
    // first argument should be date related
    // second argument shoudl be a location
    this.date = this.formatDate(argv._[0])

    this.location = this.formatLocation(argv._[1])
  }

  // the uri of page where we will scrape the data
  this.uri = 'http://www.tidetimes.co.uk/'+this.location+'-tide-times'+this.date;

  this.getTimes = function(){

    // Scrape the data
    scraperjs.StaticScraper.create(this.uri)
      .scrape(function($) {
         return $(".times").text();
     }, function(tides) {
          console.log(formatTideTimes(tides));
      })

  }

}

module.exports = TideCrawler;
<commit_msg>rename tidecrawler to tidetimes<commit_after>#!/usr/bin/env node
var moment = require('moment');
var sync = require('synchronize');
var scraperjs = require('scraperjs');

function TideTimes(argv) {

  this.location = 'falmouth';  //Default location
  this.date = '';

  // if the location argument starts with @ remove it
  this.formatLocation = function(location) {
    if (location[0] === '@') {
      return location.substring(1)
    } else {
      return location
    }
  }

  // takes tomorrow or a day of the week and converts it to a date url fragment
  this.formatDate = function(day) {
    // Vaid Day arguments
    var dayArgs = ['tomorrow', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

    var index = dayArgs.indexOf(day);
    if (index > -1 ){
      if (index === 0) {
        var date = moment().add('days', 1).format('-YYYYMMDD');
      } else {
        var date = moment().day(index).format('-YYYYMMDD');
      }
      return date;
    }
  }

  // Format the scaped tide times by removing extra spaces, adding a line break after m, and removing the leading spaces
  function formatTideTimes (times) {
    return times.replace(/\s+/g, " ").replace(/m /g, "m\n").substring(1)
  }

  //Set location and date via values passed from args
  if (argv._.length == 1) {
    // is the argument a date or a location?
    var d = this.formatDate(argv._[0])
    if (typeof(d) != "undefined"){
      this.date = d
    } else {
      location = this.formatLocation(argv._[0])
    }
    } else if (argv._.length == 2) {
    // two arguments provided
    // first argument should be date related
    // second argument shoudl be a location
    this.date = this.formatDate(argv._[0])

    this.location = this.formatLocation(argv._[1])
  }

  // the uri of page where we will scrape the data
  this.uri = 'http://www.tidetimes.co.uk/'+this.location+'-tide-times'+this.date;

  this.getTimes = function(){

    // Scrape the data
    scraperjs.StaticScraper.create(this.uri)
      .scrape(function($) {
         return $(".times").text();
     }, function(tides) {
          console.log(formatTideTimes(tides));
      })

  }

}

module.exports = TideTimes;
<|endoftext|>


<commit_before>'use strict';
/**
 * This object contains all the handlers to use for this provider
 */
var crypto = require('crypto');
var CancelError = require('anyfetch-provider').CancelError;
var async = require('async');
var request = require('supertest');

var config = require('../config/configuration.js');
var state = crypto.randomBytes(10).toString('hex');

var redirectToService = function(callbackUrl, cb) {
  var redirectUrl = 'https://app.box.com/api/oauth2/authorize?response_type=code&client_id=' + config.box.api + '&redirect_uri=' + config.providerUrl + '/init/callback' + '&state=' + state;
  cb(null, redirectUrl, {state: state});
};

var retrieveTokens = function(reqParams, storedParams, cb) {
  // Store new data
  if(reqParams.error === 'access_denied') {
    return cb(new CancelError());
  }

  if(reqParams.state !== storedParams.state) {
    console.log('Sent state and recieved state does not match: \n' + reqParams.state + '\n' + storedParams.state);
    return cb(new CancelError());
  }

  var authData;

  async.waterfall([
    function getToken(cb) {
      var apiUrl = "https://api.box.com/oauth2/";
      var url = '/token';
      request(apiUrl)
        .post(url)
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .send({grant_type: 'authorization_code',
              code: reqParams.code,
              client_secret: config.box.secret,
              client_id: config.box.api
            })
        .expect(200)
        .end(cb);
    },
    function getEmailAddress(data, cb) {
      authData = data.body;
      var apiUrl = "https://api.box.com/2.0/";
      var url = "/users/me";

      request(apiUrl)
        .get(url)
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .set('Authorization', 'Bearer ' + data.body.access_token)
        .expect(200)
        .end(cb);
    },
    function callFinalCb(data, cb) {
      cb(null, data.body.login, authData);
    }
  ], cb);
};

module.exports = {
  connectFunctions: {
    redirectToService: redirectToService,
    retrieveTokens: retrieveTokens
  },

  config: config
};
<commit_msg>Removing state checking<commit_after>'use strict';
/**
 * This object contains all the handlers to use for this provider
 */
var crypto = require('crypto');
var CancelError = require('anyfetch-provider').CancelError;
var async = require('async');
var request = require('supertest');

var config = require('../config/configuration.js');
var state = crypto.randomBytes(10).toString('hex');

var redirectToService = function(callbackUrl, cb) {
  var redirectUrl = 'https://app.box.com/api/oauth2/authorize?response_type=code&client_id=' + config.box.api + '&redirect_uri=' + config.providerUrl + '/init/callback' + '&state=' + state;
  cb(null, redirectUrl, {state: state});
};

var retrieveTokens = function(reqParams, storedParams, cb) {
  // Store new data
  if(reqParams.error === 'access_denied') {
    return cb(new CancelError());
  }

  if(!reqParams.code) {
    return cb('Error: No code received');
  }

  var authData;
  async.waterfall([
    function getToken(cb) {
      var apiUrl = "https://api.box.com/oauth2/";
      var url = '/token';
      request(apiUrl)
        .post(url)
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .send({grant_type: 'authorization_code',
              code: reqParams.code,
              client_secret: config.box.secret,
              client_id: config.box.api
            })
        .expect(200)
        .end(cb);
    },
    function getEmailAddress(data, cb) {
      authData = data.body;
      var apiUrl = "https://api.box.com/2.0/";
      var url = "/users/me";

      request(apiUrl)
        .get(url)
        .set('Content-Type', 'application/x-www-form-urlencoded')
        .set('Authorization', 'Bearer ' + data.body.access_token)
        .expect(200)
        .end(cb);
    },
    function callFinalCb(data, cb) {
      cb(null, data.body.login, authData);
    }
  ], cb);
};

module.exports = {
  connectFunctions: {
    redirectToService: redirectToService,
    retrieveTokens: retrieveTokens
  },

  config: config
};
<|endoftext|>


<commit_before>const Stadium = require('./world.js');

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var stadium = new Stadium();
gameStart();

function drawStadium() {
  ctx.beginPath();
  ctx.rect(stadium.x, stadium.y, stadium.width, stadium.height);
  ctx.fillStyle = "green";
  ctx.fill();
  ctx.closePath();
}

function drawBall() {
    ctx.beginPath();
    ctx.rect(stadium.ball.x, stadium.ball.y, stadium.ball.width, stadium.ball.height);
    ctx.fillStyle = "#663300";
    ctx.fill();
    ctx.closePath();
  }

function drawPaddle() {
      ctx.beginPath();
      ctx.rect(stadium.paddle.x, stadium.paddle.y, stadium.paddle.width, stadium.paddle.height);
      ctx.fillStyle = "#0095DD";
      ctx.fill();
      ctx.closePath();
  }

function tick(stadium) {
  stadium.tick();
}

function makeGameObjects() {
  drawStadium();
  drawBall();
  drawPaddle();
}

function gameStart(){
  requestAnimationFrame(function gameLoop() {
      makeGameObjects();
      tick(stadium);
      requestAnimationFrame(gameLoop);
    });
  }
<commit_msg>paddle moving left/right again<commit_after>const Stadium = require('./world.js');

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var stadium = new Stadium();
gameStart();

function drawStadium() {
  ctx.beginPath();
  ctx.rect(stadium.x, stadium.y, stadium.width, stadium.height);
  ctx.fillStyle = "green";
  ctx.fill();
  ctx.closePath();
}

function drawBall() {
    ctx.beginPath();
    ctx.rect(stadium.ball.x, stadium.ball.y, stadium.ball.width, stadium.ball.height);
    ctx.fillStyle = "#663300";
    ctx.fill();
    ctx.closePath();
  }

function drawPaddle() {
      ctx.beginPath();
      ctx.rect(stadium.paddle.x, stadium.paddle.y, stadium.paddle.width, stadium.paddle.height);
      ctx.fillStyle = "#0095DD";
      ctx.fill();
      ctx.closePath();
  }

function tick(stadium) {
  stadium.tick();
}

function makeGameObjects() {
  drawStadium();
  drawBall();
  drawPaddle();
}

function gameStart(){
  requestAnimationFrame(function gameLoop() {
      makeGameObjects();
      tick(stadium);
      requestAnimationFrame(gameLoop);
    });
  }

  document.addEventListener('keydown', function (event) {
  if (event.keyCode === 39) { stadium.rightArrow(); }
  if (event.keyCode === 37) { stadium.leftArrow(); }
});
<|endoftext|>


<commit_before>
/**
 * Module dependencies.
 */

var http = require('http')
  , send = require('send')
  , parse = require('url').parse
  , engine = require('engine.io')
  , Client = require('./client')
  , Namespace = require('./namespace')
  , debug = require('debug')('socket.io:server');

/**
 * Module exports.
 */

module.exports = Server;

/**
 * Read client
 */

var client = {
  source: require('socket.io-client').source,
  version: require('socket.io-client/package').version
};

/**
 * Server constructor.
 *
 * @param {http.Server|Number|Object} http server, port or options
 * @param {Object} options
 * @api public
 */

function Server(srv, opts){
  if (!(this instanceof Server)) return new Server(srv, opts);
  if ('object' == typeof srv && !srv.listen) {
    opts = srv;
    srv = null;
  }
  opts = opts || {};
  this.nsps = {};
  this.sockets = this.of('/');
  this.path(opts.path || '/socket.io');
  this.static(false !== opts.static);
  if (srv) this.attach(srv, opts);
}

/**
 * Serve client code.
 *
 * @param {Boolean} whether to serve client code
 * @api public
 */

Server.prototype.static = function(v){
  if (!arguments.length) return this._static;
  this._static = v;
  return this;
};

/**
 * Sets the client serving path.
 *
 * @param {String} pathname
 * @api public
 */

Server.prototype.path = function(v){
  if (!arguments.length) return this._path;
  this._path = v.replace(/\/$/, '');
  return this;
};

/**
 * Attaches socket.io to a server or port.
 *
 * @param {http.Server|Number} server or port
 * @param {Object} options
 * @api public
 */

Server.prototype.attach = function(srv, opts){
  if ('number' == typeof srv) {
    debug('creating engine.io on port %d', srv);
    srv = engine.listen(srv, opts);
  } else {
    debug('creating engine.io instance', opts);
    if (this._static) this.serve(srv);
    srv = engine.attach(srv, opts);
  }
  this.bind(srv);
};

/**
 * Attaches the static file serving.
 *
 * @param {Function|http.Server} http server
 * @api private
 */

Server.prototype.serve = function(srv){
  var url = this._path + '/';
  var self = this;

  function handle(req, res){
    send(req, parse(req.url).pathname.substr(url.length - 1))
    .root(__dirname + '/../client')
    .index(false)
    .pipe(res);
  }

  if (srv.use) {
    debug('attaching client serving middleware');
    srv.use(function(req, res, next){
      if (0 == req.url.indexOf(url)) {
        handle(req, res);
      } else {
        next();
      }
    });
  } else {
    debug('attaching client serving req handler');
    var evs = srv.listeners('request').slice(0);
    srv.removeAllListeners('request');
    srv.on('request', function(req, res) {
      if (0 == req.url.indexOf(url)) {
        handle(req, res);
      } else {
        for (var i = 0; i < evs.length; i++) {
          evs[i].call(srv, req.res);
        }
      }
    });
  }
};

/**
 * Binds socket.io to an engine.io instance.
 *
 * @param {engine.Server} engine.io (or compatible) server
 * @api public
 */

Server.prototype.bind = function(engine){
  this.engine = engine;
  this.engine.on('connection', this.onconnection.bind(this));
};

/**
 * Called with each incoming transport connection.
 *
 * @api public
 */

Server.prototype.onconnection = function(conn){
  debug('incoming connection with id %s', conn.id);
  var client = new Client(this, conn);
  client.connect('/');
  this.emit('client', client);
};

/**
 * Looks up a namespace.
 *
 * @param {String} nsp name
 * @api public
 */

Server.prototype.of = function(name){
  if (!this.nsps[name]) {
    debug('initializing namespace %s', name);
    var nsp = new Namespace(this, name);
    this.nsps[name] = nsp;
  }
  return this.nsps[name];
};

/**
 * Expose main namespace (/).
 */

['use', 'to', 'in', 'emit', 'send', 'write'].forEach(function(name){
  Server.prototype[name] = function(){
    var nsp = this.sockets[name];
    return nsp.apply(this.sockets, arguments);
  };
});

Namespace.flags.forEach(function(flag){
  Server.prototype.__defineGetter__(flag, function(name){
    this.flags.push(name);
    return this;
  });
});

/**
 * BC with `io.listen`
 */

Server.listen = Server;
<commit_msg>index: api cleanup<commit_after>
/**
 * Module dependencies.
 */

var http = require('http')
  , send = require('send')
  , parse = require('url').parse
  , engine = require('engine.io')
  , Client = require('./client')
  , Namespace = require('./namespace')
  , debug = require('debug')('socket.io:server');

/**
 * Module exports.
 */

module.exports = Server;

/**
 * Read client
 */

var client = {
  source: require('socket.io-client').source,
  version: require('socket.io-client/package').version
};

/**
 * Server constructor.
 *
 * @param {http.Server|Number|Object} http server, port or options
 * @param {Object} options
 * @api public
 */

function Server(srv, opts){
  if (!(this instanceof Server)) return new Server(srv, opts);
  if ('object' == typeof srv && !srv.listen) {
    opts = srv;
    srv = null;
  }
  opts = opts || {};
  this.nsps = {};
  this.sockets = this.of('/');
  this.path(opts.path || '/socket.io');
  this.static(false !== opts.static);
  if (srv) this.attach(srv, opts);
}

/**
 * Serve client code.
 *
 * @param {Boolean} whether to serve client code
 * @api public
 */

Server.prototype.static = function(v){
  if (!arguments.length) return this._static;
  this._static = v;
  return this;
};

/**
 * Sets the client serving path.
 *
 * @param {String} pathname
 * @api public
 */

Server.prototype.path = function(v){
  if (!arguments.length) return this._path;
  this._path = v.replace(/\/$/, '');
  return this;
};

/**
 * Attaches socket.io to a server or port.
 *
 * @param {http.Server|Number} server or port
 * @param {Object} options passed to engine.io
 * @return {Server} self
 * @api public
 */

Server.prototype.attach = function(srv, opts){
  if ('number' == typeof srv) {
    debug('creating engine.io on port %d', srv);
    srv = engine.listen(srv, opts);
  } else {
    debug('creating engine.io instance', opts);
    if (this._static) this.serve(srv);
    srv = engine.attach(srv, opts);
  }
  this.bind(srv);
  return this;
};

/**
 * Attaches the static file serving.
 *
 * @param {Function|http.Server} http server
 * @api private
 */

Server.prototype.serve = function(srv){
  var url = this._path + '/';
  var self = this;

  function handle(req, res){
    send(req, parse(req.url).pathname.substr(url.length - 1))
    .root(__dirname + '/../client')
    .index(false)
    .pipe(res);
  }

  if (srv.use) {
    debug('attaching client serving middleware');
    srv.use(function(req, res, next){
      if (0 == req.url.indexOf(url)) {
        handle(req, res);
      } else {
        next();
      }
    });
  } else {
    debug('attaching client serving req handler');
    var evs = srv.listeners('request').slice(0);
    srv.removeAllListeners('request');
    srv.on('request', function(req, res) {
      if (0 == req.url.indexOf(url)) {
        handle(req, res);
      } else {
        for (var i = 0; i < evs.length; i++) {
          evs[i].call(srv, req.res);
        }
      }
    });
  }
};

/**
 * Binds socket.io to an engine.io instance.
 *
 * @param {engine.Server} engine.io (or compatible) server
 * @return {Server} self
 * @api public
 */

Server.prototype.bind = function(engine){
  this.engine = engine;
  this.engine.on('connection', this.onconnection.bind(this));
  return this;
};

/**
 * Called with each incoming transport connection.
 *
 * @param {engine.Socket} socket
 * @return {Server} self
 * @api public
 */

Server.prototype.onconnection = function(conn){
  debug('incoming connection with id %s', conn.id);
  var client = new Client(this, conn);
  client.connect('/');
  this.emit('client', client);
  return this;
};

/**
 * Looks up a namespace.
 *
 * @param {String} nsp name
 * @api public
 */

Server.prototype.of = function(name){
  if (!this.nsps[name]) {
    debug('initializing namespace %s', name);
    var nsp = new Namespace(this, name);
    this.nsps[name] = nsp;
  }
  return this.nsps[name];
};

/**
 * Expose main namespace (/).
 */

['use', 'to', 'in', 'emit', 'send', 'write'].forEach(function(name){
  Server.prototype[name] = function(){
    var nsp = this.sockets[name];
    return nsp.apply(this.sockets, arguments);
  };
});

Namespace.flags.forEach(function(flag){
  Server.prototype.__defineGetter__(flag, function(name){
    this.flags.push(name);
    return this;
  });
});

/**
 * BC with `io.listen`
 */

Server.listen = Server;
<|endoftext|>


<commit_before>// Dependencies
var Promise = require("bluebird")
  , Fs = Promise.promisifyAll(require("fs"))
  , Ul = require("ul")
  , JxUtils = require("jxutils")
  , NpmKeyword = require("npm-keyword")
  , PackageJson = require("package-json")
  , FsPlus = require("fs-plus")
  , Async = require("async")
  , ReadJson = FsPlus.readJSON = require("read-json")
  , WriteJson = FsPlus.writeJSON = require("write-json")
  , Gry = require("gry")
  , GitUrlParse = require("giturlparse")
  , Tmp = require("tmp")
  , OArgv = require("oargv")
  , ExecLimiter = require("exec-limiter")
  ;

// Create the exec limiter
var execLimit = new ExecLimiter(10);

// Constants
const DEFAULT_PACKAGE = {
        components: {
            scripts: []
          , styles: []
          , markup: []
        }
      , flow: []
      , clientFlow: []
      , keywords: []
    }
  , DOT_SERVICE = {
        client: {
            methods: {}
          , emits: {}
        }
    }
  , JXENGINE_KEY = "jxengine"
  , ENGINE_APPS = process.env.ENGINE_APPS
  ;

function EngineTools() {}
EngineTools.Module = require("./module");

/**
 * generateModuleService
 * Generates content for .service.json file.
 *
 * @name generateModuleService
 * @function
 * @param {String} path The path to the package.json file.
 * @param {Function} callback A function called with error and .serivce.json file content.
 * @return {Promise} A promise used to run the function steps.
 */
EngineTools.prototype.generateModuleService = function (path, callback) {

    if (typeof path === "function") {
        callback = path;
        path = process.cwd() + "/package.json";
    }

    Fs.readFileAsync(path)
        .then(JSON.parse)
        .then(function (package) {
            package = Ul.merge(package, DEFAULT_PACKAGE);

            var dotService = Ul.clone(DOT_SERVICE)
              , mainFile = package.components.scripts[0]
              , mod = require(path.split("/").slice(0, -1).join("/") + "/" + mainFile)
              ;

            try {
                mod.init.call(mod);
            } catch (e) {}

            var methods = Object.keys(JxUtils.flattenObject(mod));
            methods.forEach(function (c) {
                if (c === "init" || !JxUtils.findFunction(mod, c)) { return; }
                dotService.client.methods[c] = {};
            });

            return dotService;
        })
        .then(function (dotService) {
            callback(null, dotService);
        })
        .catch(function (err) {
            callback(err);
        })
        ;
};

EngineTools.prototype.generateSymlinks = function (path, callback) {
    // TODO
    throw new Error("Not yet implemented.");
};

/**
 * packPath
 * Returns the full path to the package.json located into `path`.
 *
 * @name packPath
 * @function
 * @param {String} path The path to the directory containing the package.json file.
 * @return {String} The path to the package.json.
 */
EngineTools.prototype.packPath = function (path) {
    return FsPlus.normalize(path + "/package.json");
};

/**
 * projectPackPath
 * Returns the full path to the package.json located in the project.
 *
 * @name projectPackPath
 * @function
 * @param {String} project The project name.
 * @return {String} The path to the project package.json file.
 */
EngineTools.prototype.projectPackPath = function (project) {
    return this.packPath(this.getProjectPath(project));
};

/**
 * getProjectPath
 * Returns the full path to the project directory.
 *
 * @name getProjectPath
 * @function
 * @param {String} project The project name.
 * @return {String} The full path to the project directory.
 */
EngineTools.prototype.getProjectPath = function (project) {
    return FsPlus.normalize(ENGINE_APPS + "/" + project);
};

/**
 * projectExistsSync
 * Checks if the project exists or not.
 *
 * @name projectExistsSync
 * @function
 * @param {String} project The project name.
 * @return {Boolean} `true` if the project exists, `false` otherwise.
 */
EngineTools.prototype.projectExistsSync = function (project) {
    return FsPlus.existsSync(this.getProjectPath(project));
};

/**
 * projectPack
 * Fetches the project package.json object.
 *
 * @name projectPack
 * @function
 * @param {String} project The project name.
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.projectPack = function (project, callback) {
    FsPlus.readJSON(this.projectPackPath(project), callback);
    return this;
};

/**
 * getNpmModules
 * Fetches the Engine modules from NPM.
 *
 * @name getNpmModules
 * @function
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.getNpmModules = function (callback) {
    NpmKeyword(JXENGINE_KEY, function (err, packages) {
        if (err) { return callback(err); }
        packages = packages.map(function (c) {
            return new EngineTools.Module(c);
        });
        callback(null, packages);
    });
    return this;
};

/**
 * isEngineModule
 * Checks if the input is an Engine module.
 *
 * Usage
 *
 * ```js
 * // Path to the module directory
 * EngineTools.isEngineModule("path/to/node_modules/foo", function (isEngineModule, pack) {
 *    // do something
 * });
 *
 * // Path to the package.json
 * EngineTools.isEngineModule("path/to/node_modules/foo/package.json", function (isEngineModule, pack) {
 *    // do something
 * });
 *
 * // Some object
 * EngineTools.isEngineModule({
 *    keywords: ["jxengine"]
 * }, function (isEngineModule, pack) {
 *    // do something
 * });
 * ```
 *
 * @name isEngineModule
 * @function
 * @param {String|Object} input A string representing the path to a directory containing the package.json file or even to the package.json file or the package object itself.
 * @param {Function} callback The callback function called with two parameters: a boolean value (`true` if the provided input is an Engine module) and the package object.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.isEngineModule = function (input, callback) {

    var self = this;

    if (typeof input === "string") {
        if (!/package\.json$/.test(input)) {
            input += "/package.json";
        }
        FsPlus.readJSON(input, function (err, package) {
            self.isEngineModule(package, callback);
        });
        return self;
    }

    input = Ul.merge(input, DEFAULT_PACKAGE);
    callback(input.keywords.indexOf(JXENGINE_KEY) !== -1, input);

    return self;
};

/**
 * appModules
 * Fetches the installed Engine modules.
 *
 * @name appModules
 * @function
 * @param {String} path The path to the project.
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.appModules = function (path, callback) {
    var self = this;
    path = FsPlus.normalize(path);
    FsPlus.list(path + "/node_modules", [""], function (err, node_modules) {
        if (err) { return callback(err); }
        var engineModules = [];
        Async.parallel(node_modules.map(function (c) {
            return function (opt_callback) {
                self.isEngineModule(c, function (is, pack) {
                    if (!is) { return opt_callback(null, null); }
                    engineModules.push(new EngineTools.Module(pack));
                    opt_callback(null);
                });
            }
        }), function (err) {
            callback(err, engineModules);
        });
    });
    return self;
};

/**
 * availableModules
 * Fetches the available modules for a project.
 *
 * @name availableModules
 * @function
 * @param {String} app The project name.
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.availableModules = function (app, callback) {

    var self = this
      , appPath = self.getProjectPath(app)
      ;

    if (!self.projectExistsSync(app)) {
        return callback(new Error("This project doesn't exist."));
    }

    var res = [];
    Async.parallel([
        self.getNpmModules
      , self.appModules.bind(self, appPath)
    ], function (err, data) {
        if (err) { return callback(err); }
        var uniq = {};
        data[0].concat(data[1]).forEach(function (c) {
            uniq[c.name] = c;
        });
        Object.keys(uniq).forEach(function (c) {
            res.push(uniq[c]);
        });
        callback(err, res);
    });

    return self;
};

/**
 * getModuleInfo
 * Gets information about a provided module.
 *
 * @name getModuleInfo
 * @function
 * @param {String} module The module to get info about (can be a npm module name or a git url).
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.getModuleInfo = function (module, callback) {
    var self = this
      , parsed = GitUrlParse(module)
      , info = {
            fromNpm: false
          , git_url: null
          , version: null
          , name: ""
        }
      ;

    // Suppose it's a npm package
    if (parsed.protocol === "file") {
        PackageJson(module, "latest", function (err, res) {
            if (err) { return callback(err); }
            var inf = res[Object.keys(res)[0]];
            info.fromNpm = true;
            info.version = inf.version;
            info.name = inf.name;
            callback(null, info);
        });
    }

    // Git url
    else {
        var repo = new Gry(Tmp.dirSync().name);
        repo.exec(OArgv({
            _: [module, "."]
        }, "clone"), function (err) {
            if (err) { return callback(err); }
            FsPlus.readJSON(self.packPath(repo.cwd), function (err, pack) {
                if (err) { return callback(err); }
                info.version = pack.version;
                info.name = pack.name;
                info.git_url = parsed.source === "github.com"
                            ? parsed.owner + "/" + parsed.name + ".git"
                            : parsed.toString("ssh")
                            ;
                callback(null, info);
            });
        });
    }

    return self;
};

/**
 * installModule
 * Installs a provided module in a provided project.
 *
 * @name installModule
 * @function
 * @param {} project The project name.
 * @param {String} module The module to install (can be a npm module name or a git url).
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.installModule = function (project, module, callback) {
    var self = this;
    self.projectPack(project, function (err, pack) {
        if (err) { return callback(err); }
        pack = Ul.merge(pack, {
            dependencies: {}
        });

        self.getModuleInfo(module, function (err, modInfo) {
            if (err) { return callback(err); }

            function add(clb) {
                if (pack.dependencies[modInfo.name]) {
                    return clb();
                }
                pack.dependencies[modInfo.name] = modInfo.fromNpm ? modInfo.version : modInfo.git_url;
                FsPlus.writeJSON(self.projectPackPath(project), pack, clb);
            }

            function install(clb) {
                var mod = modInfo.fromNpm ? [modInfo.name, modInfo.version].join("@") : pack.dependencies[modInfo.name];
                if (FsPlus.isDirectorySync(self.getProjectPath(project) + "/node_modules/" + modInfo.name)) {
                    return clb(null, pack);
                }
                execLimit.add(OArgv({
                    _: ["i", mod]
                }, "npm"), { cwd: self.getProjectPath(project) }, function (err) {
                    if (err) { return callback(err); }
                    callback(null, pack);
                });
            }

            add(function (err) {
                if (err) { return callback(err); }
                install(callback);
            });
        });
    });
    return self;
};

module.exports = new EngineTools();
<commit_msg>Symlink name prototype<commit_after>// Dependencies
var Promise = require("bluebird")
  , Fs = Promise.promisifyAll(require("fs"))
  , Ul = require("ul")
  , JxUtils = require("jxutils")
  , NpmKeyword = require("npm-keyword")
  , PackageJson = require("package-json")
  , FsPlus = require("fs-plus")
  , Async = require("async")
  , ReadJson = FsPlus.readJSON = require("read-json")
  , WriteJson = FsPlus.writeJSON = require("write-json")
  , Gry = require("gry")
  , GitUrlParse = require("giturlparse")
  , Tmp = require("tmp")
  , OArgv = require("oargv")
  , ExecLimiter = require("exec-limiter")
  ;

// Create the exec limiter
var execLimit = new ExecLimiter(10);

// Constants
const DEFAULT_PACKAGE = {
        components: {
            scripts: []
          , styles: []
          , markup: []
        }
      , flow: []
      , clientFlow: []
      , keywords: []
    }
  , DOT_SERVICE = {
        client: {
            methods: {}
          , emits: {}
        }
    }
  , JXENGINE_KEY = "jxengine"
  , ENGINE_APPS = process.env.ENGINE_APPS
  ;

function EngineTools() {}
EngineTools.Module = require("./module");

/**
 * generateModuleService
 * Generates content for .service.json file.
 *
 * @name generateModuleService
 * @function
 * @param {String} path The path to the package.json file.
 * @param {Function} callback A function called with error and .serivce.json file content.
 * @return {Promise} A promise used to run the function steps.
 */
EngineTools.prototype.generateModuleService = function (path, callback) {

    if (typeof path === "function") {
        callback = path;
        path = process.cwd() + "/package.json";
    }

    Fs.readFileAsync(path)
        .then(JSON.parse)
        .then(function (package) {
            package = Ul.merge(package, DEFAULT_PACKAGE);

            var dotService = Ul.clone(DOT_SERVICE)
              , mainFile = package.components.scripts[0]
              , mod = require(path.split("/").slice(0, -1).join("/") + "/" + mainFile)
              ;

            try {
                mod.init.call(mod);
            } catch (e) {}

            var methods = Object.keys(JxUtils.flattenObject(mod));
            methods.forEach(function (c) {
                if (c === "init" || !JxUtils.findFunction(mod, c)) { return; }
                dotService.client.methods[c] = {};
            });

            return dotService;
        })
        .then(function (dotService) {
            callback(null, dotService);
        })
        .catch(function (err) {
            callback(err);
        })
        ;
};

EngineTools.prototype.generateSymlinks = function (path, callback) {
    userName_projectName_instanceName_dev_jillix_com.json
};

/**
 * packPath
 * Returns the full path to the package.json located into `path`.
 *
 * @name packPath
 * @function
 * @param {String} path The path to the directory containing the package.json file.
 * @return {String} The path to the package.json.
 */
EngineTools.prototype.packPath = function (path) {
    return FsPlus.normalize(path + "/package.json");
};

/**
 * projectPackPath
 * Returns the full path to the package.json located in the project.
 *
 * @name projectPackPath
 * @function
 * @param {String} project The project name.
 * @return {String} The path to the project package.json file.
 */
EngineTools.prototype.projectPackPath = function (project) {
    return this.packPath(this.getProjectPath(project));
};

/**
 * getProjectPath
 * Returns the full path to the project directory.
 *
 * @name getProjectPath
 * @function
 * @param {String} project The project name.
 * @return {String} The full path to the project directory.
 */
EngineTools.prototype.getProjectPath = function (project) {
    return FsPlus.normalize(ENGINE_APPS + "/" + project);
};

/**
 * projectExistsSync
 * Checks if the project exists or not.
 *
 * @name projectExistsSync
 * @function
 * @param {String} project The project name.
 * @return {Boolean} `true` if the project exists, `false` otherwise.
 */
EngineTools.prototype.projectExistsSync = function (project) {
    return FsPlus.existsSync(this.getProjectPath(project));
};

/**
 * projectPack
 * Fetches the project package.json object.
 *
 * @name projectPack
 * @function
 * @param {String} project The project name.
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.projectPack = function (project, callback) {
    FsPlus.readJSON(this.projectPackPath(project), callback);
    return this;
};

/**
 * getNpmModules
 * Fetches the Engine modules from NPM.
 *
 * @name getNpmModules
 * @function
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.getNpmModules = function (callback) {
    NpmKeyword(JXENGINE_KEY, function (err, packages) {
        if (err) { return callback(err); }
        packages = packages.map(function (c) {
            return new EngineTools.Module(c);
        });
        callback(null, packages);
    });
    return this;
};

/**
 * isEngineModule
 * Checks if the input is an Engine module.
 *
 * Usage
 *
 * ```js
 * // Path to the module directory
 * EngineTools.isEngineModule("path/to/node_modules/foo", function (isEngineModule, pack) {
 *    // do something
 * });
 *
 * // Path to the package.json
 * EngineTools.isEngineModule("path/to/node_modules/foo/package.json", function (isEngineModule, pack) {
 *    // do something
 * });
 *
 * // Some object
 * EngineTools.isEngineModule({
 *    keywords: ["jxengine"]
 * }, function (isEngineModule, pack) {
 *    // do something
 * });
 * ```
 *
 * @name isEngineModule
 * @function
 * @param {String|Object} input A string representing the path to a directory containing the package.json file or even to the package.json file or the package object itself.
 * @param {Function} callback The callback function called with two parameters: a boolean value (`true` if the provided input is an Engine module) and the package object.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.isEngineModule = function (input, callback) {

    var self = this;

    if (typeof input === "string") {
        if (!/package\.json$/.test(input)) {
            input += "/package.json";
        }
        FsPlus.readJSON(input, function (err, package) {
            self.isEngineModule(package, callback);
        });
        return self;
    }

    input = Ul.merge(input, DEFAULT_PACKAGE);
    callback(input.keywords.indexOf(JXENGINE_KEY) !== -1, input);

    return self;
};

/**
 * appModules
 * Fetches the installed Engine modules.
 *
 * @name appModules
 * @function
 * @param {String} path The path to the project.
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.appModules = function (path, callback) {
    var self = this;
    path = FsPlus.normalize(path);
    FsPlus.list(path + "/node_modules", [""], function (err, node_modules) {
        if (err) { return callback(err); }
        var engineModules = [];
        Async.parallel(node_modules.map(function (c) {
            return function (opt_callback) {
                self.isEngineModule(c, function (is, pack) {
                    if (!is) { return opt_callback(null, null); }
                    engineModules.push(new EngineTools.Module(pack));
                    opt_callback(null);
                });
            }
        }), function (err) {
            callback(err, engineModules);
        });
    });
    return self;
};

/**
 * availableModules
 * Fetches the available modules for a project.
 *
 * @name availableModules
 * @function
 * @param {String} app The project name.
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.availableModules = function (app, callback) {

    var self = this
      , appPath = self.getProjectPath(app)
      ;

    if (!self.projectExistsSync(app)) {
        return callback(new Error("This project doesn't exist."));
    }

    var res = [];
    Async.parallel([
        self.getNpmModules
      , self.appModules.bind(self, appPath)
    ], function (err, data) {
        if (err) { return callback(err); }
        var uniq = {};
        data[0].concat(data[1]).forEach(function (c) {
            uniq[c.name] = c;
        });
        Object.keys(uniq).forEach(function (c) {
            res.push(uniq[c]);
        });
        callback(err, res);
    });

    return self;
};

/**
 * getModuleInfo
 * Gets information about a provided module.
 *
 * @name getModuleInfo
 * @function
 * @param {String} module The module to get info about (can be a npm module name or a git url).
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.getModuleInfo = function (module, callback) {
    var self = this
      , parsed = GitUrlParse(module)
      , info = {
            fromNpm: false
          , git_url: null
          , version: null
          , name: ""
        }
      ;

    // Suppose it's a npm package
    if (parsed.protocol === "file") {
        PackageJson(module, "latest", function (err, res) {
            if (err) { return callback(err); }
            var inf = res[Object.keys(res)[0]];
            info.fromNpm = true;
            info.version = inf.version;
            info.name = inf.name;
            callback(null, info);
        });
    }

    // Git url
    else {
        var repo = new Gry(Tmp.dirSync().name);
        repo.exec(OArgv({
            _: [module, "."]
        }, "clone"), function (err) {
            if (err) { return callback(err); }
            FsPlus.readJSON(self.packPath(repo.cwd), function (err, pack) {
                if (err) { return callback(err); }
                info.version = pack.version;
                info.name = pack.name;
                info.git_url = parsed.source === "github.com"
                            ? parsed.owner + "/" + parsed.name + ".git"
                            : parsed.toString("ssh")
                            ;
                callback(null, info);
            });
        });
    }

    return self;
};

/**
 * installModule
 * Installs a provided module in a provided project.
 *
 * @name installModule
 * @function
 * @param {} project The project name.
 * @param {String} module The module to install (can be a npm module name or a git url).
 * @param {Function} callback The callback function.
 * @return {EngineTools} The `EngineTools` instance.
 */
EngineTools.prototype.installModule = function (project, module, callback) {
    var self = this;
    self.projectPack(project, function (err, pack) {
        if (err) { return callback(err); }
        pack = Ul.merge(pack, {
            dependencies: {}
        });

        self.getModuleInfo(module, function (err, modInfo) {
            if (err) { return callback(err); }

            function add(clb) {
                if (pack.dependencies[modInfo.name]) {
                    return clb();
                }
                pack.dependencies[modInfo.name] = modInfo.fromNpm ? modInfo.version : modInfo.git_url;
                FsPlus.writeJSON(self.projectPackPath(project), pack, clb);
            }

            function install(clb) {
                var mod = modInfo.fromNpm ? [modInfo.name, modInfo.version].join("@") : pack.dependencies[modInfo.name];
                if (FsPlus.isDirectorySync(self.getProjectPath(project) + "/node_modules/" + modInfo.name)) {
                    return clb(null, pack);
                }
                execLimit.add(OArgv({
                    _: ["i", mod]
                }, "npm"), { cwd: self.getProjectPath(project) }, function (err) {
                    if (err) { return callback(err); }
                    callback(null, pack);
                });
            }

            add(function (err) {
                if (err) { return callback(err); }
                install(callback);
            });
        });
    });
    return self;
};

module.exports = new EngineTools();
<|endoftext|>


<commit_before>module.exports = function (client) {
	return function (level, err) {
		if (level == 'warn')
			level = 'warning';

		client.captureError(toJsonSerializable(err), {
			level: level,
			extra: {
				reasons: err.reasons,
				meta: err.meta
			}
		});
	};
};

function toJsonSerializable(err) {
	var output = {};

	if (err.reasons) {
		output.reasons = err.reasons.map(function (e) { return toJsonSerializable(e); });
	}

	return extractGetters(err, output, 'reasons');
}

function extractGetters(err, cur, excluding) {
	var output = cur || {};
	var props = Object.getOwnPropertyNames(err);

	for (var a in props) {
		if (excluding && excluding.indexOf(props[a]) >= 0)
			continue;

		output[props[a]] = err[props[a]];
	}

	return output;
}
<commit_msg>syntax<commit_after>module.exports = function (client) {
	return function (level, err) {
		if (level == 'warn')
			level = 'warning';

		client.captureError(toJsonSerializable(err), {
			level: level,
			extra: {
				reasons: err.reasons,
				meta: err.meta
			}
		});
	};
};

function toJsonSerializable(err) {
	var output = {};

	if (err.reasons)
		output.reasons = err.reasons.map(function (e) { return toJsonSerializable(e); });

	return extractGetters(err, output, 'reasons');
}

function extractGetters(err, cur, excluding) {
	var output = cur || {};
	var props = Object.getOwnPropertyNames(err);

	for (var a in props) {
		if (excluding && excluding.indexOf(props[a]) >= 0)
			continue;

		output[props[a]] = err[props[a]];
	}

	return output;
}
<|endoftext|>


<commit_before>'use strict';

var fs = require('fs'),
    path = require('path'),
    deref = require('deref'),
    Async = require('async-parts');

var fetchURL = {
  http: require('http'),
  https: require('https')
};

function parseJSON(text, uri) {
  try {
    return JSON.parse(text);
  } catch (e) {
    throw new Error('cannot parse ' + uri);
  }
}

function extractRefs(obj) {
  var set = [];

  for (var key in obj) {
    var value = obj[key];

    if (key === '$ref') {
      set.push(value);
    }

    if (typeof value === 'object') {
      set = set.concat(extractRefs(value));
    }
  }

  return set;
}

module.exports = function(params, callback) {
  var schemas = Array.isArray(params.schemas) ? params.schemas : Array.isArray(params.schema) ? params.schema : [params.schema];

  var schema_directory = params.directory || process.cwd(),
      normalized_fakeroot = typeof params.fakeroot === 'string' && (params.fakeroot.replace(/\/+$/g, '') + '/');

  var $ = deref(),
      _ = new Async();

  function downloadSchemas(from, parent) {
    function pushReference(schema) {
      if (typeof schema.id === 'string') {
        var base = $.util.resolveURL(parent, schema.id);

        base = $.util.getDocumentURI(base) || base;

        if (!$.refs[base]) {
          $.refs[base] = schema;
        }
      }
    }

    function fetchRefs(schema) {
      if (typeof schema.id === 'string') {
        extractRefs(schema).forEach(function(ref) {
          var url = $.util.getDocumentURI(ref);

          if ($.refs[url]) {
            return;
          }

          if (url.indexOf(normalized_fakeroot) === 0) {
            var file = path.join(schema_directory, url.replace(normalized_fakeroot, ''));

            if (fs.statSync(file).isFile()) {
              downloadSchemas(parseJSON(fs.readFileSync(file).toString(), file), url);
            }
          } else {
            _.then(function(next) {
              var req = fetchURL[url.split(':')[0]];

              req.get(url, function(res) {
                var body = '';

                res.on('data', function(chunk) {
                  body += chunk;
                });

                res.on('end', function() {
                  try {
                    next(downloadSchemas(parseJSON(body, url), url));
                  } catch (e) {
                    next(e);
                  }
                });
              }).on('error', function() {
                next(new Error('cannot reach ' + url));
              });
            });
          }
        });
      }
    }

    var fixed = $.util.normalizeSchema(parent, from, pushReference);

    _.then(function(next) {
      pushReference(fixed);
      fetchRefs(fixed);
      next();
    });
  }

  try {
    if (!Array.isArray(schemas)) {
      throw new Error('Invalid schemas "' + JSON.stringify(schemas) + '" (array expected)');
    }

    schemas.forEach(function(data) {
      downloadSchemas(data, normalized_fakeroot);
    });

    _.run(function(err) {
      callback(err, $.refs, schemas.map(function(data) {
        return $.util.normalizeSchema(normalized_fakeroot, data);
      }));
    });
  } catch (e) {
    callback(e, $.refs, schemas);
  }
};
<commit_msg>Downloading will run lately; fix<commit_after>'use strict';

var fs = require('fs'),
    path = require('path'),
    deref = require('deref'),
    Async = require('async-parts');

var fetchURL = {
  http: require('http'),
  https: require('https')
};

function parseJSON(text, uri) {
  try {
    return JSON.parse(text);
  } catch (e) {
    throw new Error('cannot parse ' + uri);
  }
}

function extractRefs(obj) {
  var set = [];

  for (var key in obj) {
    var value = obj[key];

    if (key === '$ref') {
      set.push(value);
    }

    if (typeof value === 'object') {
      set = set.concat(extractRefs(value));
    }
  }

  return set;
}

module.exports = function(params, callback) {
  var schemas = Array.isArray(params.schemas) ? params.schemas : Array.isArray(params.schema) ? params.schema : [params.schema];

  var schema_directory = params.directory || process.cwd(),
      normalized_fakeroot = typeof params.fakeroot === 'string' && (params.fakeroot.replace(/\/+$/g, '') + '/');

  var $ = deref(),
      _ = new Async();

  function downloadSchemas(from, parent) {
    function pushReference(schema) {
      if (typeof schema.id === 'string') {
        var base = $.util.resolveURL(parent, schema.id);

        base = $.util.getDocumentURI(base) || base;

        if (!$.refs[base]) {
          $.refs[base] = schema;
        }
      }
    }

    function fetchRefs(schema) {
      if (typeof schema.id === 'string') {
        extractRefs(schema).forEach(function(ref) {
          var url = $.util.getDocumentURI(ref);

          if ($.refs[url]) {
            return;
          }

          if (url.indexOf(normalized_fakeroot) === 0) {
            var file = path.join(schema_directory, url.replace(normalized_fakeroot, ''));

            if (fs.statSync(file).isFile()) {
              downloadSchemas(parseJSON(fs.readFileSync(file).toString(), file), url);
            }
          } else {
            _.then(function(next) {
              var req = fetchURL[url.split(':')[0]];

              req.get(url, function(res) {
                var body = '';

                res.on('data', function(chunk) {
                  body += chunk;
                });

                res.on('end', function() {
                  try {
                    next(downloadSchemas(parseJSON(body, url), url));
                  } catch (e) {
                    next(e);
                  }
                });
              }).on('error', function() {
                next(new Error('cannot reach ' + url));
              });
            });
          }
        });
      }
    }

    var fixed = $.util.normalizeSchema(parent, from, pushReference);

    pushReference(fixed);

    _.then(function(next) {
      fetchRefs(fixed);
      next();
    });
  }

  try {
    if (!Array.isArray(schemas)) {
      throw new Error('Invalid schemas "' + JSON.stringify(schemas) + '" (array expected)');
    }

    schemas.forEach(function(data) {
      downloadSchemas(data, normalized_fakeroot);
    });

    _.run(function(err) {
      callback(err, $.refs, schemas.map(function(data) {
        return $.util.normalizeSchema(normalized_fakeroot, data);
      }));
    });
  } catch (e) {
    callback(e, $.refs, schemas);
  }
};
<|endoftext|>


<commit_before>/* jshint node:true */
'use strict';
/**
 * Module dependencies.
 */
var os = require('os');
var _ = require('underscore');

var joinRows = require('./join-rows');
var generateHeaders;
var escapeDelimiters;

var options;

/**
 * Main function that converts json to csv
 *
 * @param {Object|Array} json
 * @param {Object} [options]
 * @param {Function} callback(err, csv) - Callback function
 *      if error, returning error in call back.
 *      if csv is created successfully, returning csv output to callback.
 */
module.exports = function(json,userOptions,callback) {
    callback = callback ? callback : function(){};
    if(_.isFunction(userOptions)) {
      callback = userOptions;
      userOptions = {};
    }

    options = parseOptions(userOptions);

    escapeDelimiters = require('./escape-delimiters')(
      options.textDelimiter,
      options.rowDelimiter,
      options.endOfLine
    );

    generateHeaders = require('./generate-headers')(escapeDelimiters);

    generateCsv(json, callback);
};
/**
 * Replaces the default options with the custom user options
 *
 * @param {Options} userOptions
 */
function parseOptions(userOptions){
    /**
     * Default options
     */
    var defaultOptions = {
        headerPathString: '.', //       String
        rowDelimiter: ',', //           String
        textDelimiter: '"', //           String
        arrayPathString: ';', //        String
        undefinedString: '', //         String
        endOfLine: os.EOL || '\n', //   String
        mainPathItem: null, //          String
        booleanTrueString: null, //     String
        booleanFalseString: null, //    String
        includeHeaders: true, //        Boolean
        orderHeaders: false, //          Boolean
        verticalOutput: true, //        Boolean
        //Handlers
        handleString: null, //          Function
        handleNumber: null, //          Function
        handleBoolean: null, //         Function
        handleDate: null, //            Function
        //handleArray: null, //           Function
        //handleObject: null //           Function
    };
    return _.extend(defaultOptions, userOptions);
}
/**
 * Generates a CSV file with optional headers based on the passed JSON,
 * with can be an Object or Array.
 *
 * @param {Object|Array} json
 * @param {Function} callback(err,csv) - Callback function
 *      if error, returning error in call back.
 *      if csv is created successfully, returning csv output to callback.
 */
function generateCsv(json, callback) {
    if(_.isArray(json)){
      return callback(null, generateFromArray(json));
    }
    else if(_.isObject(json)){
        return callback(null, generateFromObject(json));
    }

    return callback(new Error('Unable to parse the JSON object, its not an Array or Object.'));
}

function generateFromArray(json) {
  var fileRows = [];
  var parseResult = [];
  var outputFile;
  var fillRows;
  json.forEach(function (item) {
      //Call checkType to list all items inside this object
      //Items are returned as a object {item: 'Prop Value, Item Name',
      //                                value: 'Prop Data Value'}
      var itemResult = checkType(item, options.mainPathItem);
      parseResult.push(itemResult);
  });

  //Generate the csv headers based on prop usage over the elements
  var headers = generateHeaders(parseResult, options.orderHeaders);
  //Generate the csv output

  if(options.includeHeaders){
      //Add the headers to the first line
      fileRows.push(headers.join(options.rowDelimiter));
  }

  fillRows = function (result) {
      //Initialize the array with empty strings to handle 'unpopular' headers
      var resultRows = [Array(headers.length).join(".").split(".")];

      result.forEach(function(element){
          var placed = false;
          resultRows.forEach(function (row) {
              if(!placed && row[headers.indexOf(element.item)] === ''){
                  row[headers.indexOf(element.item)] = escapeDelimiters(element.value);
                  placed = true;
              }
          });

          if(!placed){
              var newRow = Array(headers.length).join(".").split(".");
              newRow[headers.indexOf(element.item)] = escapeDelimiters(element.value);
              resultRows.push(newRow);
          }
      });
      resultRows.forEach(function (row) {
          fileRows.push(row.join(options.rowDelimiter));
      });
  };

  parseResult.forEach(fillRows);
  return joinRows(fileRows, options.endOfLine);
}

function generateFromObject(json) {
  var fileRows = [];
  var parseResult = [];
  var outputFile;
  var fillRows;

  var horizontalRows = [[],[]];
  fillRows = function (result) {

      var value = result.value ? result.value.toString() : options.undefinedString;
      value = escapeDelimiters(value);

      //Type header;value
      if(options.verticalOutput){
          var row = [result.item, value];
          fileRows.push(row.join(options.rowDelimiter));
      }else{
          horizontalRows[0].push(result.item);
          horizontalRows[1].push(value);
      }
  };
  for(var prop in json){
      var prefix = "";
      if(options.mainPathItem)
          prefix = options.mainPathItem + options.headerPathString;
      parseResult = checkType(json[prop], prefix + prop);

      parseResult.forEach(fillRows);
  }
  if(!options.verticalOutput){
      fileRows.push(horizontalRows[0].join(options.rowDelimiter));
      fileRows.push(horizontalRows[1].join(options.rowDelimiter));
  }
  return joinRows(fileRows, options.endOfLine);
}
/**
 * Check the element type of the element call the correct handle function
 *
 * @param element Element that will be checked
 * @param item Used to make the headers/path breadcrumb
 */
function checkType(element, item){
    var result = [];
    //Check if element is a String
    if(_.isString(element)){
        var resultString = options.handleString ? options.handleString(element, item) : handleString(element, item);
        result.push({
            item: item,
            value: resultString
        });
    }
    //Check if element is a Number
    else if(_.isNumber(element)){
        var resultNumber = options.handleNumber ? options.handleNumber(element, item) : handleNumber(element, item);
        result.push({
            item: item,
            value: resultNumber
        });
    }
    //Check if element is a Boolean
    else if(_.isBoolean(element)){
        var resultBoolean = options.handleBoolean ? options.handleBoolean(element, item) : handleBoolean(element, item);
        result.push({
            item: item,
            value: resultBoolean
        });
    }
    //Check if element is a Date
    else if(_.isDate(element)){
        var resultDate = options.handleDate ? options.handleDate(element, item) : handleDate(element, item);
        result.push({
            item: item,
            value: resultDate
        });
    }
    //Check if element is an Array
    else if(_.isArray(element)){
        var resultArray = handleArray(element, item);
        resultArray.forEach(function (resultArrayElement) {
            if(resultArrayElement.item && item){
                resultArrayElement.item = item + options.headerPathString + resultArrayElement.item;
            }
            if(!resultArrayElement.item && item){
                resultArrayElement.item = item;
            }
            result.push(resultArrayElement);
        });
    }
    //Check if element is a Object
    else if(_.isObject(element)){
        var resultObject = handleObject(element, item);
        resultObject.forEach(function (resultObjectElement) {
            if(resultObjectElement.item && item) {
                resultObjectElement.item = item + options.headerPathString + resultObjectElement.item;
            }
            if(!resultObjectElement.item && item){
                resultObjectElement.item = item;
            }
            result.push(resultObjectElement);
        });
    }
    //Finished, returning the result now
    return result;
}
/**
 * Handle all Objects
 *
 * @param {Object} obj
 * @returns {Array} result
 */
function handleObject(obj){
    var result = [];
    //Look every object props
    for(var prop in obj){
        var propData = obj[prop];
        //Check the propData type
        var resultCheckType = checkType(propData, prop);
        //Append to results
        result = result.concat(resultCheckType);
    }
    return result;
}
/**
 * Handle all Arrays, merges arrays with primitive types in a single value
 *
 * @param {Array} array
 * @returns {Array} result
 */
function handleArray(array){
    var result = [];
    array.forEach(function(element) {
        //Check the propData type
        var resultCheckType = checkType(element);

        //Append to results
        result = result.concat(resultCheckType);
    });
    //Check for results without itens, merge all itens with the first occurrence
    var firstElementWithoutItem;
    result = result.filter(function (resultElement, resultIndex) {
        //Check if there is a firstElement and if the current element dont have a item
        if(!resultElement.item && firstElementWithoutItem !== undefined){
            //Append the value to the firstElementWithoutItem.
            result[firstElementWithoutItem].value += options.arrayPathString + resultElement.value;
            //Remove the item
            return false;
        }
        //Set the firstElement if its not set
        if(!firstElementWithoutItem && !resultElement.item)
            firstElementWithoutItem = resultIndex;
        //Keep the item in the array
        return true;
    });
    //Finished, returning result now
    return result;
}
/**
 * Handle all Boolean variables, can be replaced with options.handleBoolean
 *
 * @param {Boolean} boolean
 * @returns {String} result
 */
function handleBoolean(boolean){
    var result;
    //Check for booolean options
    if(boolean){
        result = options.booleanTrueString || 'true';
    }else{
        result = options.booleanFalseString || 'false';
    }
    //Finished, returning result now
    return result;
}
/**
 * Handle all String variables, can be replaced with options.handleString
 *
 * @param {String} string
 * @returns {String} string
 */
function handleString(string){
    //Finished, returning the string now
    return string;
}
/**
 * Handle all Number variables, can be replaced with options.handleNumber
 *
 * @param {Number} number
 * @returns {Number} number
 */
function handleNumber(number){
    //Finished, returning the number now
    return number;
}
/**
 * Handle all Date variables, can be replaced with options.handleDate
 *
 * @param {Date} number
 * @returns {string} result
 */
function handleDate(date){
    //Finished, returning the number now
    return date.toLocaleString();
}
<commit_msg>refactoring checkType to lower complextity<commit_after>/* jshint node:true */
'use strict';
/**
 * Module dependencies.
 */
var os = require('os');
var _ = require('underscore');

var joinRows = require('./join-rows');
var generateHeaders;
var escapeDelimiters;

var options;

/**
 * Main function that converts json to csv
 *
 * @param {Object|Array} json
 * @param {Object} [options]
 * @param {Function} callback(err, csv) - Callback function
 *      if error, returning error in call back.
 *      if csv is created successfully, returning csv output to callback.
 */
module.exports = function(json,userOptions,callback) {
    callback = callback ? callback : function(){};
    if(_.isFunction(userOptions)) {
      callback = userOptions;
      userOptions = {};
    }

    options = parseOptions(userOptions);

    escapeDelimiters = require('./escape-delimiters')(
      options.textDelimiter,
      options.rowDelimiter,
      options.endOfLine
    );

    generateHeaders = require('./generate-headers')(escapeDelimiters);

    generateCsv(json, callback);
};
/**
 * Replaces the default options with the custom user options
 *
 * @param {Options} userOptions
 */
function parseOptions(userOptions){
    /**
     * Default options
     */
    var defaultOptions = {
        headerPathString: '.', //       String
        rowDelimiter: ',', //           String
        textDelimiter: '"', //           String
        arrayPathString: ';', //        String
        undefinedString: '', //         String
        endOfLine: os.EOL || '\n', //   String
        mainPathItem: null, //          String
        booleanTrueString: null, //     String
        booleanFalseString: null, //    String
        includeHeaders: true, //        Boolean
        orderHeaders: false, //          Boolean
        verticalOutput: true, //        Boolean
        //Handlers
        handleString: handleString, //          Function
        handleNumber: handleNumber, //          Function
        handleBoolean: handleBoolean, //         Function
        handleDate: handleDate, //            Function
        //handleArray: null, //           Function
        //handleObject: null //           Function
    };
    return _.extend(defaultOptions, userOptions);
}
/**
 * Generates a CSV file with optional headers based on the passed JSON,
 * with can be an Object or Array.
 *
 * @param {Object|Array} json
 * @param {Function} callback(err,csv) - Callback function
 *      if error, returning error in call back.
 *      if csv is created successfully, returning csv output to callback.
 */
function generateCsv(json, callback) {
    if(_.isArray(json)){
      return callback(null, generateFromArray(json));
    }
    else if(_.isObject(json)){
        return callback(null, generateFromObject(json));
    }

    return callback(new Error('Unable to parse the JSON object, its not an Array or Object.'));
}

function generateFromArray(json) {
  var fileRows = [];
  var parseResult = [];
  var outputFile;
  var fillRows;
  json.forEach(function (item) {
      //Call checkType to list all items inside this object
      //Items are returned as a object {item: 'Prop Value, Item Name',
      //                                value: 'Prop Data Value'}
      var itemResult = checkType(item, options.mainPathItem);
      parseResult.push(itemResult);
  });

  //Generate the csv headers based on prop usage over the elements
  var headers = generateHeaders(parseResult, options.orderHeaders);
  //Generate the csv output

  if(options.includeHeaders){
      //Add the headers to the first line
      fileRows.push(headers.join(options.rowDelimiter));
  }

  fillRows = function (result) {
      //Initialize the array with empty strings to handle 'unpopular' headers
      var resultRows = [Array(headers.length).join(".").split(".")];

      result.forEach(function(element){
          var placed = false;
          resultRows.forEach(function (row) {
              if(!placed && row[headers.indexOf(element.item)] === ''){
                  row[headers.indexOf(element.item)] = escapeDelimiters(element.value);
                  placed = true;
              }
          });

          if(!placed){
              var newRow = Array(headers.length).join(".").split(".");
              newRow[headers.indexOf(element.item)] = escapeDelimiters(element.value);
              resultRows.push(newRow);
          }
      });
      resultRows.forEach(function (row) {
          fileRows.push(row.join(options.rowDelimiter));
      });
  };

  parseResult.forEach(fillRows);
  return joinRows(fileRows, options.endOfLine);
}

function generateFromObject(json) {
  var fileRows = [];
  var parseResult = [];
  var outputFile;
  var fillRows;

  var horizontalRows = [[],[]];
  fillRows = function (result) {

      var value = result.value ? result.value.toString() : options.undefinedString;
      value = escapeDelimiters(value);

      //Type header;value
      if(options.verticalOutput){
          var row = [result.item, value];
          fileRows.push(row.join(options.rowDelimiter));
      }else{
          horizontalRows[0].push(result.item);
          horizontalRows[1].push(value);
      }
  };
  for(var prop in json){
      var prefix = "";
      if(options.mainPathItem)
          prefix = options.mainPathItem + options.headerPathString;
      parseResult = checkType(json[prop], prefix + prop);

      parseResult.forEach(fillRows);
  }
  if(!options.verticalOutput){
      fileRows.push(horizontalRows[0].join(options.rowDelimiter));
      fileRows.push(horizontalRows[1].join(options.rowDelimiter));
  }
  return joinRows(fileRows, options.endOfLine);
}

function headers(result, item) {

  if (!item) return result;

  return result.map(function (element) {
      element.item = element.item ? item + options.headerPathString + element.item : item;
      return element;
  });
}

/**
 * Check the element type of the element call the correct handle function
 *
 * @param element Element that will be checked
 * @param item Used to make the headers/path breadcrumb
 */
function checkType(element, item){
    var result;
    //Check if element is a String
    if(_.isString(element)){
        result = [{
            item: item,
            value: options.handleString(element, item),
        }];
    }
    //Check if element is a Number
    else if(_.isNumber(element)){
        result = [{
            item: item,
            value: options.handleNumber(element, item),
        }];
    }
    //Check if element is a Boolean
    else if(_.isBoolean(element)){
        result = [{
            item: item,
            value: options.handleBoolean(element, item),
        }];
    }
    //Check if element is a Date
    else if(_.isDate(element)){
        result = [{
            item: item,
            value: options.handleDate(element, item),
        }];
    }
    //Check if element is an Array
    else if(_.isArray(element)){
        var resultArray = handleArray(element, item);
        result = headers(resultArray, item);
    }
    //Check if element is a Object
    else if(_.isObject(element)){
        var resultObject = handleObject(element, item);
        result = headers(resultObject, item);
    }
    //Finished, returning the result now
    return result;
}
/**
 * Handle all Objects
 *
 * @param {Object} obj
 * @returns {Array} result
 */
function handleObject(obj){
    var result = [];
    //Look every object props
    for(var prop in obj){
        var propData = obj[prop];
        //Check the propData type
        var resultCheckType = checkType(propData, prop);
        //Append to results
        result = result.concat(resultCheckType);
    }
    return result;
}
/**
 * Handle all Arrays, merges arrays with primitive types in a single value
 *
 * @param {Array} array
 * @returns {Array} result
 */
function handleArray(array){
    var result = [];
    array.forEach(function(element) {
        //Check the propData type
        var resultCheckType = checkType(element);

        //Append to results
        result = result.concat(resultCheckType);
    });
    //Check for results without itens, merge all itens with the first occurrence
    var firstElementWithoutItem;
    result = result.filter(function (resultElement, resultIndex) {
        //Check if there is a firstElement and if the current element dont have a item
        if(!resultElement.item && firstElementWithoutItem !== undefined){
            //Append the value to the firstElementWithoutItem.
            result[firstElementWithoutItem].value += options.arrayPathString + resultElement.value;
            //Remove the item
            return false;
        }
        //Set the firstElement if its not set
        if(!firstElementWithoutItem && !resultElement.item)
            firstElementWithoutItem = resultIndex;
        //Keep the item in the array
        return true;
    });
    //Finished, returning result now
    return result;
}
/**
 * Handle all Boolean variables, can be replaced with options.handleBoolean
 *
 * @param {Boolean} boolean
 * @returns {String} result
 */
function handleBoolean(boolean){
    var result;
    //Check for booolean options
    if(boolean){
        result = options.booleanTrueString || 'true';
    }else{
        result = options.booleanFalseString || 'false';
    }
    //Finished, returning result now
    return result;
}
/**
 * Handle all String variables, can be replaced with options.handleString
 *
 * @param {String} string
 * @returns {String} string
 */
function handleString(string){
    //Finished, returning the string now
    return string;
}
/**
 * Handle all Number variables, can be replaced with options.handleNumber
 *
 * @param {Number} number
 * @returns {Number} number
 */
function handleNumber(number){
    //Finished, returning the number now
    return number;
}
/**
 * Handle all Date variables, can be replaced with options.handleDate
 *
 * @param {Date} number
 * @returns {string} result
 */
function handleDate(date){
    //Finished, returning the number now
    return date.toLocaleString();
}
<|endoftext|>


<commit_before>var bodyParser = require('body-parser');
var normalize = require('path').normalize;
var spawn = require('child_process').spawn;
var express = require('express');

module.exports = function (ref, action) {

  // create express instance
  var hookshot = express();

  // middleware
  hookshot.use(bodyParser.urlencoded({ extended: false }));
  hookshot.use(bodyParser.json());

  // main POST handler
  hookshot.post('/', function (req, res, next) {
    var payload = req.body;
    if (typeof payload.payload != 'undefined') {
      payload = JSON.parse(payload.payload);
    }
    // GitHub send an initial ping on webhook creation, detect it by looking for `hook_id`
    // Send a 200 response and stop further execution of this script.
    if (typeof payload.hook_id == 'number') {
      res.send(200, 'OK');
      return false;
    }
    if (typeof payload.ref != 'string') {
      throw new Error('Invalid ref');
    }
    if (payload.created) {
      hookshot.emit('create', payload);
    } else if (payload.deleted) {
      hookshot.emit('delete', payload);
    } else {
      hookshot.emit('push', payload);
    }
    hookshot.emit('hook', payload);
    hookshot.emit(payload.ref, payload);
    res.send(202, 'Accepted\n');
  });

  if (arguments.length == 1) {
    action = ref;
    ref = 'hook';
  }

  if (typeof action == 'string') {
    var shell = process.env.SHELL;
    var args = ['-c', action];
    var opts = { stdio: 'inherit' };
    if (shell && isCygwin()) {
      shell = cygpath(shell);
    } else if (isWin()) {
      shell = process.env.ComSpec;
      args = ['/s', '/c', '"' + action + '"'];
      opts.windowsVerbatimArguments = true;
    }

    hookshot.on(ref, function(payload) {
      // shell command
      spawn(shell, args, opts);
    });
  } else if (typeof action == 'function') {
    hookshot.on(ref, action);
  }

  return hookshot;
};

/**
 * Returns `true` if node is currently running on Windows, `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isWin () {
  return 'win32' == process.platform;
}

/**
 * Returns `true` if node is currently running from within a "cygwin" environment.
 * Returns `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isCygwin () {
  // TODO: implement a more reliable check here...
  return isWin() && /cygwin/i.test(process.env.HOME);
}

/**
 * Convert a Unix-style Cygwin path (i.e. "/bin/bash") to a Windows-style path
 * (i.e. "C:\cygwin\bin\bash").
 *
 * @param {String} path
 * @return {String}
 * @api private
 */

function cygpath (path) {
  path = normalize(path);
  if (path[0] == '\\') {
    // TODO: implement better cygwin root detection...
    path = 'C:\\cygwin' + path;
  }
  return path;
}
<commit_msg>Better error reporting<commit_after>var bodyParser = require('body-parser');
var normalize = require('path').normalize;
var spawn = require('child_process').spawn;
var express = require('express');

module.exports = function (ref, action) {

  // create express instance
  var hookshot = express();

  // middleware
  hookshot.use(bodyParser.urlencoded({ extended: false }));
  hookshot.use(bodyParser.json());

  // main POST handler
  hookshot.post('/', function (req, res, next) {
    var payload = req.body;
    if (typeof payload.payload != 'undefined') {
      payload = JSON.parse(payload.payload);
    }
    // GitHub send an initial ping on webhook creation, detect it by looking for `hook_id`
    // Send a 200 response and stop further execution of this script.
    if (typeof payload.hook_id == 'number') {
      res.send(200, 'OK');
      return false;
    }
    if (typeof payload.ref != 'string') {
      throw new Error('payload.ref is not string. It is:', payload.ref);
    }
    if (payload.created) {
      hookshot.emit('create', payload);
    } else if (payload.deleted) {
      hookshot.emit('delete', payload);
    } else {
      hookshot.emit('push', payload);
    }
    hookshot.emit('hook', payload);
    hookshot.emit(payload.ref, payload);
    res.send(202, 'Accepted\n');
  });

  if (arguments.length == 1) {
    action = ref;
    ref = 'hook';
  }

  if (typeof action == 'string') {
    var shell = process.env.SHELL;
    var args = ['-c', action];
    var opts = { stdio: 'inherit' };
    if (shell && isCygwin()) {
      shell = cygpath(shell);
    } else if (isWin()) {
      shell = process.env.ComSpec;
      args = ['/s', '/c', '"' + action + '"'];
      opts.windowsVerbatimArguments = true;
    }

    hookshot.on(ref, function(payload) {
      // shell command
      spawn(shell, args, opts);
    });
  } else if (typeof action == 'function') {
    hookshot.on(ref, action);
  }

  return hookshot;
};

/**
 * Returns `true` if node is currently running on Windows, `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isWin () {
  return 'win32' == process.platform;
}

/**
 * Returns `true` if node is currently running from within a "cygwin" environment.
 * Returns `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isCygwin () {
  // TODO: implement a more reliable check here...
  return isWin() && /cygwin/i.test(process.env.HOME);
}

/**
 * Convert a Unix-style Cygwin path (i.e. "/bin/bash") to a Windows-style path
 * (i.e. "C:\cygwin\bin\bash").
 *
 * @param {String} path
 * @return {String}
 * @api private
 */

function cygpath (path) {
  path = normalize(path);
  if (path[0] == '\\') {
    // TODO: implement better cygwin root detection...
    path = 'C:\\cygwin' + path;
  }
  return path;
}
<|endoftext|>


<commit_before>var bodyParser = require('body-parser');
var normalize = require('path').normalize;
var spawn = require('child_process').spawn;
var express = require('express');

module.exports = function (ref, action) {

  // create express instance
  var hookshot = express();

  // middleware
  hookshot.use(bodyParser.urlencoded({ extended: false }));
  hookshot.use(bodyParser.json());

  // main POST handler
  hookshot.post('/', function (req, res, next) {
    var payload = req.body;
    if (typeof payload.payload != 'undefined') {
      payload = JSON.parse(payload.payload);
    }
    if (typeof payload.ref != 'string') {
      throw new Error('Invalid ref');
    }
    if (payload.created) {
      hookshot.emit('create', payload);
    } else if (payload.deleted) {
      hookshot.emit('delete', payload);
    } else {
      hookshot.emit('push', payload);
    }
    hookshot.emit('hook', payload);
    hookshot.emit(payload.ref, payload);
    res.send(202, 'Accepted\n');
  });

  if (arguments.length == 1) {
    action = ref;
    ref = 'hook';
  }

  if (typeof action == 'string') {
    var shell = process.env.SHELL;
    var args = ['-c', action];
    var opts = { stdio: 'inherit' };
    if (shell && isCygwin()) {
      shell = cygpath(shell);
    } else if (isWin()) {
      shell = process.env.ComSpec;
      args = ['/s', '/c', '"' + action + '"'];
      opts.windowsVerbatimArguments = true;
    }

    hookshot.on(ref, function(payload) {
      // shell command
      spawn(shell, args, opts);
    });
  } else if (typeof action == 'function') {
    hookshot.on(ref, action);
  }

  return hookshot;
};

/**
 * Returns `true` if node is currently running on Windows, `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isWin () {
  return 'win32' == process.platform;
}

/**
 * Returns `true` if node is currently running from within a "cygwin" environment.
 * Returns `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isCygwin () {
  // TODO: implement a more reliable check here...
  return isWin() && /cygwin/i.test(process.env.HOME);
}

/**
 * Convert a Unix-style Cygwin path (i.e. "/bin/bash") to a Windows-style path
 * (i.e. "C:\cygwin\bin\bash").
 *
 * @param {String} path
 * @return {String}
 * @api private
 */

function cygpath (path) {
  path = normalize(path);
  if (path[0] == '\\') {
    // TODO: implement better cygwin root detection...
    path = 'C:\\cygwin' + path;
  }
  return path;
}
<commit_msg>acknowledge webhook creation ping<commit_after>var bodyParser = require('body-parser');
var normalize = require('path').normalize;
var spawn = require('child_process').spawn;
var express = require('express');

module.exports = function (ref, action) {

  // create express instance
  var hookshot = express();

  // middleware
  hookshot.use(bodyParser.urlencoded({ extended: false }));
  hookshot.use(bodyParser.json());

  // main POST handler
  hookshot.post('/', function (req, res, next) {
    var payload = req.body;
    if (typeof payload.payload != 'undefined') {
      payload = JSON.parse(payload.payload);
    }
    // GitHub send an initial ping on webhook creation, detect it by looking for `hook_id`
    // Send a 200 response and stop further execution of this script.
    if (typeof payload.hook_id == 'number') {
      res.send(200, 'OK');
      return false;
    }
    if (typeof payload.ref != 'string') {
      throw new Error('Invalid ref');
    }
    if (payload.created) {
      hookshot.emit('create', payload);
    } else if (payload.deleted) {
      hookshot.emit('delete', payload);
    } else {
      hookshot.emit('push', payload);
    }
    hookshot.emit('hook', payload);
    hookshot.emit(payload.ref, payload);
    res.send(202, 'Accepted\n');
  });

  if (arguments.length == 1) {
    action = ref;
    ref = 'hook';
  }

  if (typeof action == 'string') {
    var shell = process.env.SHELL;
    var args = ['-c', action];
    var opts = { stdio: 'inherit' };
    if (shell && isCygwin()) {
      shell = cygpath(shell);
    } else if (isWin()) {
      shell = process.env.ComSpec;
      args = ['/s', '/c', '"' + action + '"'];
      opts.windowsVerbatimArguments = true;
    }

    hookshot.on(ref, function(payload) {
      // shell command
      spawn(shell, args, opts);
    });
  } else if (typeof action == 'function') {
    hookshot.on(ref, action);
  }

  return hookshot;
};

/**
 * Returns `true` if node is currently running on Windows, `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isWin () {
  return 'win32' == process.platform;
}

/**
 * Returns `true` if node is currently running from within a "cygwin" environment.
 * Returns `false` otherwise.
 *
 * @return {Boolean}
 * @api private
 */

function isCygwin () {
  // TODO: implement a more reliable check here...
  return isWin() && /cygwin/i.test(process.env.HOME);
}

/**
 * Convert a Unix-style Cygwin path (i.e. "/bin/bash") to a Windows-style path
 * (i.e. "C:\cygwin\bin\bash").
 *
 * @param {String} path
 * @return {String}
 * @api private
 */

function cygpath (path) {
  path = normalize(path);
  if (path[0] == '\\') {
    // TODO: implement better cygwin root detection...
    path = 'C:\\cygwin' + path;
  }
  return path;
}
<|endoftext|>


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

var internals = {};

internals.defaults = {
  global:{limit: -1 , bucketLength: 1},
  routes: {},
  redis:{}
};

exports.register = function(plugin, options, next) {
  var settings = plugin.hapi.utils.applyToDefaults(internals.defaults, options);
  var redisClient  = redis.createClient(options.redis.port, options.redis.host, options.redis.options);
  plugin.state('cl-hapi-rate',{});
  plugin.ext('request', function(request, callback) {
    var ipts = request.info.remoteAddress+':'+Math.floor((new Date()).getTime()/1000)
    redisClient.get(ipts,function(err, token){
      if(token && token > 1) {
        return request.reply(429);
      }else{
        rediClient.multi([['INCR',ipts],['EXPIRE', 120]]).exec(function(err) {
          return next();
        });
      }
    });
  });
};
<commit_msg>Correctly named event<commit_after>var redis = require('redis');

var internals = {};

internals.defaults = {
  global:{limit: -1 , bucketLength: 1},
  routes: {},
  redis:{}
};

exports.register = function(plugin, options, next) {
  var settings = plugin.hapi.utils.applyToDefaults(internals.defaults, options);
  var redisClient  = redis.createClient(options.redis.port, options.redis.host, options.redis.options);
  plugin.state('cl-hapi-rate',{});
  plugin.ext('onRequest', function(request, callback) {
    var ipts = request.info.remoteAddress+':'+Math.floor((new Date()).getTime()/1000)
    redisClient.get(ipts,function(err, token){
      if(token && token > 1) {
        return request.reply(429);
      }else{
        rediClient.multi([['INCR',ipts],['EXPIRE', 120]]).exec(function(err) {
          return next();
        });
      }
    });
  });
};
<|endoftext|>


<commit_before>/* Copyright (c) 2014-2015 Richard Rodger, MIT License */
'use strict'

var _ = require('lodash')
var Redis = require('redis')

module.exports = function (options) {
  var seneca = this
  var plugin = 'redis-transport'

  var so = seneca.options()

  options = seneca.util.deepextend(
    {
      redis: {
        timeout: so.timeout ? so.timeout - 555 : 22222,
        type: 'redis',
        host: 'localhost',
        port: 6379
      }
    },
    so.transport,
    options)

  var tu = seneca.export('transport/utils')

  seneca.add({role: 'transport', hook: 'listen', type: 'redis'}, hook_listen_redis)
  seneca.add({role: 'transport', hook: 'client', type: 'redis'}, hook_client_redis)

  // Legacy patterns
  seneca.add({role: 'transport', hook: 'listen', type: 'pubsub'}, hook_listen_redis)
  seneca.add({role: 'transport', hook: 'client', type: 'pubsub'}, hook_client_redis)

  function hook_listen_redis (args, done) {
    var seneca = this
    var type = args.type
    var listen_options = seneca.util.clean(_.extend({}, options[type], args))

    var redis_in = Redis.createClient(listen_options.port, listen_options.host)
    var redis_out = Redis.createClient(listen_options.port, listen_options.host)

    handle_events(redis_in)
    handle_events(redis_out)

    redis_in.on('message', function (channel, msgstr) {
      var restopic = channel.replace(/_act$/, '_res')
      var data = tu.parseJSON(seneca, 'listen-' + type, msgstr)

      tu.handle_request(seneca, data, listen_options, function (out) {
        if (out == null) return
        var outstr = tu.stringifyJSON(seneca, 'listen-' + type, out)
        redis_out.publish(restopic, outstr)
      })
    })

    tu.listen_topics(seneca, args, listen_options, function (topic) {
      seneca.log.debug('listen', 'subscribe', topic + '_act', listen_options, seneca)
      redis_in.subscribe(topic + '_act')
    })

    seneca.add('role:seneca,cmd:close', function (close_args, done) {
      var closer = this

      redis_in.end()
      redis_out.end()
      closer.prior(close_args, done)
    })

    seneca.log.info('listen', 'open', listen_options, seneca)

    done()
  }

  function hook_client_redis (args, clientdone) {
    var seneca = this
    var type = args.type
    var client_options = seneca.util.clean(_.extend({}, options[type], args))

    tu.make_client(make_send, client_options, clientdone)

    function make_send (spec, topic, send_done) {
      var redis_in = Redis.createClient(client_options.port, client_options.host)
      var redis_out = Redis.createClient(client_options.port, client_options.host)

      handle_events(redis_in)
      handle_events(redis_out)

      redis_in.on('message', function (channel, msgstr) {
        var input = tu.parseJSON(seneca, 'client-' + type, msgstr)
        tu.handle_response(seneca, input, client_options)
      })

      seneca.log.debug('client', 'subscribe', topic + '_res', client_options, seneca)
      redis_in.subscribe(topic + '_res')

      send_done(null, function (args, done) {
        var outmsg = tu.prepare_request(this, args, done)
        var outstr = tu.stringifyJSON(seneca, 'client-' + type, outmsg)

        redis_out.publish(topic + '_act', outstr)
      })

      seneca.add('role:seneca,cmd:close', function (close_args, done) {
        var closer = this

        redis_in.end()
        redis_out.end()
        closer.prior(close_args, done)
      })
    }
  }

  function handle_events (redisclient) {
    // Die if you can't connect initially
    redisclient.on('ready', function () {
      redisclient.on('error', function (err) {
        seneca.log.error('transport', 'redis', err)
      })
    })
  }

  return {
    name: plugin
  }
}
<commit_msg>allow seneca-redis-transport to connect to reids servers via url<commit_after>/* Copyright (c) 2014-2015 Richard Rodger, MIT License */
'use strict'

var _ = require('lodash')
var Redis = require('redis')

module.exports = function (options) {
  var seneca = this
  var plugin = 'redis-transport'

  var so = seneca.options()

  options = seneca.util.deepextend(
    {
      redis: {
        timeout: so.timeout ? so.timeout - 555 : 22222,
        type: 'redis',
        host: 'localhost',
        port: 6379
      }
    },
    so.transport,
    options)

  var tu = seneca.export('transport/utils')

  seneca.add({role: 'transport', hook: 'listen', type: 'redis'}, hook_listen_redis)
  seneca.add({role: 'transport', hook: 'client', type: 'redis'}, hook_client_redis)

  // Legacy patterns
  seneca.add({role: 'transport', hook: 'listen', type: 'pubsub'}, hook_listen_redis)
  seneca.add({role: 'transport', hook: 'client', type: 'pubsub'}, hook_client_redis)

  function hook_listen_redis (args, done) {
    var seneca = this
    var type = args.type
    var listen_options = seneca.util.clean(_.extend({}, options[type], args))

    // if Redis url is provided use it instead for production secure redis server
    var redis_in = listen_options.url?Redis.createClient({url : listen_options.url}) : Redis.createClient(listen_options.port, listen_options.host)
    var redis_out = listen_options.url?Redis.createClient({url : listen_options.url}) : Redis.createClient(listen_options.port, listen_options.host)

    handle_events(redis_in)
    handle_events(redis_out)

    redis_in.on('message', function (channel, msgstr) {
      var restopic = channel.replace(/_act$/, '_res')
      var data = tu.parseJSON(seneca, 'listen-' + type, msgstr)

      tu.handle_request(seneca, data, listen_options, function (out) {
        if (out == null) return
        var outstr = tu.stringifyJSON(seneca, 'listen-' + type, out)
        redis_out.publish(restopic, outstr)
      })
    })

    tu.listen_topics(seneca, args, listen_options, function (topic) {
      seneca.log.debug('listen', 'subscribe', topic + '_act', listen_options, seneca)
      redis_in.subscribe(topic + '_act')
    })

    seneca.add('role:seneca,cmd:close', function (close_args, done) {
      var closer = this

      redis_in.end()
      redis_out.end()
      closer.prior(close_args, done)
    })

    seneca.log.info('listen', 'open', listen_options, seneca)

    done()
  }

  function hook_client_redis (args, clientdone) {
    var seneca = this
    var type = args.type
    var client_options = seneca.util.clean(_.extend({}, options[type], args))

    tu.make_client(make_send, client_options, clientdone)

    function make_send (spec, topic, send_done) {
      // if Redis url is provided use it instead for production secure redis server
      var redis_in = client_options.url ? Redis.createClient({url:client_options.url}) : Redis.createClient(client_options.port, client_options.host)
      var redis_out = client_options.url ? Redis.createClient({url:client_options.url}) : Redis.createClient(client_options.port, client_options.host)

      handle_events(redis_in)
      handle_events(redis_out)

      redis_in.on('message', function (channel, msgstr) {
        var input = tu.parseJSON(seneca, 'client-' + type, msgstr)
        tu.handle_response(seneca, input, client_options)
      })

      seneca.log.debug('client', 'subscribe', topic + '_res', client_options, seneca)
      redis_in.subscribe(topic + '_res')

      send_done(null, function (args, done) {
        var outmsg = tu.prepare_request(this, args, done)
        var outstr = tu.stringifyJSON(seneca, 'client-' + type, outmsg)

        redis_out.publish(topic + '_act', outstr)
      })

      seneca.add('role:seneca,cmd:close', function (close_args, done) {
        var closer = this

        redis_in.end()
        redis_out.end()
        closer.prior(close_args, done)
      })
    }
  }

  function handle_events (redisclient) {
    // Die if you can't connect initially
    redisclient.on('ready', function () {
      redisclient.on('error', function (err) {
        seneca.log.error('transport', 'redis', err)
      })
    })
  }

  return {
    name: plugin
  }
}
<|endoftext|>


<commit_before>var _ = require("lodash");
var builder = require('xmlbuilder');
var subjects = require("subjects-utils");

function create(definition, books) {
    definition =_.defaults(definition || {}, {
        author: {}
    });
    books = books || [];

    var feed = builder.create('feed', {
        version: '1.0',
        encoding: 'UTF-8'
    });

    return feed.end({
        pretty: true
    });
}

module.exports = {
    create: create
};
<commit_msg>Add base structure for xml feed<commit_after>var _ = require("lodash");
var builder = require('xmlbuilder');
var subjects = require("subjects-utils");

function create(definition, books) {
    definition =_.defaults(definition || {}, {
        author: {}
    });
    books = books || [];

    var feed = builder.create('ONIXMessage', {
        version: '1.0',
        encoding: 'UTF-8',
        standalone: false
    });

    feed.dtd('http://www.editeur.org/onix/2.1/reference/onix-international.dtd');

    return feed.end({
        pretty: true
    });
}

module.exports = {
    create: create
};
<|endoftext|>