js-beautify
Advanced tools
Comparing version 0.3.5 to 0.3.6
79
cli.js
@@ -9,3 +9,3 @@ #!/usr/bin/env node | ||
cc = require('config-chain'), | ||
beautify = require('./beautify').js_beautify, | ||
beautify = require('./index'), | ||
nopt = require('nopt'), | ||
@@ -27,2 +27,6 @@ path = require('path'), | ||
"wrap_line_length": Number, | ||
// HTML-only | ||
"max_char": Number, | ||
"unformatted": [String, Array], | ||
"indent_scripts": ["keep", "separate", "normal"], | ||
// CLI | ||
@@ -35,2 +39,3 @@ "version": Boolean, | ||
"quiet": Boolean, | ||
"type": ["js", "css", "html"], | ||
"config": path | ||
@@ -54,2 +59,6 @@ }, | ||
"w": ["--wrap_line_length"], | ||
// HTML-only | ||
"W": ["--max_char"], | ||
"U": ["--unformatted"], | ||
"S": ["--indent_scripts"], | ||
// non-dasherized hybrid shortcuts | ||
@@ -61,2 +70,5 @@ "good-stuff": [ | ||
], | ||
"js" : ["--type", "js"], | ||
"css" : ["--type", "css"], | ||
"html": ["--type", "html"], | ||
// CLI | ||
@@ -95,2 +107,3 @@ "v": ["--version"], | ||
// Verify arguments | ||
checkType(cfg); | ||
checkFiles(cfg); | ||
@@ -102,3 +115,2 @@ checkIndent(cfg); | ||
cfg.files.forEach(processInputSync, { cfg: cfg }); | ||
logToStdout('\nBeautified ' + cfg.files.length + ' files', cfg); | ||
} | ||
@@ -109,3 +121,3 @@ catch (ex) { | ||
console.error(ex); | ||
console.error('Run `js-beautify -h` for help.'); | ||
console.error('Run `' + getScriptName() + ' -h` for help.'); | ||
process.exit(1); | ||
@@ -121,4 +133,5 @@ } | ||
function usage(err) { | ||
var scriptName = getScriptName(); | ||
var msg = [ | ||
'js-beautify@' + require('./package.json').version, | ||
scriptName + '@' + require('./package.json').version, | ||
'', | ||
@@ -130,3 +143,4 @@ 'CLI Options:', | ||
' --config Path to config file', | ||
' -q, --quiet Suppress output to stdout', | ||
' --type [js|css|html] ["js"]', | ||
' -q, --quiet Suppress logging to stdout', | ||
' -h, --help Show this help', | ||
@@ -137,17 +151,27 @@ ' -v, --version Show the version', | ||
' -s, --indent-size Indentation size [4]', | ||
' -c, --indent-char Indentation character [" "]', | ||
' -l, --indent-level Initial indentation level [0]', | ||
' -t, --indent-with-tabs Indent with tabs, overrides -s and -c', | ||
' -p, --preserve-newlines Preserve existing line-breaks (--no-preserve-newlines disables)', | ||
' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]', | ||
' -j, --jslint-happy Enable jslint-stricter mode', | ||
' -b, --brace-style [collapse|expand|end-expand|expand-strict] ["collapse"]', | ||
' -B, --break-chained-methods Break chained method calls across subsequent lines', | ||
' -k, --keep-array-indentation Preserve array indentation', | ||
' -x, --unescape-strings Decode printable characters encoded in xNN notation', | ||
' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]', | ||
' --good-stuff Warm the cockles of Crockford\'s heart', | ||
'' | ||
' -c, --indent-char Indentation character [" "]' | ||
]; | ||
switch (scriptName.split('-').shift()) { | ||
case "js": | ||
msg.push(' -l, --indent-level Initial indentation level [0]'); | ||
msg.push(' -t, --indent-with-tabs Indent with tabs, overrides -s and -c'); | ||
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)'); | ||
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]'); | ||
msg.push(' -j, --jslint-happy Enable jslint-stricter mode'); | ||
msg.push(' -b, --brace-style [collapse|expand|end-expand|expand-strict] ["collapse"]'); | ||
msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines'); | ||
msg.push(' -k, --keep-array-indentation Preserve array indentation'); | ||
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation'); | ||
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]'); | ||
msg.push(' --good-stuff Warm the cockles of Crockford\'s heart'); | ||
break; | ||
case "html": | ||
msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]'); | ||
msg.push(' -S, --indent-scripts [keep|separate|normal] ["normal"]'); | ||
msg.push(' -W, --max-char Maximum characters per line (0 disables) [250]'); | ||
msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted'); | ||
break; | ||
} | ||
if (err) { | ||
@@ -195,3 +219,3 @@ msg.push(err); | ||
try { | ||
var pretty = beautify(code, config); | ||
var pretty = beautify[config.type](code, config); | ||
@@ -267,2 +291,19 @@ // ensure newline at end of beautified output | ||
function getScriptName() { | ||
return path.basename(process.argv[1]); | ||
} | ||
function checkType(parsed) { | ||
var scriptType = getScriptName().split('-').shift(); | ||
debug("executable type:", scriptType); | ||
var parsedType = parsed.type; | ||
debug("parsed type:", parsedType); | ||
if (!parsedType) { | ||
debug("type defaulted:", scriptType); | ||
parsed.type = scriptType; | ||
} | ||
} | ||
function checkIndent(parsed) { | ||
@@ -269,0 +310,0 @@ if (parsed["indent_with_tabs"]) { |
36
index.js
@@ -1,1 +0,35 @@ | ||
module.exports = require('./beautify').js_beautify; | ||
/** | ||
The following batches are equivalent: | ||
var beautify_js = require('js-beautify'); | ||
var beautify_js = require('js-beautify').js; | ||
var beautify_js = require('js-beautify').js_beautify; | ||
var beautify_css = require('js-beautify').css; | ||
var beautify_css = require('js-beautify').css_beautify; | ||
var beautify_html = require('js-beautify').html; | ||
var beautify_html = require('js-beautify').html_beautify; | ||
All methods returned accept two arguments, the source string and an options object. | ||
**/ | ||
var js_beautify = require('./beautify').js_beautify; | ||
var css_beautify = require('./beautify-css').css_beautify; | ||
var html_beautify = require('./beautify-html').html_beautify; | ||
// the default is js | ||
var beautify = function (src, config) { | ||
return js_beautify(src, config); | ||
}; | ||
// short aliases | ||
beautify.js = js_beautify; | ||
beautify.css = css_beautify; | ||
beautify.html = html_beautify; | ||
// legacy aliases | ||
beautify.js_beautify = js_beautify; | ||
beautify.css_beautify = css_beautify; | ||
beautify.html_beautify = html_beautify; | ||
module.exports = beautify; |
{ | ||
"name": "js-beautify", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "jsbeautifier.org for node", | ||
@@ -8,2 +8,4 @@ "main": "beautify.js", | ||
"bin": { | ||
"css-beautify": "./cli.js", | ||
"html-beautify": "./cli.js", | ||
"js-beautify": "./cli.js" | ||
@@ -10,0 +12,0 @@ }, |
@@ -72,2 +72,4 @@ # JS Beautifier | ||
--config Path to config file | ||
--type [js|css|html] ["js"] | ||
-q, --quiet Suppress logging to stdout | ||
-v, --version Show the version | ||
@@ -88,3 +90,4 @@ -h, --help Show this help | ||
-x, --unescape-strings Decode printable characters encoded in xNN notation | ||
-g, --good-stuff Warm the cockles of Crockford's heart | ||
-w, --wrap-line-length Wrap lines at next opportunity after N characters [0] | ||
--good-stuff Warm the cockles of Crockford's heart | ||
``` | ||
@@ -109,3 +112,4 @@ | ||
"eval_code": false, | ||
"unescape_strings": false | ||
"unescape_strings": false, | ||
"wrap_line_length": 0 | ||
} | ||
@@ -124,2 +128,31 @@ ``` | ||
#### CSS & HTML | ||
In addition to the `js-beautify` executable, `css-beautify` and `html-beautify` are also provided as an easy interface into those scripts. Alternatively, `js-beautify --css` or `js-beautify --html` will accomplish the same thing, respectively. | ||
```js | ||
// Programmatic access | ||
var beautify_js = require('js-beautify'); // also available under "js" export | ||
var beautify_css = require('js-beautify').css; | ||
var beautify_html = require('js-beautify').html; | ||
// All methods accept two arguments, the string to be beautified, and an options object. | ||
``` | ||
The CSS & HTML beautifiers are much simpler in scope, and possess far fewer options. | ||
```text | ||
CSS Beautifier Options: | ||
-s, --indent-size Indentation size [4] | ||
-c, --indent-char Indentation character [" "] | ||
HTML Beautifier Options: | ||
-s, --indent-size Indentation size [4] | ||
-c, --indent-char Indentation character [" "] | ||
-b, --brace-style [collapse|expand|end-expand] ["collapse"] | ||
-S, --indent-scripts [keep|separate|normal] ["normal"] | ||
-W, --max-char Maximum characters per line (0 disables) [250] | ||
-U, --unformatted List of tags (defaults to inline) that should not be reformatted | ||
``` | ||
## License | ||
@@ -126,0 +159,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114392
2335
167