Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
refresh config
Browse files Browse the repository at this point in the history
  • Loading branch information
不四 committed Jun 11, 2014
0 parents commit 1b3e106
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
*.log
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.11
- 0.10
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 node_modules

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
TESTS = test/*.test.js
REPORTER = tap
TIMEOUT = 3000
MOCHA_OPTS =

install:
@npm install --registry=http://registry.npm.taobao.net \
--disturl=http://npm.taobao.net/dist

test:
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
$(MOCHA_OPTS) \
$(TESTS)

.PHONY: test
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
refresh-config
------------

[![NPM version](https://badge.fury.io/js/refresh-config.svg)](http://badge.fury.io/js/refresh-config)
[![Build Status](https://travis-ci.org/node-modules/refresh-config.svg?branch=master)](https://travis-ci.org/node-modules/refresh-config)

refresh config when config file modified.

## Install

```
npm install refresh-config
```

## Usage

```js
var Config = require('refresh-config');

var config = Config('./config.json');

config.on('error', function (err) {
console.error(err.stack);
})
config.on('change', function (config) {
console.log(config);
});

```

## License

MIT
97 changes: 97 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*!
* refresh-config - index.js
* Copyright(c) 2014 dead_horse <[email protected]>
* MIT Licensed
*/

'use strict';

var debug = require('debug')('refresh-config');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var fs = require('fs');
var path = require('path');

/**
* expose `Config`
*/

module.exports = Config;

function Config(file) {
if (!(this instanceof Config)) {
return new Config(file);
}

this.file = path.resolve(file);
this.filename = path.basename(file);
this.dir = path.dirname(file);
debug('config base on %s', this.file);

this.watch();

this.data = {};
try {
var content = fs.readFileSync(this.file, 'utf-8');
this.data = JSON.parse(content);
} catch (err) {
debug('init error');
setImmediate(this.onerror.bind(this, err));
}
this.emit('change', this.data);

this.onerror = this.onerror.bind(this);
}

util.inherits(Config, EventEmitter);

Config.prototype.watch = function() {
if (this.watcher) {
return debug('already has dir watcher');
}

this.watcher = fs.watch(this.dir, {
persistent: true,
recursive: false
});
var self = this;
this.watcher.on('change', function (event, filename) {
if (filename !== self.filename) return;
self.onchange();
})
.on('error', this.onerror);
};

Config.prototype.onchange = function () {
debug('file %s changed', this.file);
var self = this;

fs.readFile(self.file, 'utf-8', function (err, content) {
if (err) {
if (err.code === 'ENOENT') {
debug('config file removed');
self.data = {};
return self.emit('change', self.data);
}
return self.emit('error', err);
}

try {
self.data = JSON.parse(content);
} catch (err) {
debug('parse json content error');
return self.emit('error', err);
}

self.emit('change', self.data);
});
};

Config.prototype.onerror = function(err) {
if (err) this.emit('error', err);
};

Config.prototype.close =
Config.prototype.destroy = function() {
this.watcher && this.watcher.close();
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "refresh-config",
"version": "0.0.0",
"description": "refresh config when config file change",
"main": "index.js",
"scripts": {
"test": "make test"
},
"keywords": [
"cluster",
"restart"
],
"repository": "node-modules/refresh-config",
"author": "dead_horse <[email protected]>",
"license": "MIT",
"dependencies": {
"debug": "~1.0.2"
},
"devDependencies": {
"mocha": "^1.20.1",
"should": "^3.3.2"
}
}
86 changes: 86 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*!
* hot-restart - test/index.test.js
* Copyright(c) 2014 dead_horse <[email protected]>
* MIT Licensed
*/

'use strict';

/**
* Module dependencies.
*/

var Config = require('..');
var path = require('path');
var fs = require('fs');

var config;
var configPath = path.join(__dirname, 'support', 'config.json');
var originContent = fs.readFileSync(configPath);

describe('refresh-config', function () {
before(function () {
config = Config(configPath);
config.data.should.eql(JSON.parse(originContent));
});

describe('init', function () {
it('should emit error when file not eixst', function (done) {
var c = Config('not_exsit.json');
c.once('error', function (err) {
err.should.be.an.Error;
done();
})
});
});

describe('onchange', function () {
after(function () {
fs.writeFileSync(configPath, originContent);
});

it('should emit change when file changed', function (done) {
var changeData = {hello: 'world'};
fs.writeFileSync(configPath, JSON.stringify( changeData ));
config.once('change', function (data) {
data.should.eql(changeData);
data.should.equal(config.data);
done();
});
});

it('should error when parse error', function (done) {
var changeData = 'parse error data';
fs.writeFileSync(configPath, changeData);
config.once('error', function (err) {
done();
});
});

it('should remove file fine', function (done) {
config.once('change', function (data) {
data.should.eql({});
done();
});
fs.unlinkSync(configPath);
});

it('should readd ok', function (done) {
fs.writeFileSync(configPath, originContent);
config.once('change', function (data) {
data.should.eql(JSON.parse(originContent));
done();
});
});

it('should change again ok', function (done) {
var changeData = {hello: 'world'};
fs.writeFileSync(configPath, JSON.stringify( changeData ));
config.once('change', function (data) {
data.should.eql(changeData);
data.should.equal(config.data);
done();
});
});
});
});
1 change: 1 addition & 0 deletions test/support/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo": "bar"}

0 comments on commit 1b3e106

Please sign in to comment.