2c5ebf8d159eaeda49558c4525334de1f82357d3.svn-base
1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
var cardinal = require('..')
var path = require('path')
var settings = require('../settings')
var args = process.argv
var theme = settings.resolveTheme()
var opts = settings.getSettings()
var highlighted
opts = opts || {}
opts.theme = theme
// jsx is only turned on when highlighting non-json files
opts.jsx = false
function usage() {
  var msg = [
      'Usage: cdl <filename.js> [options]'
    , ''
    , 'Options (~/.cardinalrc overrides):'
    , '  --nonum: turn off line printing'
    , ''
    , 'Unix Pipe Example: cat filename.js | grep console | cdl'
    , ''
  ].join('\n')
  console.log(msg)
}
function highlightFile() {
  try {
    // Enabling jsx for JSON breaks most likelely due to esprima AST generation
    // not working for JSON
    opts.jsx = path.extname(args[2]) !== '.json'
    highlighted = cardinal.highlightFileSync(args[2], opts)
    console.log(highlighted)
  } catch (e) {
    console.trace()
    console.error(e)
  }
}
(function runner() {
// E.g., "cardinal myfile.js"
if (args.length === 3) return highlightFile()
var opt = args[3]
// E.g., "cardinal myfile.js --nonum"
if (opt && opt.indexOf('--') === 0) {
  if ((/^--(nonum|noline)/i).test(opt)) opts.linenos = false
  else {
    usage()
    return console.error('Unknown option: ', opt)
  }
  return highlightFile()
}
// UNIX pipes e.g., "cat myfile.js | grep console | cardinal
var stdin = process.stdin
var stdout = process.stdout
// line numbers don't make sense when we are printing line by line
opts.linenos = false
stdin.setEncoding('utf-8')
stdin.resume()
stdin
  .on('data', function(chunk) {
    chunk.split('\n').forEach(function(line) {
      try {
        stdout.write(cardinal.highlight(line, opts) + '\n')
      } catch (e) {
        // line doesn't represent a valid js snippet and therefore cannot be parsed -> just print as is
        stdout.write(line + '\n')
      }
    })
  })
})()