Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: de5fe5f200
Fetching contributors…

Cannot retrieve contributors at this time

executable file 81 lines (75 sloc) 2.79 kB
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
nopt = require('nopt'),
minify = require(path.join(__dirname, '../lib/minify')),
input = "",
knownOpts = {
"mode": ["minify", "beautify"],
"level": Number,
"engine": String,
"verbose": Boolean,
"output": path
},
shorthands = {
"v": ["--verbose"],
"m": ["--mode minify"],
"b": ["--mode beautify"]
},
parsed = nopt(knownOpts, shorthands, process.argv);
if (parsed.help) {
console.log("Usage: minifyjs [-m|b] [-v|--verbose] [--engine ENG] [--level 0|1|2] [--output /dir/file.js]\n" +
"\n" +
"Options:\n" +
" -m Minify. If -b is not specified, this is the default.\n" +
" -b Beautify\n" +
" -v/--verbose Turn verbosity on\n" +
" --level Minification level. Can be 0-2.\n" +
" --engine Use engine x. Options are below\n" +
" --output Output to a specific file, rather than stdout\n" +
"\n\n" +
"Available minifiers:\n\t" +
Object.keys(minify.minifiers).join("\n\t")+"\n"+
"Available beautifiers:\n\t" +
Object.keys(minify.beautifiers).join("\n\t")+"\n\n");
process.exit(1);
}
parsed.mode = parsed.mode || 'minify';
parsed.level = parsed.level || 0;
if (parsed.level < 0 || parsed.level > 2) throw new Error("Level must be between 0 and 2.");
function processIt(data) {
var fn = minify.minify;
if (parsed.mode === 'beautify') fn = minify.beautify;
var options = {};
if (parsed.level) options.level = parsed.level;
if (parsed.engine) options.engine = parsed.engine;
fn(data, options, function (err, data) {
if (err) {
process.stderr.write("Looks like we got some errors:");
err = err instanceof Array ? err : [err];
err.forEach(function (e) {
process.stderr.write("\t" + (e.message || e) + "\n\n");
});
process.exit(1);
}
if (!options.output) return console.log(data.toString());
fs.writeFile(options.output, data, function (e) {
if (e) throw e;
process.exit(0);
});
});
}
if (parsed.argv.remain.length) {
input = fs.readFile(path.resolve(process.cwd(), parsed.argv.remain[0]), function (err, data) {
if (err) throw err;
processIt(data.toString());
});
} else {
process.stdin.resume();
process.stdin.on('data', function (chunk) {
input += chunk.toString();
});
process.stdin.on('end', function () {
processIt(input);
});
}
Jump to Line
Something went wrong with that request. Please try again.