This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
/
utils.coffee
380 lines (310 loc) · 9.39 KB
/
utils.coffee
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
###
Collection of utility functions.
###
Async = require 'async'
Path = require 'path'
FS = require 'fs'
FStream = require 'fstream'
Zlib = require 'zlib'
Tar = require 'tar'
Uglify = require 'uglify-js'
###
Composes a series of async functions, so they are called in sequence.
Each functions must accept an equal number of arugments, of which the last
is a callback of the form (err, n-1 args).
'this' is preserved throughout.
@param {Function} functions...
###
composeFunctions = exports.composeFunctions = (functions...) ->
if functions.length == 0
return (args..., callback) ->
callback null, args...
else
[head..., tail] = functions
head ||= []
head_composed = composeFunctions(head...)
return (args..., callback) ->
head_composed.call @, args..., (err, out_args...) =>
if err
callback err
return
tail.call @, out_args..., callback
###
Walks a tree, producing an array of files.
@param {String} path
@param {Function} filter
@param {Function} callback
###
walkTree = (path, filter, callback) ->
filter ||= () -> true
handle_file = (path, callback) =>
FS.stat path, (err, stats) =>
if err?
callback err
return
if stats.isDirectory()
# If it's a directory, we recurse.
directory_cb = (err, paths) ->
if err?
callback err
return
callback null, paths
walkTree path, filter, directory_cb
else if stats.isFile()
callback null, [path]
# Read files in directory.
FS.readdir path, (err, files) ->
if err?
callback err
if not files?
callback null, []
full_path_files = []
for file in files
if filter file
full_path = Path.join path, file
full_path_files.push full_path
Async.concat(full_path_files, handle_file, callback)
###
Walks a tree, producing an array of files, relative to the given path.
@param {String} path
@param {Function} filter
@param {Function} callback
###
exports.listFiles = listFiles = (path, filter..., callback) ->
filter = filter[0]
walkTree path, filter, (err, paths) ->
if err?
callback err
return
relative_paths = paths.map Async.apply(Path.relative, path)
callback null, relative_paths
###
Splits a path in to its component parts
@param {String} path
###
exports.splitPath = splitPath = (path) ->
basename = Path.basename path
dirname = Path.dirname path
if dirname.match /^(\/|\w\:\\)$/
paths = [path]
else if dirname.match /^[.]$/
paths = []
paths.push(basename)
else
paths = splitPath(dirname)
paths.push(basename)
return paths
###
Makes a directory recursively.
@param {String} path
@param {Function} callback
###
exports.makeDirectorySync = makeDirectorySync = (path) ->
parts = splitPath path
full_path = ''
for part in parts
full_path = Path.join full_path, part
try
stat = FS.statSync full_path
catch error
FS.mkdirSync full_path, 16877
###
Copies a file or folder from source to destination
@param {String} source
@param {String} destination
###
exports.copy = copy = (source, destination, file_callback..., callback) ->
file_callback = file_callback[0] || () ->
FS.stat source, (err, stat) ->
if stat.isFile()
file_callback source, destination, false
FS.readFile source, (err, data) ->
if err
callback err
return
FS.writeFile destination, data, callback
else if stat.isDirectory()
file_callback source, destination, true
makeDirectorySync destination
FS.readdir source, (err, files) ->
to_copy = []
for file in files
source_path = Path.join source, file
destination_path = Path.join destination, file
to_copy.push Async.apply(copy, source_path, destination_path, file_callback)
Async.parallel(to_copy, callback)
###
Changes extension of `path` to `ext`
@param {String} path
@param {String} ext
###
exports.changeExt = changeExt = (path, ext) ->
if getExt(path) != ''
path.replace /[.](\w+)$/, ".#{ext}"
else
"#{path}.#{ext}"
###
Returns the extension of a filename
@param {String} filename
###
exports.getExt = getExt = (filename) ->
parts = filename.match /[.](\w+)$/
if parts
return parts[1]
else
return ''
###
Checks if a file exists
@param {String} path
@param {Function} callback
###
exports.fileExists = fileExists = (path, callback) ->
FS.stat path, (err, stats) =>
if err?
callback false
return
if stats.isFile()
callback true
else
callback false
exports.fileExistsSync = fileExistsSync = (path) ->
try
stat = FS.statSync path
stat.isFile()
catch err
false
###
Checks if a path exists
@param {String} path
@param {Function} callback
###
exports.pathExists = pathExists = (path, callback) ->
FS.stat path, (err, stats) =>
if err?
callback false
else
callback true
exports.pathExistsSync = pathExistsSync = (path) ->
try
FS.statSync path
true
catch err
false
###
Reads a stream in to a buffer
@param {ReadableStream} stream
@param {Function} callback
###
exports.streamToBuffer = (stream, callback) ->
buffer_size = 16*1024
content_length = 0
buffer = new Buffer(buffer_size)
increase_buffer = (atleast) ->
target_size = content_length + atleast
while (buffer_size < target_size)
buffer_size = 2*buffer_size
new_buffer = new Buffer(buffer_size)
buffer.copy new_buffer
buffer = new_buffer
stream.on "data", (data) ->
buffer_remaining = buffer_size - content_length
# If it's a string
if typeof data == 'string'
length = Buffer.byteLength data.length
if length > buffer_remaining
increase_buffer length
buffer.write data, content_length
content_length = content_length + length
# If it's a buffer
else if Buffer.isBuffer(data)
length = data.length
if length > buffer_remaining
increase_buffer length
data.copy buffer, content_length
content_length = content_length + length
else
callback new Error("Data event was not 'string' or 'Buffer' object.")
stream.on "end", () ->
final_buffer = buffer.slice(0, content_length)
callback null, final_buffer
###
Returns a .tgz stream from a folder.
@param {String} path
###
exports.archive = (path) ->
files = FStream.Reader
path: path
type: "Directory"
filter: () ->
return !/^[.]/.test @basename
mode: "0755"
tar = Tar.Pack()
gz = Zlib.createGzip()
tar.pipe(gz)
files.pipe(tar)
return gz
###
Returns the settings file for the tools.
###
exports.getGlobalSettingsPath = getGlobalSettingsPath = () ->
if process.platform == 'win32'
home = process.env['USERPROFILE']
else
home = process.env['HOME']
Path.join home, ".mobify"
###
Returns an object literal parsed from the settings file.
@param {Function} callback
###
exports.getGlobalSettings = getMobifySettings = (callback) ->
path = getGlobalSettingsPath()
pathExists path, (exists) ->
if exists
FS.readFile path, (err, data) ->
if err
callback err
return
try
decoded = JSON.parse data
callback null, decoded
catch error
callback new Error("Error parsing '#{path}'.")
else
callback null, {}
exports.setGlobalSettings = setGlobalSettings = (data, callback) ->
path = getGlobalSettingsPath()
data = JSON.stringify data, null, 4
pathExists path, (exists) ->
if not exists
directory = Path.dirname 'path'
makeDirectorySync directory
FS.writeFile path, data, callback
###
Returns the version as stored in package.json.
###
version = null
exports.getVersion = getVersion = () ->
if version
return version
path = scaffoldPath = Path.join __dirname, "..", "package.json"
package_json = FS.readFileSync(path, encoding='utf8')
package_obj = JSON.parse(package_json)
version = package_obj.version
###
Returns the User-Agent/Server of the tools
###
exports.getUserAgent = getUserAgent = () ->
"mobify-client v#{getVersion()};"
###
Returns the compressed vesrion of the JavaScript string `js`.
###
exports.compressJs = compressJs = (js) ->
ast = Uglify.parser.parse js
ast = Uglify.uglify.ast_mangle ast
ast = Uglify.uglify.ast_squeeze ast
Uglify.uglify.gen_code ast
###
Gets the system proxy
###
exports.getProxy = () ->
return process.env['http_proxy'] or process.env['HTTP_PROXY']