common.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. const walker = require('walker');
  3. const anymatch = require('anymatch');
  4. const micromatch = require('micromatch');
  5. const path = require('path');
  6. const platform = require('os').platform();
  7. /**
  8. * Constants
  9. */
  10. exports.DEFAULT_DELAY = 100;
  11. exports.CHANGE_EVENT = 'change';
  12. exports.DELETE_EVENT = 'delete';
  13. exports.ADD_EVENT = 'add';
  14. exports.ALL_EVENT = 'all';
  15. /**
  16. * Assigns options to the watcher.
  17. *
  18. * @param {NodeWatcher|PollWatcher|WatchmanWatcher} watcher
  19. * @param {?object} opts
  20. * @return {boolean}
  21. * @public
  22. */
  23. exports.assignOptions = function(watcher, opts) {
  24. opts = opts || {};
  25. watcher.globs = opts.glob || [];
  26. watcher.dot = opts.dot || false;
  27. watcher.ignored = opts.ignored || false;
  28. if (!Array.isArray(watcher.globs)) {
  29. watcher.globs = [watcher.globs];
  30. }
  31. watcher.hasIgnore =
  32. Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
  33. watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
  34. if (opts.watchman && opts.watchmanPath) {
  35. watcher.watchmanPath = opts.watchmanPath;
  36. }
  37. return opts;
  38. };
  39. /**
  40. * Checks a file relative path against the globs array.
  41. *
  42. * @param {array} globs
  43. * @param {string} relativePath
  44. * @return {boolean}
  45. * @public
  46. */
  47. exports.isFileIncluded = function(globs, dot, doIgnore, relativePath) {
  48. if (doIgnore(relativePath)) {
  49. return false;
  50. }
  51. return globs.length
  52. ? micromatch.some(relativePath, globs, { dot: dot })
  53. : dot || micromatch.some(relativePath, '**/*');
  54. };
  55. /**
  56. * Traverse a directory recursively calling `callback` on every directory.
  57. *
  58. * @param {string} dir
  59. * @param {function} dirCallback
  60. * @param {function} fileCallback
  61. * @param {function} endCallback
  62. * @param {*} ignored
  63. * @public
  64. */
  65. exports.recReaddir = function(
  66. dir,
  67. dirCallback,
  68. fileCallback,
  69. endCallback,
  70. errorCallback,
  71. ignored
  72. ) {
  73. walker(dir)
  74. .filterDir(currentDir => !anymatch(ignored, currentDir))
  75. .on('dir', normalizeProxy(dirCallback))
  76. .on('file', normalizeProxy(fileCallback))
  77. .on('error', errorCallback)
  78. .on('end', () => {
  79. if (platform === 'win32') {
  80. setTimeout(endCallback, 1000);
  81. } else {
  82. endCallback();
  83. }
  84. });
  85. };
  86. /**
  87. * Returns a callback that when called will normalize a path and call the
  88. * original callback
  89. *
  90. * @param {function} callback
  91. * @return {function}
  92. * @private
  93. */
  94. function normalizeProxy(callback) {
  95. return (filepath, stats) => callback(path.normalize(filepath), stats);
  96. }