From 224b6b6d0e4ebc1a92d11ebc3bd000257f5be055 Mon Sep 17 00:00:00 2001 From: Glen Huang Date: Fri, 27 Feb 2015 14:45:09 +0800 Subject: [PATCH 1/3] Add `config` option to CLI --- bin/cssnext.js | 41 ++++++++++++++++++++++--------- test/cli.js | 8 ++++++ test/fixtures/config.css | 3 +++ test/fixtures/config.expected.css | 3 +++ test/fixtures/config.json | 11 +++++++++ 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/config.css create mode 100644 test/fixtures/config.expected.css create mode 100644 test/fixtures/config.json diff --git a/bin/cssnext.js b/bin/cssnext.js index 0bbde56..7b799ea 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,8 +55,29 @@ program.on("--help", function() { program.parse(process.argv) -var input = program.args[0] ? path.resolve(program.args[0]) : null -var output = program.args[1] ? path.resolve(program.args[1]) : null +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]) : config.input +var output = program.args[1] ? path.resolve(program.args[1]) : config.output var verbose = program.verbose if (input && !fs.existsSync(input)) { @@ -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..7d854dc 100644 --- a/test/cli.js +++ b/test/cli.js @@ -54,6 +54,14 @@ test("cli", function(t) { }) planned+=3 + exec(cssnextBin + " --config test/fixtures/config.json", function(err) { + if (err) { throw err } + var res = utils.readFixture("config.output--io") + t.equal(res, utils.readFixture("config.expected"), "should read config file on --config") + utils.remove("config.output--io") + }) + 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..c31b0ac --- /dev/null +++ b/test/fixtures/config.json @@ -0,0 +1,11 @@ +{ + "features": { + "customProperties": { + "variables": { + "--color": "#e00" + } + } + }, + "input": "test/fixtures/config.css", + "output": "test/fixtures/config.output--io.css" +} From 70b9a92c3d92b296650b8c66d3e2ba7abcc07242 Mon Sep 17 00:00:00 2001 From: Glen Huang Date: Fri, 27 Feb 2015 16:29:52 +0800 Subject: [PATCH 2/3] Remove input & output options from config file --- bin/cssnext.js | 4 ++-- test/cli.js | 6 ++---- test/fixtures/config.json | 4 +--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/bin/cssnext.js b/bin/cssnext.js index 7b799ea..05c0307 100644 --- a/bin/cssnext.js +++ b/bin/cssnext.js @@ -76,8 +76,8 @@ 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]) : config.input -var output = program.args[1] ? path.resolve(program.args[1]) : config.output +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 if (input && !fs.existsSync(input)) { diff --git a/test/cli.js b/test/cli.js index 7d854dc..5d16d02 100644 --- a/test/cli.js +++ b/test/cli.js @@ -54,11 +54,9 @@ test("cli", function(t) { }) planned+=3 - exec(cssnextBin + " --config test/fixtures/config.json", function(err) { + exec(cssnextBin + " --config test/fixtures/config.json test/fixtures/config.css", function(err, stdout) { if (err) { throw err } - var res = utils.readFixture("config.output--io") - t.equal(res, utils.readFixture("config.expected"), "should read config file on --config") - utils.remove("config.output--io") + t.equal(stdout, utils.readFixture("config.expected"), "should read config file on --config") }) planned+=1 diff --git a/test/fixtures/config.json b/test/fixtures/config.json index c31b0ac..d0aa524 100644 --- a/test/fixtures/config.json +++ b/test/fixtures/config.json @@ -5,7 +5,5 @@ "--color": "#e00" } } - }, - "input": "test/fixtures/config.css", - "output": "test/fixtures/config.output--io.css" + } } From f35d27d147d01089e1a434d9f6a5af31dcb1c990 Mon Sep 17 00:00:00 2001 From: Glen Huang Date: Tue, 3 Mar 2015 21:43:03 +0800 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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))