file-utils.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var _ = require("./lodash.custom");
  3. var fileUtils = {
  4. /**
  5. * React to file-change events that occur on "core" namespace only
  6. * @param bs
  7. * @param data
  8. */
  9. changedFile: function (bs, data) {
  10. /**
  11. * If the event property is undefined, infer that it's a 'change'
  12. * event due the fact this handler is for emitter.emit("file:changed")
  13. */
  14. if (_.isUndefined(data.event)) {
  15. data.event = "change";
  16. }
  17. /**
  18. * Chokidar always sends an 'event' property - which could be
  19. * `add` `unlink` etc etc so we need to check for that and only
  20. * respond to 'change', for now.
  21. */
  22. if (bs.options.get("watchEvents").indexOf(data.event) > -1) {
  23. if (!bs.paused && data.namespace === "core") {
  24. bs.events.emit("file:reload", fileUtils.getFileInfo(data, bs.options));
  25. }
  26. }
  27. },
  28. /**
  29. * @param data
  30. * @param options
  31. * @returns {{assetFileName: *, fileExtension: String}}
  32. */
  33. getFileInfo: function (data, options) {
  34. data.ext = require("path")
  35. .extname(data.path)
  36. .slice(1);
  37. data.basename = require("path").basename(data.path);
  38. var obj = {
  39. ext: data.ext,
  40. path: data.path,
  41. basename: data.basename,
  42. event: data.event,
  43. type: "inject"
  44. };
  45. // RELOAD page
  46. if (!_.includes(options.get("injectFileTypes").toJS(), obj.ext)) {
  47. obj.url = obj.path;
  48. obj.type = "reload";
  49. }
  50. obj.path = data.path;
  51. obj.log = data.log;
  52. return obj;
  53. }
  54. };
  55. module.exports = fileUtils;
  56. //# sourceMappingURL=file-utils.js.map