Skip to content

Commit

Permalink
Use latest browser-sync, rename to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jan 25, 2015
1 parent 5b79a45 commit 2298ff7
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 163 deletions.
134 changes: 5 additions & 129 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,9 @@
"use strict";

var async = require("./lib/async");
var hooks = require("./lib/hooks");
var UI = require("./lib/UI");
var config = require("./lib/config");
var Immutable = require("immutable");
var merge = require("./lib/opts").merge;
var Events = require("events").EventEmitter;

var defaultPlugins = {
"sync-options": require("./lib/plugins/sync-options/sync-options"),
"overview": require("./lib/plugins/overview/overview"),
"history": require("./lib/plugins/history/history"),
//"plugins": require("./lib/plugins/plugins/plugins"),
"remote-debug": require("./lib/plugins/remote-debug/remote-debug"),
"help": require("./lib/plugins/help/help"),
"connections": require("./lib/plugins/connections/connections")
};

/**
* @param {Object} opts - Any options specifically
* passed to the control panel
* @param {BrowserSync} bs
* @param {EventEmitter} emitter
* @constructor
* @returns {ControlPanel}
*/
var ControlPanel = function (opts, bs, emitter) {

var cp = this;
cp.bs = bs;
cp.config = config.merge();
cp.events = emitter;
cp.opts = merge(opts);
cp.logger = bs.getLogger(cp.config.get("pluginName"));
cp.defaultPlugins = defaultPlugins;
cp.clients = bs.io.of(bs.options.getIn(["socket", "namespace"]));
cp.socket = bs.io.of(cp.config.getIn(["socket", "namespace"]));
cp.options = Immutable.Map();

if (cp.opts.get("logLevel")) {
cp.logger.setLevel(cp.opts.get("logLevel"));
}

cp.pluginManager = new bs.utils.easyExtender(defaultPlugins, hooks).init();

return cp;
};

/**
* @param cb
*/
ControlPanel.prototype.getServer = function (cb) {
var cp = this;
if (cp.server) {
return cp.server;
}
this.events.on("cp:running", function () {
cb(null, cp.server);
});
};

/**
* Detect an available port
* @returns {ControlPanel}
*/
ControlPanel.prototype.init = function () {

var cp = this;
var asyncTasks = [
{
step: "Setting default plugins",
fn: async.initDefaultHooks
},
{
step: "Finding a free port",
fn: async.findAFreePort
},
{
step: "Starting the Control Panel Server",
fn: async.startServer
},
{
step: "Registering default plugins",
fn: async.registerPlugins
},
{
step: "Add options setting event",
fn: async.addOptionsEvent
},
{
step: "Add element events",
fn: async.addElementEvents
}
];

require("async").eachSeries(asyncTasks, function (item, cb) {
cp.logger.debug("Starting Step: " + item.step);
item.fn(cp, function (err, out) {
if (err) {
return cb(err);
}
if (out) {
if (out.options) {
Object.keys(out.options).forEach(function (key) {
cp.opts = cp.opts.set(key, out.options[key]);
});
}
if (out.optionsIn) {
out.optionsIn.forEach(function (item) {
cp.opts = cp.opts.setIn(item.path, item.value);
});
}
if (out.instance) {
Object.keys(out.instance).forEach(function (key) {
cp[key] = out.instance[key];
});
}
}
cp.logger.debug("{green:Step Complete: " + item.step);
cb();
});
}, function () {
cp.events.emit("cp:running", {instance: cp, options: cp.opts});
cp.logger.info("Running at: {magenta:http://localhost:%s", cp.opts.get("port"));
});

return this;
};

