我如何递归JS-Beautify?

时间:2014-04-23 17:33:22

标签: javascript bash shell

我在目录和子目录中有很多html文件。我可以通过命令行执行js-beautify命令,并希望以递归方式将其应用于所有这些文件。

我试过了

  找到。 -name" .html" -type f | js-beautify -r and js-beautify -r |找 。 -name" .html" -type f

但它不起作用。但是,如果我提供类似js-beautify -r myfile.htmljs-beautify -r *.html的内容(如果目录中的所有文件但不在子目录中),则JS-beautify会起作用

任何人都可以告诉我应该如何处理这两个命令?

6 个答案:

答案 0 :(得分:17)

  

然而,JS-beautify确实有效......如果目录中的所有文件都存在,但不在子目录中

您已经提到如果所有输入文件都在同一目录中,则JS-beautify可以正常工作。您的命令可能无法正常工作,因为您传递了find的所有结果,其中可能包含来自不同目录的输入文件。

如前面的评论中所述,您可以改为使用-exec

find . -type f -name "*.html" -exec js-beautify -r {} \;

较新版本的GNU find可能会使用以下语法:

find . -type f -name "*.html" -exec js-beautify -r {} +

答案 1 :(得分:14)

我遇到了类似的问题,并使用glob-run找到了一个简单的跨平台解决方案:

npm i -g glob-run js-beautify
glob-run html-beautify -r **/*.html

如果js-beautify supported globs itself会很好。

答案 2 :(得分:5)

1)将这些依赖项添加到项目中

library(R2jags)
m <- 6 # number of problems
n <- 2 # number of people
V <- matrix(c(0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)), nrow=m, ncol=3, byrow = T)
answer <- matrix(c(2,3,1,1,2,3,1,1,1,1,3,2), nrow=n, ncol=m, byrow = T) 

data <- list("m", "n", "V", "answer")
myinits <- list(list(tau = rep(1,n)))
parameters <- c("tau")

samples <- jags(data, inits=myinits, parameters,
                model.file ="model.txt", n.chains=1, n.iter=1000, 
                n.burnin=1, n.thin=1, DIC=T)

2)创建npm install --save-dev glob js-beautify

scripts/format.js

3)将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'); }); }); 脚本添加到format

package.json

4)在你的项目中,运行

"scripts": {
  "format": "node ./scripts/format.js"
}

答案 3 :(得分:4)

找到+ xargs是要走的路。它比使用-exec更快。

find . -name '*.html' | xargs js-beautify 

如果由于某种原因,你的文件名中有空格,你会想要这样做......

find . -name '*.html' -print0 | xargs -0 js-beautify

最后,如果由于一些奇怪的原因,js-beautify不能使用多个参数,那么你需要告诉xargs一次只传入一个参数。这与使用-exec选项没什么不同,但它更好的IMO,因为它更加一致。

find . -name '*.html' | xargs -n 1 js-beautify

注意,您可以将-print0xargs -0选项与xargs -n 1结合使用。

编辑:正如T.J.所指出的那样。克劳德,外壳不会用双引号打出通配符。这对我来说是新闻,也许那里有一些古老的环境,那是不可能的,希望你永远不会被迫在其中工作。

答案 4 :(得分:1)

截至file globs are supported(发布于2018年12月)似乎v1.8.9For example

js-beautify --html --replace '**/*.html'

答案 5 :(得分:0)

结合以上Bill的智慧和these answers on regexp matching这是我项目的实际解决方案

查找。 -regextype egrep -regex'./(src|test|app)/.*.(js|sass|html)'-print0 | xargs -0 ./node_modules/.bin/js-beautify -r

  • 仅在正确的文件夹中查找(即不是node_modules)
  • 跟随js,sass,html
  • 可以处理带空格的文件名
  • 就地重写(Button btn_edit; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView=inflater.inflate(R.layout.multi_frag,container,false); btn_edit=(Button)rootView.findViewById(R.id.edit); btn_edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); return inflater.inflate(R.layout.activity_account,container,false); }
  • 不依赖节点shebang
  • 使用本地安装的js-beautify(-r

在package.json脚本中使用时,./node_modules/.bin自动位于路径中,./node_modules/.bin需要转义到\.*,因此:

\\.*

"beautify2": "find . -regextype egrep -regex './(src|test|app)/.*\\.(js|sass|html)' -print0 | xargs -0 js-beautify -r"
相关问题