<commit_before>var irc = require("irc");
var fs = require("fs");
var config = require("./config.js");
var jsonfile = require("jsonfile");
var format = require("util").format;
var inspect = require("util").inspect;
var google = require('google');
var fetch = require("node-fetch");
fetch.Promise = require("bluebird");
google.resultsPerPage = 1;
require('es6-shim');

var users;

var actions = {
    JOIN: "join",
    PART: "part",
    MSG: "msg",
    ERROR: "error",
    INFO: "info"
};

var bot = new irc.Client(config.server, config.botName, {
    channels: config.channels
});

bot.addListener("names" + config.channels[0], function(nicks) {
    //Object.keys because node-irc returns nicks as a large object with empty keys...
    users = Object.keys(nicks);

});

bot.addListener("join", function(channel, who) {
    log(actions.JOIN, who + " joined " + channel);
    if (who.indexOf("lounge-user") > -1) {
        bot.say(who, "Hey, " + who + ", now that you've figured out how to use The Lounge, feel free to change your nickname to something more personal using the /nick <new_nickname> command so we know who you are :)");
    }
});

bot.addListener("message", function(from, to, text) {
    var splitMessage = text.split(" ");
    var message = "";
    log(actions.MSG, "(" + to + ") " + from + ": " + text);

    //It's a command!
    if (splitMessage[0].startsWith(config.commandPrefix)) {
        if (splitMessage[0] === "!gh" || splitMessage[0] === "!github") {
            //just !gh
            var arg = splitMessage[1];
            if (splitMessage.length === 1) {
                message = format("https://github.com/%s/%s", config.githubUser, config.githubRepo);
                //<number> or repo info
            } else if (splitMessage.length === 2 && splitMessage[1] !== "search") {
                //It's a number, so it's an issue or a commit
                if (stringIsPositiveInteger(arg)) {
                    log(actions.INFO, config.githubUser);
                    message = getIssueInformation({
                        user: config.githubUser,
                        repo: config.githubRepo,
                        issue: arg
                    });
                    //It's a commit!
                } else if (arg.length === 7 && splitMessage.length === 2 && arg !== "search") {
                    log(actions.INFO, "Commit recognized");
                    message = getIssueInformation({
                        user: config.githubUser,
                        repo: config.githubRepo,
                        issue: arg
                    });
                } else {
                    var userRepo = parseUserRepoString(arg);
                    mesage = format("https://www.github.com/%s/%s", userRepo.user, userRepo.repo);
                }
            } else if (splitMessage.length === 3 && splitMessage[1] !== "search") {
                if (stringIsPositiveInteger(splitMessage[2])) {
                    message = getIssueInformation(withIssue(parseUserRepoString(splitMessage[1]), splitMessage[2]));
                } else if (splitMessage[2].length === 7) {
                    message = getIssueInformation(withIssue(parseUserRepoString(splitMessage[1]), splitMessage[2]));
                } else {
                    message = format("https://github.com/%s/%s", splitMessage[1], splitMessage[2]);
                }
            } else if (splitMessage.length === 4 && splitMessage[1] !== "search") {
                message = getIssueInformation({
                    user: splitMessage[1],
                    repo: splitMessage[2],
                    issue: splitMessage[3]
                });
            } else if (splitMessage.length > 2 && splitMessage[1] === "search") {
                //remov !gh search
                for(var i = 0; i < 2; i++) {
                    splitMessage.shift();
                }
                //The third string is a repo
                if(splitMessage[0].includes("/")) {
                    var userRepo = parseUserRepoString(splitMessage[0]);
                    //remove the user/repo
                    splitMessage.shift();
                    message = searchGithub({
                        repo: userRepo.repo,
                        user: userRepo.user,
                        terms: splitMessage
                    });
                } else {
                    message = searchGithub({
                        repo: config.githubRepo,
                        user: config.githubUser,
                        terms: splitMessage
                    });
                }
            }

            //Google command
        } else if ((splitMessage[0] === "!g" || splitMessage[0] === "!google") && splitMessage.length > 1) {
            //Remove the command from the message array
            var query = splitMessage.slice(1).join(' ');
            log(actions.INFO, query);
            google(query, function(err, next, links) {
                if (err) {
                    log(actions.ERROR, err);
                }
                bot.say(to, links[0].title + ' - ' + links[0].link);
            });
        }
        //Just a message
    } else {
        if (text.indexOf("#") > -1 && from.toLowerCase().indexOf("github") === -1) {
            var issues = text.match(/#([0-9]*)/g);
            if(issues) {
                issues.forEach(function (issue) {
                    console.log(issue);
                    var issueNumber = issue.substr(1);
                    message = getIssueInformation({
                        user: config.githubUser,
                        repo: config.githubRepo,
                        issue: issueNumber
                    });
                    message.then(function(m) {
                        return bot.say(to, m);
                    });
                });
                return;
            }
        }
    }
    log(actions.INFO, message);
    if (message !== "") {
        if (message.then) {
            message.then(function(m) {
                return bot.say(to, m);
            });
        } else {
            bot.say(to, message);
        }
    }
});

/** Utils **/

var log = function(action, message) {
    var message = "[" + action.toUpperCase() + "]" + " " + message;
    console.log(message);
    fs.open('log.txt', 'a', 666, function(e, id) {
        fs.write(id, message + "\n", null, 'utf8', function() {
            fs.close(id);
        });
    });
}

var userInChannel = function(user) {
    if (users.indexOf(user.toString().toLowerCase()) > -1) {
        return true;
    } else {
        return false;
    }
}

var arrayObjectIndexOf = function(array, searchTerm, property) {
    for (var i = 0, len = array.length; i < len; i++) {
        if (array[i][property] === searchTerm) {
            return i;
        }
    }
    return -1;
}

/** Github **/
//Credits to tennu-github for parts of the following.
var getIssueInformation = function(options) {
    var user = options.user;
    var repo = options.repo;
    var issueNumber = options.issue;
    console.log(issueNumber);
    if (stringIsPositiveInteger(issueNumber.toString())) {
        var result = format("https://api.github.com/repos/%s/%s/issues/%s", user, repo, issueNumber);
        return fetch(result).then(function(res) {
            log(actions.INFO, result);
            return res.json();
        }).then(function(res) {
            if (res.message === "Not Found") {
                return "";
            }

            var type = res.pull_request === undefined ? "Issue" : "PR";
            var status = res.state;
            var title = res.title;
            var link = res.html_url;

            if (type === "PR" && status === "closed") {
                return fetch(res.pull_request.url).then(function(res) {
                    return res.json();
                }).then(function(res) {
                    if (res.message === "Not Found") {
                        return "";
                    }

                    var type = "PR";
                    var status = res.merged_at === null ? "closed" : "merged";
                    var title = res.title;
                    var link = res.html_url;

                    return format("[%s %s] (%s) %s (%s)", type, issueNumber, status, title, link);
                });
            } else {
                return format("[%s %s] (%s) %s (%s)", type, issueNumber, status, title, link);
            }
        });
} else {
    var result = format("https://api.github.com/repos/%s/%s/commits/%s", user, repo, issueNumber);
    return fetch(result).then(function(res) {
        log(actions.INFO, result);
        return res.json();
    }).then(function(res) {
        if (res.message === "Not Found") {
            return "";
        }

        var type = "Commit";
        var author = res.commit.author.name;
        var message = res.commit.message;
        var link = res.html_url;
        var add = res.stats.additions;
        var del = res.stats.deletions;

        return format("[%s %s] - %s (add: %s, del: %s) - %s", type, author, message, add, del, link);
    });
}
}

var searchGithub = function(options) {
    var repo = options.repo;
    var user = options.user;
    var search = options.terms;
    var result = format("https://api.github.com/search/issues?q=repo:%s/%s+%s", user, repo, search.join("+"));
    return fetch(result).then(function(res) {
        log(actions.INFO, result);
        return res.json();
    }).then(function(res) {
        if(res.items[0].state) {
            var status = res.items[0].state;
        } else {
            return "No issue found";
        }
        var title = res.items[0].title;
        var link = res.items[0].html_url;
        var issueNumber = res.items[0].number;
        return format("[%s %s] (%s) %s (%s)", "Issue", issueNumber, status, title, link);
    });
}

var withIssue = function(obj, issueNumber) {
    obj.issue = issueNumber;
    return obj;
}

var stringIsPositiveInteger = function(string) {
    var number = Number(string);

    if (isNaN(number)) {
        return false;
    }

    if (number === Infinity) {
        return false;
    }

    if (number < 1) {
        return false;
    }

    if (Math.floor(number) !== number) {
        return false;
    }

    return true;
}

var parseUserRepoString = function(userRepoString) {
    if (userRepoString.length === 0) {
        return {
            user: config.githubUser,
            repo: config.githubRepo
        };
    }

    if (userRepoString.indexOf("/") === -1) {
        return {
            user: config.githubUser,
            repo: config.userRepoString
        };
    }

    var split = userRepoString.split("/");
    var user = split[0].length !== 0 ? split[0] : config.githubUser;
    var repo = split[1].length !== 0 ? split[1] : config.githubRepo;

    return {
        user: user,
        repo: repo
    };
}<commit_msg>Updates comments<commit_after>var irc = require("irc");
var fs = require("fs");
var config = require("./config.js");
var jsonfile = require("jsonfile");
var format = require("util").format;
var inspect = require("util").inspect;
var google = require('google');
var fetch = require("node-fetch");
fetch.Promise = require("bluebird");
google.resultsPerPage = 1;
require('es6-shim');

var users;

//For logging, not really currently useful.
var actions = {
    JOIN: "join",
    PART: "part",
    MSG: "msg",
    ERROR: "error",
    INFO: "info"
};

//see: config.js
var bot = new irc.Client(config.server, config.botName, {
    channels: config.channels
});

//The 'names' event is sent whenever a user connects, disconnects, or renames themselves. Used for Karma mainly, which isn't currently implemented, but a helpful thing to have
bot.addListener("names" + config.channels[0], function(nicks) {
    //Object.keys because node-irc returns nicks as a large object with empty keys...
    users = Object.keys(nicks);

});
//Whenver a user joins, let's check their user name. Once TheLounge supports in-line notices, this should probably be changed to bot.notice()
bot.addListener("join", function(channel, who) {
    log(actions.JOIN, who + " joined " + channel);
    //TheLounge's default username
    if (who.indexOf("lounge-user") > -1) {
        bot.say(who, "Hey, " + who + ", now that you've figured out how to use The Lounge, feel free to change your nickname to something more personal using the /nick <new_nickname> command so we know who you are :)");
    }
});

bot.addListener("message", function(from, to, text) {
    //Let's turn the message into an array, easier to work with its arguments.
    var splitMessage = text.split(" ");
    var message = "";
    log(actions.MSG, "(" + to + ") " + from + ": " + text);

    //It's a command! startsWith() is provided by es6-shim.
    if (splitMessage[0].startsWith(config.commandPrefix)) {
        if (splitMessage[0] === "!gh" || splitMessage[0] === "!github") {
            var arg = splitMessage[1];
            //just !gh
            if (splitMessage.length === 1) {
                message = format("https://github.com/%s/%s", config.githubUser, config.githubRepo);
                //<number> or repo info
            } else if (splitMessage.length === 2 && splitMessage[1] !== "search") {
                //It's a number, so it's an issue or a commit
                if (stringIsPositiveInteger(arg)) {
                    log(actions.INFO, config.githubUser);
                    message = getIssueInformation({
                        user: config.githubUser,
                        repo: config.githubRepo,
                        issue: arg
                    });
                    //It's a commit!
                } else if (arg.length === 7 && splitMessage.length === 2 && arg !== "search") {
                    log(actions.INFO, "Commit recognized");
                    message = getIssueInformation({
                        user: config.githubUser,
                        repo: config.githubRepo,
                        issue: arg
                    });
                } else {
                    //!gh user/repo
                    var userRepo = parseUserRepoString(arg);
                    mesage = format("https://www.github.com/%s/%s", userRepo.user, userRepo.repo);
                }
                //!gh search
            } else if (splitMessage.length === 3 && splitMessage[1] !== "search") {
                //search is either an issue or PR
                if (stringIsPositiveInteger(splitMessage[2])) {
                    message = getIssueInformation(withIssue(parseUserRepoString(splitMessage[1]), splitMessage[2]));
                } else if (splitMessage[2].length === 7) {
                    //Temporary hack to see if it's a commit
                    message = getIssueInformation(withIssue(parseUserRepoString(splitMessage[1]), splitMessage[2]));
                } else {
                    message = format("https://github.com/%s/%s", splitMessage[1], splitMessage[2]);
                }
            } else if (splitMessage.length === 4 && splitMessage[1] !== "search") {
                message = getIssueInformation({
                    user: splitMessage[1],
                    repo: splitMessage[2],
                    issue: splitMessage[3]
                });
            } else if (splitMessage.length > 2 && splitMessage[1] === "search") {
                //remove '!gh search'
                for(var i = 0; i < 2; i++) {
                    splitMessage.shift();
                }
                //The third string is a repo
                if(splitMessage[0].includes("/")) {
                    var userRepo = parseUserRepoString(splitMessage[0]);
                    //remove the user/repo
                    splitMessage.shift();
                    message = searchGithub({
                        repo: userRepo.repo,
                        user: userRepo.user,
                        terms: splitMessage
                    });
                } else {
                    message = searchGithub({
                        repo: config.githubRepo,
                        user: config.githubUser,
                        terms: splitMessage
                    });
                }
            }

            //Google command
        } else if ((splitMessage[0] === "!g" || splitMessage[0] === "!google") && splitMessage.length > 1) {
            //Remove the command from the message array
            var query = splitMessage.slice(1).join(' ');
            log(actions.INFO, query);
            google(query, function(err, next, links) {
                if (err) {
                    log(actions.ERROR, err);
                }
                bot.say(to, links[0].title + ' - ' + links[0].link);
            });
        }
        //Just a message
    } else {
        if (text.indexOf("#") > -1 && from.toLowerCase().indexOf("github") === -1) {
            //this regex (thanks @Max-P and ##regex) checks if there's a free-floating # followed by an infinite amount of characters between 0 and 9.
            var issues = text.match(/#([0-9]*)/g);
            if(issues) {
                issues.forEach(function (issue) {
                    console.log(issue);
                    var issueNumber = issue.substr(1);
                    message = getIssueInformation({
                        user: config.githubUser,
                        repo: config.githubRepo,
                        issue: issueNumber
                    });
                    message.then(function(m) {
                        return bot.say(to, m);
                    });
                });
                return;
            }
        }
    }
    log(actions.INFO, message);
    if (message !== "") {
        //If it's returned as a Promise
        if (message.then) {
            message.then(function(m) {
                return bot.say(to, m);
            });
        } else {
            bot.say(to, message);
        }
    }
});

/** Utils **/
var log = function(action, message) {
    var message = "[" + action.toUpperCase() + "]" + " " + message;
    console.log(message);
    fs.open('log.txt', 'a', 666, function(e, id) {
        fs.write(id, message + "\n", null, 'utf8', function() {
            fs.close(id);
        });
    });
}

var userInChannel = function(user) {
    if (users.indexOf(user.toString().toLowerCase()) > -1) {
        return true;
    } else {
        return false;
    }
}

var arrayObjectIndexOf = function(array, searchTerm, property) {
    for (var i = 0, len = array.length; i < len; i++) {
        if (array[i][property] === searchTerm) {
            return i;
        }
    }
    return -1;
}

/** Github **/
//Credits to tennu-github for parts of the following.
var getIssueInformation = function(options) {
    var user = options.user;
    var repo = options.repo;
    var issueNumber = options.issue;
    console.log(issueNumber);
    if (stringIsPositiveInteger(issueNumber.toString())) {
        var result = format("https://api.github.com/repos/%s/%s/issues/%s", user, repo, issueNumber);
        return fetch(result).then(function(res) {
            log(actions.INFO, result);
            return res.json();
        }).then(function(res) {
            if (res.message === "Not Found") {
                return "";
            }

            var type = res.pull_request === undefined ? "Issue" : "PR";
            var status = res.state;
            var title = res.title;
            var link = res.html_url;

            if (type === "PR" && status === "closed") {
                return fetch(res.pull_request.url).then(function(res) {
                    return res.json();
                }).then(function(res) {
                    if (res.message === "Not Found") {
                        return "";
                    }

                    var type = "PR";
                    var status = res.merged_at === null ? "closed" : "merged";
                    var title = res.title;
                    var link = res.html_url;

                    return format("[%s %s] (%s) %s (%s)", type, issueNumber, status, title, link);
                });
            } else {
                return format("[%s %s] (%s) %s (%s)", type, issueNumber, status, title, link);
            }
        });
} else {
    var result = format("https://api.github.com/repos/%s/%s/commits/%s", user, repo, issueNumber);
    return fetch(result).then(function(res) {
        log(actions.INFO, result);
        return res.json();
    }).then(function(res) {
        if (res.message === "Not Found") {
            return "";
        }

        var type = "Commit";
        var author = res.commit.author.name;
        var message = res.commit.message;
        var link = res.html_url;
        var add = res.stats.additions;
        var del = res.stats.deletions;

        return format("[%s %s] - %s (add: %s, del: %s) - %s", type, author, message, add, del, link);
    });
}
}

var searchGithub = function(options) {
    var repo = options.repo;
    var user = options.user;
    var search = options.terms;
    var result = format("https://api.github.com/search/issues?q=repo:%s/%s+%s", user, repo, search.join("+"));
    return fetch(result).then(function(res) {
        log(actions.INFO, result);
        return res.json();
    }).then(function(res) {
        if(res.items[0].state) {
            var status = res.items[0].state;
        } else {
            return "No issue found";
        }
        var title = res.items[0].title;
        var link = res.items[0].html_url;
        var issueNumber = res.items[0].number;
        return format("[%s %s] (%s) %s (%s)", "Issue", issueNumber, status, title, link);
    });
}

var withIssue = function(obj, issueNumber) {
    obj.issue = issueNumber;
    return obj;
}

var stringIsPositiveInteger = function(string) {
    var number = Number(string);

    if (isNaN(number)) {
        return false;
    }

    if (number === Infinity) {
        return false;
    }

    if (number < 1) {
        return false;
    }

    if (Math.floor(number) !== number) {
        return false;
    }

    return true;
}

var parseUserRepoString = function(userRepoString) {
    if (userRepoString.length === 0) {
        return {
            user: config.githubUser,
            repo: config.githubRepo
        };
    }

    if (userRepoString.indexOf("/") === -1) {
        return {
            user: config.githubUser,
            repo: config.userRepoString
        };
    }

    var split = userRepoString.split("/");
    var user = split[0].length !== 0 ? split[0] : config.githubUser;
    var repo = split[1].length !== 0 ? split[1] : config.githubRepo;

    return {
        user: user,
        repo: repo
    };
}
<|endoftext|>


<commit_before>var express = require("express");
var app = express();

app.get('/', function(req, res) {
	res.send("Hello World!");
});

var server = app.listen(3000, function() {
	var host = server.address().address;
	var port = server.address().port;

	console.log("Example app listening at http://%s:%s", host, port);
});
<commit_msg>set up request<commit_after>var express = require("express");
var app = express();
var fs = require("fs");
var request = require("request");

var key = '';
var server;

app.get('/', function(req, res) {
	var url = "http://academic.research.microsoft.com/json.svc/";

	request(url, function (error, response, body) {
		if (!error && response.statusCode == 200) {
			res.send(body);
		}
	});
});

fs.readFile("API key.txt", "utf8" , function(err, data) {
	key = data;
	console.log(key);

	server = app.listen(3000, function() {
		var host = server.address().address;
		var port = server.address().port;

		console.log("Example app listening at http://%s:%s", host, port);
	});
});
<|endoftext|>


<commit_before>var cool = require('cool-ascii-faces');
var express = require('express');
var app = express();

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.send('Hello There!');
});

