.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / gonzales-pe / bin / gonzales.js
1 #!/usr/bin/env node
2
3 'use strict';
4
5 var parseArgs = require('minimist');
6 var gonzales = require('..');
7 var fs = require('fs');
8 var path = require('path');
9
10 var options = getOptions();
11
12 if (options.help) {
13   displayHelp();
14   process.exit(0);
15 }
16
17 if (isSTDIN()) {
18   processSTDIN();
19 } else {
20   processFile(options._[0]);
21 }
22
23 function getOptions() {
24   var parserOptions = {
25     boolean: ['silent', 'simple'],
26     alias: {
27       help: 'h',
28       syntax: 's',
29       context: 'c'
30     }
31   };
32   return parseArgs(process.argv.slice(2), parserOptions);
33 }
34
35 function isSTDIN() {
36   return options._.indexOf('-') !== -1;
37 }
38
39 function processSTDIN() {
40   var input = '';
41   process.stdin.resume();
42   process.stdin.setEncoding('utf8');
43   process.stdin.on('data', data => {
44     input += data;
45   });
46   process.stdin.on('end', () => {
47     processInputData(input);
48   });
49 }
50
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);
56 }
57
58 function processInputData(input) {
59   try {
60     var ast = gonzales.parse(input, {
61       syntax: options.syntax,
62       context: options.context
63     });
64     printTree(ast);
65     process.exit(0);
66   } catch (e) {
67     if (!options.silent) process.stderr.write(e.toString());
68     process.exit(1);
69   }
70 }
71
72 function printTree(ast) {
73   if (!options.simple) {
74     var tree = ast.toJson();
75     process.stdout.write(tree);
76   } else {
77     var lastLevel;
78
79     ast.traverse(function(node, i, parent, lastLevel) {
80       var type = node.type;
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);
86       } else {
87         console.log(spaces, '->', type);
88       }
89     });
90
91     var spaces = new Array(lastLevel).join(' -');
92     console.log(spaces);
93   }
94 }
95
96 function displayHelp() {
97   var help = [
98       'NAME',
99       '    gonzlaes-pe — Parse a css file and print its parse tree in JSON',
100       '',
101       'SYNOPSIS',
102       '    gonzales-pe [options] file.js',
103       '    cat file.js | gonzales-pe [options] -',
104       '',
105       'OPTIONS',
106       '    -s, --syntax',
107       '        Syntax name: css, less, sass or scss.',
108       '    -c, --context',
109       '        Context of code part. See docs on node types for more info.',
110       '    --simple',
111       '        Print a simplified parse tree structure instead of JSON.',
112       '    --silent',
113       '        Don\'t print any error messages.'
114   ];
115   console.log(help.join('\n'));
116 }