diff --git a/History.md b/History.md index ce1a810..0974c1b 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,10 @@ +0.2.0 / 2017-10-02 +================== + + * Resolve thenable result before printing. + * Resolving stdin if no args are specified. + * Redirect result streams to stdout. + * Do not hide stringifying errors. 0.1.0 / 2013-04-24 ================== diff --git a/bin/js b/bin/js index ec794c1..778e087 100755 --- a/bin/js +++ b/bin/js @@ -3,6 +3,7 @@ var fs = require('fs'); var path = require('path'); var program = require('commander'); +var stream = require('stream'); program .version(JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version) @@ -34,7 +35,9 @@ function start() { // ignore } } - + global.stdin = stdin; + global.require = require; + // expose environment variables as globals preceded with $ for (var name in process.env) { var value = process.env[name]; @@ -50,38 +53,67 @@ function start() { global['$' + name] = value; } - - var result, output; - + + var formula = program.args.join(' ') || stdin || 'undefined'; + var result; + try { - result = output = eval('(' + (program.args.join(' ') || 'undefined') + ')'); + result = (0, eval)('(' + formula + ')'); } catch (e) { if (e instanceof SyntaxError) { - result = output = eval(program.args.join(' ') || 'undefined'); + result = (0, eval)(formula); } else { throw e; } } - - if (typeof output == 'string') { - if (output[output.length - 1] != '\n') { - output = output + '\n'; - } + + print(result); +} + +function print(result) { + if (result != null && typeof result === 'object' && typeof result.then === 'function') { + result.then(print, onError); + return; + } + + var output = new stream.PassThrough(); + + output.on('end', function () { + process.exit(result ? 0 : 1); + }); + + output.on('error', onError); + + if (!program.silent) { + output.pipe(process.stdout); + } else { + output.resume(); + } + + if (result instanceof stream.Readable) { + result.pipe(output) } else { try { - if (program.ugly) { - output = JSON.stringify(output) + '\n'; + if (typeof result == 'undefined') { + output.write('undefined\n'); + } else if (typeof result == 'string') { + output.write(result); + output.write('\n'); + } else if (program.ugly) { + output.write(JSON.stringify(result)); } else { - output = JSON.stringify(output, null, 2) + '\n'; + output.write(JSON.stringify(result, null, 2)); + output.write('\n'); } - } catch (e) { - // ignore + } catch (error) { + output.emit('error', error); + } finally { + output.end(); } } - - if (!program.silent) { - process.stdout.write(output); - } - - process.exit(result ? 0 : 1); -} \ No newline at end of file +} + +function onError(error) { + console.error(error); + process.exit(1); +} diff --git a/package.json b/package.json index 09f994c..1fb95bc 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,15 @@ "name": "js", "description": "A better `node -p`.", "author": "Marco Aurelio ", - "version": "0.1.0", + "version": "0.2.1", "bin": { "js": "./bin/js" }, "dependencies": { "commander": "~1.1.1" - } -} \ No newline at end of file + }, + "engines": { + "node": ">=0.10" + }, + "engineStrict": true +}