notes/node_modules/colorguard/bin/colorguard

64 lines
1.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
var yargs = require('yargs');
var fs = require('fs');
var path = require('path');
var stdin = process.stdin;
var argv = yargs.argv;
var assign = require('object-assign');
yargs
.describe('file', 'A CSS file')
.describe('threshold', 'Threshold of allowable color difference')
.describe('allow-equivalent-notation', 'Allow equivalent notation of the same color, e.g. #000 and #000000')
.describe('options', 'An optional JSON file containing all options (Overrides `--threshold`)')
.usage('Usage: colorguard --file [style.css] ');
process.title = 'colorguard';
var colorguard = require('..');
var css = [];
var options = {};
if (argv.file) {
run(fs.readFileSync(path.resolve(process.cwd(), argv.file), 'utf8'));
}
else {
stdin.setEncoding('utf8');
if (process.stdin.isTTY) {
yargs.showHelp();
}
else {
stdin.on('data', function(chunk) {
css.push(chunk);
});
stdin.on('end', function() {
css = css.join();
run(css);
});
}
}
function run (css) {
if (argv.options) {
options = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), argv.options), 'utf8'));
}
else {
if (argv.threshold) {
options.threshold = argv.threshold;
}
options.allowEquivalentNotation = argv.allowEquivalentNotation;
}
if (argv.file) {
options = assign({
from: argv.file
}, options);
}
colorguard.process(css, options).then(function (result) {
if (result.warnings().length) {
process.exit(1);
}
});
}