TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Shelljs – Portable Unix shell commands for Node.js

116 点作者 chetangoti大约 4 年前

13 条评论

smilekzs大约 4 年前
I know this has been around for quite a while but I feel obliged to point out one major use for this: Embedding portable one-liners in the `package.json` file invoked through `npm run` [1], because you cannot assume from which shell is `npm` called by users of your package.<p>[1]: <a href="https:&#x2F;&#x2F;docs.npmjs.com&#x2F;cli&#x2F;v7&#x2F;commands&#x2F;npm-run-script" rel="nofollow">https:&#x2F;&#x2F;docs.npmjs.com&#x2F;cli&#x2F;v7&#x2F;commands&#x2F;npm-run-script</a>
评论 #27102521 未加载
statico大约 4 年前
ShellJS was a delight to use when we had to build a cross platform Electron-based (sorry) desktop application for Mac and Windows. It replaced batch and shell scripts, async operations were easy thanks to NodeJS, and the scripts were maintainable. 8&#x2F;10, would use again.
评论 #27099806 未加载
评论 #27099832 未加载
dstaley大约 4 年前
I&#x27;m a fan of Shelljs. I used it to translate shell scripts to run on any platform that supports Node[1]. I particularly like how the translated version is essentially a more readable version of the original shell script[2].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;Kinto&#x2F;kinto-http.js&#x2F;blob&#x2F;master&#x2F;bin&#x2F;dist-fx.ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Kinto&#x2F;kinto-http.js&#x2F;blob&#x2F;master&#x2F;bin&#x2F;dist-...</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;Kinto&#x2F;kinto-http.js&#x2F;blame&#x2F;6a4ba42ba13a91a5121be2771688c2cab3dd423d&#x2F;package.json#L19" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Kinto&#x2F;kinto-http.js&#x2F;blame&#x2F;6a4ba42ba13a91a...</a>
danellis大约 4 年前
If you&#x27;re going to invoke these operations from JavaScript, why on earth would you still want to control them using command-line style switches? Reproducing the functionality: fine, useful. Reproducing the interface: inelegant and probably error-prone.
评论 #27100321 未加载
0mp大约 4 年前
There is also Modernish, which is a library for writing portable shell scripts and it seems to support all kinds of different versions of shells across a variety of operating systems. It really takes care of all the quirks and bugs every platform has.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;modernish&#x2F;modernish" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;modernish&#x2F;modernish</a>
Calzifer大约 4 年前
The great thing with old tools is that it had decades of testing and most even obscure bugs are already found and fixed.<p>If I run &#x27;cat&#x27; in my shell I know it will print&#x2F;output the file content.<p>If I run &#x27;shx cat&#x27; I get an interpretation of my file content.<p>One use of &#x27;cat&#x27; is to concatenate multiple files into one. Works great with classic cat, not so much with Shelljs.<p>$ echo -ne &quot;\0xac&quot; &gt; test.dat<p>$ cat test.dat | xxd<p>00000000: ac .<p>$ shx cat test.dat | xxd<p>00000000: efbf bd ...
nodesocket大约 4 年前
I believe Google just released <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;zx" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;zx</a>. I am looking to refactor some &quot;gnarly&quot; and unreadable bash scripts in JavaScript and wondering what everybody thinks is the best option?<p>Biggest pain points in my opinion in pure bash is flow control, string manipulation, arrays, and looping.
评论 #27100986 未加载
评论 #27101369 未加载
评论 #27100802 未加载
评论 #27104153 未加载
评论 #27102828 未加载
mrbungie大约 4 年前
Does anybody know a project doing something similar but in Python?
评论 #27100368 未加载
评论 #27102251 未加载
评论 #27103309 未加载
评论 #27100354 未加载
kordlessagain大约 4 年前
Say goodbye to gnarly shell scripts? Oh Nodejs you’re funny!
评论 #27100104 未加载
BiteCode_dev大约 4 年前
Funnily, I was thinking about this the other day with Python: python is great for scripting, with the generators, string formatting and stlib modules like pathlib, datetime or argparse.<p>But there are still stuff I use bash for because it&#x27;s way more efficient for one liners and small utilities.<p>One of the things is that calling something in bash autoprints, unless you pipe it. Could it be done in Python?<p>Well, it turns out you can, if you do a little dirty hacking with __call__, __repr__, __iter__, __or__ and __del__, you get:<p><a href="https:&#x2F;&#x2F;0bin.net&#x2F;paste&#x2F;XeMvMuSh#45izWhAtQ5Bd14PDWoBqTJMvHZXayl-dTtCyQbmxGpU" rel="nofollow">https:&#x2F;&#x2F;0bin.net&#x2F;paste&#x2F;XeMvMuSh#45izWhAtQ5Bd14PDWoBqTJMvHZXa...</a><p>Which will let you do:<p>-ls(&#x27;&#x2F;etc&#x27;), auto prints all the files in the dir, in the shell and in a program.<p>- ls(&#x27;&#x2F;etc&#x27;) | keep(r&#x27;\.conf$&#x27;), pipe the output of ls, pass it to keep, filter it, then auto print it (again, works in the shell AND in a py file)<p>- res = ls(&#x27;&#x2F;etc&#x27;) | keep(r&#x27;\.conf$&#x27;), store the generator in the variable, doesn&#x27;t print, doesn&#x27;t calculate, ready to be able to be read with a for loop<p>This means we could create a lib with a very shell like experience in python for all those 5-lines-scripts that are still too good to pass bash for.<p>We could also make a @main decorator to auto run a function if __name__ == &quot;__main__&quot;, borrow the &quot;with cd()&quot; and &quot;with temp()&quot; syntax from the fabric lib.<p>The need for bash would get smaller and smaller for me. I rarely develop on a linux box that doesn&#x27;t have python, after all.<p>Now a problem people often hit is: yes, but you have to provide the dependency to your script. ShellJS proves that despite that, it has a great value already.<p>But the good news is, it&#x27;s not a problem anymore thanks to a great features of Python 3: zipapps. With shiv (<a href="https:&#x2F;&#x2F;shiv.readthedocs.io&#x2F;en&#x2F;latest&#x2F;" rel="nofollow">https:&#x2F;&#x2F;shiv.readthedocs.io&#x2F;en&#x2F;latest&#x2F;</a>), you can bundle your python script and all its dependencies (including our dirty shell lib) in a single pyz file, which then can run anywhere there is a modern python, which is everywhere I personally need it.
rodorgas大约 4 年前
So this is basically to support Windows, right? Because every other popular OS is Unix.
评论 #27103403 未加载
评论 #27104133 未加载
suifbwish大约 4 年前
Is it really that difficult to just update a database value to trigger action by a listening process that checks for that value on a short time interval? No extra libraries needed and the shell and web server world remain completely separate.
diveanon大约 4 年前
if anyone is looking for a way to use shelljs exec with async await you can do something like this <a href="https:&#x2F;&#x2F;pastebin.com&#x2F;zUBGL3Vp" rel="nofollow">https:&#x2F;&#x2F;pastebin.com&#x2F;zUBGL3Vp</a>