Skip to content

Commit a8ddb4e

Browse files
committed
Made test command will check 3 types of AST.
1 parent a76e4dc commit a8ddb4e

File tree

3 files changed

+93
-94
lines changed

3 files changed

+93
-94
lines changed

lib/cli.js

Lines changed: 84 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,90 @@
11
#!/usr/bin/env node
22

3-
(function () {
4-
var colors = require('colors');
5-
var cssparser = require('./cssparser.js');
6-
var nomnom = require('nomnom');
7-
var fs = require('fs');
8-
var path = require('path');
9-
10-
var version = require('../package.json').version;
11-
12-
var opts = require("nomnom")
13-
.script('cssparser')
14-
.option('file', {
15-
flag: true,
16-
position: 0,
17-
help: 'CSS document file'
18-
})
19-
.option('outfile', {
20-
abbr: 'o',
21-
metavar: 'FILE',
22-
help: 'Filename or base name of the generated JSON'
23-
})
24-
.option('indent', {
25-
abbr: 'i',
26-
default: 4,
27-
help: 'indentation(string or number)'
28-
})
29-
.option('type', {
30-
abbr: 't',
31-
default: 'simple',
32-
choices: [ 'simple', 'deep', 'atomic' ],
33-
metavar: 'TYPE',
34-
help: 'The type of JSON to generate (simple, deep, atomic)'
35-
})
36-
.option('console', {
37-
abbr: 'c',
38-
flag: true,
39-
help: 'Display JSON to console only. this option will ignore output-file options.'
40-
})
41-
.option('version', {
42-
abbr: 'V',
43-
flag: true,
44-
help: 'print version and exit',
45-
callback: function() {
46-
return version;
47-
}
48-
})
49-
.parse();
50-
51-
function toJSON( raw, type, indent ) {
52-
var parser = new cssparser.Parser();
53-
54-
return JSON.stringify( parser.parse( raw ), null, indent );
55-
}
56-
57-
if (opts.file) {
58-
try {
59-
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
60-
}
61-
catch (e) {
62-
console.error( e.toString().red + '\n' + e.stack.red );
63-
return;
64-
}
3+
(function() {
4+
var colors = require('colors');
5+
var cssparser = require('./cssparser.js');
6+
var nomnom = require('nomnom');
7+
var fs = require('fs');
8+
var path = require('path');
659

66-
var name = path.basename((opts.outfile||opts.file)).replace(/\..*$/g,'');
67-
var type = opts.type;
68-
var indent = opts.indent;
10+
var version = require('../package.json').version;
6911

70-
try {
71-
var json = toJSON(raw, type, indent);
72-
}
73-
catch (e) {
74-
var output = e.message
75-
if ('stack' in e) {
76-
output += '\n' + e.stack
77-
}
78-
if ('hash' in e) {
79-
output += '\n' + JSON.stringify(e[e.hash], null, '\t')
80-
}
81-
console.error( output.red );
82-
return;
83-
}
12+
var opts = require("nomnom")
13+
.script('cssparser')
14+
.option('file', {
15+
flag: true,
16+
position: 0,
17+
help: 'CSS document file'
18+
})
19+
.option('outfile', {
20+
abbr: 'o',
21+
metavar: 'FILE',
22+
help: 'Filename or base name of the generated JSON'
23+
})
24+
.option('indent', {
25+
abbr: 'i',
26+
default: 4,
27+
help: 'indentation(string or number)'
28+
})
29+
.option('type', {
30+
abbr: 't',
31+
default: 'simple',
32+
choices: ['simple', 'deep', 'atomic'],
33+
metavar: 'TYPE',
34+
help: 'The type of JSON to generate (simple, deep, atomic)'
35+
})
36+
.option('console', {
37+
abbr: 'c',
38+
flag: true,
39+
help: 'Display JSON to console only. this option will ignore output-file options.'
40+
})
41+
.option('version', {
42+
abbr: 'V',
43+
flag: true,
44+
help: 'print version and exit',
45+
callback: function() {
46+
return version;
47+
}
48+
})
49+
.parse();
8450

85-
if ( opts.console ) {
86-
console.log( json );
87-
}
88-
else {
89-
fs.writeFileSync(opts.outfile||(name + '.json'), json );
90-
}
51+
function toJSON(raw, type, indent) {
52+
var parser = new cssparser.Parser();
53+
var ast = parser.parse(raw)
54+
55+
return JSON.stringify(ast.toJSON(type), null, indent);
56+
}
57+
58+
if (opts.file) {
59+
try {
60+
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
61+
} catch (e) {
62+
console.error(e.toString().red + '\n' + e.stack.red);
63+
return;
64+
}
65+
66+
var name = path.basename((opts.outfile || opts.file)).replace(/\..*$/g, '');
67+
var type = opts.type;
68+
var indent = opts.indent;
69+
70+
try {
71+
var json = toJSON(raw, type, indent);
72+
} catch (e) {
73+
var output = e.message
74+
if ('stack' in e) {
75+
output += '\n' + e.stack
76+
}
77+
if ('hash' in e) {
78+
output += '\n' + JSON.stringify(e[e.hash], null, '\t')
79+
}
80+
console.error(output.red);
81+
return;
82+
}
83+
84+
if (opts.console) {
85+
console.log(json);
86+
} else {
87+
fs.writeFileSync(opts.outfile || (name + '.json'), json);
9188
}
92-
})();
89+
}
90+
})();

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@
5252
"scripts": {
5353
"es6": "gulp merge-js && babel dist/js -d dist/babel",
5454
"generate": "gulp merge-jison && jison dist/jison/cssparser.y dist/jison/css.l -o lib/cssparser.js",
55-
"simple-test": "node lib/cli.js test/test.css -c",
56-
"copy-web": "cp ./lib/cssparser.js ./web/",
5755
"build": "npm run es6 && npm run generate",
58-
"test": "npm run build && npm run simple-test",
56+
"test-simple": "node lib/cli.js test/test.css -c -t simple",
57+
"test-deep": "node lib/cli.js test/test.css -c -t deep",
58+
"test-atomic": "node lib/cli.js test/test.css -c -t deep",
59+
"test-all": "npm run test-simple && npm run test-deep && npm run test-atomic",
60+
"copy-web": "cp ./lib/cssparser.js ./web/",
5961
"release": "npm run build && npm run copy-web",
60-
"test-only": "npm run simple-test"
62+
"test": "npm run release && npm run test-all"
6163
},
6264
"homepage": "https://cwdoh.github.io/cssparser.js/",
6365
"optionalDependencies": {},

src/yacc/roots.y

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
stylesheet
66
: StylesheetList EOF
7-
%{
8-
console.log(JSON.stringify($1.toJSON(), null, 4))
9-
$$ = $1
10-
}%
7+
{
8+
return $1
9+
}
1110
;
1211

1312
StylesheetList

0 commit comments

Comments
 (0)