From 3660ff5df1eed0024923f2e384e152f008043050 Mon Sep 17 00:00:00 2001 From: Bart Nagel Date: Thu, 31 Oct 2013 13:57:30 -0700 Subject: [PATCH] Beautify stdin if there are no filename arguments --- README.md | 9 +++++++++ bin/cssbeautify | 38 +++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f7e78a3..0725ea2 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,15 @@ var beautified = cssbeautify('menu{opacity:.7}', { }); ``` +## Command line use ## + +``` +npm install -g cssbeautify +cssbeautify style.css # beautified CSS is output to console +cssbeautify style.css >style.beautified.css # write to file +curl http://example.com/style.css | cssbeautify # beautify stdin +``` + ## Contributing ## Contributions are welcomed! Please read the [Contribution Guide](https://github.com/senchalabs/cssbeautify/blob/master/CONTRIBUTING.md) for more info. diff --git a/bin/cssbeautify b/bin/cssbeautify index 40afd7f..c05c55e 100755 --- a/bin/cssbeautify +++ b/bin/cssbeautify @@ -25,7 +25,7 @@ /*jslint sloppy:true node:true */ -var fs, cssbeautify, fname, content, options, style; +var fs, cssbeautify, fname, content, options; fs = require('fs'); cssbeautify = require('cssbeautify'); @@ -33,6 +33,7 @@ cssbeautify = require('cssbeautify'); function showUsage() { console.log('Usage:'); console.log(' cssbeautify [options] style.css'); + console.log(' curl http://example.com/style.css | cssbeautify [options]'); console.log(); console.log('Available options:'); console.log(); @@ -41,10 +42,6 @@ function showUsage() { process.exit(1); } -if (process.argv.length <= 2) { - showUsage(); -} - options = {}; process.argv.splice(2).forEach(function (entry) { @@ -67,17 +64,28 @@ process.argv.splice(2).forEach(function (entry) { } }); -if (typeof fname !== 'string') { - console.log('Error: no input file.'); - process.exit(1); -} - -try { - content = fs.readFileSync(fname, 'utf-8'); +function beautifyAndOutput(err, content) { + var style; + if (err) { + console.log('Error: ' + err.message); + process.exit(1); + } style = cssbeautify(content); console.log(style); -} catch (e) { - console.log('Error: ' + e.message); - process.exit(1); } +if (typeof fname !== 'string') { + content = ''; + process.stdin.resume(); + process.stdin.on('data', function(buf) { + content += buf; + }); + process.stdin.on('error', function(err) { + beautifyAndOutput(err); + }); + process.stdin.on('end', function() { + beautifyAndOutput(null, content); + }); +} else { + fs.readFile(fname, 'utf-8', beautifyAndOutput); +}