Code coverage report for father/lib/package.js

Statements: 100% (59 / 59)      Branches: 81.82% (18 / 22)      Functions: 88.24% (15 / 17)      Lines: 100% (59 / 59)      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    1 1 1 1 1 1 1   1     9 7   2     9 9 9       9 9   9   9   10 10 7   10     9 9   9       9 9   9   1 12   12 12   13   12     13     3 3     12   16       12         11 4     7 7         129 58     71       9 9 45     40 4 4 9 9   4   36 36                               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 Package = Class.create({
 
  initialize: function(dir, father) {
    if (father) {
      this.father = father;
    } else {
      this.packages = {};
    }
 
    this.dest = dir;
    this._dependencies = [];
    this.run();
  },
 
  run: function() {
    var that = this, 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 (!that.get(sub.id)) {
          that.set(new Self(sub.dest, that));
        }
        this._dependencies.push(sub.id);
      }.bind(this));
 
    var keys = ['name', 'version', 'main', 'origin', 'dependencies'];
    this._exportProperty(keys);
 
    this.parseFiles();
  },
 
  parseFiles: function() {
    var dest = this.dest;
    var files = this.files = {};
 
    lookupFiles.call(this, this._pkg.main);
 
    function lookupFiles(src) {
      extname(src) || (src = src + '.js');
 
      Eif (!files[src]) {
        var req = requires(fs.readFileSync(join(dest, src)))
          .map(function(item) {
            return item.path.replace('.js', '');
          });
        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);
    }
 
    return id === this.id ? this : this.packages[id];
  },
 
  _exportProperty: function(keys) {
    var that = this;
    keys.forEach(function(key) {
      Object.defineProperty(that, key, {
        set: function() {},
        get: function() {
          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;