Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 148 lines (118 sloc) 3.84 KB
#! /usr/bin/env node
/**
* UglifyCSS
* Port of YUI CSS Compressor to NodeJS
* Author: Franck Marcia - https://github.com/fmarcia
* MIT licenced
*/
/**
* cssmin.js
* Author: Stoyan Stefanov - http://phpied.com/
* This is a JavaScript port of the CSS minification tool
* distributed with YUICompressor, itself a port
* of the cssmin utility by Isaac Schlueter - http://foohack.com/
* Permission is hereby granted to use the JavaScript version under the same
* conditions as the YUICompressor (original YUICompressor note below).
*/
/**
* YUI Compressor
* http://developer.yahoo.com/yui/compressor/
* Author: Julien Lecomte - http://www.julienlecomte.net/
* Copyright (c) 2011 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license.
*/
var uglifycss = require("./");
var fs = require("fs");
var UGLIFYCSS_VERSION = "0.0.27";
// Print usage
function usage() {
console.log(
"Usage: uglifycss [options] file1.css [file2.css [...]] > output\n" +
"options:\n" +
"--max-line-len n add a newline every n characters\n" +
"--expand-vars expand variables\n" +
"--ugly-comments remove newlines within preserved comments\n" +
"--cute-comments preserve newlines within and around preserved comments\n" +
"--convert-urls d convert relative urls using the d directory as location target\n" +
"--debug print full error stack on error\n" +
"--output f put the result in f file\n" +
"--help show this help\n" +
"--version display version number"
);
}
// Print version
function version() {
console.log("uglifycss " + UGLIFYCSS_VERSION);
}
// Transform strings like "the-parameter" to "theParameter"
function par2var(param) {
return param.replace(/-(.)/g, function (ignore, character) {
return character === "-" ? "" : character.toUpperCase();
});
}
// Parse parameters from command line
function parseArguments(argv) {
var params = {
options: uglifycss.defaultOptions,
files: []
};
var len = argv.length;
for (var i = 2; i < len; i += 1) {
// get "propertyName" from "--argument-name"
var v = par2var(argv[i]);
// boolean parameters
if (typeof params.options[v] === "boolean") {
params.options[v] = true;
// string parameter
} else if (typeof params.options[v] === "string") {
params.options[v] = argv[i + 1] || "";
i += 1;
// integer parameter
} else if (typeof params.options[v] !== "undefined") {
params.options[v] = parseInt(argv[i + 1], 10);
i += 1;
// help
} else if (argv[i] === "--help") {
usage();
return "help";
// version
} else if (argv[i] === "--version") {
version();
return "version";
// file
} else {
params.files.push(argv[i]);
}
}
return params;
}
// Entry
(function main() {
var params = parseArguments(process.argv);
if (params === "help" || params === "version") {
return;
}
var stdin = process.stdin;
var content = "";
// . files
if (params.files.length) {
var result = uglifycss.processFiles(params.files, params.options);
if (params.options.output) {
fs.writeFileSync(params.options.output, result);
} else {
console.log(result);
}
// . usage
} else if (stdin.isTTY) {
usage();
// . stdin
} else {
stdin.on("data", function (part) {
content += part;
});
stdin.on("end", function () {
console.log(uglifycss.processString(content, params.options));
});
}
}());