From 7c4d232b64e42f52bb4edcadc1884ebb626c5aad Mon Sep 17 00:00:00 2001 From: Sijmen Mulder Date: Thu, 12 May 2016 13:03:44 +0000 Subject: [PATCH] Accept input from stdin In line with Unix conventions, this lets cssbeautify be used as an intermediate step in a command: curl http://example.com/main.css | cssbeautify > main.css --- bin/cssbeautify | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/bin/cssbeautify b/bin/cssbeautify index 40afd7f..9848eb3 100755 --- a/bin/cssbeautify +++ b/bin/cssbeautify @@ -32,19 +32,16 @@ cssbeautify = require('cssbeautify'); function showUsage() { console.log('Usage:'); - console.log(' cssbeautify [options] style.css'); + console.log(' cssbeautify [options] []'); console.log(); console.log('Available options:'); console.log(); console.log(' -v, --version Shows program version'); + console.log(' FILE CSS file to read instead of standard input'); console.log(); process.exit(1); } -if (process.argv.length <= 2) { - showUsage(); -} - options = {}; process.argv.splice(2).forEach(function (entry) { @@ -67,15 +64,24 @@ 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'); - style = cssbeautify(content); - console.log(style); + if (fname) { + content = fs.readFileSync(fname, 'utf-8'); + style = cssbeautify(content); + console.log(style); + } else { + content = ''; + + process.stdin.resume(); + process.stdin.on('data', function(chunk) { + content += chunk; + }); + + process.stdin.on('end', function(chunk) { + style = cssbeautify(content); + console.log(style); + }); + } } catch (e) { console.log('Error: ' + e.message); process.exit(1);