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

Commit 085c175

Browse files
committed
minimal cli
Close #10
1 parent dd0b177 commit 085c175

File tree

6 files changed

+122
-10
lines changed

6 files changed

+122
-10
lines changed

bin/cssnext

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
3+
var colors = require("colors")
4+
5+
var program = require("commander")
6+
7+
var path = require("path")
8+
9+
var cssnext = require("..")
10+
var pkg = require("../package")
11+
12+
program
13+
.version(pkg.version)
14+
.usage("[options] [<input> [<output>]]")
15+
16+
program.parse(process.argv)
17+
18+
var input = program.args[0] ? path.resolve(program.args[0]) : null
19+
var output = program.args[1] ? path.resolve(program.args[1]) : null
20+
function transform() {
21+
require("read-file-stdin")(input, function(err, buffer) {
22+
if (err) {
23+
throw err
24+
}
25+
26+
try {
27+
var css = cssnext(buffer.toString(), {
28+
features: program,
29+
from: input,
30+
sourcemap: Boolean(program.sourcemap)
31+
})
32+
33+
require("write-file-stdout")(output, css)
34+
}
35+
catch (e) {
36+
console.error()
37+
console.error()
38+
console.error(colors.bold("cssnext encounters an error:"))
39+
console.error()
40+
console.error(e.message.red)
41+
console.error()
42+
console.error(e.stack.split("\n").slice(1).join("\n").grey)
43+
console.error()
44+
console.error("If this error looks like a bug, please report it here:")
45+
console.error(colors.grey("❯ ") + pkg.bugs.url.cyan)
46+
console.error()
47+
process.exit(2)
48+
}
49+
})
50+
}
51+
52+
transform()

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@
2828
],
2929
"dependencies": {
3030
"autoprefixer": "^2.2.0",
31+
"colors": "^0.6.2",
32+
"commander": "^2.3.0",
3133
"csswring": "^1.0.0",
34+
"node-watch": "^0.3.4",
3235
"postcss": "^2.1.0",
3336
"postcss-calc": "^2.0.1",
3437
"postcss-color": "^1.0.0",
3538
"postcss-custom-media": "^1.0.0",
3639
"postcss-custom-properties": "^0.1.0",
3740
"postcss-import": "^1.0.0",
38-
"to-slug-case": "^0.1.2"
41+
"read-file-stdin": "0.0.4",
42+
"to-slug-case": "^0.1.2",
43+
"to-space-case": "^0.1.3",
44+
"write-file-stdout": "0.0.2"
3945
},
4046
"devDependencies": {
4147
"browserify": "^5.10.0",

test/cli/error.css

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

test/cli/input.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:root {
2+
--red: #e00;
3+
}
4+
5+
body {
6+
color: var(--red);
7+
}

test/cli/input.expected.css

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

test/index.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ test("sourcemap", function(t) {
8181
})
8282

8383
/**
84+
* CLI tests.
85+
*/
86+
87+
test("cli", function(t) {
88+
var exec = require("child_process").exec
89+
90+
var output = read("cli/input.expected")
91+
92+
var planned = 0
93+
94+
exec("bin/cssnext test/cli/input.css test/cli/output.css", function(err) {
95+
if (err) { throw err }
96+
var res = read("cli/output")
97+
t.equal(res, output, "should read from a file and write to a file")
98+
remove("cli/output")
99+
})
100+
planned+=1
101+
102+
exec("bin/cssnext test/cli/error.css", function(err, stdout, stderr) {
103+
t.ok(err && err.code === 2, "should throw an error")
104+
t.ok(contains(stderr, "encounters an error"), "should output a readable error")
105+
t.ok(contains(stderr, "If this error looks like a bug, please report it here"), "should show the url where to report bugs")
106+
})
107+
planned+=3
108+
109+
t.plan(planned)
110+
})
111+
112+
/*
84113
* Resolve a fixture by `filename`.
85114
*
86115
* @param {String} filename
@@ -107,13 +136,23 @@ function read(filename) {
107136
*
108137
* @param {String} filename
109138
*/
139+
function remove(filename) {
140+
var fs = require("fs")
141+
142+
var file = resolve(filename)
143+
if (!fs.existsSync(file)) {
144+
return
145+
}
146+
fs.unlinkSync(file)
147+
}
110148

111-
// function remove(filename) {
112-
// var fs = require("fs")
113-
//
114-
// var file = resolve(filename)
115-
// if (!fs.existsSync(file)) {
116-
// return
117-
// }
118-
// fs.unlinkSync(file)
119-
// }
149+
/**
150+
* Check if a string is contained into another
151+
*
152+
* @param {String} string string to look into
153+
* @param {String} piece string to find
154+
* @return {Boolean} returns true if piece is contained in string
155+
*/
156+
function contains(string, piece) {
157+
return Boolean(string.indexOf(piece) + 1)
158+
}

0 commit comments

Comments
 (0)