Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beautify stdin if there are no filename arguments #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 23 additions & 15 deletions bin/cssbeautify
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@

/*jslint sloppy:true node:true */

var fs, cssbeautify, fname, content, options, style;
var fs, cssbeautify, fname, content, options;

fs = require('fs');
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();
Expand All @@ -41,10 +42,6 @@ function showUsage() {
process.exit(1);
}

if (process.argv.length <= 2) {
showUsage();
}

options = {};

process.argv.splice(2).forEach(function (entry) {
Expand All @@ -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);
}