1 |
|
2 | coffee = require('coffee-script')
|
3 | fsUtil = require('fs')
|
4 | pathUtil = require('path')
|
5 |
|
6 |
|
7 | wait = (delay,fn) -> setTimeout(fn,delay)
|
8 |
|
9 |
|
10 | CSON =
|
11 |
|
12 |
|
13 | parseFile: (filePath,opts,next) ->
|
14 |
|
15 | if opts? is true and next? is false
|
16 | next = opts
|
17 | opts = null
|
18 | opts or= {}
|
19 |
|
20 |
|
21 | filePath = pathUtil.resolve(filePath)
|
22 |
|
23 |
|
24 | if /\.(js|coffee)$/.test(filePath)
|
25 | try
|
26 | delete require.cache[filePath]
|
27 | result = require(filePath)
|
28 | delete require.cache[filePath]
|
29 | catch err
|
30 | return next(err,result)
|
31 | finally
|
32 | return next(null,result)
|
33 |
|
34 |
|
35 | else if /\.(json|cson)$/.test(filePath)
|
36 | fsUtil.readFile filePath, (err,data) =>
|
37 |
|
38 | return next(err) if err
|
39 |
|
40 |
|
41 | dataStr = data.toString()
|
42 | @parse(dataStr,opts,next)
|
43 |
|
44 |
|
45 | else
|
46 | err = new Error("CSON.parseFile: Unknown extension type for #{filePath}")
|
47 | next(err)
|
48 |
|
49 |
|
50 | @
|
51 |
|
52 |
|
53 |
|
54 | parseFileSync: (filePath,opts) ->
|
55 |
|
56 | opts or= {}
|
57 |
|
58 |
|
59 | filePath = pathUtil.resolve(filePath)
|
60 |
|
61 |
|
62 | if /\.(js|coffee)$/.test(filePath)
|
63 | try
|
64 | delete require.cache[filePath]
|
65 | result = require(filePath)
|
66 | delete require.cache[filePath]
|
67 | return result
|
68 | catch err
|
69 | return err
|
70 |
|
71 |
|
72 | else if /\.(json|cson)$/.test(filePath)
|
73 | data = fsUtil.readFileSync(filePath)
|
74 |
|
75 |
|
76 | if data instanceof Error
|
77 |
|
78 | result = data
|
79 | else
|
80 |
|
81 | dataStr = data.toString()
|
82 | result = @parseSync(dataStr,opts)
|
83 |
|
84 |
|
85 | return result
|
86 |
|
87 |
|
88 | else
|
89 | err = new Error("CSON.parseFileSync: Unknown extension type for #{filePath}")
|
90 | return err
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | parse: (src,opts,next) ->
|
96 |
|
97 | if opts? is true and next? is false
|
98 | next = opts
|
99 | opts = null
|
100 | opts or= {}
|
101 |
|
102 |
|
103 |
|
104 | wait 0, =>
|
105 |
|
106 | result = @parseSync(src,opts)
|
107 |
|
108 |
|
109 | if result instanceof Error
|
110 |
|
111 | next(result)
|
112 | else
|
113 |
|
114 | next(null,result)
|
115 |
|
116 |
|
117 | @
|
118 |
|
119 |
|
120 |
|
121 | parseSync: (src,opts) ->
|
122 |
|
123 | opts or= {}
|
124 | opts.sandbox ?= true
|
125 |
|
126 |
|
127 | try
|
128 | result = JSON.parse(src)
|
129 |
|
130 |
|
131 | catch err
|
132 | try
|
133 | result = coffee.eval(src,opts)
|
134 | catch err
|
135 | result = err
|
136 |
|
137 |
|
138 | result
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | module.exports = CSON |
\ | No newline at end of file |