node package manager

auto-watch

Utilities for auto watching file trees in node.js.

auto-watch

Utilities for auto watching file trees.

自动监视目录树中文件和目录的创建、删除、改变等等事件。当添加新目录时自动给新目录及其子目录和文件添加监视任务。

  npm install auto-watching
	require('./watch' ).create(function(watch){
		//...处理代码
	});
	//或者
	var watch = require('./watch' ).create();
	//...处理代码
  • create - 创建文件或目录时触发事件。两个参数:path,stat。path:创建文件或目录的路径;stat:fs.Stats
  • createfile - 创建文件时触发的事件。两个参数:path,stat。path:创建文件的路径;stat:fs.Stats
  • createdirectory - 创建目录时触发的事件。两个参数:path,stat。path:创建目录的路径;stat:fs.Stats
  • delete - 删除文件或目录时触发事件。两个参数:path,stat。path:删除文件或目录的路径;stat:fs.Stats
  • deletefile - 删除文件时触发的事件。两个参数:path,stat。path:删除文件的路径;stat:fs.Stats
  • deletedirectory - 删除目录时触发的事件。两个参数:path,stat。path:删除目录的路径;stat:fs.Stats
  • change - 修改文件或目录时触发事件。两个参数:path,stat。path:修改文件或目录的路径;stat:fs.Stats
  • changefile - 修改文件时触发的事件。两个参数:path,stat。path:修改文件的路径;stat:fs.Stats
  • changedirectory - 修改目录时触发的事件。两个参数:path,stat。path:修改目录的路径;stat:fs.Stats
	//字符串
	watch.exclude(['*index.js']);
	//正则表达式
	watch.exclude(/.*index.js$/);
	watch.exclude(new RegExp(/.*index.js$/));
	//函数
	watch.exclude(function(path){
		return path !== __filename;
	});
  • watch.listeners(object) - object必须,返回scanFS对象。
	watch.listeners({
		'create': function(path, stat){
			//...
		},
		'delete': function(path, stat){
			//...
		}
		//...
	});
  • 通过调用方法添加事件: onCreate(fn)onCreateFile(fn)onCreateDirectory(fn)onDelete(fn)onDeleteFile(fn)onDeleteDirectory(fn)onChange(fn)onChangeFile(fn)onChangeDirectory(fn)
var watch = require('./watch' ).create(function(watch){
 
    /*watch.exclude(['*index.js']);
     watch.exclude(/.*index.js$/);
     watch.exclude(new RegExp(/.*index.js$/));*/
 
    watch.listeners({
        'create': function(path, stat){
            console.log('create:' + path, stat);
        },
        'delete': function(path, stat){
            console.log('delete:' + path, stat);
        },
        'change': function(path, stat){
            console.log('change:' + path, stat);
        }
    }).watchTree(__dirname + '/test', function(err, fileCount, dirCount){
        console.log('test: ', arguments);
    });
 
});
 
//或者 
 
var watch = require('./watch' ).create(function(watch){
 
    /*watch.exclude(['*index.js']);
     watch.exclude(/.*index.js$/);
     watch.exclude(new RegExp(/.*index.js$/));*/
 
    watch.listeners({
        'createfile': function(path, stat){
            console.log('createfile:' + path, stat);
            console.log(watch.files);
        },
        'deletefile': function(path, stat){
            console.log('deletefile:' + path, stat);
            console.log(watch.files);
        },
        'changefile': function(path, stat){
            console.log('changefile:' + path, stat);
            console.log(watch.files);
        },
        'createdirectory': function(path, stat){
            console.log('createdirectory:' + path, stat);
            console.log(watch.files);
        },
        'deletedirectory': function(path, stat){
            console.log('deletedirectory:' + path, stat);
            console.log(watch.files);
        },
        'changedirectory': function(path, stat){
            console.log('changedirectory:' + path, stat);
            console.log(watch.files);
        }
    } ).watchTree(__dirname + '/test', function(err, fileCount, dirCount){
        console.log('test: ', arguments);
    });
 
});