Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

auto-reload

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

auto-reload - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

lib/fileWatcher.js

67

lib/reload.js
const fs = require('fs');
const vm = require('vm');
const Path = require('path');
const fw = require('./fileWatcher');
// TODO deprecate this.. and use async/await to replace this
function reload(path, interval) {

@@ -8,7 +11,15 @@ const stack = getStack();

const whole = getWhole(Path.join(cwd, path || ''));
interval = interval || 5000;
// TODO use options
interval = Number(interval);
// todo check interval is num, or json
let module = require(whole);
setInterval(function flush() {
if (interval > 1000) {
setInterval(flush, interval);
} else {
fw.check(whole, flush);
}
return module;
function flush() {
debug('it\'s time to update!');

@@ -18,4 +29,3 @@ // todo check modify if optioned

try {
let data = fs.readFileSync(whole).toString();
let update = JSON.parse(data);
let update = load(whole);
// renew new module data to old reference

@@ -26,8 +36,53 @@ renew(module, update);

} catch(err) {
// TODO use Pomise.reject to return the error
console.error('flush error', err);
}
}, interval);
return module;
}
};
// TODO rewrite with promise
function load(path) {
const ext = Path.extname(path);
if (ext == '.json') {
return loadJson(path);
}
if (ext == '.js') {
return loadCode(path);
}
// TODO Pomise.reject
throw new Error('Bad file extname ' + ext);
}
function loadJson(path) {
try {
const data = fs.readFileSync(path).toString();
return JSON.parse(data);
} catch(err) {
throw new Error(err);
}
}
function loadCode(path) {
try {
const code = fs.readFileSync(path).toString();
const module = {
exports: {}
};
const ctx = vm.createContext(Object.assign({
module,
exports: module.exports,
__filename: path,
__dirname: Path.dirname(path),
require
}, global));
vm.runInContext(`
(function (exports, require, module, __filename, __dirname) {
${code}
})(exports, require, module, __filename, __dirname)`, ctx);
return module.exports;
} catch(err) {
throw new Error(err);
}
}
function renew(dest, src) {

@@ -34,0 +89,0 @@ if (!src) return;

2

package.json
{
"name": "auto-reload",
"version": "0.0.5",
"version": "0.1.0",
"description": "To clear require cache, and auto reload module",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,51 +0,92 @@

node-reload
===========
# node-reload
For node.js to reload the module ,which was to be load by `require`, automaticly.
**realod .js file havent support** it's only work for .json now.
## Install
Install
```
npm install auto-reload
```
npm install auto-reload
## Example
example
------------
### Reload Json
data.json
`test/data/json.json`
{ "name" : "Alan" }
```json
{ "name" : "Alan" }
```
test.js
`test/json.js`
```js
const fs = require('fs');
const reload = require('auto-reload');
const data = reload('./data/json'); // reload after file changed
var fs = require('fs');
var reload = require('auto-reload');
var data = reload('./data', 3000); // reload every 3 secs
// print data every sec
setInterval(function() {
console.log(data);
}, 1000);
// update data.json every 3 secs
setInterval(function() {
var data = '{ "name":"' + Math.random() + '" }';
fs.writeFile('./data.json', data);
}, 3000);
// print data every sec
setInterval(() => {
console.log(data.rand, data.list[0].name);
}, 1000);
// update data.json after startup
setTimeout(() => {
const text = `{
"rand": ${Math.random()},
"list": [{
"name": "Test"
}]
}`;
fs.writeFileSync(path.join(__dirname, './data/json.json'), text);
}, 0);
```
Result:
{ name: 'Alan' }
{ name: 'Alan' }
{ name: 'Alan' }
{ name: 'Alan' }
{ name: 'Alan' }
{ name: '0.8272748321760446' }
{ name: '0.8272748321760446' }
{ name: '0.8272748321760446' }
{ name: '0.07935990858823061' }
{ name: '0.07935990858823061' }
{ name: '0.07935990858823061' }
{ name: '0.20851597073487937' }
{ name: '0.20851597073487937' }
{ name: '0.20851597073487937' }
```js
0 'Alan'
0 'Alan'
0 'Alan'
0 'Alan'
0.41179045320583496 'Test'
```
### Reload Js file
**Realod function haven't support**
`test/data/code.js`
```js
module.exports = {
num: 0,
str: 'string',
obj: {
name: 'Alan',
age: 18
},
list: [1,3,5,7,9]
};
```
`test/code.js`
```js
const reload = require('../');
const data = reload('./data/code');
// print data from module every sec
setInterval(function() {
console.log(new Date, data);
}, 1000);
// If you update the `time` in test/data/code.js
// the output will change immediately
```
## Aims
- [x] auto reload json file
- [x] auto reload js file
- [x] fix memory leak
- [ ] provide more options (include logger etc.)
- [ ] rewrite with promise (include improve exception catch)
- [ ] add tests with ava
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc