1 |
|
2 | fs = require('fs')
|
3 | CSON = require('./')
|
4 | opts = {}
|
5 |
|
6 |
|
7 | outputHelp = ->
|
8 | process.stdout.write '''
|
9 | CSON CLI
|
10 |
|
11 | Usage:
|
12 |
|
13 | # Convert a JSON file into a CSON file
|
14 | json2cson in.json > out.cson
|
15 |
|
16 | # Same thing via piping
|
17 | cat in.json | json2cson > out.cson
|
18 |
|
19 | # Convert a CSON file into a JSON file
|
20 | cson2json in.cson > out.json
|
21 |
|
22 | # Same thing via piping
|
23 | cat in.cson | cson2json > out.json
|
24 |
|
25 | Options
|
26 |
|
27 | # Display this help
|
28 | --help
|
29 |
|
30 | # Indentation for CSON output
|
31 | --tabs
|
32 | --2spaces
|
33 | --4spaces
|
34 |
|
35 | '''
|
36 |
|
37 |
|
38 | if process.argv.indexOf('--help') isnt -1
|
39 | outputHelp()
|
40 | process.exit(0)
|
41 |
|
42 |
|
43 | if process.argv.toString().indexOf('cson2json') isnt -1
|
44 | conversion ='cson2json'
|
45 | else if process.argv.toString().indexOf('json2cson') isnt -1
|
46 | conversion = 'json2cson'
|
47 | opts.indent =
|
48 | if (i = process.argv.indexOf('--tabs')) isnt -1
|
49 | '\t'
|
50 | else if (i = process.argv.indexOf('--2spaces')) isnt -1
|
51 | ' '
|
52 | else if (i = process.argv.indexOf('--4spaces')) isnt -1
|
53 | ' '
|
54 | if i isnt -1
|
55 | process.argv = process.argv.slice(0, i).concat process.argv.slice(i+1)
|
56 | else
|
57 | process.stderr.write('Unknown conversion')
|
58 | process.exit(1)
|
59 |
|
60 |
|
61 | if process.argv.length is 3
|
62 | filePath = process.argv[2]
|
63 |
|
64 | if conversion is 'cson2json'
|
65 | parse = CSON.parseCSONFile.bind(CSON)
|
66 | create = CSON.createJSONString.bind(CSON)
|
67 | else
|
68 | parse = CSON.parseJSONFile.bind(CSON)
|
69 | create = CSON.createCSONString.bind(CSON)
|
70 |
|
71 | result = parse(filePath)
|
72 | throw result if result instanceof Error
|
73 | result = create(result, opts)
|
74 | throw result if result instanceof Error
|
75 | process.stdout.write(result)
|
76 |
|
77 |
|
78 | else if process.argv.length is 2
|
79 |
|
80 | data = ''
|
81 |
|
82 | hasData = ->
|
83 | return data.replace(/\s+/, '').length isnt 0
|
84 |
|
85 | processData = ->
|
86 | if conversion is 'cson2json'
|
87 | parse = CSON.parseCSONString.bind(CSON)
|
88 | create = CSON.createJSONString.bind(CSON)
|
89 | else
|
90 | parse = CSON.parseJSONString.bind(CSON)
|
91 | create = CSON.createCSONString.bind(CSON)
|
92 |
|
93 | result = parse(data)
|
94 | throw result if result instanceof Error
|
95 | result = create(result, opts)
|
96 | throw result if result instanceof Error
|
97 | process.stdout.write(result)
|
98 |
|
99 |
|
100 | timeoutFunction = ->
|
101 |
|
102 | timeout = null
|
103 |
|
104 |
|
105 | if hasData() is false
|
106 | stdin.pause()
|
107 | process.stderr.write('No STDIN data received...')
|
108 | process.exit(1)
|
109 |
|
110 | timeout = setTimeout(timeoutFunction, 1000)
|
111 |
|
112 |
|
113 | stdin = process.stdin
|
114 | stdin.setEncoding('utf8')
|
115 | stdin.resume()
|
116 | stdin.on 'data', (_data) ->
|
117 | data += _data.toString()
|
118 | process.stdin.on 'end', ->
|
119 | if timeout
|
120 | clearTimeout(timeout)
|
121 | timeout = null
|
122 | processData()
|
123 |
|
124 | else
|
125 | outputHelp()
|
126 | process.exit(1)
|