Skip to content

Commit

Permalink
file has lookup method to transform all dependencies of file
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Jul 10, 2014
1 parent 7b99195 commit 39499d7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- move extraDeps to transport
- lookupPackage
- file has lookup method to transform all dependencies of file

## 0.7.3

Expand Down
41 changes: 41 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var path = require('path');
var join = path.join;
var dirname = path.dirname;
var isRelative = require('./util').isRelative;

module.exports = File;

function File(filepath, pkg) {
this.filepath = filepath;
this.pkg = pkg;
}

File.prototype.lookup = function(cb) {
var deps = [];
var fileDeps = this.dependencies;
var pkg = this.pkg;
var filepath = this.filepath;

fileDeps.forEach(function(file) {
if (isRelative(file)) {
file = resolvePath(file, filepath);
deps.push(cb(file, pkg, true));
} else {
var pkg_ = pkg.dependencies[file];
deps.push(cb(pkg_.main, pkg_));

file = pkg_.files[pkg_.main];
deps = deps.concat(file.lookup(cb));
}
});

return deps.filter(function(item, index, arr) {
return item !== false && index === arr.indexOf(item);
});
};

function resolvePath(relative, base) {
return join(dirname(base), relative);
}
3 changes: 2 additions & 1 deletion lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Class = require('arale').Class;
var Events = require('arale').Events;
var debug = require('debug')('father:package');
var util = require('./util');
var File = require('./file');

var defaults = {
// another entry point for parse
Expand Down Expand Up @@ -168,7 +169,7 @@ var Package = Class.create({
// dependencies exist if file has been parsed,
return files[src].dependencies;
} else {
files[src] = {};
files[src] = new File(src, that);
}

// file dependencies
Expand Down
47 changes: 47 additions & 0 deletions test/spm.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,53 @@ describe('Father.SpmPackage', function() {
pkg.files['index.js'].dependencies.should.eql(['b', './a.js']);
});

it('lookup files', function() {
var pkg = getPackage('normal');

// all deps
var ret1 = pkg.files['a.js'].lookup(function(filepath, pkg) {
return [pkg.name, pkg.version, filepath].join('/');
});
ret1.should.eql([
'b/1.1.0/src/b.js',
'c/1.1.1/index.js',
'd/0.1.0/index.js',
'b/1.1.0/src/b.tpl',
'a/1.0.0/b.js',
'd/0.1.1/index.js',
'a/1.0.0/a.json',
'a/1.0.0/a.handlebars'
]);

// relative deps
var ret2 = pkg.files['a.js'].lookup(function(filepath, pkg_) {
return pkg.name === pkg_.name ?
[pkg_.name, pkg_.version, filepath].join('/') :
[pkg_.name, pkg_.version, pkg_.main].join('/');
});
ret2.should.eql([
'b/1.1.0/src/b.js',
'c/1.1.1/index.js',
'd/0.1.0/index.js',
'a/1.0.0/b.js',
'd/0.1.1/index.js',
'a/1.0.0/a.json',
'a/1.0.0/a.handlebars'
]);

// isRelative
var ret3 = pkg.files['a.js'].lookup(function(filepath, pkg, isRelative) {
if (isRelative) return false;
return [pkg.name, pkg.version, filepath].join('/');
});
ret3.should.eql([
'b/1.1.0/src/b.js',
'c/1.1.1/index.js',
'd/0.1.0/index.js',
'd/0.1.1/index.js'
]);
});

describe('error', function() {

it('not found ./b', function() {
Expand Down

0 comments on commit 39499d7

Please sign in to comment.