pahen/madge

--read Option does not work under Windows

jdl2007 opened this issue · 2 comments

http://stackoverflow.com/questions/8452957/synchronously-reading-stdin-in-windows

In bin/madge, line 39:

src = JSON.parse(fs.readFileSync('/dev/stdin').toString());

Fails with

fs.js:427
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory 'C:\dev\stdin'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at Object. (C:\Users\jdl\AppData\Roaming\npm\node_modules\madge\bin\madge:39:22)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3

Similar issue for chaplinjs was fixed here:

chaplinjs/chaplin@b0d5b6b

Please assist. Thanks!

I was able to fix this by doing the following:

Put everything after the 'if (program.read) {...}' block inside a function. If not program.read, call that function. Otherwise, if (program.read), do the following:

if (program.read) {
    var buffer = '';
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data', function (chunk) {
        buffer += chunk;
    });
    process.stdin.on('end', function () {
        src = JSON.parse(buffer);
        doRest();   // Resume with rest of program
    });
}
else {
    doRest();   // Resume with rest of program
}   

Probably not the most efficient way to do this, but works well enough for my test case (a 3,100 line file).

Thanks! I've added your fix now.