diff --git a/CHANGELOG.md b/CHANGELOG.md index 5354682..b7523f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Unreleased + +- Added: `--config` CLI option + # 1.0.1 - 2015-02-18 - Fixed: cssnext binary doesn't exit on an error if --watch is enabled ([#69](https://github.com/cssnext/cssnext/pull/69)) diff --git a/bin/cssnext.js b/bin/cssnext.js index 0bbde56..05c0307 100644 --- a/bin/cssnext.js +++ b/bin/cssnext.js @@ -17,6 +17,7 @@ var pkg = require("../package") program .version(pkg.version) .usage("[options] [ []]") + .option("-C, --config ", "use the config file") .option("-I, --no-import", "do not inline @import") .option("-U, --no-url", "do not adjust url()") .option("-c, --compress", "compress output") @@ -54,6 +55,27 @@ program.on("--help", function() { program.parse(process.argv) +var config = program.config ? require(path.resolve(program.config)) : {} +if (!config.features) { + config.features = {} +} +// command line flags override config file +Object.keys(cssnext.features).forEach(function(feature) { + if (typeof config.features[feature] === "object") { + if (program[feature] === false) { + config.features[feature] = false + } + } + else { + config.features[feature] = program[feature] + } +}) +if ("import" in program) { config.import = program.import } +if ("url" in program) { config.url = program.url } +if ("sourcemap" in program) { config.sourcemap = program.sourcemap } +if ("compress" in program) { config.compress = program.compress } +if ("watch" in program) { config.watch = program.watch } + var input = program.args[0] ? path.resolve(program.args[0]) : null var output = program.args[1] ? path.resolve(program.args[1]) : null var verbose = program.verbose @@ -63,6 +85,8 @@ if (input && !fs.existsSync(input)) { exit(1) } +config.from = input + function transform() { require("read-file-stdin")(input, function(err, buffer) { if (err) { @@ -70,14 +94,7 @@ function transform() { } try { - var css = cssnext(buffer.toString(), { - features: program, - from: input, - import: program.import, - url: program.url, - sourcemap: program.sourcemap, - compress: program.compress - }) + var css = cssnext(buffer.toString(), config) require("write-file-stdout")(output, css) if (verbose && output) { @@ -96,7 +113,7 @@ function transform() { console.error("If this error looks like a bug, please report it here:") console.error(colors.grey("❯ ") + pkg.bugs.url.cyan) console.error() - if (!program.watch) { + if (!config.watch) { exit(2) } } @@ -105,7 +122,7 @@ function transform() { transform() -if (program.watch) { +if (config.watch) { if (!input || !output) { console.error(colors.red("--watch option need both & files to work")) exit(3) diff --git a/test/cli.js b/test/cli.js index fc5ae6c..5d16d02 100644 --- a/test/cli.js +++ b/test/cli.js @@ -54,6 +54,12 @@ test("cli", function(t) { }) planned+=3 + exec(cssnextBin + " --config test/fixtures/config.json test/fixtures/config.css", function(err, stdout) { + if (err) { throw err } + t.equal(stdout, utils.readFixture("config.expected"), "should read config file on --config") + }) + planned+=1 + exec(cssnextBin + " --verbose test/fixtures/cli.css test/fixtures/cli.output--verbose.css", function(err, stdout) { if (err) { throw err } t.ok(utils.contains(stdout, "Output written"), "should log on --verbose") diff --git a/test/fixtures/config.css b/test/fixtures/config.css new file mode 100644 index 0000000..0badc1a --- /dev/null +++ b/test/fixtures/config.css @@ -0,0 +1,3 @@ +body { + color: var(--color) +} diff --git a/test/fixtures/config.expected.css b/test/fixtures/config.expected.css new file mode 100644 index 0000000..03903cc --- /dev/null +++ b/test/fixtures/config.expected.css @@ -0,0 +1,3 @@ +body { + color: #e00 +} diff --git a/test/fixtures/config.json b/test/fixtures/config.json new file mode 100644 index 0000000..d0aa524 --- /dev/null +++ b/test/fixtures/config.json @@ -0,0 +1,9 @@ +{ + "features": { + "customProperties": { + "variables": { + "--color": "#e00" + } + } + } +}