buddy

node MVC discord bot
Log | Files | Refs | README

sync.js (2536B)


      1 var core = require('./core');
      2 var fs = require('fs');
      3 var path = require('path');
      4 var caller = require('./caller.js');
      5 var nodeModulesPaths = require('./node-modules-paths.js');
      6 
      7 module.exports = function (x, opts) {
      8     if (!opts) opts = {};
      9     var isFile = opts.isFile || function (file) {
     10         try { var stat = fs.statSync(file) }
     11         catch (err) { if (err && err.code === 'ENOENT') return false }
     12         return stat.isFile() || stat.isFIFO();
     13     };
     14     var readFileSync = opts.readFileSync || fs.readFileSync;
     15     
     16     var extensions = opts.extensions || [ '.js' ];
     17     var y = opts.basedir || path.dirname(caller());
     18 
     19     opts.paths = opts.paths || [];
     20 
     21     if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
     22         var res = path.resolve(y, x);
     23         if (x === '..') res += '/';
     24         var m = loadAsFileSync(res) || loadAsDirectorySync(res);
     25         if (m) return m;
     26     } else {
     27         var n = loadNodeModulesSync(x, y);
     28         if (n) return n;
     29     }
     30     
     31     if (core[x]) return x;
     32     
     33     throw new Error("Cannot find module '" + x + "' from '" + y + "'");
     34     
     35     function loadAsFileSync (x) {
     36         if (isFile(x)) {
     37             return x;
     38         }
     39         
     40         for (var i = 0; i < extensions.length; i++) {
     41             var file = x + extensions[i];
     42             if (isFile(file)) {
     43                 return file;
     44             }
     45         }
     46     }
     47     
     48     function loadAsDirectorySync (x) {
     49         var pkgfile = path.join(x, '/package.json');
     50         if (isFile(pkgfile)) {
     51             var body = readFileSync(pkgfile, 'utf8');
     52             try {
     53                 var pkg = JSON.parse(body);
     54                 if (opts.packageFilter) {
     55                     pkg = opts.packageFilter(pkg, x);
     56                 }
     57                 
     58                 if (pkg.main) {
     59                     var m = loadAsFileSync(path.resolve(x, pkg.main));
     60                     if (m) return m;
     61                     var n = loadAsDirectorySync(path.resolve(x, pkg.main));
     62                     if (n) return n;
     63                 }
     64             }
     65             catch (err) {}
     66         }
     67         
     68         return loadAsFileSync(path.join( x, '/index'));
     69     }
     70     
     71     function loadNodeModulesSync (x, start) {
     72         var dirs = nodeModulesPaths(start, opts);
     73         for (var i = 0; i < dirs.length; i++) {
     74             var dir = dirs[i];
     75             var m = loadAsFileSync(path.join( dir, '/', x));
     76             if (m) return m;
     77             var n = loadAsDirectorySync(path.join( dir, '/', x ));
     78             if (n) return n;
     79         }
     80     }
     81 };