如何使用命令行美化 JavaScript 代码?

发布于 2024-07-04 09:05:29 字数 130 浏览 3 评论 0原文

我正在编写一个批处理脚本来美化 JavaScript 代码。 它需要在WindowsLinux上运行。

如何使用命令行工具美化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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

通知家属抬走 2024-07-11 09:05:29

我无法向已接受的答案添加评论,因此这就是为什么您看到的帖子本来就不应该存在。

基本上我还需要一个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.

信愁 2024-07-11 09:05:29

首先,选择您最喜欢的基于 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)。 然后,您可以使用类似于此的调用来运行脚本。

java -cp js.jar org.mozilla.javascript.tools.shell.Main name-of-script.js

使用步骤 1 中的 Pretty Print/Beautifier 编写一个小型 shell 脚本,该脚本将读取您的 javascript 文件,并通过步骤 1 中的 Pretty Print/Beautifier 运行它。 例如,

//original code    
(function() { ... js_beautify code ... }());

//new code
print(global.js_beautify(readFile(arguments[0])));

Rhino 为 javascript 提供了一些额外有用的功能,这些功能在浏览器上下文中不一定有意义,但在控制台上下文中却有意义。 函数 print 执行您所期望的操作,并打印出一个字符串。 函数 readFile 接受文件路径字符串作为参数并返回该文件的内容。

您可以调用上面的内容,例如

java -cp js.jar org.mozilla.javascript.tools.shell.Main beautify.js file-to-pp.js

您可以在 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

java -cp js.jar org.mozilla.javascript.tools.shell.Main name-of-script.js

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

//original code    
(function() { ... js_beautify code ... }());

//new code
print(global.js_beautify(readFile(arguments[0])));

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

java -cp js.jar org.mozilla.javascript.tools.shell.Main beautify.js file-to-pp.js

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.

痞味浪人 2024-07-11 09:05:29

2014年4月更新

自从我在2010年回答这个问题以来,美化器已经被重写。现在那里有一个python模块,一个用于nodejs的npm包,并且jar文件消失了。 请阅读 github.com 上的项目页面

Python风格:

 $ pip install jsbeautifier

NPM风格:

 $ npm -g install js-beautify

使用它(这将在终端上返回经过美化的js文件,主文件保持不变):

 $ js-beautify file.js

使更改在文件上生效,您应该使用此命令:

$ js-beautify -r file.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 文件消失了)

  java -jar js.jar  name-of-script.js

新的(替代)

从 svn 安装/编译 v8 lib,请参阅上面的 v8/README.txt -提到的 zip 文件

  ./jsbeautify somefile.js

- 与 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:

 $ pip install jsbeautifier

NPM style:

 $ npm -g install js-beautify

to use it (this will return the beatified js file on the terminal, the main file remains unchanged):

 $ js-beautify file.js

To make the changes take effect on the file, you should use this command:

$ js-beautify -r file.js

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)

  java -jar js.jar  name-of-script.js

new (alternative)

install/compile v8 lib FROM svn, see v8/README.txt in above-mentioned zip file

  ./jsbeautify somefile.js

-has slightly different command line options than the rhino version,

-and works great in Eclipse when configured as an "External Tool"

美人骨 2024-07-11 09:05:29

如果您使用的是nodejs,请尝试uglify-js

在Linux或Mac上,假设您已经安装了nodejs ,您可以使用以下命令安装 uglify:

sudo npm install -g uglify-js

然后获取选项:

uglifyjs -h

因此,如果我有一个如下所示的源文件 foo.js

// foo.js -- minified
function foo(bar,baz){console.log("something something");return true;}

我可以像这样美化它:

uglifyjs foo.js --beautify --output Cutefoo.js

uglify 默认使用空格进行缩进,所以如果我想将 4 个空格缩进转换为制表符,我可以通过 Ubuntu 12.04 自带的 unexpand 运行它:

unexpand --tabs=4 Cutefoo.js > cuterfoo.js

或者你可以一次性完成这一切:

uglifyjs foo.js --beautify | 取消展开--tabs=4> 最可爱的foo.js

了解有关 unexpand 的更多信息

您可以此处 毕竟,我最终得到一个如下所示的文件:

function foo(bar, baz) {
    console.log("something something");
    return true;
}

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:

sudo npm install -g uglify-js

And then get the options:

uglifyjs -h

So if I have a source file foo.js which looks like this:

