index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var fs = require("fs");
  2. var path = require("path");
  3. /**
  4. * @type {string}
  5. */
  6. var PLUGIN_NAME = "Snippet Injector";
  7. /**
  8. * @type
  9. */
  10. var messages = {
  11. added: function (path) {
  12. return "{green:Snippet added to {cyan:" + path
  13. },
  14. removed: function (path) {
  15. return "{green:Snippet removed from {cyan:" + path
  16. },
  17. exists: function (path) {
  18. return "{green:Snippet already exists in: {cyan:" + path
  19. },
  20. notFound: function (path) {
  21. return "{red:ERROR:} Closing body tag not found in: {cyan:" + path
  22. },
  23. fileNotFound: function (path) {
  24. return "{red:ERROR:} File not found!: {cyan:" + path
  25. }
  26. };
  27. /**
  28. * Main export
  29. * @type {{name: string, plugin: plugin}}
  30. */
  31. module.exports = {
  32. "plugin:name": PLUGIN_NAME,
  33. plugin: function (opts, bs) {
  34. opts.file = opts.file || "";
  35. opts.currentFilePath = path.resolve(opts.file);
  36. opts.logger = bs.getLogger(PLUGIN_NAME);
  37. opts.logger.debug("Setting events");
  38. bs.events.on("service:running", addSnippet.bind(null, bs, opts));
  39. bs.events.on("service:exit", removeSnippet.bind(null, bs, opts));
  40. }
  41. };
  42. /**
  43. * Add the snippet before a body tag
  44. * @param {BrowserSync} bs
  45. * @param {Object} opts - plugin specific options
  46. */
  47. function addSnippet(bs, opts) {
  48. var currentFilePath = opts.currentFilePath;
  49. opts.logger.debug("Reading the file: %s", currentFilePath);
  50. var read;
  51. try {
  52. read = fs.readFileSync(currentFilePath, "utf8");
  53. } catch (e) {
  54. opts.errored = true;
  55. return opts.logger.info(messages.fileNotFound(path.basename(currentFilePath)));
  56. }
  57. var found = false;
  58. if (read.indexOf(bs.options.get("snippet")) > -1) {
  59. opts.logger.info(messages.exists(currentFilePath));
  60. return;
  61. }
  62. var modded = read.replace(/<\/body>(?![\s\S]*<\/body>)/, function () {
  63. opts.currentSnippet = wrap(bs.options.get("snippet")) + "\n" + arguments[0];
  64. found = true;
  65. return opts.currentSnippet;
  66. });
  67. if (found) {
  68. opts.logger.debug("Writing the file: %s", currentFilePath);
  69. fs.writeFileSync(currentFilePath, modded);
  70. opts.logger.info(messages.added(path.basename(currentFilePath)));
  71. } else {
  72. opts.logger.info(messages.notFound(path.basename(currentFilePath)));
  73. }
  74. }
  75. /**
  76. * @param item snippet
  77. * @returns {string}
  78. */
  79. function wrap (item) {
  80. return "<!-- BS:SNIPPET-->" + item + "<!-- BS:SNIPPET:END-->";
  81. }
  82. /**
  83. * @param {BrowserSync} bs
  84. * @param {Object} opts - plugin specific options
  85. */
  86. function removeSnippet(bs, opts) {
  87. if (opts.errored) {
  88. return;
  89. }
  90. var read = fs.readFileSync(opts.currentFilePath, "utf8");
  91. var modded = read.replace(opts.currentSnippet, function () {
  92. return "</body>";
  93. });
  94. fs.writeFileSync(opts.currentFilePath, modded);
  95. opts.logger.info(messages.removed(path.basename(opts.currentFilePath)));
  96. }