#!/usr/bin/env node global.util = require("util"); var argv = require('optimist').argv, cleanCss = require('cleancss'), fs = require('fs'); var options = { source: null, target: null }; var cleanOptions = {}; if (argv.o) options.target = argv.o; if (argv._) options.source = argv._[0]; if (argv.e) cleanOptions.removeEmpty = true; if (argv.h || argv.help) { global.util.print('Usage: cleancss [-e] -o \n'); process.exit(0); } if (options.source) { fs.readFile(options.source, 'utf8', function(error, text) { if (error) throw error; output(cleanCss.process(text)); }); } else { var stdin = process.openStdin(); stdin.setEncoding('utf-8'); var text = ''; stdin.on('data', function(chunk) { text += chunk; }); stdin.on('end', function() { output(cleanCss.process(text, cleanOptions)); }); } function output(cleaned) { if (options.target) { var out = fs.createWriteStream(options.target, { flags: 'w', encoding: 'utf-8', mode: 0644 }); out.write(cleaned); out.end(); } else { process.stdout.write(cleaned); } };