Code coverage report for father/lib/package.js

Statements: 98.72% (77 / 78)      Branches: 84.38% (27 / 32)      Functions: 93.75% (15 / 16)      Lines: 100% (76 / 76)      Ignored: none     

All files » father/lib/ » package.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159    1 1 1 1 1 1 1 1     1       1         10 7   3     10 10   10 10       10 10 10   10   10   10 10 7   10     10 10 10 10       10 10   10   1 16   16 16 16 16   1 1     15   16 16     16 16 1   16     15     17     6 6     15   20       15         11 4     7 7         151 69     82 82       10 10 60   52 52 5 5 9 9   5   47 47                               1  
'use strict';
 
var fs = require('fs');
var path = require('path');
var join = path.join;
var dirname = path.dirname;
var extname = path.extname;
var requires = require('requires');
var Class = require('arale').Class;
var Events = require('arale').Events;
 
// config an extension as key, when that extension is found, value will be added to deps
var extraDeps = {
  handlebars: 'handlebars'
};
 
var Package = Class.create({
 
  Implements: [Events],
 
  initialize: function(dir, father) {
    if (father) {
      this.father = father;
    } else {
      this.packages = {};
    }
 
    this.dest = dir;
    this._dependencies = [];
 
    var keys = ['name', 'version', 'main', 'origin', 'dependencies', 'files'];
    this._exportProperty(keys);
  },
 
  _parse: function() {
    this._running = true;
    var Self = this.constructor;
    var pkg = this._pkg = this.readPackage();
 
    this.id = pkg.id;
 
    Object.keys(pkg.dependencies)
      .forEach(function(name) {
        var sub = pkg.dependencies[name];
        if (!this.get(sub.id)) {
          this.set(new Self(sub.dest, this)._parse());
        }
        this._dependencies.push(sub.id);
      }.bind(this));
 
    this._parseFiles();
    this._parsed = true;
    this._running = false;
    return this;
  },
 
  _parseFiles: function() {
    var that = this, dest = this.dest;
    var pkg = this._pkg, files = pkg.files = {};
 
    lookupFiles(pkg.main);
 
    function lookupFiles(src) {
      extname(src) || (src = src + '.js');
 
      Eif (!files[src]) {
        var data;
        try {
          data = fs.readFileSync(join(dest, src));
        } catch(e) {
          that.trigger('notfound', src);
          return [];
        }
 
        var req = requires(data)
          .reduce(function(previous, current) {
            var path = current.path;
            previous.push(path.replace(/\.js$/, ''));
 
            // extension in extraDeps will be added to deps
            var ext = extname(path).substring(1);
            if (extraDeps[ext]) {
              previous.push(extraDeps[ext]);
            }
            return previous;
          }, []);
 
        req
          .slice()
          .filter(function(item) {
            return item.charAt(0) === '.';
          })
          .forEach(function(item) {
            item = join(dirname(src), item);
            req = req.concat(lookupFiles(item));
          });
 
        files[src] = {
          dependencies: req.filter(function(item, index, arr) {
            return index === arr.indexOf(item);
          })
        };
      }
      return req || [];
    }
  },
 
  set: function(pkg) {
    if (this.father) {
      return this.father.set(pkg);
    }
 
    Eif (!this.packages[pkg.id]) {
      this.packages[pkg.id] = pkg;
    }
  },
 
  get: function(id) {
    if (this.father) {
      return this.father.get(id);
    }
 
    Iif (!this._parsed && !this._running) this._parse();
    return id === this.id ? this : this.packages[id];
  },
 
  _exportProperty: function(keys) {
    var that = this;
    keys.forEach(function(key) {
      Object.defineProperty(that, key, {
        get: function() {
          if (!that._parsed && !that._running) that._parse();
          if (key === 'dependencies') {
            var deps = {};
            that._dependencies.forEach(function(id) {
              var pkg = that.get(id);
              deps[pkg.name] = pkg;
            });
            return deps;
          } else {
            var pkg = that.get(that.id);
            return pkg ? pkg._pkg[key] : '';
          }
        },
        configurable: true
      });
    });
  },
 
  /*
    Method below can be overridden
  */
 
  readPackage: function() {}
 
});
 
module.exports = Package;