// foo.js -- minified
function foo(bar,baz){console.log("something something");return true;}

I can beautify it like so:

uglifyjs foo.js --beautify --output cutefoo.js

uglify uses spaces for indentation by default so if I want to convert the 4-space-indentation to tabs I can run it through unexpand which Ubuntu 12.04 comes with:

unexpand --tabs=4 cutefoo.js > cuterfoo.js

Or you can do it all in one go:

uglifyjs foo.js --beautify | unexpand --tabs=4 > cutestfoo.js

You can find out more about unexpand here

so after all this I wind up with a file that looks like so:

function foo(bar, baz) {
    console.log("something something");
    return true;
}

update 2016-06-07

It appears that the maintainer of uglify-js is now working on version 2 though installation is the same.

情痴 2024-07-11 09:05:29

在 Ubuntu LTS 上,

$ sudo apt install jsbeautifier
$ js-beautify ugly.js > beautiful.js

要进行就地美化,可以使用以下任意命令:

$ js-beautify -r file.js
$ js-beautify --replace file.js

On Ubuntu LTS

$ sudo apt install jsbeautifier
$ js-beautify ugly.js > beautiful.js

For in place beautifying, any of the follwing commands:

$ js-beautify -r file.js
$ js-beautify --replace file.js
孤千羽 2024-07-11 09:05:29

您有几种单班轮选择。 与 npm 一起使用或独立与 npx 一起使用。

半标准

npx semistandard "js/**/*.js" --fix

标准< /a>

npx standard "js/**/*.js" --fix

更漂亮

npx prettier --single-quote --write --trailing-comma all "js/**/*.js"

You have a few one liner choices. Use with npm or standalone with npx.

Semistandar

npx semistandard "js/**/*.js" --fix

Standard

npx standard "js/**/*.js" --fix

Prettier

npx prettier --single-quote --write --trailing-comma all "js/**/*.js"
只等公子 2024-07-11 09:05:29

在控制台中,您可以将 艺术风格(又名 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).

柏林苍穹下 2024-07-11 09:05:29

使用现代 JavaScript 方式:

GruntGrunt 的 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.

等风也等你 2024-07-11 09:05:29

我相信当您询问命令行工具时,您只是想批量美化所有 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.

ペ泪落弦音 2024-07-11 09:05:29

我写了一篇文章解释如何构建 实现的命令行 JavaScript 美化器在 JavaScript 中,不到 5 分钟即可完成。 YMMV。

  1. 下载最新的稳定版 Rhino 并将其解压到某个位置,例如 ~/dev/javascript/rhino
  2. 下载从前面提到的 jsbeautifier.org 引用的 beautify.js,然后将其复制到某个地方,例如 ~/dev/javascript/bin/cli-beautifier.js
  3. 将其添加到 beautify.js 的末尾(使用 JavaScript 的一些附加顶级属性):

    // 对作为第一个参数传递的文件运行美化器。 
      打印(j23s_beautify(readFile(参数[0]))); 
      
  4. 将以下代码复制粘贴到可执行文件中,例如 ~/dev/javascript/bin/jsbeautifier.sh:

    <前><代码>#!/bin/sh
    java -cp ~/dev/javascript/rhino/js.jar org.mozilla.javascript.tools.shell.Main ~/dev/web/javascript/bin/cli-beautifier.js $*

  5. (可选)将包含 jsbeautifier.js 的文件夹添加到 PATH 或移动到已有的文件夹。

I've written an article explaining how to build a command-line JavaScript beautifier implemented in JavaScript in under 5 minutes. YMMV.

  1. Download the latest stable Rhino and unpack it somewhere, e.g. ~/dev/javascript/rhino
  2. Download beautify.js which is referenced from aforementioned jsbeautifier.org then copy it somewhere, e.g. ~/dev/javascript/bin/cli-beautifier.js
  3. Add this at the end of beautify.js (using some additional top-level properties to JavaScript):

    // Run the beautifier on the file passed as the first argument.
    print( j23s_beautify( readFile( arguments[0] )));
    
  4. Copy-paste the following code in an executable file, e.g. ~/dev/javascript/bin/jsbeautifier.sh:

    #!/bin/sh
    java -cp ~/dev/javascript/rhino/js.jar org.mozilla.javascript.tools.shell.Main ~/dev/web/javascript/bin/cli-beautifier.js $*
    
  5. (optional) Add the folder with jsbeautifier.js to PATH or moving to some folder already there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文