2012-07-13 23:54:36 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
beautify = require('./beautify').js_beautify,
|
2012-07-16 20:34:06 +00:00
|
|
|
options = require('./config/defaults'),
|
2012-07-13 23:54:36 +00:00
|
|
|
argv = require('optimist')
|
|
|
|
.usage("Reformat JS files in idiomatic style.\n\nUsage:\n $0 [options] path/to/file [...]")
|
2012-07-16 20:34:06 +00:00
|
|
|
.options(require('./config/options'))
|
2012-07-13 23:54:36 +00:00
|
|
|
.check(function (argv) {
|
|
|
|
if (argv.file) {
|
|
|
|
if (Array.isArray(argv.file)) {
|
2012-07-14 00:21:32 +00:00
|
|
|
// might pass multiple -f args
|
2012-07-13 23:54:36 +00:00
|
|
|
argv._ = argv._.concat(argv.file);
|
|
|
|
}
|
2012-07-14 00:21:32 +00:00
|
|
|
else if ('string' === typeof argv.file) {
|
|
|
|
// `cat foo.js | cli.js -f -`, avoid ['-', true]
|
2012-07-13 23:54:36 +00:00
|
|
|
argv._.push(argv.file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 20:20:45 +00:00
|
|
|
if ('string' === typeof argv.outfile && !argv._.length) {
|
|
|
|
// use outfile as input when no other files passed in args
|
|
|
|
argv._.push(argv.outfile);
|
|
|
|
// operation is now an implicit overwrite
|
|
|
|
argv.replace = true;
|
|
|
|
}
|
|
|
|
|
2012-07-13 23:54:36 +00:00
|
|
|
if (!argv._.length) {
|
|
|
|
throw 'Must define at least one file.';
|
|
|
|
}
|
|
|
|
|
|
|
|
argv._.forEach(function (filepath) {
|
|
|
|
try {
|
2012-07-14 00:21:32 +00:00
|
|
|
if (filepath !== "-") {
|
|
|
|
fs.statSync(filepath);
|
|
|
|
}
|
2012-07-13 23:54:36 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw 'Unable to open path "' + filepath + '"';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.argv;
|
|
|
|
|
|
|
|
if (argv["indent-with-tabs"]) {
|
|
|
|
argv["indent-size"] = 1;
|
|
|
|
argv["indent-char"] = "\t";
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:32 +00:00
|
|
|
// translate dashed options into weird python underscored keys,
|
|
|
|
// avoiding single character aliases.
|
2012-07-13 23:54:36 +00:00
|
|
|
Object.keys(argv).forEach(function (key) {
|
|
|
|
if (key.length > 1) {
|
|
|
|
options[key.replace(/-/g, '_')] = argv[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
argv._.forEach(function (filepath) {
|
|
|
|
var data = '',
|
2012-07-16 18:23:53 +00:00
|
|
|
input;
|
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-14 00:21:32 +00:00
|
|
|
} else {
|
2012-07-16 18:23:53 +00:00
|
|
|
input = fs.createReadStream(filepath, { encoding: 'utf8' });
|
2012-07-14 00:21:32 +00:00
|
|
|
}
|
2012-07-13 23:54:36 +00:00
|
|
|
|
2012-07-16 18:23:53 +00:00
|
|
|
input.on('data', function (chunk) {
|
2012-07-13 23:54:36 +00:00
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
|
2012-07-16 18:23:53 +00:00
|
|
|
input.on('end', function () {
|
|
|
|
var pretty = beautify(data, options),
|
|
|
|
output;
|
|
|
|
|
2012-07-16 20:20:45 +00:00
|
|
|
// -o passed with no value overwrites
|
|
|
|
if (options.outfile === true || options.replace) {
|
2012-07-16 18:23:53 +00:00
|
|
|
options.outfile = filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.outfile) {
|
|
|
|
output = fs.createWriteStream(options.outfile, {
|
|
|
|
flags: "w",
|
|
|
|
encoding: "utf8",
|
|
|
|
mode: 0644
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
output = process.stdout;
|
|
|
|
}
|
|
|
|
|
2012-07-16 18:36:00 +00:00
|
|
|
// ensure newline at end of beautified output
|
|
|
|
pretty += '\n';
|
|
|
|
|
2012-07-16 18:23:53 +00:00
|
|
|
output.write(pretty);
|
|
|
|
|
|
|
|
if (options.outfile) {
|
|
|
|
output.end();
|
|
|
|
}
|
2012-07-13 23:54:36 +00:00
|
|
|
});
|
|
|
|
});
|