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