3 ms·
JavaScript as a shell replacement, what a time to be alive! I am familiar with and use quite a bit of js on the front end, but how is node for shelling out? Th
by lordgroff 6y ago
JavaScript as a shell replacement, what a time to be alive!
I am familiar with and use quite a bit of js on the front end, but how is node for shelling out? This is something I find Python to be quite terrible at.
- brundolf 6y agoI wouldn't use it as an interactive prompt personally - it's a bit too verbose - though I did know someone in college who did that :) Calling shell commands is reasonably straightforward: const { exec} = require('child_process') const child = exec('ls', ['-lh', '/usr']) console.log('error', child.error) console.log('stdout ', child.stdout) console.log('stderr ', child.stderr) However, you can also get cool packages like this to use instead: https://github.com/shelljs/shelljs https://github.com/shelljs/shelljs I will admit though that in most of the cases where I've used it I didn't really do any shelling-out, so I can't speak much to how that experience scales in practice. Mostly I was either 1) doing self-contained logic with files, web requests, etc, or 2) delegating out to JS build tools (which even when they have a CLI, almost always expose a nice JS API too). Ymmv, etc.
- mekster 6y agoIt's far easier to do it on perl. $output = `ls -lh /usr`; I don't think I can remember all the boilerplate of using the wrong language for the job if I only happen to write that once a month or less. Even PHP is easy. $output = shell_exec('ls -lh /usr');
- sbacic 6y agoLet me put it this way; I recently wrote a script to parse the output of last and format it. The Node version was some 90 LoC but if I included the dependencies it came to about 5 MB. Then I rewrote the same script in Python3 and it was 60 LoC and no dependencies that needed to be installed separately. I will admit that the JS code looked _a lot_ cleaner than the the Python code though.
- schwartzworld 6y agoNode is great for shell scripts. Async programming is a breeze in JavaScript. I wrote a quick wrapper function around child_process.exec to promisify it, and this allows me to use features like async / await and Promise.all in my scripts.