chokidar-cli vs grunt-contrib-watch vs gulp-watch vs nodemon
文件监视工具
chokidar-cligrunt-contrib-watchgulp-watchnodemon类似的npm包:

文件监视工具

文件监视工具用于监控文件系统中的变更,并在文件发生变化时自动执行特定的任务。这些工具在开发过程中非常有用,可以提高工作效率,减少手动操作的需要。它们通常用于自动化构建、测试和部署流程。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
chokidar-cli00-05 年前MIT
grunt-contrib-watch01,968-1268 年前MIT
gulp-watch0637-708 年前MIT
nodemon026,696219 kB122 个月前MIT

功能对比: chokidar-cli vs grunt-contrib-watch vs gulp-watch vs nodemon

监视机制

  • chokidar-cli:

    chokidar-cli 使用高效的文件系统事件 API,能够快速响应文件变化。它支持多种操作系统,并且能够处理大量文件的监视,适合需要高性能监视的场景。

  • grunt-contrib-watch:

    grunt-contrib-watch 通过 Grunt 的任务系统来监视文件变化,适合与其他 Grunt 插件结合使用。它的监视机制相对简单,适合小型项目或已有 Grunt 工作流的项目。

  • gulp-watch:

    gulp-watch 利用 Gulp 的流式处理特性,能够在文件变化时立即执行任务。它支持多种文件操作,并且能够与 Gulp 的其他插件无缝集成,适合需要复杂构建流程的项目。

  • nodemon:

    nodemon 主要用于监视 Node.js 应用程序的文件变化,并在检测到变化时自动重启应用。它能够监视 JavaScript、JSON、HTML 等多种文件类型,适合后端开发。

配置灵活性

  • chokidar-cli:

    chokidar-cli 提供简单的命令行选项,用户可以根据需要快速配置监视的文件和目录。它的灵活性使得用户可以轻松适应不同的项目需求。

  • grunt-contrib-watch:

    grunt-contrib-watch 的配置相对复杂,需要在 Gruntfile 中定义任务和监视规则。适合需要高度定制化的项目,但学习曲线较陡。

  • gulp-watch:

    gulp-watch 的配置相对简单,用户可以直接在 Gulp 任务中定义监视规则,适合快速开发和迭代。它的灵活性使得用户可以轻松调整监视的文件和任务。

  • nodemon:

    nodemon 的配置非常简单,用户只需在命令行中指定要监视的文件和启动命令即可。它适合快速开发和调试 Node.js 应用。

性能

  • chokidar-cli:

    chokidar-cli 在性能方面表现优异,能够处理大量文件的监视而不会显著影响系统性能。它使用了高效的文件系统 API,能够快速响应文件变化。

  • grunt-contrib-watch:

    grunt-contrib-watch 的性能依赖于 Grunt 的任务执行效率,可能在处理大量文件时出现性能瓶颈。适合小型项目,但在大型项目中可能需要优化。

  • gulp-watch:

    gulp-watch 利用 Gulp 的流式处理特性,能够高效处理文件变化,性能表现良好。适合需要快速响应的项目,能够有效减少构建时间。

  • nodemon:

    nodemon 的性能表现良好,能够快速检测文件变化并重启应用。适合后端开发,但在大型应用中可能需要注意重启的频率。

社区支持

  • chokidar-cli:

    chokidar-cli 拥有活跃的社区支持,用户可以轻松找到文档和示例,适合新手使用。

  • grunt-contrib-watch:

    grunt-contrib-watch 是 Grunt 生态系统的一部分,拥有丰富的文档和社区支持,但由于 Grunt 的使用逐渐减少,社区活跃度有所下降。

  • gulp-watch:

    gulp-watch 在 Gulp 社区中有良好的支持,用户可以找到大量的插件和示例,适合需要扩展功能的项目。

  • nodemon:

    nodemon 拥有广泛的用户基础和活跃的社区支持,用户可以轻松找到解决方案和使用示例,适合新手和经验丰富的开发者。

学习曲线

  • chokidar-cli:

    chokidar-cli 的学习曲线较平缓,用户可以快速上手并进行基本配置,非常适合初学者。

  • grunt-contrib-watch:

    grunt-contrib-watch 的学习曲线较陡,用户需要了解 Grunt 的任务系统和配置方式,适合有一定经验的开发者。

  • gulp-watch:

    gulp-watch 的学习曲线相对平缓,用户可以快速理解并使用,适合快速开发和迭代。

  • nodemon:

    nodemon 的学习曲线非常平缓,用户只需了解基本命令即可快速使用,适合所有级别的开发者。

如何选择: chokidar-cli vs grunt-contrib-watch vs gulp-watch vs nodemon

  • chokidar-cli:

    选择 chokidar-cli 如果你需要一个轻量级的命令行工具来监视文件变化,并且希望能够快速地执行简单的命令。它非常适合小型项目或简单的监视需求。

  • grunt-contrib-watch:

    选择 grunt-contrib-watch 如果你已经在使用 Grunt 作为构建工具,并希望在构建过程中集成文件监视功能。它与 Grunt 的其他插件兼容性好,适合需要复杂构建流程的项目。

  • gulp-watch:

    选择 gulp-watch 如果你使用 Gulp 作为构建工具,并希望利用 Gulp 的流式处理特性来处理文件变化。它适合需要高效处理文件流的项目,能够轻松集成到现有的 Gulp 任务中。

  • nodemon:

    选择 nodemon 如果你正在开发 Node.js 应用程序,并希望在代码更改时自动重启服务器。它非常适合后端开发,可以提高开发效率,避免手动重启服务器的麻烦。

chokidar-cli的README

Chokidar CLI

Build Status

Fast cross-platform command line utility to watch file system changes.

The underlying watch library is Chokidar, which is one of the best watch utilities for Node. Chokidar is battle-tested:

It is used in brunch, gulp, karma, PM2, browserify, webpack, BrowserSync, socketstream, derby, and many others. It has proven itself in production environments.

Prerequisites

  • Node.js v8.10.0 or newer

Install

If you need it only with npm scripts:

npm install chokidar-cli

Or globally

npm install -g chokidar-cli

Usage

Chokidar can be invoked using the chokidar command, without the -cli suffix.

Arguments use the form of runtime flags with string parameters, delimited by quotes. While in principal both single and double quotes are supported by chokidar-cli, the actual command line argument parsing is dependent on the operating system and shell used; for cross-platform compatibility, use double quotes (with escaping, if necessary), as single quotes are not universally supported by all operating systems.

This is particularly important when using chokidar-cli for run scripts specified in package.json. For maximum platform compatibility, make sure to use escaped double quotes around chokidar's parameters:

"run": {
  "chokidar": "chokidar \"**/*.js\" -c \"...\""
},

Default behavior

By default chokidar streams changes for all patterns to stdout:

$ chokidar "**/*.js" "**/*.less"
change:test/dir/a.js
change:test/dir/a.less
add:test/b.js
unlink:test/b.js

Each change is represented with format event:relativepath. Possible events: add, unlink, addDir, unlinkDir, change.

Output only relative paths on each change

$ chokidar "**/*.js" "**/*.less" | cut -d ":" -f 2-
test/dir/a.js
test/dir/a.less
test/b.js
test/b.js

Run npm run build-js whenever any .js file changes in the current work directory tree

chokidar "**/*.js" -c "npm run build-js"

Watching in network directories must use polling

chokidar "**/*.less" -c "npm run build-less" --polling

Pass the path and event details in to your custom command

chokidar "**/*.less" -c "if [ '{event}' = 'change' ]; then npm run build-less -- {path}; fi;"

Detailed help

Usage: chokidar <pattern> [<pattern>...] [options]

<pattern>:
Glob pattern to specify files to be watched.
Multiple patterns can be watched by separating patterns with spaces.
To prevent shell globbing, write pattern inside quotes.
Guide to globs: https://github.com/isaacs/node-glob#glob-primer


Options:
  -c, --command           Command to run after each change. Needs to be
                          surrounded with quotes when command contains spaces.
                          Instances of `{path}` or `{event}` within the command
                          will be replaced by the corresponding values from the
                          chokidar event.
  -d, --debounce          Debounce timeout in ms for executing command
                                                                  [default: 400]
  -t, --throttle          Throttle timeout in ms for executing command
                                                                  [default: 0]
  -s, --follow-symlinks   When not set, only the symlinks themselves will be
                          watched for changes instead of following the link
                          references and bubbling events through the links path
                                                      [boolean] [default: false]
  -i, --ignore            Pattern for files which should be ignored. Needs to be
                          surrounded with quotes to prevent shell globbing. The
                          whole relative or absolute path is tested, not just
                          filename. Supports glob patterns or regexes using
                          format: /yourmatch/i
  --initial               When set, command is initially run once
                                                      [boolean] [default: false]
  -p, --polling           Whether to use fs.watchFile(backed by polling) instead
                          of fs.watch. This might lead to high CPU utilization.
                          It is typically necessary to set this to true to
                          successfully watch files over a network, and it may be
                          necessary to successfully watch files in other non-
                          standard situations         [boolean] [default: false]
  --poll-interval         Interval of file system polling. Effective when --
                          polling is set                          [default: 100]
  --poll-interval-binary  Interval of file system polling for binary files.
                          Effective when --polling is set         [default: 300]
  --verbose               When set, output is more verbose and human readable.
                                                      [boolean] [default: false]
  --silent                When set, internal messages of chokidar-cli won't be
                          written.                    [boolean] [default: false]
  -h, --help              Show help                                    [boolean]
  -v, --version           Show version number                          [boolean]

Examples:
  chokidar "**/*.js" -c "npm run build-js"  build when any .js file changes
  chokidar "**/*.js" "**/*.less"            output changes of .js and .less
                                            files

License

MIT