Code coverage report for father/lib/package.js

Statements: 99.13% (114 / 115)      Branches: 91.84% (45 / 49)      Functions: 95% (19 / 20)      Lines: 100% (112 / 112)      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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221    1 1 1 1 1 1 1 1 1 1 1     1         21   21 10   11       21 21 21 21   21 21       21 21   21 21   21 21   21   13 13 10 10   13   21   21   21 21 21 21       21 21     21     21 21 21   9     21   1 47 47 8 8     47 5     42 42   4 4       38 38     38 31   31 14     17 17   8 2 2 2   17 17       38 39     38       38   38         14 4     10 10         190 76     114 114       21 21 147   81 81 8 8 12 12   8   73 73                               1     1 38   26   7       5     1 28       1 10 10 20 34     10    
'use strict';
 
var fs = require('fs');
var path = require('path');
var join = path.join;
var dirname = path.dirname;
var extname = path.extname;
var relative = path.relative;
var requires = require('requires');
var imports = require('css-imports');
var Class = require('arale').Class;
var Events = require('arale').Events;
var debug = require('debug')('father:package');
 
 
var Package = Class.create({
 
  Implements: [Events],
 
  initialize: function(dir, options) {
    this.options = options || {};
 
    if (this.options.father) {
      this.father = this.options.father;
    } else {
      this.packages = {};
    }
 
    // config an extension as key, when that extension is found, value will be added to deps
    this.options.extraDeps = this.options.extraDeps || {};
    this.options.output = this.options.output || [];
    this.dest = dir;
    this._dependencies = [];
 
    var keys = ['name', 'version', 'main', 'origin', 'dependencies', 'files', 'output'];
    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)) {
          var opt = extend({}, this.options, {father: this});
          this.set(new Self(sub.dest, opt)._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 = {};
 
    // base on pkg.main
    lookupFiles(pkg.main);
 
    // base on pkg.output
    var output = this.options.output;
    Eif (Array.isArray(pkg.output)) {
      output = output.concat(pkg.output)
        .filter(function(item, index, arr) {
          return index === arr.indexOf(item);
        });
    }
    output.forEach(lookupFiles);
 
    function lookupFiles(src) {
      var ext = extname(src).substring(1);
      if (!ext) {
        src = src + '.js';
        ext = 'js';
      }
 
      if (files[src]) {
        return files[src].dependencies;
      }
 
      try {
        var data = fs.readFileSync(join(dest, src)).toString();
      } catch(e) {
        that.trigger('notfound', src);
        return [];
      }
 
      // file dependencies
      var deps = getFileDeps(data, ext, that.options.extraDeps);
      debug('file %s, deps %s', src, deps);
 
      // conbime with the dependencies of the dependent file
      deps = deps.reduce(function(previous, current) {
        previous.push(current);
 
        if (current.charAt(0) !== '.') {
          return previous;
        }
 
        current = join(dirname(src), current);
        var deps_ = lookupFiles(current)
          .map(function(it) {
            if (it.charAt(0) !== '.') return it;
            it = join(dirname(current), it);
            it = relative(dirname(src), it);
            return it.charAt(0) === '.' ? it : './' + it;
          });
        previous = previous.concat(deps_);
        return previous;
      }, []);
 
      // unique
      deps = deps.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 ext === 'css' ? [] : deps;
    }
  },
 
  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;
 
 
function getFileDeps(code, ext, extraDeps) {
  switch(ext) {
    case 'js':
      return requires(code).map(transform);
    case 'css':
      return imports(code).map(transform);
    default:
      // extension in extraDeps will be added to deps
      // E.g. a.handlebars should require `handlebars`
      return extraDeps[ext] ? [extraDeps[ext]] : [];
  }
 
  function transform(item) {
    return item.path.replace(/\.js$/, '');
  }
}
 
function extend(target) {
  var args = [].slice.call(arguments, 1);
  args.forEach(function(obj) {
    for (var i in obj) {
      target[i] = obj[i];
    }
  });
  return target;
}