parse.js 804 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var fs = require('fs');
  3. /**
  4. * Parse the nodemon config file, supporting both old style
  5. * plain text config file, and JSON version of the config
  6. *
  7. * @param {String} filename
  8. * @param {Function} callback
  9. */
  10. function parse(filename, callback) {
  11. var rules = {
  12. ignore: [],
  13. watch: [],
  14. };
  15. fs.readFile(filename, 'utf8', function (err, content) {
  16. if (err) {
  17. return callback(err);
  18. }
  19. var json = null;
  20. try {
  21. json = JSON.parse(content);
  22. } catch (e) {}
  23. if (json !== null) {
  24. rules = {
  25. ignore: json.ignore || [],
  26. watch: json.watch || [],
  27. };
  28. return callback(null, rules);
  29. }
  30. // otherwise return the raw file
  31. return callback(null, { raw: content.split(/\n/) });
  32. });
  33. }
  34. module.exports = parse;