UNPKG

1.28 kBtext/coffeescriptView Raw
1Facade = require('./lib/facade')
2
3Facade.fs = require('fs')
4
5
6###*
7Requires js file
8in Titanium, file-not-found-like-exception occurred in require function cannot be caught.
9Thus, before require function is called, check the existence of the file.
10Only in iOS this check occurs.
11File extension must be '.js' in Titanium.
12
13@param {String} file name without extension
14@return {any} required value
15###
16Facade.requireFile = (file) ->
17 if not Ti?
18 ret = require file
19 return if ret.default then ret.default else ret
20
21 # in Titanium
22 path = file + '.js'
23
24 if Ti.Platform.name is 'android'
25 return require file
26
27 fileInfo = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, path)
28
29 if fileInfo.exists()
30 return require file
31 else
32 throw new Error("#{path}: no such file.")
33
34
35###*
36Parse a file as JSON format.
37In Titanium, requiring JSON does not work.
38
39@param {String} path
40@return {any} required value
41###
42Facade.requireJSON = (path) ->
43 if not Ti?
44 return require path
45
46 fileInfo = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, path)
47
48 if fileInfo.exists()
49 return JSON.parse fileInfo.read().getText()
50 else
51 throw new Error("#{path}: no such file.")
52
53
54module.exports = Facade