Skip to content

Commit

Permalink
Support an --end parameter to output a final expression at the end of…
Browse files Browse the repository at this point in the history
… processing
  • Loading branch information
jhs committed Jun 12, 2011
1 parent 863c9d8 commit 8216315
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cli.js
Expand Up @@ -50,6 +50,9 @@ if(argv.state) {
stream.state = state_init(require, util, json_from_file);
}

if(argv.end)
stream.on_end = new Function('scope', 'with (scope) { return (' + argv.end + ') }');

if(argv.bulk_docs || argv['bulk-docs']) {
argv.head = '{"docs":\n';
argv.tail = ']}\n';
Expand Down
21 changes: 21 additions & 0 deletions jss.js
Expand Up @@ -26,6 +26,7 @@ function Stream () {
self.tail = null;
self.silent = null;
self.state = null;
self.on_end = null;

self.on('line', function on_line(line) {
var obj;
Expand Down Expand Up @@ -173,6 +174,26 @@ Stream.prototype.pump = function() {
})

self.in.on('end', function() {
var result, scope;

if(self.on_end) {
result = undefined;
scope = { '$s' : self.state.caller
, 'require': require
, 'util' : util
};
try { result = self.on_end.call(self, scope); }
catch (er) { /* noop */ }

if(Array.isArray(result))
result.forEach(function(elem) {
self.out.write(printable(elem) + "\n");
})

else if(typeof result !== 'undefined')
self.out.write(printable(result) + "\n");
}

self.out.write(self.tail || '');
})

Expand Down

0 comments on commit 8216315

Please sign in to comment.