Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d50bff7
Showing
9 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
npm-debug.log | ||
tests/data/* | ||
!tests/data/.keep |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Node.js Bar Module | ||
*/ | ||
|
||
var bar = exports; | ||
|
||
// Utils | ||
bar.utils = require('./bar/utils'); | ||
|
||
// Drinker | ||
bar.drinker = require('./bar/drinker'); | ||
|
||
// Core | ||
bar.create = require('./bar/core').create; | ||
bar.Bar = require('./bar/core').Bar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Node.js Bar Module | ||
* | ||
* Core | ||
*/ | ||
|
||
var core = exports; | ||
|
||
var fs = require('fs'), | ||
watch = require('watch'); | ||
|
||
var bar = require('../bar'); | ||
|
||
/** | ||
* Bar @constructor | ||
*/ | ||
function Bar(dir, options) { | ||
var that = this; | ||
|
||
/** | ||
* Drinkers in a Bar | ||
*/ | ||
this.drinkers = {}; | ||
|
||
/** | ||
* Store options | ||
*/ | ||
this.options = options = bar.utils.merge({ | ||
drinker: { | ||
} | ||
}, options); | ||
|
||
/** | ||
* Monitor file changes in directory | ||
*/ | ||
watch.watchTree(dir, function(f, curr, prev) { | ||
if (curr !== null && curr.nlink === 0) { | ||
/** | ||
* File was removed | ||
*/ | ||
that.leave(f); | ||
} else { | ||
/** | ||
* File was updated/created | ||
*/ | ||
that.enter(curr === null ? Object.keys(f) : [f]); | ||
} | ||
}); | ||
}; | ||
core.Bar = Bar; | ||
|
||
/** | ||
* Constructor wrapper | ||
*/ | ||
function create(dir, options) { | ||
return new Bar(dir, options); | ||
}; | ||
core.create = create; | ||
|
||
/** | ||
* Add files | ||
*/ | ||
Bar.prototype.enter = function(drinkers) { | ||
var that = this; | ||
|
||
drinkers.forEach(function(file) { | ||
/** | ||
* Load each file | ||
*/ | ||
fs.readFile(file, function(err, content) { | ||
if (err) { | ||
that.log('Failed to load file %s', file, err); | ||
return; | ||
}; | ||
|
||
/** | ||
* Convert content from Buffer to String | ||
*/ | ||
content = content.toString(); | ||
|
||
/** | ||
* If that drinker was already here | ||
* Kick him out and let one come in | ||
*/ | ||
if (that.drinkers[file]) { | ||
that.drinkers[file].emit('kick'); | ||
} | ||
|
||
that.drinkers[file] = bar.drinker.create(file, content, | ||
that.options.drinker); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Remove file | ||
*/ | ||
Bar.prototype.leave = function(drinker) { | ||
}; | ||
|
||
/** | ||
* Log errors | ||
*/ | ||
Bar.prototype.log = function() { | ||
console.log.apply(console, arguments); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Node.js Bar Module | ||
* | ||
* Drinker | ||
*/ | ||
|
||
var drinker = exports; | ||
|
||
var bar = require('../bar'); | ||
|
||
/** | ||
* Drinker @constructor | ||
*/ | ||
function Drinker(file, content, options) { | ||
this.file = file; | ||
}; | ||
drinker.Drinker = Drinker; | ||
|
||
/** | ||
* Constructor wrapper | ||
*/ | ||
function create(file, content, options) { | ||
return new Drinker(file, content, options); | ||
}; | ||
drinker.create = create; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Node.js Bar Module | ||
*/ | ||
|
||
var utils = exports; | ||
|
||
/** | ||
* Merge `a` and `b` into new object `c` | ||
*/ | ||
function merge(a, b) { | ||
var c = {}; | ||
|
||
/** | ||
* Copy all properties of a into c | ||
*/ | ||
if (a) { | ||
for (var i in a) { | ||
if (a.hasOwnProperty(i)) { | ||
c[i] = a[i]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Do same with b, but merge existing properties | ||
*/ | ||
if (b) { | ||
for (var i in b) { | ||
if (b.hasOwnProperty(i)) { | ||
c[i] = typeof b[i] === 'object' && | ||
typeof c[i] === 'object' ? | ||
merge(c[i], b[i]) | ||
: | ||
b[i]; | ||
} | ||
} | ||
} | ||
|
||
return c; | ||
}; | ||
utils.merge = merge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "bar", | ||
"description": "", | ||
"version": "0.1.0", | ||
"author": "Fedor Indutny <fedor.indutny@gmail.com>", | ||
|
||
"dependencies": { | ||
"watch": ">= 0.3.2", | ||
"eventemitter2": ">= 0.1.3" | ||
}, | ||
|
||
"scripts": { | ||
"test": "vows --spec tests/*-test.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var vows = require('vows'), | ||
assert = require('assert'); | ||
|
||
var bar = require('../lib/bar'); | ||
|
||
vows.describe('Bar').addBatch({ | ||
'Calling bar.create()': { | ||
topic: function() { | ||
bar.create(__dirname + '/data'); | ||
}, | ||
'should create bar.Bar instance': function(I) { | ||
assert.instanceOf(I, bar.Bar); | ||
} | ||
} | ||
}).export(module); |
Empty file.