Code coverage report for father/lib/package.js

Statements: 98.82% (84 / 85)      Branches: 84.38% (27 / 32)      Functions: 93.75% (15 / 16)      Lines: 100% (83 / 83)      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 160 161 162 163 164 165 166 167 168 169 170    1 1 1 1 1 1 1 1 1     1       1         12 8   4     12 12   12 12       12 12   12 12   12 12   12   11 11 8   11   12   12   12 12 12 12       12 12   12   1 18   18 18 18 18   2 2     16   16 16     16 16 1   16     16     17     6 6     16 20     16       16   16         12 4     8 8         158 71     87 87       12 12 72   56 56 7 7 11 11   7   49 49                               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;
var debug = require('debug')('father:package');
 
// 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() {
    debug('start parse %s', this.dest);
    this._running = true;
 
    var Self = this.constructor;
    var pkg = this._pkg = this.readPackage();
 
    debug('pkg(%s) id %s', pkg.id, pkg.id);
    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));
    debug('pkg(%s) dependencies [%s]', this.id, Object.keys(pkg.dependencies));
 
    this._parseFiles();
 
    debug('end parse %s', this.dest);
    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));
          });
 
        var deps = req.filter(function(item, index, arr) {
          return index === arr.indexOf(item);
        });
 
        files[src] = {
          dependencies: deps
        };
 
        debug('pkg(%s) file %s, deps %s', pkg.id, src, deps);
      }
      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;