UNPKG

3.59 kBtext/coffeescriptView Raw
1# Requires
2coffee = require('coffee-script')
3fsUtil = require('fs')
4pathUtil = require('path')
5
6# Awesomeness
7wait = (delay,fn) -> setTimeout(fn,delay)
8
9# Define
10CSON =
11 # Parse a CSON file
12 # next(err,obj)
13 parseFile: (filePath,opts,next) ->
14 # Prepare
15 if opts? is true and next? is false
16 next = opts
17 opts = null
18 opts or= {}
19
20 # Resolve
21 filePath = pathUtil.resolve(filePath)
22
23 # Try require
24 if /\.(js|coffee)$/.test(filePath)
25 try
26 delete require.cache[filePath] # clear require cache for the config file
27 result = require(filePath)
28 delete require.cache[filePath] # clear require cache for the config file
29 catch err
30 return next(err,result)
31 finally
32 return next(null,result)
33
34 # Try read
35 else if /\.(json|cson)$/.test(filePath)
36 fsUtil.readFile filePath, (err,data) =>
37 # Check
38 return next(err) if err
39
40 # Parse
41 dataStr = data.toString()
42 @parse(dataStr,opts,next)
43
44 # Unknown
45 else
46 err = new Error("CSON.parseFile: Unknown extension type for #{filePath}")
47 next(err)
48
49 # Chain
50 @
51
52
53 # Parse a CSON file
54 parseFileSync: (filePath,opts) ->
55 # Prepare
56 opts or= {}
57
58 # Resolve
59 filePath = pathUtil.resolve(filePath)
60
61 # Try require
62 if /\.(js|coffee)$/.test(filePath)
63 try
64 delete require.cache[filePath] # clear require cache for the config file
65 result = require(filePath)
66 delete require.cache[filePath] # clear require cache for the config file
67 return result
68 catch err
69 return err
70
71 # Try read
72 else if /\.(json|cson)$/.test(filePath)
73 data = fsUtil.readFileSync(filePath)
74
75 # Check the result
76 if data instanceof Error
77 # An error occured
78 result = data
79 else
80 # Parse the result
81 dataStr = data.toString()
82 result = @parseSync(dataStr,opts)
83
84 # Return
85 return result
86
87 # Unknown
88 else
89 err = new Error("CSON.parseFileSync: Unknown extension type for #{filePath}")
90 return err
91
92
93 # Parse a CSON string
94 # next(err,obj)
95 parse: (src,opts,next) ->
96 # Prepare
97 if opts? is true and next? is false
98 next = opts
99 opts = null
100 opts or= {}
101
102 # currently the parser only exists in a synchronous version
103 # so we use an instant timeout to simulate async code without any overhead
104 wait 0, =>
105 # Parse
106 result = @parseSync(src,opts)
107
108 # Check for error
109 if result instanceof Error
110 # Error
111 next(result)
112 else
113 # Success
114 next(null,result)
115
116 # Chain
117 @
118
119
120 # Parse a CSON string Synchronously
121 parseSync: (src,opts) ->
122 # Prepare
123 opts or= {}
124 opts.sandbox ?= true
125
126 # Try parse JSON
127 try
128 result = JSON.parse(src)
129
130 # Try parse CSON
131 catch err
132 try
133 result = coffee.eval(src,opts)
134 catch err
135 result = err
136
137 # Return
138 result
139
140 #
141 # # Turn an object into CSON
142 # # next(err,str)
143 # stringify: (obj,next) ->
144 # # currently the parser only exists in a synchronous version
145 # # so we use an instant timeout to simulate async code without any overhead
146 # wait 0, =>
147 # # Stringify
148 # result = @stringifySync(obj)
149 #
150 # # Check
151 # if result instanceof Error
152 # # Error
153 # next(result)
154 # else
155 # # Success
156 # next(null,result)
157 #
158 # # Chain
159 # @
160
161 #
162 # # Turn an object into JSON/CSON Synchronously
163 # stringifySync: (obj) ->
164 # # Stringify
165 # try
166 # src = "var result = #{JSON.stringify obj}"
167 # result = js2coffee.build(src)
168 # result = result.replace(/^\s*result\s*\=\s/,'')
169 # if typeof obj is 'object'
170 # unless Array.isArray(obj)
171 # result = '{\n'+result+'\n}'
172 # catch err
173 # result = err
174 #
175 # # Return
176 # result
177
178
179# Export
180module.exports = CSON
\No newline at end of file