Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function watchedRequire(file)
- {
- var fs = require('fs');
- var path = require('path');
- path.exists(file, function(exists) {
- if(exists)
- {
- fs.watchFile(file, function(current, previous) {
- if(current.nlink === 0) // file does not exist anymore
- {
- delete(require.cache[file]);
- fs.unwatchFile(file);
- console.log('Deleted');
- return;
- }
- if(current.mtime - previous.mtime) // has changed
- {
- delete(require.cache[file]);
- console.log('Changed');
- require(file); // rebuild cache with new file immediately
- }
- // console.log(require.cache);
- });
- return require(file);
- }
- throw new Error('File not found: '+file);
- });
- }
- var file = watchedRequire(process.cwd()+'/file.js');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement