Skip to content

Stream, promise #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
==================
Expand Down
78 changes: 55 additions & 23 deletions bin/js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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];
Expand All @@ -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);
}
}

function onError(error) {
console.error(error);
process.exit(1);
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"name": "js",
"description": "A better `node -p`.",
"author": "Marco Aurelio <[email protected]>",
"version": "0.1.0",
"version": "0.2.1",
"bin": {
"js": "./bin/js"
},
"dependencies": {
"commander": "~1.1.1"
}
}
},
"engines": {
"node": ">=0.10"
},
"engineStrict": true
}