From 7118882db388bd41fffc4f17091960095837fb3a Mon Sep 17 00:00:00 2001 From: Vadzim Date: Mon, 2 Oct 2017 13:52:57 +0300 Subject: [PATCH 01/10] Resolve thenable result before printing --- History.md | 4 ++++ bin/js | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/History.md b/History.md index ce1a810..aae3719 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +0.2.0 / 2017-10-02 +================== + + * Resolve thenable result before printing 0.1.0 / 2013-04-24 ================== diff --git a/bin/js b/bin/js index ec794c1..32222d4 100755 --- a/bin/js +++ b/bin/js @@ -51,18 +51,29 @@ function start() { global['$' + name] = value; } - var result, output; + var result; try { - result = output = eval('(' + (program.args.join(' ') || 'undefined') + ')'); + result = eval('(' + (program.args.join(' ') || 'undefined') + ')'); } catch (e) { if (e instanceof SyntaxError) { - result = output = eval(program.args.join(' ') || 'undefined'); + result = eval(program.args.join(' ') || 'undefined'); } else { throw e; } } - + + print(result); +} + +function print(result) { + if (result != null && typeof result === 'object' && typeof result.then === 'function') { + result.then(print, onError); + return; + } + + var output = result; + if (typeof output == 'string') { if (output[output.length - 1] != '\n') { output = output + '\n'; @@ -84,4 +95,10 @@ function start() { } process.exit(result ? 0 : 1); -} \ No newline at end of file +} + +function onError(error) { + process.nextTick(function () { + throw error; + }); +} From 55ebd32dd3a424c13696653acb61cdee0f4c81c1 Mon Sep 17 00:00:00 2001 From: Vadzim Date: Mon, 2 Oct 2017 20:32:59 +0300 Subject: [PATCH 02/10] do not provide local symbols to evaluated script --- bin/js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/js b/bin/js index 32222d4..f73486a 100755 --- a/bin/js +++ b/bin/js @@ -34,7 +34,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]; @@ -51,13 +53,14 @@ function start() { global['$' + name] = value; } + var formula = program.args.join(' ') || 'undefined'; var result; try { - result = eval('(' + (program.args.join(' ') || 'undefined') + ')'); + result = (0, eval)('(' + formula + ')'); } catch (e) { if (e instanceof SyntaxError) { - result = eval(program.args.join(' ') || 'undefined'); + result = (0, eval)(formula); } else { throw e; } From 6b0229f4a00ca9becb7972e82714d9d10d7024d9 Mon Sep 17 00:00:00 2001 From: Vadzim Date: Mon, 2 Oct 2017 20:33:29 +0300 Subject: [PATCH 03/10] Resolving stdin if no args are specified. --- History.md | 3 ++- bin/js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index aae3719..60205da 100644 --- a/History.md +++ b/History.md @@ -1,7 +1,8 @@ 0.2.0 / 2017-10-02 ================== - * Resolve thenable result before printing + * Resolve thenable result before printing. + * Resolving stdin if no args are specified. 0.1.0 / 2013-04-24 ================== diff --git a/bin/js b/bin/js index f73486a..c8bbaa8 100755 --- a/bin/js +++ b/bin/js @@ -53,7 +53,7 @@ function start() { global['$' + name] = value; } - var formula = program.args.join(' ') || 'undefined'; + var formula = program.args.join(' ') || stdin || 'undefined'; var result; try { From 85ef93f20e13c7ab61a2f4d33c926f3f1f23c2f7 Mon Sep 17 00:00:00 2001 From: Vadzim Date: Mon, 2 Oct 2017 21:19:40 +0300 Subject: [PATCH 04/10] remove white spaces at end of lines --- bin/js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/js b/bin/js index c8bbaa8..e5f585d 100755 --- a/bin/js +++ b/bin/js @@ -52,10 +52,10 @@ function start() { global['$' + name] = value; } - + var formula = program.args.join(' ') || stdin || 'undefined'; var result; - + try { result = (0, eval)('(' + formula + ')'); } catch (e) { @@ -84,7 +84,7 @@ function print(result) { } else { try { if (program.ugly) { - output = JSON.stringify(output) + '\n'; + output = JSON.stringify(output) + '\n'; } else { output = JSON.stringify(output, null, 2) + '\n'; } @@ -92,11 +92,11 @@ function print(result) { // ignore } } - + if (!program.silent) { process.stdout.write(output); } - + process.exit(result ? 0 : 1); } From 0ce3f9aa7ec220a99529fd6ee07ae6b865359935 Mon Sep 17 00:00:00 2001 From: Vadzim Date: Mon, 2 Oct 2017 21:20:30 +0300 Subject: [PATCH 05/10] do not add new line in ugly mode --- bin/js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/js b/bin/js index e5f585d..2e5627b 100755 --- a/bin/js +++ b/bin/js @@ -84,7 +84,7 @@ function print(result) { } else { try { if (program.ugly) { - output = JSON.stringify(output) + '\n'; + output = JSON.stringify(output); } else { output = JSON.stringify(output, null, 2) + '\n'; } From 2b5edeec66f44201e7a0ebb03e0b7753ff12146b Mon Sep 17 00:00:00 2001 From: Vadzim Date: Tue, 3 Oct 2017 11:26:53 +0300 Subject: [PATCH 06/10] redirect streams to stdout instead of sringifying them --- History.md | 1 + bin/js | 37 +++++++++++++++++++++++-------------- package.json | 4 +++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/History.md b/History.md index 60205da..f84228f 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ * Resolve thenable result before printing. * Resolving stdin if no args are specified. + * Redirect result streams to stdout. 0.1.0 / 2013-04-24 ================== diff --git a/bin/js b/bin/js index 2e5627b..0b97fe4 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) @@ -75,29 +76,37 @@ function print(result) { return; } - var output = result; + var output = new stream.PassThrough(); - if (typeof output == 'string') { - if (output[output.length - 1] != '\n') { - output = output + '\n'; - } + output.on('end', function () { + process.exit(result ? 0 : 1); + }); + + 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); + 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 + } finally { + output.end(); } } - - if (!program.silent) { - process.stdout.write(output); - } - - process.exit(result ? 0 : 1); } function onError(error) { diff --git a/package.json b/package.json index 09f994c..8a49370 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,7 @@ }, "dependencies": { "commander": "~1.1.1" - } + }, + "engines" : { "node" : ">=0.10" }, + "engineStrict" : true } \ No newline at end of file From 45ac14f5ab0bbfb9d60085006524ae5194d053ff Mon Sep 17 00:00:00 2001 From: Vadzim Date: Tue, 3 Oct 2017 11:29:05 +0300 Subject: [PATCH 07/10] do not hide stringifying exceptions --- History.md | 1 + bin/js | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/History.md b/History.md index f84228f..0974c1b 100644 --- a/History.md +++ b/History.md @@ -4,6 +4,7 @@ * 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 0b97fe4..fa894f5 100755 --- a/bin/js +++ b/bin/js @@ -82,6 +82,8 @@ function print(result) { process.exit(result ? 0 : 1); }); + output.on('error', onError); + if (!program.silent) { output.pipe(process.stdout); } else { @@ -101,8 +103,8 @@ function print(result) { output.write(JSON.stringify(result, null, 2)); output.write('\n'); } - } catch (e) { - // ignore + } catch (error) { + output.emit('error', error); } finally { output.end(); } @@ -110,7 +112,6 @@ function print(result) { } function onError(error) { - process.nextTick(function () { - throw error; - }); + console.error(error); + process.exit(1); } From 2247c13a06ef19ba0bf17e3ca4f3ae6d3b9a8f0a Mon Sep 17 00:00:00 2001 From: Vadzim Date: Tue, 3 Oct 2017 12:05:56 +0300 Subject: [PATCH 08/10] 0.2.0 --- package.json | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8a49370..111b24a 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,15 @@ "name": "js", "description": "A better `node -p`.", "author": "Marco Aurelio ", - "version": "0.1.0", + "version": "0.2.0", "bin": { "js": "./bin/js" }, "dependencies": { "commander": "~1.1.1" }, - "engines" : { "node" : ">=0.10" }, - "engineStrict" : true -} \ No newline at end of file + "engines": { + "node": ">=0.10" + }, + "engineStrict": true +} From eec1e2c8843d765a29e646d07fa344e2e01d3cd1 Mon Sep 17 00:00:00 2001 From: Vadzim Date: Tue, 3 Oct 2017 12:12:32 +0300 Subject: [PATCH 09/10] fix printing undefined --- bin/js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/js b/bin/js index fa894f5..778e087 100755 --- a/bin/js +++ b/bin/js @@ -94,7 +94,9 @@ function print(result) { result.pipe(output) } else { try { - if (typeof result == 'string') { + if (typeof result == 'undefined') { + output.write('undefined\n'); + } else if (typeof result == 'string') { output.write(result); output.write('\n'); } else if (program.ugly) { From 867f50e8820c28760137536f9600990ad56349cc Mon Sep 17 00:00:00 2001 From: Vadzim Date: Tue, 3 Oct 2017 12:13:22 +0300 Subject: [PATCH 10/10] 0.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 111b24a..1fb95bc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "js", "description": "A better `node -p`.", "author": "Marco Aurelio ", - "version": "0.2.0", + "version": "0.2.1", "bin": { "js": "./bin/js" },