msgflo
Version:
Polyglot FBP runtime based on message queues
123 lines (111 loc) • 3.36 kB
JavaScript
var common, fbp, fs, generate, generateWithLibrary, library, main, parse, path, program, readGraph,
indexOf = [].indexOf;
program = require('commander');
fbp = require('fbp');
path = require('path');
fs = require('fs');
library = require('./library');
common = require('./common');
readGraph = function(filepath, callback) {
var ext;
ext = path.extname(filepath);
return fs.readFile(filepath, {
encoding: 'utf-8'
}, (err, contents) => {
var e, graph;
if (err) {
return callback(err);
}
try {
if (ext === '.fbp') {
graph = fbp.parse(contents);
} else {
graph = JSON.parse(contents);
}
return callback(null, graph);
} catch (error) {
e = error;
return callback(e);
}
});
};
generateWithLibrary = function(lib, graph, options) {
var cmd, component, iips, includes, lines, name, out, proc, ref;
lines = [];
ref = graph.processes;
for (name in ref) {
proc = ref[name];
if (indexOf.call(options.ignore, name) >= 0) {
continue;
}
component = proc.component;
iips = common.iipsForRole(graph, name);
cmd = lib.componentCommand(component, name, iips);
lines.push(`${name}: ${cmd}`);
}
includes = options.include.join('\n');
out = lines.join('\n');
return `${out}\n${includes}`;
};
// Generating Heroku/foreman Profile definiton
// from a FBP graph definition
exports.generate = generate = function(graph, options, callback) {
var lib, libOptions;
libOptions = {
configfile: options.library
};
if (!libOptions.configfile) {
libOptions.configfile = path.join(process.cwd(), 'package.json');
}
libOptions.componentdir = options.components;
lib = new library.Library(libOptions);
return lib.load(function(err) {
var out;
if (err) {
return callback(err);
}
out = generateWithLibrary(lib, graph, options);
return callback(null, out);
});
};
exports.parse = parse = function(args) {
var addIgnore, addInclude, graph;
addInclude = function(include, list) {
list.push(include);
return list;
};
addIgnore = function(ignore, list) {
list.push(ignore);
return list;
};
graph = null;
program.arguments('<graph.fbp/.json>').option('--library <FILE.json>', 'Use FILE.json as the library definition', String, 'package.json').option('--ignore [NODE]', 'Do not generate output for NODE. Can be specified multiple times.', addIgnore, []).option('--include [DATA]', 'Include DATA as-is in generated file. Can be specified multiple times.', addInclude, []).option('--components <DIR>', 'Lookup components from DIR', String, 'participants').action(function(gr, env) {
return graph = gr;
}).parse(args);
program.graphfile = graph;
return program;
};
exports.main = main = function() {
var callback, options;
options = parse(process.argv);
if (!options.graphfile) {
console.error('ERROR: No graph file specified');
program.help();
process.exit();
}
callback = function(err, out) {
if (err) {
throw err;
}
return console.log(out);
};
// TODO: support writing directly to Procfile?
return readGraph(options.graphfile, function(err, graph) {
if (err) {
return callback(err);
}
return generate(graph, options, function(err, out) {
return callback(err, out);
});
});
};