ECMAScript code beautifier/formatter
ECMAScript code beautifier/formatter.
This tool is still missing support for many important features. Please report any bugs you find, the code is only as good as the test cases. Feature requests are very welcome.
We are looking for contributors!!
For more reasoning behind it and history of the project see: esformatter & rocambole
var esformatter = require'esformatter';var fs = require'fs';var codeStr = fsreadFileSync'path/to/js/file.js'toString;// for a list of available options check "lib/preset/default.json"var options =indent :value : ' 'lineBreak :before :// at least one line break before BlockStatementBlockStatement : '>=1'// only one line break before DoWhileStatementOpeningBraceDoWhileStatementOpeningBrace : 1// ...whiteSpace :// ...;// return a string with the formatted codevar formattedCode = esformatterformatcodeStr options;
See the doc/api.md file for a list of all the public methods and detailed documentation about each one.
See doc/config.md for more info about the configuration options.
You can also use the simple command line interface to process the
read from a file.
npm install [-g] esformatter
--help flag to see the available options or see
# Format# ======# format "test.js" and output result to stdoutesformatter test.js# you can also pipe other shell commands (read file from stdin)cat test.js | esformatter# format "test.js" using options in "options.json" and output result to stdoutesformatter --config options.json test.js# process "test.js" and writes to "test.out.js"esformatter test.js > test.out.js# you can override the default settings, see lib/preset/default.json for# a list of available optionsesformatter test.js --indent.value="\t" --lineBreak.before.IfStatementOpeningBrace=0# format "test.js" and output result to "test.js"esformatter -i test.js# format and overwrite all the ".js" files inside the "lib/" folderesformatter -i 'lib/*.js'# format and overwrite all the ".js" files inside "lib/" and it's subfoldersesformatter -i 'lib/**/*.js'**important:** surround the glob with single quotes to avoid expansion; [globsyntax reference]# Diff# ======# check if "test.js" matches style and output diff to stdoutesformatter --diff test.js# check if "test.js" matches style and output unified diff to stdoutesformatter --diff-unified test.js# check if "test.js" matches "options.json" style and output diff to stdoutesformatter --diff --config options.json test.js# check all files inside "lib/" and it's subfoldersesformatter --diff 'lib/**/*.js'
If a locally installed
esformatter is found, the CLI uses that instead of the
global executable (this means you can have multiple projects depending on
different versions of esformatter).
esformatter and all the plugins that you need on your project
to the package.json
that way you can use locally installed plugins and also make sure everyone on
your team is using the same version/settings.
Since esformatter is available as a command-line tool, it can be used in any editor that supports external shell commands.
Since we don't expect everyone to write plugins that only works with esformatter we decided to encourage the usage of standalone CLI tools.
// pipe is a simple way to "pipe" multiple binaries input/output"pipe":// scripts listed as "before" will be executed before esformatter// and will forward output to next command in the queue"before":"strip-debug""./bin/my-custom-script.sh --foo true -zx"// scripts listed as "after" will be executed after esformatter"after":"baz --keepLineBreaks"
Plugins are automatically loaded from
node_modules if you pass the module id
in the config file:
List of plugins and plugins wish list: https://github.com/millermedeiros/esformatter/wiki/Plugins
List of plugins with easy filterable search: http://pgilad.github.io/esformatter-plugins/
For detailed information about plugins structure and API see doc/plugins.md
We have an IRC channel #esformatter on irc.freenode.net for quick discussions about the project development/structure.
See project Wiki for more info: https://github.com/millermedeiros/esformatter/wiki
Released under the MIT license