Browse Source

add basic CLI support

Kyle P Davis 11 years ago
parent
commit
0e4d1eaa98
2 changed files with 33 additions and 0 deletions
  1. 32 0
      main.js
  2. 1 0
      mungedb-aggregate.js

+ 32 - 0
main.js

@@ -0,0 +1,32 @@
+var readline = require("readline"),
+	aggregate = require("./lib/");
+
+module.exports = function main() {
+	var args = process.argv.slice(2);
+	if (args.length === 0) console.error("USAGE:\n  aggregate PIPELINE_JSON < INPUT_DOCS.jsonl"), process.exit(1);
+
+	var pipeline = [].concat.apply([], args.map(JSON.parse)),
+		inputs = [];
+
+	var	rl = readline.createInterface({
+		input: process.stdin,
+		output: process.stdout,
+		terminal: false
+	})
+		.on("line", function(line){
+			try {
+				inputs.push(JSON.parse(line));
+			} catch (err) {
+				console.error("ERROR: Unable to parse line #" + inputs.length);
+				console.error("LINE: " + line);
+				throw err;
+			}
+		})
+		.on("close", function () {
+			var results = aggregate(pipeline, inputs);
+			results.forEach(function(result){
+				console.log(JSON.stringify(result));
+			});
+			process.exit(0);
+		});
+};

+ 1 - 0
mungedb-aggregate.js

@@ -1 +1,2 @@
 module.exports = require("./lib/");
 module.exports = require("./lib/");
+if (!module.parent) require("./main")();