app.get('/cool', function(request, response) {
  response.send(cool());
});

app.post('/commands', function(request, response) {
  console.log(request);
	var commands = request.body;

	if(commands.indexOf('I challenge') > -1) {
		response.send(buildResponse("Rock, Paper, Scissors, SHOOT!"));
		console.log(request.body.text.toLowerCase());
	} else if (commands.indexOf('rock') > -1) {
		response.send(buildResponse('You chose rock'));
	} else if (commands.indexOf('paper') > -1) {
		response.send(buildResponse('You chose paper'));
	} else if (commands.indexOf('scissors') > -1) {
		response.send(buildResponse('You chose scissors'));
	}
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

/*
* Helper function to build the JSON to send back to Slack.
*/
function buildResponse(text) {
  var json = {
    "text": text,
  }
  return JSON.stringify(json);
}

/*
* Helper function to match commands, instead of a switch statement,
* because then you can do stuff like use Regex here or something fancier.
* Also keeps all the possible commands and their trigger words in one place.
*/
function matchCommands(commandArray, command) {
  var commandsDict = {
    "CHALLENGE": "I challenge",
    "PAPER": "paper",
    "ROCK" : "rock",
    "SCISSORS" : "scissors"
  }
  var cmdString = commandArray.join(" ").toLowerCase().replace("rps ", "");
  return cmdString.indexOf(commandsDict[command]) === 0;
}<commit_msg>More console logs<commit_after>var express = require('express'),
  slack = require('slack-client'),
  bodyParser = require('body-parser'),
  redis = require('redis.js'),
  app = express();

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.send('Hello There!');
});

