5 var parseArgs = require('minimist');
6 var gonzales = require('..');
7 var fs = require('fs');
8 var path = require('path');
10 var options = getOptions();
20 processFile(options._[0]);
23 function getOptions() {
25 boolean: ['silent', 'simple'],
32 return parseArgs(process.argv.slice(2), parserOptions);
36 return options._.indexOf('-') !== -1;
39 function processSTDIN() {
41 process.stdin.resume();
42 process.stdin.setEncoding('utf8');
43 process.stdin.on('data', data => {
46 process.stdin.on('end', () => {
47 processInputData(input);
51 function processFile(file) {
52 if (!file) process.exit(0);
53 if (!options.syntax) options.syntax = path.extname(file).substring(1);
54 var css = fs.readFileSync(file, 'utf-8').trim();
55 processInputData(css);
58 function processInputData(input) {
60 var ast = gonzales.parse(input, {
61 syntax: options.syntax,
62 context: options.context
67 if (!options.silent) process.stderr.write(e.toString());
72 function printTree(ast) {
73 if (!options.simple) {
74 var tree = ast.toJson();
75 process.stdout.write(tree);
79 ast.traverse(function(node, i, parent, lastLevel) {
81 var spaces = new Array(lastLevel).join(' |');
82 if (typeof node.content === 'string') {
83 var content = JSON.stringify(node.content);
84 console.log(spaces, '->', type);
85 console.log(spaces, ' ', content);
87 console.log(spaces, '->', type);
91 var spaces = new Array(lastLevel).join(' -');
96 function displayHelp() {
99 ' gonzlaes-pe — Parse a css file and print its parse tree in JSON',
102 ' gonzales-pe [options] file.js',
103 ' cat file.js | gonzales-pe [options] -',
107 ' Syntax name: css, less, sass or scss.',
109 ' Context of code part. See docs on node types for more info.',
111 ' Print a simplified parse tree structure instead of JSON.',
113 ' Don\'t print any error messages.'
115 console.log(help.join('\n'));