#! /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("./"); // Print usage function usage() { console.log( "Usage: uglifycss [options] file1.css [file2.css [...]] > output\n" + "options:\n" + "--max-line-len x add a newline every x 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 with the d directory as css location\n" + "--debug print full error stack on error" ); } // 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; // file } else { params.files.push(argv[i]); } } return params; } // Entry (function main() { var params = parseArguments(process.argv); var nFiles = params.files.length; var stdin = process.stdin; var content = ""; // . files if (nFiles) { console.log(uglifycss.processFiles(params.files, params.options)); // . 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)); }); } }());