app.get('/cool', function(request, response) {
  response.send(cool());
});

app.post('/commands', function(request, response) {
  console.log(request);
	var commands = request.body;
  console.log(commands);

	if(commands.indexOf('I challenge') > -1) {
		response.send(buildResponse("Rock, Paper, Scissors, SHOOT!"));
		console.log(request.body.text.toLowerCase());
	} else if (commands.indexOf('rock') > -1) {
		response.send(buildResponse('You chose rock'));
	} else if (commands.indexOf('paper') > -1) {
		response.send(buildResponse('You chose paper'));
	} else if (commands.indexOf('scissors') > -1) {
		response.send(buildResponse('You chose scissors'));
	}
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

/*
* Helper function to build the JSON to send back to Slack.
*/
function buildResponse(text) {
  var json = {
    "text": text,
  }
  return JSON.stringify(json);
}

/*
* Helper function to match commands, instead of a switch statement,
* because then you can do stuff like use Regex here or something fancier.
* Also keeps all the possible commands and their trigger words in one place.
*/
function matchCommands(commandArray, command) {
  var commandsDict = {
    "CHALLENGE": "I challenge",
    "PAPER": "paper",
    "ROCK" : "rock",
    "SCISSORS" : "scissors"
  }
  var cmdString = commandArray.join(" ").toLowerCase().replace("rps ", "");
  return cmdString.indexOf(commandsDict[command]) === 0;
}<|endoftext|>


<commit_before>// Node.js core modules
var crypto = require('crypto');

/**
 * The encryption algorithm (cipher) type to be used.
 * @type {String}
 * @const
 * @private
 */
var CIPHER_ALGORITHM = 'aes-256-ctr';

//
// Primary API
//

/**
 * An API to allow for greatly simplified AES-256 encryption and decryption using a passphrase of
 * any length plus a random Initialization Vector.
 * @exports aes256
 * @public
 */
var aes256 = {

  /**
   * Encrypt a clear-text message using AES-256 plus a random Initialization Vector.
   * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
   * @param {String} plaintext  The clear-text message to be encrypted.
   * @returns {String} A custom-encrypted version of the input.
   * @public
   * @method
   */
  encrypt: function(key, plaintext) {
    if (typeof key !== 'string' || !key) {
      throw new TypeError('Provided "key" must be a non-empty string');
    }
    if (typeof plaintext !== 'string' || !plaintext) {
      throw new TypeError('Provided "plaintext" must be a non-empty string');
    }

    var sha256 = crypto.createHash('sha256');
    sha256.update(key);

    // Initialization Vector
    var iv = crypto.randomBytes(16);
    var cipher = crypto.createCipheriv(CIPHER_ALGORITHM, sha256.digest(), iv);

    var ciphertext = cipher.update(new Buffer(plaintext));
    var encrypted = Buffer.concat([iv, ciphertext, cipher.final()]).toString('base64');

    return encrypted;
  },

  /**
   * Decrypt an encrypted message back to clear-text using AES-256 plus a random Initialization Vector.
   * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
   * @param {String} encrypted  The encrypted message to be decrypted.
   * @returns {String} The original plain-text message.
   * @public
   * @method
   */
  decrypt: function(key, encrypted) {
    if (typeof key !== 'string' || !key) {
      throw new TypeError('Provided "key" must be a non-empty string');
    }
    if (typeof encrypted !== 'string' || !encrypted || encrypted.length < 17) {
      throw new TypeError('Provided "encrypted" must be a non-empty string');
    }

    var sha256 = crypto.createHash('sha256');
    sha256.update(key);

    var input = new Buffer(encrypted, 'base64');

    // Initialization Vector
    var iv = input.slice(0, 16);
    var decipher = crypto.createDecipheriv(CIPHER_ALGORITHM, sha256.digest(), iv);

    var ciphertext = input.slice(16);
    var plaintext = decipher.update(ciphertext) + decipher.final();

    return plaintext;
  }

};

/**
 * Create a symmetric cipher with a given passphrase to then encrypt/decrypt data symmetrically.
 * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
 * @protected
 * @constructor
 */
function AesCipher(key) {
  if (typeof key !== 'string' || !key) {
    throw new TypeError('Provided "key" must be a non-empty string');
  }

  /**
   * A passphrase of any length to used to generate a symmetric session key.
   * @member {String} key
   * @readonly
   */
  Object.defineProperty(this, 'key', { value: key });

}

/**
 * Encrypt a clear-text message using AES-256 plus a random Initialization Vector.
 * @param {String} plaintext  The clear-text message to be encrypted.
 * @returns {String} A custom-encrypted version of the input.
 * @protected
 * @method
 */
AesCipher.prototype.encrypt = function(plaintext) {
  return aes256.encrypt(this.key, plaintext);
};

/**
 * Decrypt an encrypted message back to clear-text using AES-256 plus a random Initialization Vector.
 * @param {String} encrypted  The encrypted message to be decrypted.
 * @returns {String} The original plain-text message.
 * @protected
 * @method
 */
AesCipher.prototype.decrypt = function(encrypted) {
  return aes256.decrypt(this.key, encrypted);
};

//
// API Extension
//

/**
 * Create a symmetric cipher with a given passphrase to then encrypt/decrypt data symmetrically.
 * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
 * @returns {AesCipher}
 * @public
 * @method
 */
aes256.createCipher = function(key) {
  return new AesCipher(key);
};

//
// Export the API
//

module.exports = aes256;
<commit_msg>Defer input length check to after base64 decoding<commit_after>// Node.js core modules
var crypto = require('crypto');

/**
 * The encryption algorithm (cipher) type to be used.
 * @type {String}
 * @const
 * @private
 */
var CIPHER_ALGORITHM = 'aes-256-ctr';

//
// Primary API
//

/**
 * An API to allow for greatly simplified AES-256 encryption and decryption using a passphrase of
 * any length plus a random Initialization Vector.
 * @exports aes256
 * @public
 */
var aes256 = {

  /**
   * Encrypt a clear-text message using AES-256 plus a random Initialization Vector.
   * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
   * @param {String} plaintext  The clear-text message to be encrypted.
   * @returns {String} A custom-encrypted version of the input.
   * @public
   * @method
   */
  encrypt: function(key, plaintext) {
    if (typeof key !== 'string' || !key) {
      throw new TypeError('Provided "key" must be a non-empty string');
    }
    if (typeof plaintext !== 'string' || !plaintext) {
      throw new TypeError('Provided "plaintext" must be a non-empty string');
    }

    var sha256 = crypto.createHash('sha256');
    sha256.update(key);

    // Initialization Vector
    var iv = crypto.randomBytes(16);
    var cipher = crypto.createCipheriv(CIPHER_ALGORITHM, sha256.digest(), iv);

    var ciphertext = cipher.update(new Buffer(plaintext));
    var encrypted = Buffer.concat([iv, ciphertext, cipher.final()]).toString('base64');

    return encrypted;
  },

  /**
   * Decrypt an encrypted message back to clear-text using AES-256 plus a random Initialization Vector.
   * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
   * @param {String} encrypted  The encrypted message to be decrypted.
   * @returns {String} The original plain-text message.
   * @public
   * @method
   */
  decrypt: function(key, encrypted) {
    if (typeof key !== 'string' || !key) {
      throw new TypeError('Provided "key" must be a non-empty string');
    }
    if (typeof encrypted !== 'string' || !encrypted) {
      throw new TypeError('Provided "encrypted" must be a non-empty string');
    }

    var sha256 = crypto.createHash('sha256');
    sha256.update(key);

    var input = new Buffer(encrypted, 'base64');

    if (input.length < 17) {
      throw new TypeError('Provided "encrypted" must decrypt to a non-empty string');
    }

    // Initialization Vector
    var iv = input.slice(0, 16);
    var decipher = crypto.createDecipheriv(CIPHER_ALGORITHM, sha256.digest(), iv);

    var ciphertext = input.slice(16);
    var plaintext = decipher.update(ciphertext) + decipher.final();

    return plaintext;
  }

};

/**
 * Create a symmetric cipher with a given passphrase to then encrypt/decrypt data symmetrically.
 * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
 * @protected
 * @constructor
 */
function AesCipher(key) {
  if (typeof key !== 'string' || !key) {
    throw new TypeError('Provided "key" must be a non-empty string');
  }

  /**
   * A passphrase of any length to used to generate a symmetric session key.
   * @member {String} key
   * @readonly
   */
  Object.defineProperty(this, 'key', { value: key });

}

/**
 * Encrypt a clear-text message using AES-256 plus a random Initialization Vector.
 * @param {String} plaintext  The clear-text message to be encrypted.
 * @returns {String} A custom-encrypted version of the input.
 * @protected
 * @method
 */
AesCipher.prototype.encrypt = function(plaintext) {
  return aes256.encrypt(this.key, plaintext);
};

/**
 * Decrypt an encrypted message back to clear-text using AES-256 plus a random Initialization Vector.
 * @param {String} encrypted  The encrypted message to be decrypted.
 * @returns {String} The original plain-text message.
 * @protected
 * @method
 */
AesCipher.prototype.decrypt = function(encrypted) {
  return aes256.decrypt(this.key, encrypted);
};

//
// API Extension
//

/**
 * Create a symmetric cipher with a given passphrase to then encrypt/decrypt data symmetrically.
 * @param {String} key  A passphrase of any length to used to generate a symmetric session key.
 * @returns {AesCipher}
 * @public
 * @method
 */
aes256.createCipher = function(key) {
  return new AesCipher(key);
};

//
// Export the API
//

module.exports = aes256;
<|endoftext|>


<commit_before>var path = require('path');
var merge = require('merge');
var compileCompass = require('broccoli-compass-compiler');
var checker = require('ember-cli-version-checker');
var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
var glob = require('glob');

function CompassCompilerPlugin(optionsFn) {
  this.name = 'ember-cli-compass-compiler';
  this.ext = ['scss', 'sass'];
  this.optionsFn = optionsFn;
}

CompassCompilerPlugin.prototype.toTree = function(tree, inputPath, outputPath, inputOptions) {

  var removeLeadingSlash = function(string) {
    return (string.length > 0 && string[0] === '/') ? string.slice(1) : string;
  };

  // Normalize paths
  inputPath = removeLeadingSlash(inputPath);
  outputPath = removeLeadingSlash(outputPath);

  // Merge default options with supplied options
  var defaultOptions = {
    outputStyle: 'compressed',
    compassCommand: 'compass',
    importPath: [],
    getCssDir: function(outputDir) {
      return '"' + path.join(outputDir, outputPath) + '"';
    }
  };
  var compassOptions = merge({}, defaultOptions, this.optionsFn(), inputOptions);

  var outputPaths = compassOptions.outputPaths;
  var trees = Object.keys(outputPaths).map(function(file) {

    // Watch inputTrees and compassOptions.importPath directories
    var inputTrees = [tree];

    // Define getSassDir function if not overridden by user
    if (!compassOptions.getSassDir) {
      compassOptions.getSassDir = function(inputTrees, inputPaths) {
        return inputPaths.reduce(function(pathFound, currentPath) {
          // Return early if input path is already found
          if (pathFound !== '') {
            return pathFound;
          }

          // Filter pattern for .sass or .scss files
          var pattern = path.join(currentPath, inputPath, file + '.s@(a|c)ss');
          var result = glob.sync(pattern);

          // Found the file we're looking for
          if (result.length > 0) {
            return path.dirname(result[0]);
          }

          return '';
        }, '');
      };
    }

    // Compile
    var compassTree = compileCompass(inputTrees, compassOptions);

    // Rename and move files
    var extension = path.extname(outputPaths[file]); // compiled asset extension
    var fileName = file + extension; // current file name
    var outputFileName = path.basename(outputPaths[file], extension) + extension; // new name for asset
    var outputDir = removeLeadingSlash(path.dirname(outputPaths[file])); // new directory for asset
    var node = new Funnel(compassTree, {
      srcDir: outputPath,
      destDir: outputDir,
      files: [fileName],
      getDestinationPath: function(relativePath) {
        if (relativePath === path.join(this.destDir, fileName)) {
          return path.join(this.destDir, outputFileName);
        }

        return relativePath;
      }
    });
    return node;
  });

  return mergeTrees(trees);
};

module.exports = {
  name: 'Ember CLI Compass Compiler',

  shouldSetupRegistryInIncluded: function() {
    return !checker.isAbove(this, '0.2.0');
  },

  setupPreprocessorRegistry: function(type, registry) {
    registry.add('css', new CompassCompilerPlugin(this.compassOptions.bind(this)));

    // prevent conflict with broccoli-sass if it's installed
    if (registry.remove) registry.remove('css', 'broccoli-sass');
  },

  included: function(app) {
    this.app = app;
    this._super.included.apply(this, arguments);

    if (this.shouldSetupRegistryInIncluded()) {
      this.setupPreprocessorRegistry('parent', app.registry);
    }
  },

  compassOptions: function () {
    var app = this.app;

    if (app && !app.options && app.app) {
      app = app.app;
    }

    return (app && app.options && app.options.compassOptions) || {};
  }
};
<commit_msg>Enable file renaming in Windows environments. Funnel only understands posix paths<commit_after>var path = require('path');
var merge = require('merge');
var compileCompass = require('broccoli-compass-compiler');
var checker = require('ember-cli-version-checker');
var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
var glob = require('glob');

function CompassCompilerPlugin(optionsFn) {
  this.name = 'ember-cli-compass-compiler';
  this.ext = ['scss', 'sass'];
  this.optionsFn = optionsFn;
}

CompassCompilerPlugin.prototype.toTree = function(tree, inputPath, outputPath, inputOptions) {

  var removeLeadingSlash = function(string) {
    return (string.length > 0 && string[0] === '/') ? string.slice(1) : string;
  };

  // Normalize paths
  inputPath = removeLeadingSlash(inputPath);
  outputPath = removeLeadingSlash(outputPath);

  // Merge default options with supplied options
  var defaultOptions = {
    outputStyle: 'compressed',
    compassCommand: 'compass',
    importPath: [],
    getCssDir: function(outputDir) {
      return '"' + path.join(outputDir, outputPath) + '"';
    }
  };
  var compassOptions = merge({}, defaultOptions, this.optionsFn(), inputOptions);

  var outputPaths = compassOptions.outputPaths;
  var trees = Object.keys(outputPaths).map(function(file) {

    // Watch inputTrees and compassOptions.importPath directories
    var inputTrees = [tree];

    // Define getSassDir function if not overridden by user
    if (!compassOptions.getSassDir) {
      compassOptions.getSassDir = function(inputTrees, inputPaths) {
        return inputPaths.reduce(function(pathFound, currentPath) {
          // Return early if input path is already found
          if (pathFound !== '') {
            return pathFound;
          }

          // Filter pattern for .sass or .scss files
          var pattern = path.join(currentPath, inputPath, file + '.s@(a|c)ss');
          var result = glob.sync(pattern);

          // Found the file we're looking for
          if (result.length > 0) {
            return path.dirname(result[0]);
          }

          return '';
        }, '');
      };
    }

    // Compile
    var compassTree = compileCompass(inputTrees, compassOptions);

    // Rename and move files
    var extension = path.extname(outputPaths[file]); // compiled asset extension
    var fileName = file + extension; // current file name
    var outputFileName = path.basename(outputPaths[file], extension) + extension; // new name for asset
    var outputDir = removeLeadingSlash(path.dirname(outputPaths[file])); // new directory for asset
    var node = new Funnel(compassTree, {
      srcDir: outputPath,
      destDir: outputDir,
      files: [fileName],
      getDestinationPath: function(relativePath) {
        if (relativePath === path.posix.join(this.destDir, fileName)) {
          return path.posix.join(this.destDir, outputFileName);
        }

        return relativePath;
      }
    });
    return node;
  });

  return mergeTrees(trees);
};

module.exports = {
  name: 'Ember CLI Compass Compiler',

  shouldSetupRegistryInIncluded: function() {
    return !checker.isAbove(this, '0.2.0');
  },

  setupPreprocessorRegistry: function(type, registry) {
    registry.add('css', new CompassCompilerPlugin(this.compassOptions.bind(this)));

    // prevent conflict with broccoli-sass if it's installed
    if (registry.remove) registry.remove('css', 'broccoli-sass');
  },

  included: function(app) {
    this.app = app;
    this._super.included.apply(this, arguments);

    if (this.shouldSetupRegistryInIncluded()) {
      this.setupPreprocessorRegistry('parent', app.registry);
    }
  },

  compassOptions: function () {
    var app = this.app;

    if (app && !app.options && app.app) {
      app = app.app;
    }

    return (app && app.options && app.options.compassOptions) || {};
  }
};
<|endoftext|>


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

'use strict';

var program = require('commander')
  , gulp    = require('gulp')
  , chalk   = require('chalk')
  , exec    = require('exec')
  , pjson   = require('./package.json')

var strings = {
  create: 'Creating new project',
  install: 'Installing dependencies',
  complete: 'Done!'
}

function notify(message) {
  if (!program.quiet)
    console.log(chalk.green(message))
}

function installDependencies(name) {
  notify(strings.install)

  exec('cd ' + name + ' && npm install', function () {
    notify(strings.complete)
  })
}

function newProject(name) {
  notify(strings.create, name)

  gulp.src([__dirname + '/assets/**/*', __dirname + '/assets/.*'])
    .pipe(gulp.dest(process.cwd() + '/' + name))
    .on('end', installDependencies.bind(this, name))
}

program
  .version(pjson.version)
  .option('-q, --quiet', 'Hide logging information')

program
  .command('new <name>')
  .description('scaffold out a new app in the current directory')
  .action(newProject)

program.parse(process.argv);
<commit_msg>Move path strings into a config object<commit_after>#!/usr/bin/env node

'use strict';

var program = require('commander')
  , gulp    = require('gulp')
  , chalk   = require('chalk')
  , exec    = require('exec')
  , pjson   = require('./package.json')

var strings = {
  create: 'Creating new project',
  install: 'Installing dependencies',
  complete: 'Done!'
}

var paths = {
  basefiles: __dirname + '/assets/**/*',
  dotfiles: __dirname + '/assets/.*'
}

function notify(message) {
  if (!program.quiet)
    console.log(chalk.green(message))
}

function installDependencies(name) {
  notify(strings.install)

  exec('cd ' + name + ' && npm install', function () {
    notify(strings.complete)
  })
}

function newProject(name) {
  notify(strings.create, name)

  gulp.src([paths.basefiles, paths.dotfiles])
    .pipe(gulp.dest(process.cwd() + '/' + name))
    .on('end', installDependencies.bind(this, name))
}

program
  .version(pjson.version)
  .option('-q, --quiet', 'Hide logging information')

program
  .command('new <name>')
  .description('scaffold out a new app in the current directory')
  .action(newProject)

program.parse(process.argv);
<|endoftext|>


<commit_before>// var express = require('express');
// var app = express();

// app.set('port', (process.env.PORT || 5000));

// app.use(express.static(__dirname + '../build'));

// // views is directory for all template files
// app.set('views', __dirname + '../build');
// app.set('view engine', 'html');

// app.get('/', function(request, response) {
//   response.render('index.html');
// });

// app.listen(app.get('port'), function() {
//   console.log('Node app is running on port', app.get('port'));
// });

'use strict';

var config  = require('../config');
var http    = require('http');
var express = require('express');
var gulp    = require('gulp');
var gutil   = require('gulp-util');
var morgan  = require('morgan');

  var server = express();

  // log all requests to the console
  server.use(morgan('dev'));
  server.use(express.static(config.dist.root));

  // Serve index.html for all routes to leave routing up to Angular
  server.all('/*', function(req, res) {
      res.sendFile('index.html', { root: 'build' });
  });

  // Start webserver if not already running
  var s = http.createServer(server);
  s.on('error', function(err){
    if(err.code === 'EADDRINUSE'){
      gutil.log('Development server is already started at port ' + config.serverport);
    }
    else {
      throw err;
    }
  });

  s.listen(config.serverport);<commit_msg>fixed server<commit_after>// var express = require('express');
// var app = express();

// app.set('port', (process.env.PORT || 5000));

// app.use(express.static(__dirname + '../build'));

// // views is directory for all template files
// app.set('views', __dirname + '../build');
// app.set('view engine', 'html');

// app.get('/', function(request, response) {
//   response.render('index.html');
// });

// app.listen(app.get('port'), function() {
//   console.log('Node app is running on port', app.get('port'));
// });
	var express = require('express');
	var http    = require('http');
  var server = express();

  server.set('port', (process.env.PORT || 5000));

  // log all requests to the console
  server.use(express.static('build'));

  // Serve index.html for all routes to leave routing up to Angular
  server.all('/*', function(req, res) {
      res.sendFile('index.html', { root: 'build' });
  });

  // Start webserver if not already running
  var s = http.createServer(server);
  s.on('error', function(err){
    if(err.code === 'EADDRINUSE'){
      gutil.log('Development server is already started at port ' + server.get('port'));
    }
    else {
      throw err;
    }
  });

  s.listen(server.get('port'), function() {
  	console.log('Node app is running on port', server.get('port'));
	});<|endoftext|>


<commit_before>/* jshint asi: true, expr: true, globalstrict: true */
/* global require, Buffer, process, module */
"use strict";

var fs              = require('fs'),
    path            = require('path'),
    rng             = require('crypto').rng,
    _               = require('underscore'),
    q               = require('kew'),
    through         = require('through'),
    nodeResolve     = require('resolve'),
    browserResolve  = require('browser-resolve'),
    detective       = require('detective'),
    aggregate       = require('stream-aggregate-promise'),
    asStream        = require('as-stream')

function resolveWith(resolve, id, parent) {
  var p = q.defer()
  resolve(id, parent, function(err, filename, pkg) {
    err ? p.reject(err) : p.resolve({id: id, filename: filename, package: pkg})
  })
  return p
}

module.exports = function(mains, opts) {
  opts = opts || {}

  var output = through(),
      basedir = opts.basedir || process.cwd(),
      resolve = resolveWith.bind(null, opts.resolve || browserResolve),
      transformResolve = resolveWith.bind(null, nodeResolve),
      cache = {},
      seen = {},
      entries = [].concat(mains).filter(Boolean).map(makeModule)

  q.all(entries.map(function(mod) {return walk(mod, {filename: '/', id: '/'})}))
    .fail(output.emit.bind(output, 'error'))
    .fin(output.queue.bind(output, null))

  return output

  function makeModule(m) {
    var filename,
        mod = {entry: true, deps: {}}
    if (typeof m.pipe === 'function') {
      filename = path.join(basedir, rng(8).toString('hex') + '.js')
      mod.id = filename
      mod.filename = filename
      mod.sourcePromise = aggregate(m)
    } else {
      filename = path.resolve(m)
      mod.id = filename
      mod.filename = filename
    }
    return mod
  }

  function moduleToResult(mod) {
    var result = {id: mod.filename, source: mod.source, deps: mod.deps}
    if (mod.entry) result.entry = true
    return result
  }

  function resolver(id, parent) {
    parent.packageFilter = opts.packageFilter
    parent.extensions = opts.extensions
    parent.modules = opts.modules
    parent.paths = []
    var bucket = (cache[parent.filename] = cache[parent.filename] || {})
    return bucket[id] ? bucket[id] : bucket[id] = resolve(id, parent)
  }

  function checkCache(mod, parent) {
    if (!(opts.cache && opts.cache[parent.filename])) return
    var curFilename = opts.cache[parent.filename].deps[mod.id]
    var result = opts.cache[curFilename]
    if (!result) return
    return {id: mod.id, filename: curFilename,
      deps: result.deps, source: result.source, entry: result.entry,
      package: opts.packageCache && opts.packageCache[result.id]}
  }

  function walk(mod, parent) {
    var cached = checkCache(mod, parent)
    if (cached) {
      output.emit(moduleToResult(cached))
      return walkDeps(cached, parent)
    }
    return ((!mod.filename && mod.id) ?
      resolver(mod.id, parent).then(_.extend.bind(null, mod)) :
      q.resolve(mod))
    .then(function(mod) {
      if (seen[mod.filename]) return
      seen[mod.filename] = true

      if (!mod.sourcePromise)
        mod.sourcePromise = aggregate(fs.createReadStream(mod.filename))

      return applyTransforms(mod).then(function(mod) {
        if (Buffer.isBuffer(mod.source)) mod.source = mod.source.toString()
        output.queue(moduleToResult(mod))
        return walkDeps(mod)
      })
    })
  }

  function extractDeps(filename, mod) {
    if (opts.noParse && opts.noParse.indexOf(filename) > -1) return {}
    var deps = detective(mod.source)
    return q.all(deps.map(function(id) {
      return ((opts.filter && !opts.filter(id)) ?
        q.resolve({id: id, filename: false}) : resolver(id, mod))
    })).then(function(deps) {
      deps.forEach(function(dep) { mod.deps[dep.id] = dep.filename })
      return mod
    })
  }

  function walkDeps(mod) {
    return q.all(Object.keys(mod.deps)
      .filter(function(depId) { return mod.deps[depId] })
      .map(function(depId) { return walk({id: depId, deps: {}}, mod) }))
  }

  function getTransform(pkg) {
    if (opts.transformKey) {
      var n = pkg
      opts.transformKey.forEach(function (key) {
        if (n && typeof n === 'object') n = n[key]
      })
      return [].concat(n).filter(Boolean)
    }
  }

  function loadTransform(mod, transform) {
    if (!_.isString(transform)) return q.resolve(transform)

    return transformResolve(transform, {basedir: path.dirname(mod.filename)})
      .fail(function() {
        return transformResolve(transform, {basedir: process.cwd()})
      })
      .then(function(res) {
        if (!res)
          throw new Error([
            'cannot find transform module ', transform,
            ' while transforming ', mod.filename
          ].join(''))
        return require(res.filename)
      })
  }

  function doTransform(transform, mod) {
    if (transform.length === 1) {
      return aggregate(asStream(mod.source).pipe(transform(mod.filename)))
        .then(function(source) {
          mod.source = source
          return mod
        })
    } else {
      return q.resolve(transform(mod.filename, mod))
        .then(function(transformed) {
          if (transformed.source) mod.source = transformed.source
          if (transformed.deps) mod.deps = _.extend(mod.deps, transformed.deps)
          return mod
        })
    }
  }

  function applyTransforms(mod) {
    var isTopLevel = entries.some(function (entry) {
          return path.relative(path.dirname(entry.filename), mod.filename)
            .split('/').indexOf('node_modules') < 0
        }),
        txs = [extractDeps],
        p = mod.sourcePromise.then(function(source) {
          mod.source = source
          return mod
        })

    if (isTopLevel) txs = txs.concat(opts.transform)
    if (mod.package) txs = txs.concat(getTransform(mod.package))

    return q.all(txs.filter(Boolean).map(loadTransform.bind(null, mod)))
      .then(function(txs) {
        txs.forEach(function(t) { p = p.then(doTransform.bind(null, t)) })
        return p
      })
  }
}
<commit_msg>add comments<commit_after>/* jshint asi: true, expr: true, globalstrict: true */
/* global require, Buffer, process, module */
"use strict";

var fs                = require('fs'),
    path              = require('path'),
    rng               = require('crypto').rng,
    _                 = require('underscore'),
    q                 = require('kew'),
    through           = require('through'),
    nodeResolve       = require('resolve'),
    browserResolve    = require('browser-resolve'),
    detective         = require('detective'),
    aggregate         = require('stream-aggregate-promise'),
    asStream          = require('as-stream'),
    transformResolve  = resolveWith.bind(null, nodeResolve)

/**
 * Resolve module by id
 *
 * @param {Function<String, Object, Callback>} resolve
 * @param {String} id
 * @param {Object} parent
 * @return {Promise<Object>} an object with id, filename and package keys
 */
function resolveWith(resolve, id, parent) {
  var p = q.defer()
  resolve(id, parent, function(err, filename, pkg) {
    err ? p.reject(err) : p.resolve({id: id, filename: filename, package: pkg})
  })
  return p
}

/**
 * Run stream transform over module
 *
 * Such transforms are compatible with transforms for browserify
 *
 * @param {Function<String>} transform a transform factory
 * @param {Object} mod a module to run transform over
 * @return {Promise<Object>} a transformed module
 */
function runStreamTransform(transform, mod) {
  return aggregate(asStream(mod.source).pipe(transform(mod.filename)))
    .then(function(source) {
      mod.source = source
      return mod
    })
}

/**
 * Run transform over module
 *
 * @param {Function<String>} transform a transform factory
 * @param {Object} mod a module to run transform over
 * @return {Promise<Object>} a transformed module
 */
function runTransform(transform, mod) {
  return q.resolve(transform(mod.filename, mod)).then(function(transformed) {
    if (transformed.source) mod.source = transformed.source
    if (transformed.deps) mod.deps = _.extend(mod.deps, transformed.deps)
    return mod
  })
}

/**
 * Load transform from a package
 *
 * @param {Object} pkg a package to load transform from
 * @param {Array<String>} key an array of strings represents a path in a package
 * @return {Array<String>} a list of transform ids
 */
function getTransform(pkg, key) {
  var n = pkg
  key.forEach(function (k) { if (n && typeof n === 'object') n = n[k] })
  return [].concat(n).filter(Boolean)
}

/**
 * Load transform relative to specified module
 *
 * It tries to resolve transform relative to module's filename first and then
 * fallback's to process.cwd()
 *
 * @param {Object} mod a module
 * @param {String} transform a transform module id
 * @return {Promise<Function>} a loaded transform function
 */
function loadTransform(mod, transform) {
  if (!_.isString(transform)) return q.resolve(transform)

  return transformResolve(transform, {basedir: path.dirname(mod.filename)})
    .fail(function() {
      return transformResolve(transform, {basedir: process.cwd()})
    })
    .then(function(res) {
      if (!res)
        throw new Error([
          'cannot find transform module ', transform,
          ' while transforming ', mod.filename
        ].join(''))
      return require(res.filename)
    })
}

/**
 * Resolve a graph of dependencies for a specified set of modules
 *
 * This is compatible with module-deps implementation with a few addition
 * features
 *
 * @param {Array<String|Stream>} mains a list of module ids, paths or streams
 * @param {Object} opts an options object compatible with module-deps
 * @return {Stream<Object>} a stream of resolved modules with dependencies
 */
module.exports = function(mains, opts) {
  opts = opts || {}

  var output = through(),
      basedir = opts.basedir || process.cwd(),
      resolve = resolveWith.bind(null, opts.resolve || browserResolve),
      cache = {},
      seen = {},
      entries = [].concat(mains).filter(Boolean).map(makeMainModule)

  q.all(entries.map(function(mod) {return walk(mod, {filename: '/', id: '/'})}))
    .fail(output.emit.bind(output, 'error'))
    .fin(output.queue.bind(output, null))

  return output

  function makeMainModule(m) {
    var filename,
        mod = {entry: true, deps: {}}
    if (typeof m.pipe === 'function') {
      filename = path.join(basedir, rng(8).toString('hex') + '.js')
      mod.id = filename
      mod.filename = filename
      mod.sourcePromise = aggregate(m)
    } else {
      filename = path.resolve(m)
      mod.id = filename
      mod.filename = filename
    }
    return mod
  }

  function moduleToResult(mod) {
    var result = {id: mod.filename, source: mod.source, deps: mod.deps}
    if (mod.entry) result.entry = true
    return result
  }

  function resolver(id, parent) {
    parent.packageFilter = opts.packageFilter
    parent.extensions = opts.extensions
    parent.modules = opts.modules
    parent.paths = []
    var bucket = (cache[parent.filename] = cache[parent.filename] || {})
    return bucket[id] ? bucket[id] : bucket[id] = resolve(id, parent)
  }

  function checkCache(mod, parent) {
    if (!(opts.cache && opts.cache[parent.filename])) return
    var curFilename = opts.cache[parent.filename].deps[mod.id]
    var result = opts.cache[curFilename]
    if (!result) return
    return {id: mod.id, filename: curFilename,
      deps: result.deps, source: result.source, entry: result.entry,
      package: opts.packageCache && opts.packageCache[result.id]}
  }

  function walk(mod, parent) {
    var cached = checkCache(mod, parent)
    if (cached) {
      output.emit(moduleToResult(cached))
      return walkDeps(cached, parent)
    }
    return ((!mod.filename && mod.id) ?
      resolver(mod.id, parent).then(_.extend.bind(null, mod)) :
      q.resolve(mod))
    .then(function(mod) {
      if (seen[mod.filename]) return
      seen[mod.filename] = true

      if (!mod.sourcePromise)
        mod.sourcePromise = aggregate(fs.createReadStream(mod.filename))

      return applyTransforms(mod).then(function(mod) {
        if (Buffer.isBuffer(mod.source)) mod.source = mod.source.toString()
        output.queue(moduleToResult(mod))
        return walkDeps(mod)
      })
    })
  }

  function walkDeps(mod) {
    return q.all(Object.keys(mod.deps)
      .filter(function(depId) { return mod.deps[depId] })
      .map(function(depId) { return walk({id: depId, deps: {}}, mod) }))
  }

  function extractDeps(filename, mod) {
    if (opts.noParse && opts.noParse.indexOf(filename) > -1) return {}
    var deps = detective(mod.source)
    return q.all(deps.map(function(id) {
      return ((opts.filter && !opts.filter(id)) ?
        q.resolve({id: id, filename: false}) : resolver(id, mod))
    })).then(function(deps) {
      deps.forEach(function(dep) { mod.deps[dep.id] = dep.filename })
      return mod
    })
  }

  function applyTransforms(mod) {
    var isTopLevel = entries.some(function (entry) {
          return path.relative(path.dirname(entry.filename), mod.filename)
            .split('/').indexOf('node_modules') < 0
        }),
        txs = [extractDeps],
        p = mod.sourcePromise.then(function(source) {
          mod.source = source
          return mod
        })

    if (isTopLevel)
      txs = txs.concat(opts.transform)

    if (mod.package && opts.transformKey)
      txs = txs.concat(getTransform(mod.package, opts.transformKey))

    return q.all(txs.filter(Boolean).map(loadTransform.bind(null, mod)))
      .then(function(txs) {
        txs.forEach(function(t) {
          p = p.then(function(p) {
            return (t.length === 1 ? runStreamTransform : runTransform)(t, p)
          })
        })
        return p
      })
  }
}
<|endoftext|>


<commit_before>var fs         = require('fs');
var path       = require('path');
var chalk      = require('chalk');
var findup     = require('findup-sync');
var mkdirp     = require('mkdirp');
var walkSync   = require('walk-sync');
var COFFEELINT = require('coffeelint').lint;
var helpers    = require('broccoli-kitchen-sink-helpers');
var Filter     = require('broccoli-filter');

var mapSeries  = require('promise-map-series')

CoffeeLint.prototype = Object.create(Filter.prototype);
CoffeeLint.prototype.constructor = CoffeeLint;
function CoffeeLint (inputTree, options) {
  if (!(this instanceof CoffeeLint)) return new CoffeeLint(inputTree, options);

  options = options || {};

  this.inputTree = inputTree;
  this.log       = true;

  for (var key in options) {
    if (options.hasOwnProperty(key)) {
      this[key] = options[key]
    }
  }
};

CoffeeLint.prototype.extensions = ['coffee'];
CoffeeLint.prototype.targetExtension = 'coffeelint.js';

CoffeeLint.prototype.write = function (readTree, destDir) {
  var self = this
  self._errors = [];

  return readTree(this.inputTree).then(function (srcDir) {
    var paths = walkSync(srcDir)
    if (!self.coffeelintJSON) {
      var coffeelintPath  = self.coffeelintJSONPath || path.join(srcDir, self.coffeelintJSONRoot || '');
      self.coffeelintJSON = self.getConfig(coffeelintPath);
    }
    return mapSeries(paths, function (relativePath) {
      if (relativePath.slice(-1) === '/') {
        mkdirp.sync(destDir + '/' + relativePath)
      } else {
        if (self.canProcessFile(relativePath)) {
          return self.processAndCacheFile(srcDir, destDir, relativePath)
        } else {
          helpers.copyPreserveSync(
            srcDir + '/' + relativePath, destDir + '/' + relativePath)
        }
      }
    })
  })
  .finally(function() {
    if (self._errors.length > 0) {
      var label = ' CoffeeLint Error' + (self._errors.length > 1 ? 's' : '')
      console.log('\n' + self._errors.join('\n'));
      console.log(chalk.yellow('===== ' + self._errors.length + label + '\n'));
    }
  })
}

CoffeeLint.prototype.processString = function (content, relativePath) {
  var passed = COFFEELINT(content, this.coffeelintJSON);
  var errors = this.processErrors(relativePath, passed);
  if (errors && this.log) {
    this.logError(errors)
  }

  if (!this.disableTestGenerator) {
    return this.testGenerator(relativePath, passed, errors);
  }
};

CoffeeLint.prototype.processErrors = function (file, errors) {
  if (!errors) { return ''; }

  var len = errors.length,
  str = '',
  error, idx;

  if (len === 0) { return ''; }

  for (idx=0; idx<len; idx++) {
    error = errors[idx];
    if (error !== null) {
      str += file  + ': line ' + error.lineNumber + ', message: ' +
        error.message;
    }
  }

  return str + "\n" + len + ' error' + ((len === 1) ? '' : 's') + "\n";
}

CoffeeLint.prototype.testGenerator = function(relativePath, passed, errors) {
  if (errors) {
    errors = "\\n" + this.escapeErrorString(errors);
  } else {
    errors = ""
  }

  return "module('CoffeeLint - " + path.dirname(relativePath) + "');\n" +
         "test('" + relativePath + " should pass coffeelint', function() { \n" +
         "  ok(" + !!passed + ", '" + relativePath + " should pass coffeelint." + errors + "'); \n" +
         "});\n"
};

CoffeeLint.prototype.logError = function(message, color) {

  color = color || 'red';

  this._errors.push(chalk[color](message) + "\n");
};

CoffeeLint.prototype.getConfig = function(rootPath) {
  if (!rootPath) { rootPath = process.cwd(); }
  var coffeelintJSONPath = findup('coffeelint.json', {cwd: rootPath, nocase: true});
  if (coffeelintJSONPath) {
    var config = fs.readFileSync(coffeelintJSONPath, {encoding: 'utf8'});
    try {
      return JSON.parse(this.stripComments(config));
    } catch (e) {
      console.error(chalk.red('Error occured parsing cofeelint.json.'));
      console.error(e.stack);
      return null;
    }
  }
};

CoffeeLint.prototype.stripComments = function(string) {
  string = string || "";

  string = string.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\//g, "");
  string = string.replace(/\/\/[^\n\r]*/g, ""); // Everything after '//'

  return string;
};

CoffeeLint.prototype.escapeErrorString = function(string) {
  string = string.replace(/\n/gi, "\\n");
  string = string.replace(/'/gi, "\\'");

  return string;
};

module.exports = CoffeeLint;
<commit_msg>Update format of error logs.<commit_after>var fs         = require('fs');
var path       = require('path');
var chalk      = require('chalk');
var findup     = require('findup-sync');
var mkdirp     = require('mkdirp');
var walkSync   = require('walk-sync');
var COFFEELINT = require('coffeelint').lint;
var helpers    = require('broccoli-kitchen-sink-helpers');
var Filter     = require('broccoli-filter');

var mapSeries  = require('promise-map-series')

CoffeeLint.prototype = Object.create(Filter.prototype);
CoffeeLint.prototype.constructor = CoffeeLint;
function CoffeeLint (inputTree, options) {
  if (!(this instanceof CoffeeLint)) return new CoffeeLint(inputTree, options);

  options = options || {};

  this.inputTree = inputTree;
  this.log       = true;

  for (var key in options) {
    if (options.hasOwnProperty(key)) {
      this[key] = options[key]
    }
  }
};

CoffeeLint.prototype.extensions = ['coffee'];
CoffeeLint.prototype.targetExtension = 'coffeelint.js';

CoffeeLint.prototype.write = function (readTree, destDir) {
  var self = this
  self._errors = [];
  self._errorLength = 0;

  return readTree(this.inputTree).then(function (srcDir) {
    var paths = walkSync(srcDir)
    if (!self.coffeelintJSON) {
      var coffeelintPath  = self.coffeelintJSONPath || path.join(srcDir, self.coffeelintJSONRoot || '');
      self.coffeelintJSON = self.getConfig(coffeelintPath);
    }
    return mapSeries(paths, function (relativePath) {
      if (relativePath.slice(-1) === '/') {
        mkdirp.sync(destDir + '/' + relativePath)
      } else {
        if (self.canProcessFile(relativePath)) {
          return self.processAndCacheFile(srcDir, destDir, relativePath)
        } else {
          helpers.copyPreserveSync(
            srcDir + '/' + relativePath, destDir + '/' + relativePath)
        }
      }
    })
  })
  .finally(function() {
    if (self._errors.length > 0) {
      var label = ' CoffeeLint Error' + (self._errorLength > 1 ? 's' : '')
      console.log('\n' + self._errors.join('\n'));
      console.log(chalk.yellow('===== ' + self._errorLength + label + '\n'));
    }
  })
}

CoffeeLint.prototype.processString = function (content, relativePath) {
  var passed = COFFEELINT(content, this.coffeelintJSON);
  var errors = this.processErrors(relativePath, passed);
  this._errorLength += passed.length
  if (errors && this.log) {
    this.logError(errors)
  }

  if (!this.disableTestGenerator) {
    return this.testGenerator(relativePath, passed, errors);
  }
};

CoffeeLint.prototype.processErrors = function (file, errors) {
  if (!errors) { return ''; }

  var len = errors.length,
  str = '',
  error, idx;

  if (len === 0) { return ''; }
  str += '============================================================\n';
  str += file + ' (' + len + ' error' + ((len === 1) ? '' : 's') + '):\n';
  for (idx=0; idx<len; idx++) {
    error = errors[idx];
    if (error !== null) {
      str += '------------------------------------------------------------\n'
      if (error.level) {
        str += 'level: ' + error.level + '\n';
      }
      str += 'line: ' + error.lineNumber + '\n';
      str += 'rule: ' + error.rule + '\n';
      str += 'message: ' + error.message + '\n';
      if (error.line) {
        str += error.line + '\n';
      }
      if (error.context) {
        str += error.context + '\n';
      }
    }
  }
  return str;
}

CoffeeLint.prototype.testGenerator = function(relativePath, passed, errors) {
  if (errors) {
    errors = "\\n" + this.escapeErrorString(errors);
  } else {
    errors = ""
  }

  return "module('CoffeeLint - " + path.dirname(relativePath) + "');\n" +
         "test('" + relativePath + " should pass coffeelint', function() { \n" +
         "  ok(" + !!passed + ", '" + relativePath + " should pass coffeelint." + errors + "'); \n" +
         "});\n"
};

CoffeeLint.prototype.logError = function(message, color) {
  color = color || 'red';
  this._errors.push(chalk[color](message) + "\n");
};

CoffeeLint.prototype.getConfig = function(rootPath) {
  if (!rootPath) { rootPath = process.cwd(); }
  var coffeelintJSONPath = findup('coffeelint.json', {cwd: rootPath, nocase: true});
  if (coffeelintJSONPath) {
    var config = fs.readFileSync(coffeelintJSONPath, {encoding: 'utf8'});
    try {
      return JSON.parse(this.stripComments(config));
    } catch (e) {
      console.error(chalk.red('Error occured parsing cofeelint.json.'));
      console.error(e.stack);
      return null;
    }
  }
};

CoffeeLint.prototype.stripComments = function(string) {
  string = string || "";

  string = string.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\//g, "");
  string = string.replace(/\/\/[^\n\r]*/g, ""); // Everything after '//'

  return string;
};

CoffeeLint.prototype.escapeErrorString = function(string) {
  string = string.replace(/\n/gi, "\\n");
  string = string.replace(/'/gi, "\\'");

  return string;
};

module.exports = CoffeeLint;
<|endoftext|>


<commit_before>// Started as a PDF.js example file, originally licensed public domain

var fs = require('fs'),
    util = require('util'),
    dsv = require('d3-dsv'),
    path = require('path');

global.DOMParser = require('./lib/domparsermock.js').DOMParserMock;

var pdfjsLib = require('pdfjs-dist');
var filePath = __dirname + '/test/data/';
var skipHeaders = {};

function processFiling(pdfPath) {
    var data = new Uint8Array(fs.readFileSync(pdfPath));

    console.log(pdfPath);

    var tables = [];

    var ignoreRest = false;

    return pdfjsLib.getDocument(data).then(function(doc) {
        var numPages = doc.numPages;

        var lastPromise; // will be used to chain promises

        var loadPage = function(pageNum) {
            return doc.getPage(pageNum).then(function(page) {
                var viewport = page.getViewport(1.0 /* scale */ );

                return page.getTextContent().then(function(content) {
                    var boldFont = '';

                    var styleKeys = Object.keys(content.styles);
                    styleKeys.forEach(function (styleKey) {
                        if (content.styles[styleKey].fontFamily == 'sans-serif' &&
                            content.styles[styleKey].descent < -0.295) {
                            boldFont = styleKey;
                        }
                    });

                    var ignorePage = false;
                    var rowY = 0;

                    content.items.sort(function (a,b) {
                        if (a.transform[4]-b.transform[4] !== 0) {
                            return a.transform[4]-b.transform[4];
                        }
                        else {
                            return a.transform[5]-b.transform[5];
                        }
                    }).forEach(function (item,i) {
                        if (item.str.indexOf('Data Revised') !== -1 ||
                            item.str === 'U.S. Office of Government Ethics Certification') {
                            ignorePage = true;
                        }

                        if (!ignorePage && !ignoreRest) {
                            if (item.height == 14) {
                                if (item.str == 'Filer\'s Information') {
                                    ignorePage = true;
                                }
                                else if (item.str == 'Summary of Contents') {
                                    ignoreRest = true;
                                }
                                else if (item.str.trim().length > 2) {
                                    var headerParts = item.str.split('. ');
                                    var part = null,
                                        name = null;

                                    if (headerParts.length > 1) {
                                        part = parseInt(headerParts[0]);
                                        name = headerParts.slice(1).join('. ');
                                    }
                                    else {
                                        name = item.str;
                                    }

                                    tables.push({
                                        part: part,
                                        name: name,
                                        cols: [],
                                        rows: []
                                    });
                                }
                            }
                            else if (item.height == 10) {
                                var curTable = tables[tables.length-1];
                                if (curTable) {

                                    if (item.fontName == boldFont) {
                                        if (item.str == 'PART' || (item.str == '#' && curTable.cols.length > 1)) {
                                            curTable.cols = [];
                                        }

                                        var append = true;

                                        curTable.cols.forEach(function (col) {
                                            if (col.x == item.transform[5]) {
                                                col.name += ' ' + item.str;
                                                col.slug = col.name.toLowerCase().replace(/[ ,]+/g, '-');
                                                append = false;
                                            }
                                        });

                                        if (append) {
                                            curTable.cols.push({
                                                name: item.str,
                                                slug: item.str.toLowerCase().replace(/[ ,]+/g, '-'),
                                                x: item.transform[5],
                                                y: item.transform[4]
                                            });
                                        }
                                    }
                                    else {
                                        curTable.cols.forEach(function (col, i2) {
                                            if (col.x == item.transform[5]) {
                                                if (i2 === 0 && item.transform[4] - rowY > 12) {
                                                    curTable.rows.push({});
                                                    rowY = item.transform[4];
                                                }
                                                var curRow = curTable.rows[curTable.rows.length - 1];

                                                if (col.slug in curRow) {
                                                    if (col.slug !== '#') {
                                                        curRow[col.slug] += ' ';
                                                    }
                                                    curRow[col.slug] += item.str;
                                                }
                                                else {
                                                    curRow[col.slug] = item.str;
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    });

                });
            });
        };

        lastPromise = loadPage(2);
        for (var i = 3; i <= numPages-4; i++) {
            lastPromise = lastPromise.then(loadPage.bind(null, i));
        }
        return lastPromise;
    }).then(function() {
        var fileName = path.basename(pdfPath,'.pdf');
        return new Promise(function (resolve, reject) {
            tables.forEach(function (table) {
                var csvFile = filePath + table.name.toLowerCase().replace(/[ ,']+/g,'-') + '.csv';

                table.rows.forEach(function (row) {
                    row.file = fileName;
                });

                if (table.cols.length > 0) {
                    var columns = ['file'].concat(table.cols.map(function (col) {
                        return col.slug;
                    }));
                    var csvString = dsv.csvFormat(table.rows, columns);
                    if (skipHeaders[table.name]) {
                        csvString = csvString.substring(csvString.indexOf('\n')+1);
                    } else {
                        skipHeaders[table.name] = true;
                    }
                    fs.appendFileSync(csvFile, csvString + '\n');
                }
            });

            resolve();
        });
    });
}

var files = fs.readdirSync(filePath).filter(function (file) {
    return file.indexOf('.pdf') !== -1;
});

var filingPromise = processFiling(filePath + files[0]);
for (var pos = 1; pos < files.length; pos++) {
    filingPromise = filingPromise
        .then(processFiling.bind(null, filePath + files[pos]),
            function(reason) { console.log(reason) });
}
filingPromise.then(function () {
    console.log('done');
});
<commit_msg>Code style tweaks, ES6<commit_after>// jshint esnext:true
// Started as a PDF.js example file, originally licensed public domain

var fs = require('fs'),
    util = require('util'),
    dsv = require('d3-dsv'),
    path = require('path');

global.DOMParser = require('./lib/domparsermock.js').DOMParserMock;

var pdfjsLib = require('pdfjs-dist');

const filePath = `${__dirname}/test/data/`;
let skipHeaders = {};

function processFiling(pdfPath) {
    const data = new Uint8Array(fs.readFileSync(pdfPath));

    console.log(pdfPath);

    const tables = [];

    let ignoreRest = false;

    return pdfjsLib.getDocument(data).then(doc => {
        const numPages = doc.numPages;

        let lastPromise; // will be used to chain promises

        const loadPage = pageNum => doc.getPage(pageNum).then(page => {
            const viewport = page.getViewport(1.0 /* scale */ );

            return page.getTextContent().then(content => {
                let boldFont = '';

                const styleKeys = Object.keys(content.styles);
                styleKeys.forEach(styleKey => {
                    if (content.styles[styleKey].fontFamily == 'sans-serif' &&
                        content.styles[styleKey].descent < -0.295) {
                        boldFont = styleKey;
                    }
                });

                let ignorePage = false;
                let rowY = 0;

                content.items.sort((a, b) => {
                    if (a.transform[4] - b.transform[4] !== 0) {
                        return a.transform[4] - b.transform[4];
                    }
                    else {
                        return a.transform[5] - b.transform[5];
                    }
                }).forEach((item, i) => {
                    if (item.str.includes('Data Revised') ||
                        item.str === 'U.S. Office of Government Ethics Certification') {
                        ignorePage = true;
                    }

                    if (!ignorePage && !ignoreRest) {
                        if (item.height == 14) {
                            if (item.str == 'Filer\'s Information') {
                                ignorePage = true;
                            }
                            else if (item.str == 'Summary of Contents') {
                                ignoreRest = true;
                            }
                            else if (item.str.trim().length > 2) {
                                const headerParts = item.str.split('. ');
                                let part = null;
                                let name = null;

                                if (headerParts.length > 1) {
                                    part = parseInt(headerParts[0]);
                                    name = headerParts.slice(1).join('. ');
                                }
                                else {
                                    name = item.str;
                                }

                                tables.push({
                                    part,
                                    name,
                                    cols: [],
                                    rows: []
                                });
                            }
                        }
                        else if (item.height == 10) {
                            const curTable = tables[tables.length - 1];
                            if (curTable) {

                                if (item.fontName == boldFont) {
                                    if (item.str == 'PART' || (item.str == '#' && curTable.cols.length > 1)) {
                                        curTable.cols = [];
                                    }

                                    let append = true;

                                    curTable.cols.forEach(col => {
                                        if (col.x == item.transform[5]) {
                                            col.name += ` ${item.str}`;
                                            col.slug = col.name.toLowerCase().replace(/[ ,]+/g, '-');
                                            append = false;
                                        }
                                    });

                                    if (append) {
                                        curTable.cols.push({
                                            name: item.str,
                                            slug: item.str.toLowerCase().replace(/[ ,]+/g, '-'),
                                            x: item.transform[5],
                                            y: item.transform[4]
                                        });
                                    }
                                }
                                else {
                                    curTable.cols.forEach((col, i2) => {
                                        if (col.x == item.transform[5]) {
                                            if (i2 === 0 && item.transform[4] - rowY > 12) {
                                                curTable.rows.push({});
                                                rowY = item.transform[4];
                                            }
                                            const curRow = curTable.rows[curTable.rows.length - 1];

                                            if (col.slug in curRow) {
                                                if (col.slug !== '#') {
                                                    curRow[col.slug] += ' ';
                                                }
                                                curRow[col.slug] += item.str;
                                            }
                                            else {
                                                curRow[col.slug] = item.str;
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }
                });

            });
        });

        lastPromise = loadPage(2);
        for (let i = 3; i <= numPages - 4; i++) {
            lastPromise = lastPromise.then(loadPage.bind(null, i));
        }
        return lastPromise;
    }).then(() => {
        const fileName = path.basename(pdfPath, '.pdf');
        return new Promise((resolve, reject) => {
            tables.forEach(table => {
                const csvFile = `${filePath + table.name.toLowerCase().replace(/[ ,']+/g, '-')}.csv`;

                table.rows.forEach(row => {
                    row.file = fileName;
                });

                if (table.cols.length > 0) {
                    const columns = ['file'].concat(table.cols.map(col => col.slug));
                    let csvString = dsv.csvFormat(table.rows, columns);
                    if (skipHeaders[table.name]) {
                        csvString = csvString.substring(csvString.indexOf('\n') + 1);
                    }
                    else {
                        skipHeaders[table.name] = true;
                    }
                    fs.appendFileSync(csvFile, `${csvString}\n`);
                }
            });

            resolve();
        });
    });
}

const files = fs.readdirSync(filePath).filter(file => file.includes('.pdf'));

let filingPromise = processFiling(filePath + files[0]);
for (let pos = 1; pos < files.length; pos++) {
    filingPromise = filingPromise
        .then(processFiling.bind(null, filePath + files[pos]),
            console.log);
}
filingPromise.then(() => {
    console.log('done');
});
<|endoftext|>


<commit_before>var P = (function(prototype, undefined) {
  // used to extend the prototypes of superclasses (which might not
  // have `.Bare`s)
  function SuperclassBare() {}

  return function P(_superclass /* = Object */, definition) {
    // handle the case where no superclass is given
    if (definition === undefined) {
      definition = _superclass;
      _superclass = Object;
    }

    // C is the class to be returned.
    //
    // When called, creates and initializes an instance of C, unless
    // `this` is already an instance of C, then just initializes `this`;
    // either way, returns the instance of C that was initialized.
    //
    //  TODO: the Chrome inspector shows all created objects as `C`
    //        rather than `Object`.  Setting the .name property seems to
    //        have no effect.  Is there a way to override this behavior?
    function C() {
      var self = this instanceof C ? this : new Bare;
      self.init.apply(self, arguments);
      return self;
    }

    // C.Bare is a class with a noop constructor.  Its prototype is the
    // same as C, so that instances of C.Bare are also instances of C.
    // New objects can be allocated without initialization by calling
    // `new MyClass.Bare`.
    function Bare() {}
    C.Bare = Bare;

    // Set up the prototype of the new class.
    var _super = SuperclassBare[prototype] = _superclass[prototype];
    var proto = Bare[prototype] = C[prototype] = C.p = new SuperclassBare;

    // set the constructor property on the prototype, for convenience
    proto.constructor = C;

    C.extend = function(def) { return P(C, def); }

    return (C.open = function(def) {
      if (typeof def === 'function') {
        // call the defining function with all the arguments you need
        // extensions captures the return value.
        def = def.call(C, proto, _super, C, _superclass);
      }

      // ...and extend it
      if (typeof def === 'object') {
        for (var key in def) {
          if (({}).hasOwnProperty.call(def, key)) {
            proto[key] = def[key];
          }
        }
      }

      // if no init, assume we're inheriting from a non-Pjs class, so
      // default to using the superclass constructor.
      if (!('init' in proto)) proto.init = _superclass;

      return C;
    })(definition);
  }

  // as a minifier optimization, we've closured in a few helper functions
  // and the string 'prototype' (C[p] is much shorter than C.prototype)
})('prototype');
<commit_msg>Revert "inline the hasOwnProperty check"<commit_after>var P = (function(prototype, ownProperty, undefined) {
  // used to extend the prototypes of superclasses (which might not
  // have `.Bare`s)
  function SuperclassBare() {}

  return function P(_superclass /* = Object */, definition) {
    // handle the case where no superclass is given
    if (definition === undefined) {
      definition = _superclass;
      _superclass = Object;
    }

    // C is the class to be returned.
    //
    // When called, creates and initializes an instance of C, unless
    // `this` is already an instance of C, then just initializes `this`;
    // either way, returns the instance of C that was initialized.
    //
    //  TODO: the Chrome inspector shows all created objects as `C`
    //        rather than `Object`.  Setting the .name property seems to
    //        have no effect.  Is there a way to override this behavior?
    function C() {
      var self = this instanceof C ? this : new Bare;
      self.init.apply(self, arguments);
      return self;
    }

    // C.Bare is a class with a noop constructor.  Its prototype is the
    // same as C, so that instances of C.Bare are also instances of C.
    // New objects can be allocated without initialization by calling
    // `new MyClass.Bare`.
    function Bare() {}
    C.Bare = Bare;

    // Set up the prototype of the new class.
    var _super = SuperclassBare[prototype] = _superclass[prototype];
    var proto = Bare[prototype] = C[prototype] = C.p = new SuperclassBare;

    // set the constructor property on the prototype, for convenience
    proto.constructor = C;

    C.extend = function(def) { return P(C, def); }

    return (C.open = function(def) {
      if (typeof def === 'function') {
        // call the defining function with all the arguments you need
        // extensions captures the return value.
        def = def.call(C, proto, _super, C, _superclass);
      }

      // ...and extend it
      if (typeof def === 'object') {
        for (var key in def) {
          if (ownProperty.call(def, key)) {
            proto[key] = def[key];
          }
        }
      }

      // if no init, assume we're inheriting from a non-Pjs class, so
      // default to using the superclass constructor.
      if (!('init' in proto)) proto.init = _superclass;

      return C;
    })(definition);
  }

  // as a minifier optimization, we've closured in a few helper functions
  // and the string 'prototype' (C[p] is much shorter than C.prototype)
})('prototype', ({}).hasOwnProperty);
<|endoftext|>


<commit_before>'use strict'

const Promise = require('bluebird')
const Through2 = require('through2')
const ReadFile = require('fs').createReadStream
const Reduce = Array.prototype.reduce;

/* MODULE */
function Punc(filePath, options){
  if (!options) {
    options = { encoding: 'utf8' }
  } else if (typeof options === 'string') {
    options = { encoding: options }
  } else {
    throw new TypeError('Punc: expected options to be either an object or a string, ' +
    'but got ' + typeof options + ' instead');
  }

  let punctuationsSeen = { ';': 0
    , ':': 0
    , "'": 0
    , '"': 0
    , ',': 0
    , '!': 0
    , '?': 0
    , '.': 0
    , '(': 0
    , ')': 0
    , '-': 0
    }

  let punctuationsOnly = [];

  console.log('inside Punc Module')

  return new Promise((resolve, reject) => {
    ReadFile(filePath, options.encoding)
      /**
       * remove words, leave only punctuations and count them too!
       * ; : ' " , ! ? . ( ) -
       */
      .pipe(Through2.obj(function(chunk, _, callback) {

        for (let i = 0; i < chunk.length; i++) {

          if ( chunk[i] in punctuationsSeen ) {
            punctuationsSeen[ chunk[i] ]++
            punctuationsOnly.push(chunk[i])
          }
        }
        return callback(null, chunk)
      }))
      .on('data', data => {

      })
      .on('end', _ => {
        resolve({
          body: punctuationsOnly.join(''),
          count: punctuationsSeen
        })
      })
      .on('error', error => reject(error))
  })
}

/* THE TRUTH IS OUT THERE */
module.exports = Punc<commit_msg>updates Punc internal options logic<commit_after>'use strict'

const Promise = require('bluebird')
const Through2 = require('through2')
const ReadFile = require('fs').createReadStream
const Reduce = Array.prototype.reduce;

/* MODULE */
function Punc(filePath, options){
  if (!options) {
    options = { encoding: 'utf8' }
  } else if (typeof options === 'string') {
    options = { encoding: options }
  } else if (typeof options !== 'object') {
    throw new TypeError(
      'Punc: expected options to be either an object or a \
      string, ' + 'but got ' + typeof options + ' instead'
    )
  }

  let punctuationsSeen = { ';': 0
    , ':': 0
    , "'": 0
    , '"': 0
    , ',': 0
    , '!': 0
    , '?': 0
    , '.': 0
    , '(': 0
    , ')': 0
    , '-': 0
    }

  let punctuationsOnly = [];

  console.log('inside Punc Module')

  return new Promise((resolve, reject) => {
    ReadFile(filePath, options.encoding)
      /**
       * remove words, leave only punctuations and count them too!
       * ; : ' " , ! ? . ( ) -
       */
      .pipe(Through2.obj(function(chunk, _, callback) {

        for (let i = 0; i < chunk.length; i++) {

          if ( chunk[i] in punctuationsSeen ) {
            punctuationsSeen[ chunk[i] ]++
            punctuationsOnly.push(chunk[i])
          }
        }
        return callback(null, chunk)
      }))
      .on('data', data => {

      })
      .on('end', _ => {
        resolve({
          body: punctuationsOnly.join(''),
          count: punctuationsSeen
        })
      })
      .on('error', error => reject(error))
  })
}

/* THE TRUTH IS OUT THERE */
module.exports = Punc<|endoftext|>


<commit_before>"use strict";

var MaterialBigDialog = require('./lib/MaterialUiBigDialog.js');

module.exports = MaterialUiBigDialog;<commit_msg>corrected the name of the exported variable<commit_after>"use strict";

var MaterialBigDialog = require('./lib/MaterialUiBigDialog.js');

module.exports = MaterialBigDialog;<|endoftext|>


<commit_before>/*!
 * parse-author <https://github.com/jonschlinkert/parse-author>
 *
 * Copyright (c) 2014 Jon Schlinkert, contributors.
 * Licensed under the MIT license.
 */

'use strict';

var re = require('author-regex');

module.exports = function(str) {
  var author = re().exec(str);
  return {
    name: RegExp.$1,
    email: RegExp.$2,
    url: RegExp.$3
  };
};
<commit_msg>Use regex return in place of deprecated RegEx.$<commit_after>/*!
 * parse-author <https://github.com/jonschlinkert/parse-author>
 *
 * Copyright (c) 2014 Jon Schlinkert, contributors.
 * Licensed under the MIT license.
 */

'use strict';

var re = require('author-regex');

module.exports = function(str) {
  var author = re().exec(str);
  return {
    name: author[1] || '',
    email: author[2] || '',
    url: author[3] || ''
  };
};
<|endoftext|>