add.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. var utils = require('../utils');
  3. // internal
  4. var reEscComments = /\\#/g;
  5. // note that '^^' is used in place of escaped comments
  6. var reUnescapeComments = /\^\^/g;
  7. var reComments = /#.*$/;
  8. var reEscapeChars = /[.|\-[\]()\\]/g;
  9. var reAsterisk = /\*/g;
  10. module.exports = add;
  11. /**
  12. * Converts file patterns or regular expressions to nodemon
  13. * compatible RegExp matching rules. Note: the `rules` argument
  14. * object is modified to include the new rule and new RegExp
  15. *
  16. * ### Example:
  17. *
  18. * var rules = { watch: [], ignore: [] };
  19. * add(rules, 'watch', '*.js');
  20. * add(rules, 'ignore', '/public/');
  21. * add(rules, 'watch', ':(\d)*\.js'); // note: string based regexp
  22. * add(rules, 'watch', /\d*\.js/);
  23. *
  24. * @param {Object} rules containing `watch` and `ignore`. Also updated during
  25. * execution
  26. * @param {String} which must be either "watch" or "ignore"
  27. * @param {String|RegExp} the actual rule.
  28. */
  29. function add(rules, which, rule) {
  30. if (!{ ignore: 1, watch: 1}[which]) {
  31. throw new Error('rules/index.js#add requires "ignore" or "watch" as the ' +
  32. 'first argument');
  33. }
  34. if (Array.isArray(rule)) {
  35. rule.forEach(function (rule) {
  36. add(rules, which, rule);
  37. });
  38. return;
  39. }
  40. // support the rule being a RegExp, but reformat it to
  41. // the custom :<regexp> format that we're working with.
  42. if (rule instanceof RegExp) {
  43. // rule = ':' + rule.toString().replace(/^\/(.*?)\/$/g, '$1');
  44. utils.log.error('RegExp format no longer supported, but globs are.');
  45. return;
  46. }
  47. // remove comments and trim lines
  48. // this mess of replace methods is escaping "\#" to allow for emacs temp files
  49. // first up strip comments and remove blank head or tails
  50. rule = (rule || '').replace(reEscComments, '^^')
  51. .replace(reComments, '')
  52. .replace(reUnescapeComments, '#').trim();
  53. var regexp = false;
  54. if (typeof rule === 'string' && rule.substring(0, 1) === ':') {
  55. rule = rule.substring(1);
  56. utils.log.error('RegExp no longer supported: ' + rule);
  57. regexp = true;
  58. } else if (rule.length === 0) {
  59. // blank line (or it was a comment)
  60. return;
  61. }
  62. if (regexp) {
  63. // rules[which].push(rule);
  64. } else {
  65. // rule = rule.replace(reEscapeChars, '\\$&')
  66. // .replace(reAsterisk, '.*');
  67. rules[which].push(rule);
  68. // compile a regexp of all the rules for this ignore or watch
  69. var re = rules[which].map(function (rule) {
  70. return rule.replace(reEscapeChars, '\\$&')
  71. .replace(reAsterisk, '.*');
  72. }).join('|');
  73. // used for the directory matching
  74. rules[which].re = new RegExp(re);
  75. }
  76. }