UI.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. var fs = require("fs");
  2. var path = require("path");
  3. var config = require("./config");
  4. var eachSeries = require("async-each-series");
  5. var asyncTasks = require("./async-tasks");
  6. var hooks = require("./hooks");
  7. var merge = require("./opts").merge;
  8. var defaultPlugins = {
  9. "sync-options": require("./plugins/sync-options/sync-options.plugin"),
  10. "overview": require("./plugins/overview/overview.plugin"),
  11. "history": require("./plugins/history/history.plugin"),
  12. "plugins": require("./plugins/plugins/plugins.plugin"),
  13. "remote-debug": require("./plugins/remote-debug/remote-debug.plugin"),
  14. "help": require("./plugins/help/help.plugin"),
  15. "connections": require("./plugins/connections/connections.plugin"),
  16. "network-throttle": require("./plugins/network-throttle/network-throttle.plugin")
  17. };
  18. /**
  19. * @param {Object} opts - Any options specifically
  20. * passed to the control panel
  21. * @param {BrowserSync} bs
  22. * @param {EventEmitter} emitter
  23. * @constructor
  24. * @returns {UI}
  25. */
  26. var UI = function (opts, bs, emitter) {
  27. var ui = this;
  28. ui.bs = bs;
  29. ui.config = config.merge();
  30. ui.events = emitter;
  31. ui.options = merge(opts);
  32. ui.logger = bs.getLogger(ui.config.get("pluginName"));
  33. ui.defaultPlugins = defaultPlugins;
  34. ui.listeners = {};
  35. ui.clients = bs.io.of(bs.options.getIn(["socket", "namespace"]));
  36. ui.socket = bs.io.of(ui.config.getIn(["socket", "namespace"]));
  37. if (ui.options.get("logLevel")) {
  38. ui.logger.setLevel(ui.options.get("logLevel"));
  39. }
  40. /**
  41. *
  42. */
  43. ui.pluginManager = new bs.utils.easyExtender(defaultPlugins, hooks).init();
  44. /**
  45. * Transform/save data RE: plugins
  46. * @type {*}
  47. */
  48. ui.bsPlugins = require("./resolve-plugins")(bs.getUserPlugins());
  49. return ui;
  50. };
  51. /**
  52. * Detect an available port
  53. * @returns {UI}
  54. */
  55. UI.prototype.init = function () {
  56. var ui = this;
  57. eachSeries(
  58. asyncTasks,
  59. taskRunner(ui),
  60. tasksComplete(ui)
  61. );
  62. return this;
  63. };
  64. /**
  65. * @param cb
  66. */
  67. UI.prototype.getServer = function (cb) {
  68. var ui = this;
  69. if (ui.server) {
  70. return ui.server;
  71. }
  72. this.events.on("ui:running", function () {
  73. cb(null, ui.server);
  74. });
  75. };
  76. /**
  77. * @returns {Array}
  78. */
  79. UI.prototype.getInitialTemplates = function () {
  80. var prefix = path.resolve(__dirname, "../templates/directives");
  81. return fs.readdirSync(prefix)
  82. .map(function (name) {
  83. return path.resolve(prefix, name);
  84. });
  85. };
  86. /**
  87. * @param event
  88. */
  89. UI.prototype.delegateEvent = function (event) {
  90. var ui = this;
  91. var listeners = ui.listeners[event.namespace];
  92. if (listeners) {
  93. if (listeners.event) {
  94. listeners.event.call(ui, event);
  95. } else {
  96. if (event.event && listeners[event.event]) {
  97. listeners[event.event].call(ui, event.data);
  98. }
  99. }
  100. }
  101. };
  102. /**
  103. * @param cb
  104. */
  105. UI.prototype.listen = function (ns, events) {
  106. var ui = this;
  107. if (Array.isArray(ns)) {
  108. ns = ns.join(":");
  109. }
  110. if (!ui.listeners[ns]) {
  111. ui.listeners[ns] = events;
  112. }
  113. };
  114. /**
  115. * @param name
  116. * @param value
  117. * @returns {Map|*}
  118. */
  119. UI.prototype.setOption = function (name, value) {
  120. var ui = this;
  121. ui.options = ui.options.set(name, value);
  122. return ui.options;
  123. };
  124. /**
  125. * @param path
  126. * @param value
  127. * @returns {Map|*}
  128. */
  129. UI.prototype.setOptionIn = function (path, value) {
  130. this.options = this.options.setIn(path, value);
  131. return this.options;
  132. };
  133. /**
  134. * @param fn
  135. */
  136. UI.prototype.setMany = function (fn) {
  137. this.options = this.options.withMutations(fn);
  138. return this.options;
  139. };
  140. /**
  141. * @param path
  142. * @returns {any|*}
  143. */
  144. UI.prototype.getOptionIn = function (path) {
  145. return this.options.getIn(path);
  146. };
  147. /**
  148. * Run each setup task in sequence
  149. * @param ui
  150. * @returns {Function}
  151. */
  152. function taskRunner (ui) {
  153. return function (item, cb) {
  154. ui.logger.debug("Starting Step: " + item.step);
  155. /**
  156. * Give each step access to the UI Instance
  157. */
  158. item.fn(ui, function (err, out) {
  159. if (err) {
  160. return cb(err);
  161. }
  162. if (out) {
  163. handleOut(ui, out);
  164. }
  165. ui.logger.debug("{green:Step Complete: " + item.step);
  166. cb();
  167. });
  168. };
  169. }
  170. /**
  171. * Setup tasks may return options or instance properties to be set
  172. * @param {UI} ui
  173. * @param {Object} out
  174. */
  175. function handleOut (ui, out) {
  176. if (out.options) {
  177. Object.keys(out.options).forEach(function (key) {
  178. ui.options = ui.options.set(key, out.options[key]);
  179. });
  180. }
  181. if (out.optionsIn) {
  182. out.optionsIn.forEach(function (item) {
  183. ui.options = ui.options.setIn(item.path, item.value);
  184. });
  185. }
  186. if (out.instance) {
  187. Object.keys(out.instance).forEach(function (key) {
  188. ui[key] = out.instance[key];
  189. });
  190. }
  191. }
  192. /**
  193. * All async tasks complete at this point
  194. * @param ui
  195. */
  196. function tasksComplete (ui) {
  197. return function (err) {
  198. /**
  199. * Log any error according to BrowserSync's Logging level
  200. */
  201. if (err) {
  202. ui.logger.setOnce("useLevelPrefixes", true).error(err.message || err);
  203. }
  204. /**
  205. * Running event
  206. */
  207. ui.events.emit("ui:running", {instance: ui, options: ui.options});
  208. /**
  209. * Finally call the user-provided callback
  210. */
  211. ui.cb(null, ui);
  212. };
  213. }
  214. module.exports = UI;