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

Commit 653b25a

Browse files
committed
Merge pull request #74 from hgl/config-option
Add `config` option to CLI
2 parents fff1450 + f35d27d commit 653b25a

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
3+
- Added: `--config` CLI option
4+
15
# 1.0.1 - 2015-02-18
26

37
- Fixed: cssnext binary doesn't exit on an error if --watch is enabled ([#69](https://github.com/cssnext/cssnext/pull/69))

bin/cssnext.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var pkg = require("../package")
1717
program
1818
.version(pkg.version)
1919
.usage("[options] [<input> [<output>]]")
20+
.option("-C, --config <file>", "use the config file")
2021
.option("-I, --no-import", "do not inline @import")
2122
.option("-U, --no-url", "do not adjust url()")
2223
.option("-c, --compress", "compress output")
@@ -54,6 +55,27 @@ program.on("--help", function() {
5455

5556
program.parse(process.argv)
5657

58+
var config = program.config ? require(path.resolve(program.config)) : {}
59+
if (!config.features) {
60+
config.features = {}
61+
}
62+
// command line flags override config file
63+
Object.keys(cssnext.features).forEach(function(feature) {
64+
if (typeof config.features[feature] === "object") {
65+
if (program[feature] === false) {
66+
config.features[feature] = false
67+
}
68+
}
69+
else {
70+
config.features[feature] = program[feature]
71+
}
72+
})
73+
if ("import" in program) { config.import = program.import }
74+
if ("url" in program) { config.url = program.url }
75+
if ("sourcemap" in program) { config.sourcemap = program.sourcemap }
76+
if ("compress" in program) { config.compress = program.compress }
77+
if ("watch" in program) { config.watch = program.watch }
78+
5779
var input = program.args[0] ? path.resolve(program.args[0]) : null
5880
var output = program.args[1] ? path.resolve(program.args[1]) : null
5981
var verbose = program.verbose
@@ -63,21 +85,16 @@ if (input && !fs.existsSync(input)) {
6385
exit(1)
6486
}
6587

88+
config.from = input
89+
6690
function transform() {
6791
require("read-file-stdin")(input, function(err, buffer) {
6892
if (err) {
6993
throw err
7094
}
7195

7296
try {
73-
var css = cssnext(buffer.toString(), {
74-
features: program,
75-
from: input,
76-
import: program.import,
77-
url: program.url,
78-
sourcemap: program.sourcemap,
79-
compress: program.compress
80-
})
97+
var css = cssnext(buffer.toString(), config)
8198

8299
require("write-file-stdout")(output, css)
83100
if (verbose && output) {
@@ -96,7 +113,7 @@ function transform() {
96113
console.error("If this error looks like a bug, please report it here:")
97114
console.error(colors.grey("❯ ") + pkg.bugs.url.cyan)
98115
console.error()
99-
if (!program.watch) {
116+
if (!config.watch) {
100117
exit(2)
101118
}
102119
}
@@ -105,7 +122,7 @@ function transform() {
105122

106123
transform()
107124

108-
if (program.watch) {
125+
if (config.watch) {
109126
if (!input || !output) {
110127
console.error(colors.red("--watch option need both <input> & <output> files to work"))
111128
exit(3)

test/cli.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ test("cli", function(t) {
5454
})
5555
planned+=3
5656

57+
exec(cssnextBin + " --config test/fixtures/config.json test/fixtures/config.css", function(err, stdout) {
58+
if (err) { throw err }
59+
t.equal(stdout, utils.readFixture("config.expected"), "should read config file on --config")
60+
})
61+
planned+=1
62+
5763
exec(cssnextBin + " --verbose test/fixtures/cli.css test/fixtures/cli.output--verbose.css", function(err, stdout) {
5864
if (err) { throw err }
5965
t.ok(utils.contains(stdout, "Output written"), "should log on --verbose")

test/fixtures/config.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: var(--color)
3+
}

test/fixtures/config.expected.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: #e00
3+
}

test/fixtures/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"features": {
3+
"customProperties": {
4+
"variables": {
5+
"--color": "#e00"
6+
}
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)