web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.4 KiB

  1. # watch -- Utilities for watching file trees in node.js
  2. ## Install
  3. <pre>
  4. npm install watch
  5. </pre>
  6. ## Purpose
  7. The intention of this module is provide tools that make managing the watching of file & directory trees easier.
  8. #### watch.watchTree(root, [options,] callback)
  9. The first argument is the directory root you want to watch.
  10. The options object is passed to fs.watchFile but can also be used to provide two additional watchTree specific options:
  11. * `'ignoreDotFiles'` - When true this option means that when the file tree is walked it will ignore files that being with "."
  12. * `'filter'` - You can use this option to provide a function that returns true or false for each file and directory to decide whether or not that file/directory is included in the watcher.
  13. * `'interval'` - Specifies the interval duration in seconds, the time period between polling for file changes.
  14. * `'ignoreUnreadableDir'` - When true, this options means that when a file can't be read, this file is silently skipped.
  15. * `'ignoreNotPermitted'` - When true, this options means that when a file can't be read due to permission issues, this file is silently skipped.
  16. * `'ignoreDirectoryPattern'` - When a regex pattern is set, e.g. /node_modules/, these directories are silently skipped.
  17. The callback takes 3 arguments. The first is the file that was modified. The second is the current stat object for that file and the third is the previous stat object.
  18. When a file is new the previous stat object is null.
  19. When watchTree is finished walking the tree and adding all the listeners it passes the file hash (keys are the file/directory names and the values are the current stat objects) as the first argument and null as both the previous and current stat object arguments.
  20. <pre>
  21. watch.watchTree('/home/mikeal', function (f, curr, prev) {
  22. if (typeof f == "object" && prev === null && curr === null) {
  23. // Finished walking the tree
  24. } else if (prev === null) {
  25. // f is a new file
  26. } else if (curr.nlink === 0) {
  27. // f was removed
  28. } else {
  29. // f was changed
  30. }
  31. })
  32. </pre>
  33. ### watch.unwatchTree(root)
  34. Unwatch a previously watched directory root using `watch.watchTree`.
  35. ### watch.createMonitor(root, [options,] callback)
  36. This function creates an EventEmitter that gives notifications for different changes that happen to the file and directory tree under the given root argument.
  37. The options object is passed to watch.watchTree.
  38. The callback receives the monitor object.
  39. The monitor object contains a property, `files`, which is a hash of files and directories as keys with the current stat object as the value.
  40. The monitor has the following events.
  41. * `'created'` - New file has been created. Two arguments, the filename and the stat object.
  42. * `'removed'` - A file has been moved or deleted. Two arguments, the filename and the stat object for the fd.
  43. * `'changed'` - A file has been changed. Three arguments, the filename, the current stat object, and the previous stat object.
  44. The monitor can be stopped using `.stop` (calls `unwatchTree`).
  45. <pre>
  46. var watch = require('watch')
  47. watch.createMonitor('/home/mikeal', function (monitor) {
  48. monitor.files['/home/mikeal/.zshrc'] // Stat object for my zshrc.
  49. monitor.on("created", function (f, stat) {
  50. // Handle new files
  51. })
  52. monitor.on("changed", function (f, curr, prev) {
  53. // Handle file changes
  54. })
  55. monitor.on("removed", function (f, stat) {
  56. // Handle removed files
  57. })
  58. monitor.stop(); // Stop watching
  59. })
  60. </pre>
  61. ### CLI
  62. This module includes a simple command line interface, which you can install with `npm install watch -g`.
  63. ```
  64. Usage: watch <command> [...directory] [OPTIONS]
  65. OPTIONS:
  66. --wait=<seconds>
  67. Duration, in seconds, that watching will be disabled
  68. after running <command>. Setting this option will
  69. throttle calls to <command> for the specified duration.
  70. --filter=<file>
  71. Path to a require-able .js file that exports a filter
  72. function to be passed to watchTreeOptions.filter.
  73. Path is resolved relative to process.cwd().
  74. --interval=<seconds>
  75. Specifies the interval duration in seconds, the time period between polling for file changes.
  76. --ignoreDotFiles, -d
  77. Ignores dot or hidden files in the watch [directory].
  78. --ignoreUnreadable, -u
  79. Silently ignores files that cannot be read within the
  80. watch [directory].
  81. --ignoreDirectoryPattern=<regexp>, -p
  82. Silently skips directories that match the regular
  83. expression.
  84. ```
  85. It will watch the given directories (defaults to the current working directory) with `watchTree` and run the given command every time a file changes.
  86. ### Examples
  87. As stated above the pattern is:
  88. watch <command> [...directory] [OPTIONS]
  89. To run the watch command in the terminal you have to write the following:
  90. watch 'npm run test -s' directory
  91. As the command has to be written in quotation marks Windows users may need to use double quotes rather than single quotes
  92. watch "npm run test -s" directory
  93. Note: Because Windows users may need to use double quotes rather than single quotes they need to escape the them in npm scripts
  94. "scripts": {
  95. "watch:osx": "watch 'npm run test -s' source"
  96. "watch:windows": "watch \"npm run test -s\" source",
  97. }
  98. ## Contributing
  99. ### Releasing
  100. On the latest clean `master`:
  101. ```
  102. npm run release:major
  103. npm run release:minor
  104. npm run release:patch
  105. ```