gaze 基于 NodeJS 本地目录文件变化监听器

gaze 是一个基于 NodeJS 的本地目录文件变化监听器,一个通配符 fs.watch 包装从其他精细腕表库中最好的部分建成。与 Node.js >= 4.x、Windows、macOS 和 Linux 兼容。

gaze 基于 NodeJS 本地目录文件变化监听器使用方法

使用以下命令安装模块:npm install gaze 或放入您的 package.json 并运行 npm install

const gaze = require('gaze');

// Watch all .js files/dirs in process.cwd()

gaze('**/*.js', (err, watcher) => {

// Files have all started watching

// Get all watched files

const watched = watcher.watched();

// On file changed

watcher.on('changed', filepath => {

console.log(filepath + ' was changed');

});

// On file added

watcher.on('added', filepath => {

console.log(filepath + ' was added');

});

// On file deleted

watcher.on('deleted', filepath => {

console.log(filepath + ' was deleted');

});

// On changed/added/deleted

watcher.on('all', (event, filepath) => {

console.log(filepath + ' was ' + event);

});

// Get watched files with relative paths

const files = watcher.relative();

});

// Also accepts an array of patterns

gaze(['stylesheets/*.css', 'images/**/*.png'], () => {

// Add more patterns later to be watched

watcher.add(['js/*.js']);

});

备用接口

const {Gaze} = require('gaze');

const gaze = new Gaze('**/*');

// Files have all started watching

gaze.on('ready', watcher => { });

// A file has been added/changed/deleted has occurred

gaze.on('all', (event, filepath) => { });

错误

gaze('**/*', (error, watcher) => {

if (error) {

// Handle error if it occurred while starting up

}

});

// Or with the alternative interface

const gaze = new Gaze();

gaze.on('error', error => {

// Handle error here

});

gaze.add('**/*');

迷你匹配 / 全局

有关glob 模式的更多信息,请参阅 isaacs minimatch

文档

gaze([patterns, options, callback])

  • patterns{ String| Array要匹配的文件模式
  • options{ Object}
  • callback{ Function}

    • err{ Error| null}
    • watcher{ Object}Gaze观察者的实例

Class: gaze.Gaze

Gaze 通过实例化 gaze.Gaze 类来创建一个对象。

const Gaze = require('gaze').Gaze;

const gaze = new Gaze(pattern, options, callback);

  • options 传入的选项对象。

    • interval {整数} 传递到的间隔 fs.watchFile
    • debounceDelay {integer} 以毫秒为单位为同一文件/事件连续调用的事件的延迟
    • mode {string} 强制观看模式。无论是 'auto'(默认值), 'watch'(力原生的事件),或 'poll'(力统计轮询)。
    • cwd {string} 当前工作目录,以从中获取基本文件模式。默认为 process.cwd()

事件

  • ready(watcher) 当文件被 globbed 并开始观看时。
  • all(event, filepath)addedchangedrenamed,或 deleted 事件发生时。
  • added(filepath) 当文件被添加到监视目录时。
  • changed(filepath) 当文件被更改时。
  • deleted(filepath) 当文件被删除时。
  • renamed(newPath, oldPath) 当文件被重命名时。
  • end() 当观察者关闭并且手表已被移除时。
  • error(err) 发生错误时。
  • nomatch 当没有匹配的文件时。

方法

  • emit(event, [...]) 的包装器 EventEmitter.emitadded| changed| renamed| deleted events 也会触发 all 事件。
  • close() 取消监视所有文件并重置监视实例。
  • add(patterns, callback) 添加 patterns 要观看的文件。
  • remove(filepath) 从被监视的文件或目录中删除。不递归目录。
  • watched() 返回当前观看的文件。
  • relative([dir, unixify]) 返回具有相对路径的当前观看的文件。

    • dir {string} 只返回这个目录的相关文件。
    • unixify {boolean}在 Windows 上返回路径 / 而不是 \\ if。

项目地址:https://github.com/shama/gaze

以上是 gaze 基于 NodeJS 本地目录文件变化监听器 的全部内容, 来源链接: utcz.com/p/233746.html

回到顶部