4 ms·
People gotta realize that Node.js is actually excellent for this sort of task. It hits quite a few sweet spots as an all-round Unix scripting language: * Famil
by w56rjrtyu6ru 7y ago
People gotta realize that Node.js is actually excellent for this sort of task. It hits quite a few sweet spots as an all-round Unix scripting language:
* Familiar syntax and semantics (Bash/Zsh/Fish/Csh/Ksh/Ash/Wtfsh are all way weirder than JS - and they are _only_ scripting languages, while JS is already everywhere)
* Standard library (https://nodejs.org/dist/latest-v12.x/docs/api/fs.html https://nodejs.org/dist/latest-v12.x/docs/api/fs.html) gives you access to POSIX APIs ("batteries included" - but not bloated)
* Sane packaging, package management, and ecosystem (overreliance on third-party module is a thing exactly because NPM works with much less friction than e.g. the Python situation)
* Asynchronous event model lets you safely do multiple things in parallel without worrying about low-level memory management, or which concurrency boilerplate library to use, etc.
All in all, it's the perfect Unix glue. All it misses is a mature S-expression-based syntax.
For example, take the problem of locking your encrypted partitions before suspending your Linux machine. Since distro developers are too "busy" to provide what one might say is basic functionality, power users took matters in their own hands and wrote this 100-ish line Bash script (https://github.com/vianney/arch-luks-suspend https://github.com/vianney/arch-luks-suspend), which evolved into this 1500-ish line Go monstrosity (https://github.com/guns/go-luks-suspend https://github.com/guns/go-luks-suspend)... as well as these ~75 lines of Node.js (https://github.com/egasimus/node-luks-suspend https://github.com/egasimus/node-luks-suspend).
But yeah, sure, hurr durr JaavaaScriipt baad, 0.1+0.2!==0.3, 'true'==true, etc... The horror!
- StavrosK 7y agoAll of these features are things other languages have too (I personally disagree that npm's "download the world" model is better than Python's envs). A huge drawback Node has is that it's not installed by default on any Linux distribution I know of, so I'd take a Python script over a Node one any day. Compiled languages have an even better distribution story, and Rust's Cargo is nothing to laugh at. I'm not sure what you hope your posted comparisons show, but the Go "monstrosity" seems to have many more features than the Node one.
- pushpop 7y agoSure, the the node code uses fewer lines than the Go code. But what you’ve failed to notice is the Go code calls APIs directly rather than fork()ing and does so within its own code base (how many lines of code is in the countless imports of your node example?). Plus the Go code also has unit tests which the others don’t. Line numbers are always a terrible metric for proving anything but in this case your especially abusing that data.
- w56rjrtyu6ru 7y agoIn absence of more accessible complexity measurements, line counts do matter somewhat. E.g. the briefer a program is, the more readable it is (last time I checked, brainpower and time is much more expensive than computing power and bandwidth.) The Node example has zero external dependencies outside of the Node.js standard library.
- pushpop 7y ago> In absence of more accessible complexity measurements, line counts do matter somewhat. E.g. the briefer a program is, the more readable it is (last time I checked, brainpower and time is much more expensive than computing power and bandwidth.) There are as many exceptions to that rule are there are examples when it’s true. Code golfing, minifified code, shell 1 liners, Perl, etc. Line counts mean jack shit.
- w56rjrtyu6ru 7y agoI would argue that even an esoteric, densely packed one-liner can still be more readable than the equivalent program spread out over a greater number of lines - simply because you can keep it all in sight at the same time and work through it without losing the bigger picture. But of course, scary symbols is scary.
- mises 7y agoSo you'd like to use APL, then? He didn't say symbols. Verbose code can be much easier, though stupidly verbose is stupid. Compromise is, as usual, the best solution.
- chaosite 7y agoLook, I'm generally with you, there isn't anything particularly bad about modern JavaSscript, and it's a perfectly fine language to write your tools in if that's what you like. But your points are... Weird. 1. Familiar to people who know JavaScript, sure. But shell is the language you interact with the CLI with, so more people who use the CLI are familiar with a shell. 2. Most languages have a standard library. 3. I know many people who would disagree with you. Anyway, pip works fine in a Unix environment, which is what we're discussing here. 4. We're talking about glue code, right? Glue doesn't tend to be very parallel. 5. So, perfect Unix glue? Sure, you like it, and that's great for you! Keep on doing you. It's obviously capable of making nice tools, but so are most languages... 6. You luks-suspend example is weird because the Node.js code obviously does so much less than the other versions. I have no idea what the Go version is doing, but the shell version does things like remount the filesystem, handle udev, etc... A bash version of the JavaScript code would be shorter! 7. 0.1+0.2!=0.3 is a result of floating point arithmetic being floating point arithmetic. It's not specific to JavaScript.
- w56rjrtyu6ru 7y ago1. Most people who interact with a shell know how to make it run one command with arguments; some eventually learn about IO redirection. Many such people who use Bash as a "command prompt" would find the way it does even simple things like variables and conditionals exceedingly arcane. Readability is important, and, for better or worse, a compact curly bracket syntax is seen as "the norm". 2. Different languages' standard libraries have different scopes. Node's is just right for the task at hand. 3. I know many people who disagree with me, too. That doesn't make me wrong (or right ;-)). 4. Why not? Doing a task and displaying a rich progress indicator is doing two tasks. Glue is a continuum - for me, the concept includes orchestration of external "heavy lifting" tasks according to the desired logic, as well as processing intermediary outputs in simple ways so the whole pipeline fits together. 5. Well, I've evaluated it and found it to my liking. I guess you grow to like the tools you use to accomplish things, and if everyone who went out of their way to publicly dismiss Node.js instead went out of their way to use it in place of Bash scripting, they might find it to their liking as well. 6. Yet ironically, it has been the most reliable in achieving its purpose of locking my encrypted partitions when my computer goes to sleep, not to mention unlocking them when I wake it up. 7. Exactly - but people keep pulling this and other quirks out of their ass as "proof" JavaScript is some horrible language. Show me a shell scripting language that even has floating point arithmetic! But maybe glue code can't ever need that, either?
- dang 7y agoCould you please stop creating accounts for every few comments you post? We ban accounts that do that. This is in the site guidelines: https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html. HN is a community. Users needn't use their real name, but should have some identity for others to relate to. Otherwise we may as well have no usernames and no community, and that would be a different kind of forum. https://hn.algolia.com/?sort=byDate&dateRange=all&type=comment&storyText=false&prefix&page=0&query=by:dang%20community%20identity https://hn.algolia.com/?sort=byDate&dateRange=all&type=comme...