1 /* 2 * NodeLoader.js - Loader implementation for nodejs 3 * 4 * Copyright © 2015, JEDLSoft 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 /** 21 * @class 22 * Implementation of Loader for nodejs. 23 * 24 * @constructor 25 * @private 26 */ 27 module.exports = function (ilib) { 28 var path = require("./Path.js"), 29 fs = require("fs"), 30 util = require("util"), 31 Loader = require("./Loader.js"); 32 33 var NodeLoader = function (ilib) { 34 // util.print("new common NodeLoader instance\n"); 35 36 this.parent.call(this, ilib); 37 38 // root of the app that created this loader 39 // this.root = root || process.cwd(); 40 this.root = process.cwd(); 41 42 this.base = (typeof(module) !== 'undefined' && module.filename) ? 43 path.join(path.dirname(module.filename), "..") : 44 this.root; 45 46 //console.log("module.filename is " + module.filename + "\n"); 47 //console.log("base is defined as " + this.base + "\n"); 48 49 this.includePath.push(path.join(this.root, "resources")); // always check the application's resources dir first 50 51 // then a standard locale dir of a built version of ilib from npm 52 this._exists(path.join(this.base, "locale"), "localeinfo.json"); 53 54 // try the standard install directories 55 this._exists("/usr/share/javascript/ilib/locale", "localeinfo.json"); 56 57 // ... else fall back to see if we're in a check-out dir of ilib 58 // this._exists(path.join(this.base, "data", "locale"), "localeinfo.json"); 59 60 // console.log("NodeLoader: include path is now " + JSON.stringify(this.includePath)); 61 }; 62 63 // make this a subclass of loader 64 NodeLoader.prototype = new Loader(); 65 NodeLoader.prototype.parent = Loader; 66 NodeLoader.prototype.constructor = NodeLoader; 67 68 NodeLoader.prototype.name = "NodeLoader"; 69 NodeLoader.prototype._loadFile = function (pathname, sync, cb) { 70 var text; 71 //console.log("NodeLoader._loadFile: loading " + pathname + (sync ? " sync" : " async")); 72 try { 73 // on node, just secret load everything synchronously, even when asynchronous 74 // load is requested, or else you will get crazy results where files are not read 75 // until a long time later when the run queue is free 76 text = fs.readFileSync(pathname, "utf-8"); 77 if (typeof(cb) === 'function') { 78 cb(text); 79 } 80 } catch (e) { 81 //console.log("NodeLoader._loadFile: caught exception"); 82 if (typeof(cb) === 'function') { 83 cb(); 84 } 85 } 86 return text; 87 }; 88 89 NodeLoader.prototype._exists = function(dir, file) { 90 var fullpath = path.normalize(path.join(dir, file)); 91 //console.log("NodeLoader._exists: checking for the existence of " + dir); 92 if (fs.existsSync(fullpath)) { 93 //console.log("NodeLoader._exists: found"); 94 this.includePath.push(dir); 95 } 96 }; 97 98 return new NodeLoader(ilib); 99 }; 100