1 /*
  2  * NodeLoader.js - Loader implementation for nodejs
  3  *
  4  * Copyright © 2015, 2020 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         // console.log("new common NodeLoader instance");
 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);
 47         //console.log("base is defined as " + this.base);
 48 
 49         // this.includePath.push(path.join(this.root, "resources"));     // always check the application's resources dir first
 50         this._exists(this.root, "resources"); // always check the application's resources dir first
 51         this._exists(path.join(this.root, "locale"), "localeinfo.json");
 52 
 53         // then a standard locale dir of a built version of ilib from npm
 54         this._exists(path.join(this.base, "locale"), "localeinfo.json");
 55 
 56         // try the standard install directories
 57         this._exists("/usr/share/javascript/ilib/locale", "localeinfo.json");
 58 
 59         // ... else fall back to see if we're in a check-out dir of ilib
 60         // this._exists(path.join(this.base, "data", "locale"), "localeinfo.json");
 61 
 62         // console.log("NodeLoader: include path is now " + JSON.stringify(this.includePath));
 63     };
 64 
 65     // make this a subclass of loader
 66     NodeLoader.prototype = new Loader();
 67     NodeLoader.prototype.parent = Loader;
 68     NodeLoader.prototype.constructor = NodeLoader;
 69 
 70     NodeLoader.prototype.name = "NodeLoader";
 71     NodeLoader.prototype._loadFile = function (pathname, sync, cb) {
 72         var text;
 73         //console.log("NodeLoader._loadFile: loading " + pathname + (sync ? " sync" : " async"));
 74         try {
 75             // on node, just secret load everything synchronously, even when asynchronous
 76             // load is requested, or else you will get crazy results where files are not read
 77             // until a long time later when the run queue is free
 78             text = fs.readFileSync(pathname, "utf-8");
 79             if (typeof(cb) === 'function') {
 80                 cb(text);
 81             }
 82         } catch (e) {
 83             //console.log("NodeLoader._loadFile: caught exception");
 84             if (typeof(cb) === 'function') {
 85                 cb();
 86             }
 87         }
 88         return text;
 89     };
 90 
 91     NodeLoader.prototype._exists = function(dir, file) {
 92         var fullpath = path.normalize(path.join(dir, file));
 93         //console.log("NodeLoader._exists: checking for the existence of " + dir);
 94         if (this.includePath.indexOf(fullpath) === -1 && fs.existsSync(fullpath)) {
 95             //console.log("NodeLoader._exists: found");
 96             this.includePath.push(dir);
 97         }
 98     };
 99 
100     return new NodeLoader(ilib);
101 };
102