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+ } ) ( ) ;
0 commit comments