From 4a9f2de9568e122d94dc82394b54c7f1f722ce30 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 15 Mar 2011 10:50:24 -0700 Subject: [PATCH] Allow script to be read from stdin --- src/node.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/node.js b/src/node.js index 01280900a4..916239d989 100644 --- a/src/node.js +++ b/src/node.js @@ -80,9 +80,28 @@ console.log(rv); } else { - // REPL + var binding = process.binding('stdio'); + var fd = binding.openStdin(); var Module = NativeModule.require('module'); - Module.requireRepl().start(); + + if (NativeModule.require('tty').isatty(fd)) { + // REPL + Module.requireRepl().start(); + + } else { + // Read all of stdin - execute it. + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + + var code = ''; + process.stdin.on('data', function(d) { + code += d; + }); + + process.stdin.on('end', function() { + new Module()._compile(code, '[stdin]'); + }); + } } }