Search code examples
javascriptnode.jsbashshellread-eval-print-loop

How to have information about stdout-redirected filename in a node.js script


my question maybe quite straightforward or may have no solution. So, I ask you to kindly help me finding out how to access information of the stdout redirect to out.txt

node do-nothing.js < in.txt > out.txt

In particular I need to access the filename "out.txt" in the script do-nothing.js (i.e. accessing process.stdout.______)

Tips: I've already noticed that process.stdout._type become 'fs' instead of 'tty' while applying a redirection.


Solution

  • I got it. Here is the solution, if you launch the script as node stdout_redirect.js > out.txt && more out.txt you have the sought information in the d variable. Thanks for help and hope this help someone else.

    var fs = require('fs');
    
    fs.readdir(process.env.PWD,function(err, dir){
        var stdout_ino = 0;
        fs.stat('/dev/stdout',function(err,stats){
            stdout_ino = stats.ino;
            dir.forEach(function(d){
                fs.stat(d,function(err,stats){
                    if (stats.ino === stdout_ino)
                    {
                        console.log(d);
                    }
                });
            });
        });
    });