summaryrefslogtreecommitdiff
path: root/build-tools/bin/cleancss
blob: 0ec2ef32117a6768ed3bc905125dec9554988ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/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 <output-file> <input-file>\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);
  }
};