Socket
Socket
Sign inDemoInstall

browser-sync

Package Overview
Dependencies
Maintainers
1
Versions
300
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browser-sync - npm Package Compare versions

Comparing version 0.1.10 to 0.2.0

32

lib/browser-sync.js
var portScanner = require('portscanner');
var chokidar = require('chokidar');
var gaze = require('gaze');
var _ = require("lodash");

@@ -20,7 +20,13 @@ var fs = require("fs");

var io;
browserSync.prototype = {
options: {
injectFileTypes: ['css', 'png', 'jpg', 'svg', 'gif']
},
/**
* Files patterns
* @param files
* @param options
*/
init: function (files, options) {

@@ -364,16 +370,20 @@

var _this = this;
var _this = this, log = this.log;
var watcher = chokidar.watch(files, {persistent: true});
gaze(files, function(err, watcher) {
watcher.on('change', function (filepath, info) {
if (!info.size) {
return;
var key = Object.keys(this.watched())[0];
if (key) {
log(messages.fileWatching(watcher._patterns), options, true);
} else {
log(messages.fileWatching(false), options, true);
}
callback(filepath, io, options, _this);
});
this.log(messages.fileWatching(Array.isArray(files) ? files.length : 1), options, true);
// On file changed
this.on('changed', function(filepath) {
callback(filepath, io, options, _this);
});
});
},

@@ -380,0 +390,0 @@ getFileExtension: function (path) {

@@ -5,4 +5,3 @@ #! /usr/bin/env node

var argv = require('optimist').argv;
var si = require("./browser-sync");
var glob = require("glob");
var bs = require("./browser-sync");
var fs = require("fs");

@@ -27,3 +26,3 @@ var _ = require("lodash");

var browserSync = new si();
var browserSync = new bs();

@@ -49,2 +48,8 @@ var setup = {

},
/**
* @param defaultConfig
* @param argv
* @returns {*}
* @private
*/
_mergeConfigs: function (defaultConfig, argv) {

@@ -61,2 +66,8 @@

},
/**
* @param defaultConfig
* @param argv
* @returns {*}
* @private
*/
_setDefaultServerConfig: function (defaultConfig, argv) {

@@ -80,2 +91,8 @@

},
/**
* Retrieve the config file
* @param path
* @returns {*}
* @private
*/
_getConfigFile: function (path) {

@@ -92,3 +109,3 @@ if (fs.existsSync(path)) {

* @param {object} config
* @returns {argv.files|*|.watch.negate.files|exports.files|.echo.files|.watch.files}
* @returns {argv.files}
*/

@@ -98,39 +115,29 @@ getFilesArg: function (argv, config) {

},
getFiles: function (files, cb) {
/**
* Resolve the file patterns
* @param filesArg
* @returns {*}
*/
getFiles: function (filesArg) {
var isString;
var returnFiles = [];
if (typeof filesArg === "string") {
if (typeof files === "string") {
isString = true;
if(files.indexOf(",") !== -1) {
files = files.split(",");
isString = false;
if(filesArg.indexOf(",") !== -1) {
return filesArg.split(",");
} else {
return (filesArg.length > 0) ? filesArg : false;
}
}
if (Array.isArray(filesArg)) {
return filesArg
}
if (Array.isArray(files)) { // if an array given
files.forEach(function (file, i) {
glob(file, function (er, foundFiles) {
returnFiles.push(foundFiles);
if (i === files.length - 1) {
cb(_.flatten(returnFiles));
}
});
});
} else {
if (isString) {
glob(files, function (er, foundFiles) {
cb(foundFiles);
});
} else {
cb([]);
}
}
return false;
},
/**
* Kickoff browser-sync
* @param files
* @param config
*/
kickoff: function (files, config) {

@@ -141,12 +148,14 @@ browserSync.init(files || [], config);

/**
* Was it run from the command line?
*/
if (require.main === module) {
var config = setup.getConfig(defaultConfig, argv);
var filesArg = setup.getFilesArg(argv, config);
var config = setup.getConfig(defaultConfig, argv),
filesArg = setup.getFilesArg(argv, config),
files = setup.getFiles(filesArg);
setup.getFiles(filesArg, function (files) {
setup.kickoff(files, config);
});
setup.kickoff(files, config);
}
module.exports.setup = setup;

@@ -5,59 +5,68 @@ var clc = require("cli-color");

module.exports = {
connection: function (browser) {
return clc.cyan("Browser Connected! (" + browser.name + ", version: " + browser.version + ")");
},
init: function (hostIp, socketIoPort, scriptPort) {
return clc.yellow('\n\nAll Set Up! Now copy & paste this snippet just before the closing </body> tag in your website.\n\n') +
connection: function (browser) {
return clc.cyan("Browser Connected! (" + browser.name + ", version: " + browser.version + ")");
},
init: function (hostIp, socketIoPort, scriptPort) {
return clc.yellow('\n\nAll Set Up! Now copy & paste this snippet just before the closing </body> tag in your website.\n\n') +
this.scriptTags(hostIp, socketIoPort, scriptPort, false);
},
initServer: function (hostIp, scriptPort, baseDir) {
},
initServer: function (hostIp, scriptPort, baseDir) {
return clc.green("\nOK, Server running at ") + clc.magenta("http://" + hostIp + ":" + scriptPort +"\n\n") +
clc.green("Serving files from: ") + clc.magenta(baseDir) + "\n\n" +
clc.green("Go load a browser & check back here. If you set up everthing correctly, you'll see a " +
"'Browser Connected' message.\n");
},
scriptTags : function (hostIp, socketIoPort, scriptPort, colors) {
var tags = "<script src='http://" + hostIp + ":" + socketIoPort + this.socketIoScript + "'></script>\n" +
"<script src='http://" + hostIp + ":" + scriptPort + this.clientScript + "'></script>\n\n";
return clc.green("\nOK, Server running at ") + clc.magenta("http://" + hostIp + ":" + scriptPort + "\n\n") +
clc.green("Serving files from: ") + clc.magenta(baseDir) + "\n\n" +
clc.green("Go load a browser & check back here. If you set up everthing correctly, you'll see a " +
"'Browser Connected' message.\n");
},
scriptTags: function (hostIp, socketIoPort, scriptPort, colors) {
var tags = "<script src='http://" + hostIp + ":" + socketIoPort + this.socketIoScript + "'></script>\n" +
"<script src='http://" + hostIp + ":" + scriptPort + this.clientScript + "'></script>\n\n";
if (colors) {
return clc.magenta(tags);
}
if (colors) {
return clc.magenta(tags);
}
return tags;
return tags;
},
invalidBaseDir: function () {
return clc.cyan("Invalid Base Directory path for server. ( baseDir: )\n\n") +
clc.green("TIP: Don't use a forward slash at the beginning, and if you " +
"want to serve files from the root folder, just set the baseDir option to './' ");
},
fileWatching: function (patterns) {
var string;
if (Array.isArray(patterns) && patterns.length) {
string = clc.cyan("Watching the following:\n");
patterns.forEach(function (item) {
string += item.replace(" ", "") + "\n";
});
} else {
string = "Not watching anything...";
}
return string;
},
fileChanged: function (path) {
return clc.magenta("File Changed: " + clc.green(path));
},
browser: {
reload: function () {
return clc.yellow("Reloading all connected browsers...");
},
invalidBaseDir: function () {
return clc.cyan("Invalid Base Directory path for server. ( baseDir: )\n\n") +
clc.green("TIP: Don't use a forward slash at the beginning, and if you " +
"want to serve files from the root folder, just set the baseDir option to './' ");
},
fileWatching: function (numFiles) {
if (numFiles === 1) {
return "Watching 1 file";
}
if (numFiles > 1) {
return "Watching "+ numFiles + " files";
}
return "Not watching any files...";
},
fileChanged: function (path) {
return clc.magenta("File Changed: " + clc.green(path));
},
browser: {
reload: function () {
return clc.yellow("Reloading all connected browsers...");
},
inject: function () {
return clc.yellow("Injecting file into all connected browsers...");
}
},
location: function (url) {
return clc.yellow("Link clicked! Redirecting all browser to " + clc.green(url));
},
socketConnector: function (host, port) {
return "var ___socket___ = io.connect('" + host + ":" + port + "');";
},
clientScript: "/browser-sync-client.min.js",
socketIoScript: "/socket.io/socket.io.js"
inject: function () {
return clc.yellow("Injecting file into all connected browsers...");
}
},
location: function (url) {
return clc.yellow("Link clicked! Redirecting all browser to " + clc.green(url));
},
socketConnector: function (host, port) {
return "var ___socket___ = io.connect('" + host + ":" + port + "');";
},
clientScript: "/browser-sync-client.min.js",
socketIoScript: "/socket.io/socket.io.js"
};
{
"name": "browser-sync",
"description": "Live CSS Reload & Browser Syncing",
"version": "0.1.10",
"version": "0.2.0",
"homepage": "https://github.com/shakyshane/browser-sync",

@@ -31,14 +31,14 @@ "author": {

"portscanner": "~0.1.3",
"path": "*",
"url": "*",
"open": "*",
"chokidar": "~0.6.2",
"path": "~0.4.9",
"url": "~0.7.9",
"open": "~0.0.4",
"lodash": "~1.3.1",
"cli-color": "~0.2.2",
"socket.io": "~0.9.16",
"connect": "~2.8.4",
"connect": "~2.11.0",
"ua-parser-js": "~0.6.2",
"optimist": "0.6.0",
"glob": "~3.2.6",
"dev-ip": "~0.1.5"
"glob": "~3.2.7",
"dev-ip": "~0.1.5",
"gaze": "~0.4.3"
},

@@ -45,0 +45,0 @@ "devDependencies": {

@@ -6,11 +6,2 @@ 'use strict';

var file1 = "test/fixtures/index.html";
var file2 = "test/fixtures/forms.html";
var file3 = "test/fixtures/scrolling.html";
var css = "test/fixtures/assets/style.css";
var scss = "test/fixtures/scss/main.scss";
var timeoutMsg = "Took too long to access DISK for files";
describe("Browser-sync: transform the files option into useable watchers", function () {

@@ -22,21 +13,18 @@

describe("accepting a comma separated lists of files", function () {
describe("accepting a comma separated lists of patterns (files)", function () {
var files;
var cb;
beforeEach(function(){
cb = jasmine.createSpy();
beforeEach(function () {
var arg = "test/fixtures/assets/style.css,test/fixtures/scss/main.scss";
files = setup.getFiles(arg);
});
it("should return an array of files", function () {
files = setup.getFiles("test/fixtures/assets/style.css,test/fixtures/scss/main.scss", cb);
waits(100);
runs(function () {
expect(cb).toHaveBeenCalledWith([css, scss]);
});
it("should return an array of patterns", function () {
expect(files.length).toBe(2);
});
it("should return an array of patterns (2)", function () {
expect(files[0]).toBe("test/fixtures/assets/style.css");
});
it("should return an array of patterns (3)", function () {
expect(files[1]).toBe("test/fixtures/scss/main.scss");
});
});

@@ -46,139 +34,63 @@ describe("accepting a comma separated lists of globs", function () {

var files;
var cb;
beforeEach(function(){
cb = jasmine.createSpy();
var arg = "test/fixtures/assets/*.css,test/fixtures/scss/*.scss";
files = setup.getFiles(arg);
});
it("should return an array of files", function () {
files = setup.getFiles("test/fixtures/assets/*.css,test/fixtures/scss/*.scss", cb);
waitsFor(function () {
return cb.callCount > 0;
}, timeoutMsg, 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith([css, scss]);
});
it("should return an array of file globs", function () {
expect(files.length).toBe(2);
});
it("should return an array of file globs (2)", function () {
expect(files[0]).toBe("test/fixtures/assets/*.css");
});
it("should return an array of file globs (3)", function () {
expect(files[1]).toBe("test/fixtures/scss/*.scss");
});
});
describe("When getting single files with a string", function () {
describe("accepting a single pattern", function () {
var files;
var cb;
var files, arg
beforeEach(function(){
cb = jasmine.createSpy();
arg = "test/fixtures/assets/*.css";
files = setup.getFiles(arg);
});
it("should return an array of files even if only 1 file", function () {
files = setup.getFiles(file1, cb);
waitsFor(function () {
return cb.callCount > 0;
}, timeoutMsg, 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith([file1]);
});
it("should return the pattern", function () {
expect(files).toBe(arg);
});
});
describe("accepting an array of patterns", function () {
it("should return an array of files if an array given", function () {
files = setup.getFiles([file1, file2], cb);
waitsFor(function () {
return cb.callCount > 0;
}, timeoutMsg, 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith([file1, file2]);
});
var files, arg
beforeEach(function(){
arg = ["**/*.css", "*.html"];
files = setup.getFiles(arg);
});
it("should return the pattern", function () {
expect(files).toBe(arg);
});
});
describe("accepting an array of patterns (2)", function () {
describe("when getting multiple files given as strings", function () {
describe("When the files DO exist", function () {
var files = [file1, file2];
var cb;
beforeEach(function(){
cb = jasmine.createSpy("callback1");
});
it("should return an array of the files", function () {
files = setup.getFiles(files, cb);
waitsFor(function () {
return cb.callCount > 0;
}, timeoutMsg, 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith([file1, file2]);
});
});
var files, arg
beforeEach(function(){
arg = ["**/*.css"];
files = setup.getFiles(arg);
});
describe("When the files DO NOT exist", function () {
//
var files = ["test/fixtures/index.html", "test/fixtures/kittie.html"];
var cb;
beforeEach(function(){
cb = jasmine.createSpy("callback1");
});
it("should return an array of the files", function () {
files = setup.getFiles(files, cb);
waitsFor(function () {
return cb.callCount > 0;
}, "Took too long", 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith(["test/fixtures/index.html"]);
});
});
it("should return the pattern", function () {
expect(files).toBe(arg);
});
});
describe("returning false if empty string given", function () {
describe("Getting files from a glob", function () {
var cb;
var files;
var files, arg;
beforeEach(function(){
cb = jasmine.createSpy();
arg = "";
files = setup.getFiles(arg);
});
it("should return files from a single glob string", function () {
files = setup.getFiles("test/fixtures/*.html", cb);
waitsFor(function () {
return cb.callCount > 0;
}, "Took too long to get files", 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith([file2, file1, file3]);
});
it("should return false", function () {
expect(files).toBe(false);
});
it("should return files from an array of globs", function () {
files = setup.getFiles([
"test/fixtures/*.html",
"test/fixtures/assets/*.css",
"test/fixtures/scss/*.scss"], cb);
waitsFor(function () {
return cb.callCount > 0;
}, "Took too long to get files!", 10000);
runs(function () {
expect(cb).toHaveBeenCalledWith([file2, file1, file3, css, scss]);
});
});
});
});

@@ -27,20 +27,2 @@ var fs = require("fs");

it("should log a single file being watched", function () {
methods.watchFiles(testFile1, io, methods.changeFile, {});
expect(methods.log).toHaveBeenCalledWith("Watching 1 file", {}, true);
});
it("should log multiple files being watched", function () {
methods.watchFiles([testFile1, testFile2], io, methods.changeFile, {});
expect(methods.log).toHaveBeenCalledWith("Watching 2 files", {}, true);
});
it("should log when no files are watched", function () {
methods.watchFiles([], io, methods.changeFile, {});
expect(methods.log).toHaveBeenCalledWith("Not watching any files...", {}, true);
});
it("should call changeFile when a watched file is changed", function () {

@@ -47,0 +29,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc