bash 我如何递归地进行 JS-Beautify?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23251400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I JS-Beautify recursively?
提问by Pervy Sage
I have lots of html files in a directory and sub-directories. I can execute js-beautify
command through the command line and want to apply it recursively to all those files.
我在一个目录和子目录中有很多 html 文件。我可以js-beautify
通过命令行执行命令,并希望将它递归地应用于所有这些文件。
I have tried
我试过了
find . -name ".html" -type f | js-beautify -r
and
js-beautify -r | find . -name ".html" -type f
找 。-name " .html" -type f | js-beautify -r
and
js-beautify -r | 找 。-name ".html" -type f
but it doesn't work. However, JS-beautify does work if I give something like js-beautify -r myfile.html
or js-beautify -r *.html
(in case of all the files in a directory but not in sub-directory)
但它不起作用。但是,如果我给出类似js-beautify -r myfile.html
或的内容,JS-beautify 确实可以工作(如果js-beautify -r *.html
所有文件都在目录中但不在子目录中)
Can anyone tell how should I be piping these two commands?
谁能告诉我应该如何管道这两个命令?
回答by devnull
However, JS-beautify does work ... in case of all the files in a directory but not in sub-directory
但是,JS-beautify 确实有效……如果所有文件都在目录中但不在子目录中
You've mentioned that JS-beautify works if all the input files are in the same directory. Your command doesn't probably work because you pass all the results from find
which might include input files from different directories.
您已经提到如果所有输入文件都在同一目录中,则 JS-beautify 可以工作。您的命令可能不起作用,因为您传递的所有结果find
可能包括来自不同目录的输入文件。
As mentioned in the comment earlier, you could use -exec
instead:
正如前面评论中提到的,您可以-exec
改用:
find . -type f -name "*.html" -exec js-beautify -r {} \;
Newer versions of GNU find
might use this syntax:
较新版本的 GNUfind
可能使用以下语法:
find . -type f -name "*.html" -exec js-beautify -r {} +
回答by 1j01
I've run into a similar problem and found a simple cross-platform solution using glob-run:
我遇到了类似的问题,并使用glob-run找到了一个简单的跨平台解决方案:
npm i -g glob-run js-beautify
glob-run html-beautify -r **/*.html
It would be nice if js-beautify supported globs itself, though.
不过,如果 js-beautify本身支持 globs那就太好了。
回答by Bill
find+xargs is the way to go. It is faster than find with -exec.
find+xargs 是要走的路。它比使用 -exec 查找要快。
find . -name '*.html' | xargs js-beautify
If, for some reason, you have spaces in your filenames, you'll want to do it like this...
如果由于某种原因,您的文件名中有空格,您会想要这样做......
find . -name '*.html' -print0 | xargs -0 js-beautify
Finally, if for some strange reason, js-beautify won't work with multiple arguments, then you'll need to tell xargs to only pass in one argument at a time. This isn't much different than using the -exec option, but it's better IMO, because it's just more consistent.
最后,如果由于某种奇怪的原因,js-beautify 不能处理多个参数,那么您需要告诉 xargs 一次只传递一个参数。这与使用 -exec 选项没有太大区别,但它更好 IMO,因为它更一致。
find . -name '*.html' | xargs -n 1 js-beautify
Note, you can combine the -print0
and xargs -0
options with xargs -n 1
.
请注意,您可以将-print0
和xargs -0
选项与xargs -n 1
.
edit: As pointed out by T.J. Crowder, the shell will not glob wildcards in double quotes. This was news to me, perhaps there is some ancient environment out there where that isn't true, and hopefully you'll never be forced to work in it.
编辑:正如 TJ Crowder 所指出的,shell 不会在双引号中使用通配符。这对我来说是个新闻,也许有一些古老的环境并非如此,希望你永远不会被迫在其中工作。
回答by Jamie Mason
1) Add these dependencies to your project
1)将这些依赖项添加到您的项目中
npm install --save-dev glob js-beautify
2) Create scripts/format.js
2) 创建 scripts/format.js
const jsBeautify = require('js-beautify')['js_beautify'];
const fs = require('fs');
const glob = require('glob');
const options = {
indent_size: 2,
indent_char: ' ',
indent_with_tabs: false,
eol: '\n',
end_with_newline: true,
indent_level: 0,
preserve_newlines: true,
max_preserve_newlines: 10,
space_in_paren: false,
space_in_empty_paren: false,
jslint_happy: false,
space_after_anon_function: false,
brace_style: 'collapse',
break_chained_methods: false,
keep_array_indentation: false,
unescape_strings: false,
wrap_line_length: 0,
e4x: false,
comma_first: false,
operator_position: 'before-newline'
};
glob('**/!(node_modules)/**/*.js', { absolute: true }, (er, files) => {
files.forEach(file => {
console.log(`js-beautify ${file}`);
const data = fs.readFileSync(file, 'utf8');
const nextData = jsBeautify(data, options);
fs.writeFileSync(file, nextData, 'utf8');
});
});
3) Add a format
script to package.json
3)添加format
脚本package.json
"scripts": {
"format": "node ./scripts/format.js"
}
4) In your project, run
4)在你的项目中,运行
npm run format
回答by Frank Nocke
Combining Bill's wisdom above and these answers on regexp matching, this is the actual solution for my project:
结合上面比尔的智慧和这些关于正则表达式匹配的答案,这是我项目的实际解决方案:
find . -regextype egrep -regex './(src|test|app)/.*.(js|sass|html)' -print0 | xargs -0 ./node_modules/.bin/js-beautify -r
找 。-regextype egrep -regex './(src|test|app)/.*.(js|sass|html)' -print0 | xargs -0 ./node_modules/.bin/js-beautify -r
- only looks in the right folders (i.e. not node_modules)
- goes after js, sass, html
- can handle file names with spaces
- rewrites in place (
-r
) - does not rely on the node shebang
- works with a locally installed js-beautify (
./node_modules/.bin
)
- 只在正确的文件夹中查找(即不是 node_modules)
- 追求 js、sass、html
- 可以处理带空格的文件名
- 原地重写 (
-r
) - 不依赖节点shebang
- 使用本地安装的 js-beautify (
./node_modules/.bin
)
When used inside a package.json script, ./node_modules/.bin
is automatically in the path, \.*
needs to be escaped to \\.*
, thus:
在 package.json 脚本中使用时,./node_modules/.bin
会自动在路径中,\.*
需要转义为\\.*
,因此:
"beautify2": "find . -regextype egrep -regex './(src|test|app)/.*\.(js|sass|html)' -print0 | xargs -0 js-beautify -r"
→
→
beautified app/index.html
beautified app/sass/_atomic.sass
beautified app/sass/_mixin.sass
beautified app/sass/space test.sass
beautified test/Log.test.js
beautified test/deleteAction.test.js
beautified src/util/fileUtils.js
...