This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
不四
committed
Jun 11, 2014
0 parents
commit 1b3e106
Showing
9 changed files
with
286 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,3 @@ | ||
node_modules | ||
coverage | ||
*.log |
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 @@ | ||
language: node_js | ||
node_js: | ||
- 0.11 | ||
- 0.10 |
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,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. |
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,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 |
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,33 @@ | ||
refresh-config | ||
------------ | ||
|
||
[](http://badge.fury.io/js/refresh-config) | ||
[](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 |
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,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(); | ||
}; |
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,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" | ||
} | ||
} |
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,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(); | ||
}); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
{"foo": "bar"} |