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,
|
2013-03-18 20:30:07 +00:00
|
|
|
// HTML-only
|
|
|
|
"max_char": Number,
|
|
|
|
"unformatted": [String, Array],
|
|
|
|
"indent_scripts": ["keep", "separate", "normal"],
|
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,
|
2013-03-18 18:53:41 +00:00
|
|
|
"type": ["js", "css", "html"],
|
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"],
|
2013-03-18 20:30:07 +00:00
|
|
|
// HTML-only
|
|
|
|
"W": ["--max_char"],
|
|
|
|
"U": ["--unformatted"],
|
|
|
|
"S": ["--indent_scripts"],
|
2012-12-06 21:56:34 +00:00
|
|
|
// non-dasherized hybrid shortcuts
|
|
|
|
"good-stuff": [
|
|
|
|
"--keep_array_indentation",
|
|
|
|
"--keep_function_indentation",
|
|
|
|
"--jslint_happy"
|
|
|
|
],
|
2013-03-18 18:53:41 +00:00
|
|
|
"js" : ["--type", "js"],
|
|
|
|
"css" : ["--type", "css"],
|
|
|
|
"html": ["--type", "html"],
|
2012-12-06 21:56:34 +00:00
|
|
|
// 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
|
2013-03-18 18:53:41 +00:00
|
|
|
checkType(cfg);
|
2013-03-13 19:01:31 +00:00
|
|
|
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);
|
2013-03-18 19:48:28 +00:00
|
|
|
console.error('Run `' + getScriptName() + ' -h` for help.');
|
2013-03-13 19:01:31 +00:00
|
|
|
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) {
|
2013-03-18 19:48:28 +00:00
|
|
|
var scriptName = getScriptName();
|
2012-12-06 21:56:34 +00:00
|
|
|
var msg = [
|
2013-03-18 19:48:28 +00:00
|
|
|
scriptName + '@' + require('./package.json').version,
|
2012-12-06 21:56:34 +00:00
|
|
|
'',
|
|
|
|
'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-18 18:53:41 +00:00
|
|
|
' --type [js|css|html] ["js"]',
|
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]',
|
2013-03-18 20:30:07 +00:00
|
|
|
' -c, --indent-char Indentation character [" "]'
|
2012-12-06 21:56:34 +00:00
|
|
|
];
|
2012-07-13 23:54:36 +00:00
|
|
|
|
2013-03-18 20:30:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-03-18 21:00:43 +00:00
|
|
|
var fileType = getOutputType(outfile, config.type);
|
|
|
|
var pretty = beautify[fileType](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;
|
|
|
|
}
|
|
|
|
|
2013-03-18 21:00:43 +00:00
|
|
|
function getOutputType(outfile, configType) {
|
|
|
|
if (outfile && /\.(js|css|html)$/.test(outfile)) {
|
|
|
|
return outfile.split('.').pop();
|
|
|
|
}
|
|
|
|
return configType;
|
|
|
|
}
|
|
|
|
|
2013-03-18 19:48:28 +00:00
|
|
|
function getScriptName() {
|
|
|
|
return path.basename(process.argv[1]);
|
|
|
|
}
|
|
|
|
|
2013-03-18 18:53:41 +00:00
|
|
|
function checkType(parsed) {
|
2013-03-18 19:48:28 +00:00
|
|
|
var scriptType = getScriptName().split('-').shift();
|
|
|
|
debug("executable type:", scriptType);
|
|
|
|
|
2013-03-18 18:53:41 +00:00
|
|
|
var parsedType = parsed.type;
|
|
|
|
debug("parsed type:", parsedType);
|
|
|
|
|
|
|
|
if (!parsedType) {
|
2013-03-18 19:48:28 +00:00
|
|
|
debug("type defaulted:", scriptType);
|
|
|
|
parsed.type = scriptType;
|
2013-03-18 18:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 21:56:34 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|