如何使用命令行美化 JavaScript 代码?
我正在编写一个批处理脚本来美化 JavaScript 代码。 它需要在Windows和Linux上运行。
如何使用命令行工具美化JavaScript代码?
I am writing a batch script in order to beautify JavaScript code. It needs to work on both Windows and Linux.
How can I beautify JavaScript code using the command line tools?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我无法向已接受的答案添加评论,因此这就是为什么您看到的帖子本来就不应该存在。
基本上我还需要一个java代码中的javascript美化器,令我惊讶的是,据我所知,没有一个可用。 因此,我完全根据接受的答案自己编写了一个代码(它包装了 jsbeautifier.org beautifier .js 脚本,但可以从 java 或命令行调用)。
代码位于 https://github.com/belgampaul/JsBeautifier
我使用了 rhino 和 beautifier.js
控制台的用法: java -jar jsbeautifier.jar 脚本缩进
示例: java -jar jsbeautifier.jar "function ff() {return;}" 2
java 代码的用法:
public static String jsBeautify(String jsCode, int indentSize)
欢迎您扩展代码。 就我而言,我只需要缩进,这样我就可以在开发时检查生成的 JavaScript。
希望它能为您的项目节省一些时间。
I'm not able to add a comment to the accepted answer so that's why you see a post that should have not existed in the first place.
Basically I also needed a javascript beautifier in a java code and to my surprise none is available as far as I could find. So I coded one myself entirely based on the accepted answer (it wraps the jsbeautifier.org beautifier .js script but is callable from java or the command line).
The code is located at https://github.com/belgampaul/JsBeautifier
I used rhino and beautifier.js
USAGE from console: java -jar jsbeautifier.jar script indentation
example: java -jar jsbeautifier.jar "function ff() {return;}" 2
USAGE from java code:
public static String jsBeautify(String jsCode, int indentSize)
You are welcome to extend the code. In my case I only needed the indentation so I could check the generated javascript while developing.
In the hope it'll save you some time in your project.
首先,选择您最喜欢的基于 Javascript 的漂亮打印/美化器。 我更喜欢 http 上的那个://jsbeautifier.org/,因为这是我首先发现的。 下载其文件 https://github.com/ beautify-web/js-beautify/blob/master/js/lib/beautify.js
其次,下载并安装 Mozilla 集团基于 Java 的 Javascript 引擎,犀牛。 “安装”有点误导; 下载 zip 文件,解压所有内容,将 js.jar 放入 Java 类路径(或 OS X 上的 Library/Java/Extensions)。 然后,您可以使用类似于此的调用来运行脚本。
使用步骤 1 中的 Pretty Print/Beautifier 编写一个小型 shell 脚本,该脚本将读取您的 javascript 文件,并通过步骤 1 中的 Pretty Print/Beautifier 运行它。 例如,
Rhino 为 javascript 提供了一些额外有用的功能,这些功能在浏览器上下文中不一定有意义,但在控制台上下文中却有意义。 函数 print 执行您所期望的操作,并打印出一个字符串。 函数 readFile 接受文件路径字符串作为参数并返回该文件的内容。
您可以调用上面的内容,例如
您可以在 Rhino 运行脚本中混合并匹配 Java 和 Javascript,因此,如果您了解一点 Java,那么使用文本流运行它应该不会太难。
First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at http://jsbeautifier.org/, because it's what I found first. Downloads its file https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js
Second, download and install The Mozilla group's Java based Javascript engine, Rhino. "Install" is a little bit misleading; Download the zip file, extract everything, place js.jar in your Java classpath (or Library/Java/Extensions on OS X). You can then run scripts with an invocation similar to this
Use the Pretty Print/Beautifier from step 1 to write a small shell script that will read in your javascript file and run it through the Pretty Print/Beautifier from step one. For example
Rhino gives javascript a few extra useful functions that don't necessarily make sense in a browser context, but do in a console context. The function print does what you'd expect, and prints out a string. The function readFile accepts a file path string as an argument and returns the contents of that file.
You'd invoke the above something like
You can mix and match Java and Javascript in your Rhino run scripts, so if you know a little Java it shouldn't be too hard to get this running with text-streams as well.
2014年4月更新:
自从我在2010年回答这个问题以来,美化器已经被重写。现在那里有一个python模块,一个用于nodejs的npm包,并且jar文件消失了。 请阅读 github.com 上的项目页面。
Python风格:
NPM风格:
使用它(这将在终端上返回经过美化的js文件,主文件保持不变):
要使更改在文件上生效,您应该使用此命令:
原始答案
添加到@Alan Storm的答案,
基于http://jsbeautifier.org的命令行美化器/ 变得更容易使用了,因为它现在(或者)基于 V8 javascript 引擎(c++ 代码)而不是 rhino(基于 java 的 JS 引擎,打包为“js.jar”)。 所以你可以使用V8代替rhino。
如何使用:
从以下位置下载 jsbeautifier.org zip 文件
http://github.com/einars/js-beautify/zipball/master
(这是链接到 zip 文件的下载 URL,例如 http://download. github.com/einars-js-beautify-10384df.zip)
旧的(不再工作,jar 文件消失了)
新的(替代)
从 svn 安装/编译 v8 lib,请参阅上面的 v8/README.txt -提到的 zip 文件
- 与 rhino 版本的命令行选项略有不同,
- 当配置为“外部工具”时,在 Eclipse 中工作得很好
UPDATE April 2014:
The beautifier has been rewritten since I answered this in 2010. There is now a python module in there, an npm Package for nodejs, and the jar file is gone. Please read the project page on github.com.
Python style:
NPM style:
to use it (this will return the beatified js file on the terminal, the main file remains unchanged):
To make the changes take effect on the file, you should use this command:
Original answer
Adding to Answer of @Alan Storm
the command line beautifier based on http://jsbeautifier.org/ has gotten a bit easier to use, because it is now (alternatively) based on the V8 javascript engine (c++ code) instead of rhino (java-based JS engine, packaged as "js.jar"). So you can use V8 instead of rhino.
How to use:
download jsbeautifier.org zip file from
http://github.com/einars/js-beautify/zipball/master
(this is a download URL linked to a zip file such as http://download.github.com/einars-js-beautify-10384df.zip)
old (no longer works, jar file is gone)
new (alternative)
install/compile v8 lib FROM svn, see v8/README.txt in above-mentioned zip file
-has slightly different command line options than the rhino version,
-and works great in Eclipse when configured as an "External Tool"
如果您使用的是nodejs,请尝试uglify-js
在Linux或Mac上,假设您已经安装了nodejs ,您可以使用以下命令安装 uglify:
然后获取选项:
因此,如果我有一个如下所示的源文件
foo.js
:我可以像这样美化它:
uglify
默认使用空格进行缩进,所以如果我想将 4 个空格缩进转换为制表符,我可以通过 Ubuntu 12.04 自带的unexpand
运行它:或者你可以一次性完成这一切:
了解有关 unexpand 的更多信息
您可以此处 毕竟,我最终得到一个如下所示的文件:
update 2016-06-07
看来 uglify-js 的维护者现在正在开发 版本 2 尽管安装是相同的。
If you're using nodejs then try uglify-js
On Linux or Mac, assuming you already have nodejs installed, you can install uglify with:
And then get the options:
So if I have a source file
foo.js
which looks like this:I can beautify it like so:
uglify
uses spaces for indentation by default so if I want to convert the 4-space-indentation to tabs I can run it throughunexpand
which Ubuntu 12.04 comes with:Or you can do it all in one go:
You can find out more about unexpand here
so after all this I wind up with a file that looks like so:
update 2016-06-07
It appears that the maintainer of uglify-js is now working on version 2 though installation is the same.
在 Ubuntu LTS 上,
要进行就地美化,可以使用以下任意命令:
On Ubuntu LTS
For in place beautifying, any of the follwing commands:
您有几种单班轮选择。 与 npm 一起使用或独立与 npx 一起使用。
半标准
标准< /a>
更漂亮
You have a few one liner choices. Use with npm or standalone with npx.
Semistandar
Standard
Prettier
在控制台中,您可以将 艺术风格(又名 AStyle)与
--mode=java.
它运行良好,并且免费、开源且跨平台(Linux、Mac OS X、Windows)。
In the console, you can use Artistic Style (a.k.a. AStyle) with
--mode=java
.It works great and it's free, open-source and cross-platform (Linux, Mac OS X, Windows).
使用现代 JavaScript 方式:
将 Grunt 与 Grunt 的 jsbeautifier 插件
您可以使用 npm。
您所需要的只是设置一个包含适当任务的 Gruntfile.js,其中还可能涉及文件串联、lint、uglify、minify 等,然后运行 grunt 命令。
Use the modern JavaScript way:
Use Grunt in combination with the jsbeautifier plugin for Grunt
You can install everything easily into your dev environment using npm.
All you will need is set up a Gruntfile.js with the appropriate tasks, which can also involve file concatenation, lint, uglify, minify etc, and run the grunt command.
我相信当您询问命令行工具时,您只是想批量美化所有 js 文件。
在这种情况下,Intellij IDEA(使用 11.5 进行测试)可以做到这一点。
您只需选择任何项目文件,然后在主 IDE 菜单中选择“代码”->“重新格式化代码..”。 然后在对话框中选择“目录中的所有文件...”并按“Enter”。
只要确保为 JVM 分配了足够的内存即可。
I believe when you asked about command line tool you just wanted to beautify all your js files in batch.
In this case Intellij IDEA (tested with 11.5) can do this.
You just need to select any of your project files and select "Code"->"Reformat code.." in main IDE menu. Then in the dialog select "all files in directory ..." and press "enter".
Just make sure you dedicated enough memory for the JVM.
我写了一篇文章解释如何构建 实现的命令行 JavaScript 美化器在 JavaScript 中,不到 5 分钟即可完成。 YMMV。
I've written an article explaining how to build a command-line JavaScript beautifier implemented in JavaScript in under 5 minutes. YMMV.