Skip to content

Commit

Permalink
trigger notfound event when parse a not existed file
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Mar 26, 2014
1 parent b40bdd1 commit b8cb02b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

---

## 0.3.0

- lazy parse
- trigger `notfound` event when parse a not existed file

## 0.2.1

fix replace .js
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ coveralls: test
cat ./coverage/lcov.info | ./node_modules/.bin/coveralls

debug:
node --debug-brk ./node_modules/.bin/_mocha -R spec -t 20000
node ./node_modules/.bin/_mocha -R spec -t 20000

.PHONY: test
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ Example for returned object by readPackage
}
```

## Properties

- pkg.id
- pkg.name
- pkg.version
- pkg.main
- pkg.origin
- pkg.files
- pkg.dest
- pkg.get(depPackage.id)

### Files

pkg.files contains export files and it's dependencies in your module.
Expand Down
38 changes: 27 additions & 11 deletions lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ var dirname = path.dirname;
var extname = path.extname;
var requires = require('requires');
var Class = require('arale').Class;
var Events = require('arale').Events;

var Package = Class.create({

Implements: [Events],

initialize: function(dir, father) {
if (father) {
this.father = father;
Expand All @@ -19,10 +22,13 @@ var Package = Class.create({

this.dest = dir;
this._dependencies = [];
this.run();

var keys = ['name', 'version', 'main', 'origin', 'dependencies', 'files'];
this._exportProperty(keys);
},

run: function() {
_parse: function() {
this._running = true;
var that = this, Self = this.constructor;
var pkg = this._pkg = this.readPackage();

Expand All @@ -32,28 +38,36 @@ var Package = Class.create({
.forEach(function(name) {
var sub = pkg.dependencies[name];
if (!that.get(sub.id)) {
that.set(new Self(sub.dest, that));
that.set(new Self(sub.dest, that)._parse());
}
this._dependencies.push(sub.id);
}.bind(this));

var keys = ['name', 'version', 'main', 'origin', 'dependencies'];
this._exportProperty(keys);

this.parseFiles();
this._parseFiles();
this._parsed = true;
this._running = false;
return this;
},

parseFiles: function() {
var dest = this.dest;
var files = this.files = {};
_parseFiles: function() {
var that = this, dest = this.dest;
var files = this._pkg.files = {};

lookupFiles.call(this, this._pkg.main);

function lookupFiles(src) {
extname(src) || (src = src + '.js');

if (!files[src]) {
var req = requires(fs.readFileSync(join(dest, src)))
var data;
try {
data = fs.readFileSync(join(dest, src));
} catch(e) {
that.trigger('notfound', src);
return [];
}

var req = requires(data)
.map(function(item) {
return item.path.replace(/\.js$/, '');
});
Expand Down Expand Up @@ -92,6 +106,7 @@ var Package = Class.create({
return this.father.get(id);
}

if (!this._parsed && !this._running) this._parse();
return id === this.id ? this : this.packages[id];
},

Expand All @@ -101,6 +116,7 @@ var Package = Class.create({
Object.defineProperty(that, key, {
set: function() {},
get: function() {
if (!that._parsed && !that._running) that._parse();
if (key === 'dependencies') {
var deps = {};
that._dependencies.forEach(function(id) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/not-found/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./b');
8 changes: 8 additions & 0 deletions test/fixtures/not-found/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "a",
"version": "1.0.0",
"main": "a.js",
"spm": {
"dependencies": {}
}
}
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ describe('Father', function() {
func.files['index.js'].dependencies.should.eql([]);
});

it('not found', function() {
var pkg = new SpmPackage(join(base, 'not-found'));
pkg.on('notfound', function(src) {
src.should.eql('b.js');
});
pkg.dependencies.should.eql({});
});

});

0 comments on commit b8cb02b

Please sign in to comment.