2012-07-13 23:54:36 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2012-12-06 21:56:34 +00:00
|
|
|
var debug = process.env.DEBUG_JSBEAUTIFY || process.env.JSBEAUTIFY_DEBUG
|
|
|
|
? function () { console.error.apply(console, arguments); }
|
|
|
|
: function () {};
|
|
|
|
|
2012-07-13 23:54:36 +00:00
|
|
|
var fs = require('fs'),
|
2012-12-06 21:56:34 +00:00
|
|
|
cc = require('config-chain'),
|
2013-03-18 18:47:24 +00:00
|
|
|
beautify = require('./index'),
|
2012-12-06 21:56:34 +00:00
|
|
|
nopt = require('nopt'),
|
|
|
|
path = require('path'),
|
|
|
|
knownOpts = {
|
|
|
|
// Beautifier
|
|
|
|
"indent_size": Number,
|
|
|
|
"indent_char": String,
|
|
|
|
"indent_level": Number,
|
|
|
|
"indent_with_tabs": Boolean,
|
|
|
|
"preserve_newlines": Boolean,
|
|
|
|
"max_preserve_newlines": Number,
|
|
|
|
"jslint_happy": Boolean,
|
|
|
|
"brace_style": ["collapse", "expand", "end-expand", "expand-strict"],
|
|
|
|
"break_chained_methods": Boolean,
|
|
|
|
"keep_array_indentation": Boolean,
|
|
|
|
"unescape_strings": Boolean,
|
2013-03-13 18:23:39 +00:00
|
|
|
"wrap_line_length": Number,
|
2012-12-06 21:56:34 +00:00
|
|
|
// CLI
|
|
|
|
"version": Boolean,
|
|
|
|
"help": Boolean,
|
|
|
|
"files": [path, Array],
|
|
|
|
"outfile": path,
|
|
|
|
"replace": Boolean,
|
2013-02-28 11:59:30 +00:00
|
|
|
"quiet": Boolean,
|
2012-12-06 21:56:34 +00:00
|
|
|
"config": path
|
|
|
|
},
|
|
|
|
// dasherizeShorthands provides { "indent-size": ["--indent_size"] }
|
|
|
|
// translation, allowing more convenient dashes in CLI arguments
|
|
|
|
shortHands = dasherizeShorthands({
|
|
|
|
// Beautifier
|
|
|
|
"s": ["--indent_size"],
|
|
|
|
"c": ["--indent_char"],
|
|
|
|
"l": ["--indent_level"],
|
|
|
|
"t": ["--indent_with_tabs"],
|
|
|
|
"p": ["--preserve_newlines"],
|
|
|
|
"m": ["--max_preserve_newlines"],
|
|
|
|
"j": ["--jslint_happy"],
|
|
|
|
"b": ["--brace_style"],
|
|
|
|
"B": ["--break_chained_methods"],
|
|
|
|
"k": ["--keep_array_indentation"],
|
|
|
|
"x": ["--unescape_strings"],
|
2013-03-13 18:23:39 +00:00
|
|
|
"w": ["--wrap_line_length"],
|
2012-12-06 21:56:34 +00:00
|
|
|
// non-dasherized hybrid shortcuts
|
|
|
|
"good-stuff": [
|
|
|
|
"--keep_array_indentation",
|
|
|
|
"--keep_function_indentation",
|
|
|
|
"--jslint_happy"
|
|
|
|
],
|
|
|
|
// CLI
|
|
|
|
"v": ["--version"],
|
|
|
|
"h": ["--help"],
|
|
|
|
"f": ["--files"],
|
|
|
|
"o": ["--outfile"],
|
2013-02-28 11:59:30 +00:00
|
|
|
"r": ["--replace"],
|
|
|
|
"q": ["--quiet"]
|
2012-12-06 21:56:34 +00:00
|
|
|
// no shorthand for "config"
|
2013-03-13 19:01:31 +00:00
|
|
|
});
|
2012-12-06 21:56:34 +00:00
|
|
|
|
2013-03-13 19:01:31 +00:00
|
|
|
// var cli = require('js-beautify/cli'); cli.interpret();
|
|
|
|
var interpret = exports.interpret = function (argv, slice) {
|
|
|
|
var parsed = nopt(knownOpts, shortHands, argv, slice);
|
2012-07-17 17:51:00 +00:00
|
|
|
|
2013-03-13 19:01:31 +00:00
|
|
|
if (parsed.version) {
|
|
|
|
console.log(require('./package.json').version);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
else if (parsed.help) {
|
|
|
|
usage();
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2012-07-13 23:54:36 +00:00
|
|
|
|
2013-03-13 19:01:31 +00:00
|
|
|
var cfg = cc(
|
|
|
|
parsed,
|
|
|
|
cleanOptions(cc.env('jsbeautify_'), knownOpts),
|
|
|
|
parsed.config,
|
|
|
|
cc.find('.jsbeautifyrc'),
|
|
|
|
__dirname + '/config/defaults.json'
|
|
|
|
).snapshot;
|
2012-12-10 20:24:37 +00:00
|
|
|
|
2013-03-13 19:01:31 +00:00
|
|
|
try {
|
|
|
|
// Verify arguments
|
|
|
|
checkFiles(cfg);
|
|
|
|
checkIndent(cfg);
|
|
|
|
debug(cfg);
|
2012-12-10 20:24:37 +00:00
|
|
|
|
2013-03-13 19:01:31 +00:00
|
|
|
// Process files synchronously to avoid EMFILE error
|
|
|
|
cfg.files.forEach(processInputSync, { cfg: cfg });
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
debug(cfg);
|
|
|
|
// usage(ex);
|
|
|
|
console.error(ex);
|
|
|
|
console.error('Run `js-beautify -h` for help.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
2012-07-16 20:20:45 +00:00
|
|
|
|
2013-03-13 19:01:31 +00:00
|
|
|
// interpret args immediately when called as executable
|
|
|
|
if (require.main === module) {
|
|
|
|
interpret();
|
|
|
|
}
|
2012-07-13 23:54:36 +00:00
|
|
|
|
2012-12-06 21:56:34 +00:00
|
|
|
function usage(err) {
|
|
|
|
var msg = [
|
|
|
|
'js-beautify@' + require('./package.json').version,
|
|
|
|
'',
|
|
|
|
'CLI Options:',
|
2013-03-13 18:28:55 +00:00
|
|
|
' -f, --file Input file(s) (Pass \'-\' for stdin)',
|
|
|
|
' -r, --replace Write output in-place, replacing input',
|
|
|
|
' -o, --outfile Write output to file (default stdout)',
|
|
|
|
' --config Path to config file',
|
2013-03-15 19:00:14 +00:00
|
|
|
' -q, --quiet Suppress logging to stdout',
|
2013-03-13 18:28:55 +00:00
|
|
|
' -h, --help Show this help',
|
|
|
|
' -v, --version Show the version',
|
2012-12-06 21:56:34 +00:00
|
|
|
'',
|
|
|
|
'Beautifier Options:',
|
|
|
|
' -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',
|
2013-03-13 18:23:39 +00:00
|
|
|
' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]',
|
2013-03-13 18:16:15 +00:00
|
|
|
' --good-stuff Warm the cockles of Crockford\'s heart',
|
2012-12-06 21:56:34 +00:00
|
|
|
''
|
|
|
|
];
|
2012-07-13 23:54:36 +00:00
|
|
|
|
2012-12-06 21:56:34 +00:00
|
|
|
if (err) {
|
|
|
|
msg.push(err);
|
|
|
|
msg.push('');
|
|
|
|
console.error(msg.join('\n'));
|
|
|
|
} else {
|
|
|
|
console.log(msg.join('\n'));
|
2012-08-02 19:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 21:56:34 +00:00
|
|
|
// main iterator, {cfg} passed as thisArg of forEach call
|
2012-12-10 20:24:37 +00:00
|
|
|
function processInputSync(filepath) {
|
2012-07-13 23:54:36 +00:00
|
|
|
var data = '',
|
2012-12-06 21:56:34 +00:00
|
|
|
config = this.cfg,
|
2012-12-10 20:24:37 +00:00
|
|
|
outfile = config.outfile,
|
2012-07-16 18:23:53 +00:00
|
|
|
input;
|
2012-07-14 00:21:32 +00:00
|
|
|
|
2012-12-10 20:24:37 +00:00
|
|
|
// -o passed with no value overwrites
|
|
|
|
if (outfile === true || config.replace) {
|
|
|
|
outfile = filepath;
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:32 +00:00
|
|
|
if (filepath === '-') {
|
2012-07-16 18:23:53 +00:00
|
|
|
input = process.stdin;
|
|
|
|
input.resume();
|
|
|
|
input.setEncoding('utf8');
|
2012-07-13 23:54:36 +00:00
|
|
|
|
2012-12-10 20:24:37 +00:00
|
|
|
input.on('data', function (chunk) {
|
|
|
|
data += chunk;
|
|
|
|
});
|
2012-07-16 18:23:53 +00:00
|
|
|
|
2012-12-10 20:24:37 +00:00
|
|
|
input.on('end', function () {
|
|
|
|
makePretty(data, config, outfile, writePretty);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = fs.readFileSync(filepath, 'utf8');
|
|
|
|
makePretty(data, config, outfile, writePretty);
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 18:23:53 +00:00
|
|
|
|
2012-12-10 20:24:37 +00:00
|
|
|
function makePretty(code, config, outfile, callback) {
|
|
|
|
try {
|
|
|
|
var pretty = beautify(code, config);
|
2012-07-16 18:23:53 +00:00
|
|
|
|
2012-07-16 18:36:00 +00:00
|
|
|
// ensure newline at end of beautified output
|
|
|
|
pretty += '\n';
|
|
|
|
|
2013-03-13 18:39:57 +00:00
|
|
|
callback(null, pretty, outfile, config);
|
2012-12-10 20:24:37 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
callback(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 18:39:57 +00:00
|
|
|
function writePretty(err, pretty, outfile, config) {
|
2012-12-10 20:24:37 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2012-07-16 18:23:53 +00:00
|
|
|
|
2012-12-10 20:24:37 +00:00
|
|
|
if (outfile) {
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(outfile, pretty, 'utf8');
|
2013-03-13 18:39:57 +00:00
|
|
|
logToStdout('beautified ' + path.relative(process.cwd(), outfile), config);
|
2012-07-16 18:23:53 +00:00
|
|
|
}
|
2012-12-10 20:24:37 +00:00
|
|
|
catch (ex) {
|
|
|
|
onOutputError(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
process.stdout.write(pretty);
|
|
|
|
}
|
2012-12-06 21:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// workaround the fact that nopt.clean doesn't return the object passed in :P
|
|
|
|
function cleanOptions(data, types) {
|
|
|
|
nopt.clean(data, types);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// error handler for output stream that swallows errors silently,
|
|
|
|
// allowing the loop to continue over unwritable files.
|
|
|
|
function onOutputError(err) {
|
|
|
|
if (err.code === 'EACCES') {
|
|
|
|
console.error(err.path + " is not writable. Skipping!");
|
|
|
|
}
|
2012-12-10 20:24:37 +00:00
|
|
|
else {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2012-12-06 21:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// turn "--foo_bar" into "foo-bar"
|
|
|
|
function dasherizeFlag(str) {
|
|
|
|
return str.replace(/^\-+/, '').replace(/_/g, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate weird python underscored keys into dashed argv,
|
|
|
|
// avoiding single character aliases.
|
|
|
|
function dasherizeShorthands(hash) {
|
|
|
|
// operate in-place
|
|
|
|
Object.keys(hash).forEach(function (key) {
|
|
|
|
// each key value is an array
|
|
|
|
var val = hash[key][0];
|
|
|
|
// only dasherize one-character shorthands
|
|
|
|
if (key.length === 1 && val.indexOf('_') > -1) {
|
|
|
|
hash[dasherizeFlag(val)] = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkIndent(parsed) {
|
|
|
|
if (parsed["indent_with_tabs"]) {
|
|
|
|
parsed["indent_size"] = 1;
|
|
|
|
parsed["indent_char"] = "\t";
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkFiles(parsed) {
|
|
|
|
var argv = parsed.argv;
|
|
|
|
|
|
|
|
if (!parsed.files) {
|
|
|
|
parsed.files = [];
|
2012-12-10 20:24:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-12-06 21:56:34 +00:00
|
|
|
if (argv.cooked.indexOf('-') > -1) {
|
|
|
|
// strip stdin path eagerly added by nopt in '-f -' case
|
|
|
|
parsed.files.some(removeDashedPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.remain.length) {
|
|
|
|
// assume any remaining args are files
|
|
|
|
argv.remain.forEach(function (f) {
|
|
|
|
parsed.files.push(path.resolve(f));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('string' === typeof parsed.outfile && !parsed.files.length) {
|
|
|
|
// use outfile as input when no other files passed in args
|
|
|
|
parsed.files.push(parsed.outfile);
|
|
|
|
// operation is now an implicit overwrite
|
|
|
|
parsed.replace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.original.indexOf('-') > -1) {
|
|
|
|
// ensure '-' without '-f' still consumes stdin
|
|
|
|
parsed.files.push('-');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parsed.files.length) {
|
|
|
|
throw 'Must define at least one file.';
|
|
|
|
}
|
2012-12-10 20:24:37 +00:00
|
|
|
debug('files.length ' + parsed.files.length);
|
2012-12-06 21:56:34 +00:00
|
|
|
|
|
|
|
parsed.files.forEach(testFilePath);
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeDashedPath(filepath, i, arr) {
|
|
|
|
var found = filepath.lastIndexOf('-') === (filepath.length - 1);
|
|
|
|
if (found) {
|
|
|
|
arr.splice(i, 1);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
function testFilePath(filepath) {
|
|
|
|
try {
|
|
|
|
if (filepath !== "-") {
|
|
|
|
fs.statSync(filepath);
|
|
|
|
}
|
2012-12-10 20:24:37 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2012-12-06 21:56:34 +00:00
|
|
|
throw 'Unable to open path "' + filepath + '"';
|
|
|
|
}
|
|
|
|
}
|
2013-02-28 11:59:30 +00:00
|
|
|
|
2013-03-13 18:39:57 +00:00
|
|
|
function logToStdout(str, config) {
|
|
|
|
if (typeof config.quiet === "undefined" || !config.quiet) {
|
2013-02-28 11:59:30 +00:00
|
|
|
console.log(str);
|
|
|
|
}
|
|
|
|
}
|