Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Add config option to CLI #74

Merged
merged 3 commits into from
Mar 5, 2015
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
37 changes: 27 additions & 10 deletions bin/cssnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var pkg = require("../package")
program
.version(pkg.version)
.usage("[options] [<input> [<output>]]")
.option("-C, --config <file>", "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")
Expand Down Expand Up @@ -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
Expand All @@ -63,21 +85,16 @@ if (input && !fs.existsSync(input)) {
exit(1)
}

config.from = input

function transform() {
require("read-file-stdin")(input, function(err, buffer) {
if (err) {
throw err
}

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) {
Expand All @@ -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)
}
}
Expand All @@ -105,7 +122,7 @@ function transform() {

transform()

if (program.watch) {
if (config.watch) {
if (!input || !output) {
console.error(colors.red("--watch option need both <input> & <output> files to work"))
exit(3)
Expand Down
6 changes: 6 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: var(--color)
}
3 changes: 3 additions & 0 deletions test/fixtures/config.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: #e00
}
9 changes: 9 additions & 0 deletions test/fixtures/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"features": {
"customProperties": {
"variables": {
"--color": "#e00"
}
}
}
}