Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks bs.active to determine if plugin should start browserSync. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 36 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,46 @@ var _ = require('lodash');
var bs = require('browser-sync').create();
var debug = require('debug')('metalsmith-browser-sync');

var PLUGIN_NAME = 'browser-sync';
function browserSyncPlugin(options){
function plugin(files, metalsmith, done) {
// first time through remove ourselves, since we will be continously building
// and we only need one browser-sync server active
metalsmith.plugins.forEach(function(plugin, index){
if(plugin._pluginName === PLUGIN_NAME){
metalsmith.plugins.splice(index, 1);
}
});
//default options
var bsOptions = {
server : "build",
files : ["src/**/*.md", "templates/**/*.hbs"]
};
_.merge(bsOptions, options, function merger(a, b){
//Always use the array given if there is one
if(_.isArray(a)){
return b;
}
});

function rebuild() {
if (!bs.paused) {
bs.pause();

metalsmith.build(function(err) {
var buildMessage = err ? err : 'Build successful';
debug(buildMessage);

bs.resume();
bs.reload();
});
}
};

var watched = bsOptions.files;
delete bsOptions.files;

bs.watch(watched, { ignoreInitial: true }).on('all', rebuild);
bs.init(bsOptions);

// Only execute the plugin if browser-sync isn't active.
if (!bs.active) {
//default options
var bsOptions = {
server : "build",
files : ["src/**/*.md", "templates/**/*.hbs"]
};
_.merge(bsOptions, options, function merger(a, b){
//Always use the array given if there is one
if(_.isArray(a)){
return b;
}
});

function rebuild() {
if (!bs.paused) {
bs.pause();

metalsmith.build(function(err) {
var buildMessage = err ? err : 'Build successful';
debug(buildMessage);

bs.resume();
bs.reload();
});
}
};

var watched = bsOptions.files;
delete bsOptions.files;

bs.watch(watched, { ignoreInitial: true }).on('all', rebuild);
bs.init(bsOptions);
}

// Always continue the plugin chain.
done();
}
plugin._pluginName = PLUGIN_NAME;
return plugin;
}

Expand Down