/**
* These hooks are for attaching functionality to BrowserSync
*/
Expand All @@ -142,12 +18,12 @@ module.exports.hooks = {
* BrowserSync Plugin interface
* @param opts
* @param bs
* @returns {ControlPanel}
* @returns {UI}
*/
module.exports["plugin"] = function (opts, bs) {
var controlPanel = new ControlPanel(opts, bs, new Events());
controlPanel.init();
return controlPanel;
var ui = new UI(opts, bs, new Events());
ui.init();
return ui;
};

module.exports["plugin:name"] = config.defaults.pluginName;
121 changes: 121 additions & 0 deletions lib/UI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
var config = require("./config");
var Immutable = require("immutable");
var asyncTasks = require("./async-tasks");
var hooks = require("./hooks");
var merge = require("./opts").merge;

var defaultPlugins = {
"sync-options": require("./plugins/sync-options/sync-options"),
"overview": require("./plugins/overview/overview"),
"history": require("./plugins/history/history"),
//"plugins": require("./plugins/plugins/plugins"),
"remote-debug": require("./plugins/remote-debug/remote-debug"),
"help": require("./plugins/help/help"),
"connections": require("./plugins/connections/connections")
};

/**
* @param {Object} opts - Any options specifically
* passed to the control panel
* @param {BrowserSync} bs
* @param {EventEmitter} emitter
* @constructor
* @returns {UI}
*/
var UI = function (opts, bs, emitter) {

var ui = this;
ui.bs = bs;
ui.config = config.merge();
ui.events = emitter;
ui.opts = merge(opts);
ui.logger = bs.getLogger(ui.config.get("pluginName"));
ui.defaultPlugins = defaultPlugins;
ui.clients = bs.io.of(bs.options.getIn(["socket", "namespace"]));
ui.socket = bs.io.of(ui.config.getIn(["socket", "namespace"]));
ui.options = Immutable.Map();

if (ui.opts.get("logLevel")) {
ui.logger.setLevel(ui.opts.get("logLevel"));
}

ui.pluginManager = new bs.utils.easyExtender(defaultPlugins, hooks).init();

return ui;
};

/**
* Detect an available port
* @returns {UI}
*/
UI.prototype.init = function () {

var ui = this;

ui.bs.utils.async.eachSeries(
asyncTasks,
taskRunner(ui),
tasksComplete(ui)
);

return this;
};

/**
* @param cb
*/
UI.prototype.getServer = function (cb) {
var ui = this;
if (ui.server) {
return ui.server;
}
this.events.on("cp:running", function () {
cb(null, ui.server);
});
};

/**
* Run each setup task in sequence
* @param ui
* @returns {Function}
*/
function taskRunner (ui) {
return function (item, cb) {
ui.logger.debug("Starting Step: " + item.step);
item.fn(ui, function (err, out) {
if (err) {
return cb(err);
}
if (out) {
if (out.options) {
Object.keys(out.options).forEach(function (key) {
ui.opts = ui.opts.set(key, out.options[key]);
});
}
if (out.optionsIn) {
out.optionsIn.forEach(function (item) {
ui.opts = ui.opts.setIn(item.path, item.value);
});
}
if (out.instance) {
Object.keys(out.instance).forEach(function (key) {
ui[key] = out.instance[key];
});
}
}
ui.logger.debug("{green:Step Complete: " + item.step);
cb();
});
}
}

/**
* All async tasks complete at this point
* @param ui
*/
function tasksComplete (ui) {
ui.events.emit("cp:running", {instance: ui, options: ui.opts});
ui.logger.info("Running at: {magenta:http://localhost:%s", ui.opts.get("port"));
}

module.exports = UI;
28 changes: 28 additions & 0 deletions lib/async-tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var async = require("./async");

module.exports = [
{
step: "Setting default plugins",
fn: async.initDefaultHooks
},
{
step: "Finding a free port",
fn: async.findAFreePort
},
{
step: "Starting the Control Panel Server",
fn: async.startServer
},
{
step: "Registering default plugins",
fn: async.registerPlugins
},
{
step: "Add options setting event",
fn: async.addOptionsEvent
},
{
step: "Add element events",
fn: async.addElementEvents
}
];
Loading

0 comments on commit 2298ff7

Please sign in to comment.