Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

(initial)

  • Loading branch information...
commit d50bff7f15eea5533a79de834a58e6aa66e86281 0 parents
@indutny authored
4 .gitignore
@@ -0,0 +1,4 @@
+node_modules
+npm-debug.log
+tests/data/*
+!tests/data/.keep
0  README.md
No changes.
15 lib/bar.js
@@ -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;
106 lib/bar/core.js
@@ -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);
+};
25 lib/bar/drinker.js
@@ -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;
41 lib/bar/utils.js
@@ -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;
15 package.json
@@ -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"
+ }
+}
15 tests/bar-basic-test.js
@@ -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);
0  tests/data/.keep
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.