UNPKG

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