nixfilter
Version:
Simplify the development of (UNIX) "Filters"
116 lines (104 loc) • 3.44 kB
JavaScript
'use strict';
var Event_Emitter, Filter, argparse, create_filter_class, reader, run_filter_class, setup_filter, utils, writer;
// Import/Require the "argparse" module
argparse = require('argparse');
// Import the EventEmitter class from the "events" module
Event_Emitter = require('events');
// Import/require the local "reader" module
reader = require('./reader');
// Import/require the local "utils" module
utils = require('./utils');
// Import/require the local "writer" module
writer = require('./writer');
Filter = utils.extend(Event_Emitter, {
// The default description
description: 'No description available',
// The default add_arguments(argument_parser) function: A no-op function
add_arguments: function(argument_parser) {},
// Setup an ArgumentParser for this module and return it
get_argument_parser: function() {
var argument_parser;
argument_parser = new argparse.ArgumentParser({
description: this.description
});
this.add_arguments(argument_parser);
return argument_parser;
},
// The default setup(args) function: A no-op function
setup: function(args) {},
// The default terminate() function: A no-op function
terminate: function() {},
stop: function() {
this.emit('stopping');
},
input_reader: reader.line('utf8'),
output_writer: writer.line('utf8'),
run: function(args1) {
this.args = args1;
if (!this.args) {
this.args = this.get_argument_parser().parseArgs();
}
this.emit('starting');
return Promise.resolve(this.setup(this.args)).then(() => {
if (this.input_reader) {
// The filter is a sink, it consumes input data
this.input_stream = process.stdin;
this.input_stream.on('data', this.input_reader(this.on_input.bind(this)));
this.input_stream.on('close', () => {
this.stop();
});
}
if (this.output_writer) {
// The filter is a source, it produces output data
this.output_stream = process.stdout;
this.output = this.output_writer((output_bytes) => {
this.output_stream.write(output_bytes);
});
this.output_stream.on('close', () => {
this.stop();
});
}
this.emit('started');
return new Promise((resolve, reject) => {
this.once('stopping', () => {
Promise.resolve(this.terminate()).then(() => {
this.emit('stopped');
resolve();
}).catch((error) => {
reject(error);
});
});
});
});
}
});
create_filter_class = function(filter_specification) {
return utils.extend(Filter, filter_specification);
};
run_filter_class = function(Filter_Class) {
var filter;
filter = new Filter_Class();
return filter.run();
};
setup_filter = function() {
var Filter_Class, args, filter_module, filter_specification;
args = Array.prototype.slice.call(arguments);
filter_specification = args.pop();
filter_module = args.pop();
Filter_Class = create_filter_class(filter_specification);
if (filter_module) {
filter_module.exports.Filter = Filter_Class;
if (require.main === filter_module) {
run_filter_class(Filter_Class).catch(function(error) {
console.error(error);
return process.exit(1);
});
}
}
return Filter_Class;
};
// What this module exports
module.exports = {
setup_filter: setup_filter
};
//# sourceMappingURL=filter.js.map