Skip to content

Commit

Permalink
fix(options): Ignore cli options from public api usage fix #314
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 2, 2014
1 parent dfae15a commit 1de4a3b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"indent": 4,
"eqnull": true,
"node": true,
"strict": true,
"strict": false,
"mocha": true
}
2 changes: 1 addition & 1 deletion lib/cli/cli-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports.startFromCommandLine = function (args, cb) {
userConfig = info._getConfigFile(args.config);
}

var options = merge(defaultConfig, userConfig || {}, false, cliOptions.callbacks);
var options = merge(defaultConfig, userConfig || {}, cliOptions.callbacks);

cb(null, {
files: options.files || [],
Expand Down
66 changes: 34 additions & 32 deletions lib/cli/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ module.exports.callbacks = {
/**
* Merge server options
* @param defaultValue
* @param merged
* @param newValue
* @param args
* @returns {*}
*/
server: function (defaultValue, newValue, args) {
server: function (defaultValue, merged, newValue, args) {

// Return if object or array given
if (typeof newValue === "undefined" || newValue === false) {
if (typeof merged === "undefined" || merged === false) {
return defaultValue;
}

Expand Down Expand Up @@ -171,46 +172,47 @@ module.exports.callbacks = {
* @param defaultValue
* @returns {*}
*/
ghostMode: function (defaultValue, newValue) {

var def = _.cloneDeep(defaultValue);

if (def === false) {
def = {
clicks: false,
scroll: false,
location: false,
forms: {
submit: false,
inputs: false,
toggles: false
}
};
return def;
ghostMode: function (defaultValue, merged, newValue, args) {

var def = _.cloneDeep(merged);

var trueAll = {
clicks: true,
scroll: true,
location: false,
forms: {
submit: true,
inputs: true,
toggles: true
}
};
var falseAll = {
clicks: false,
scroll: false,
location: false,
forms: {
submit: false,
inputs: false,
toggles: false
}
};

if (merged === false || merged === "false" || args && args.ghost === false) {
return falseAll;
}

if (def === true) {
def = {
clicks: true,
scroll: true,
location: true,
forms: {
submit: true,
inputs: true,
toggles: true
}
};
return def;
if (merged === true || merged === "true" || args && args.ghost === true) {
return trueAll;
}

if (def.forms === false || newValue === false || newValue === "false") {
if (merged.forms === false || newValue === false || newValue === "false") {
def.forms = {
submit: false,
inputs: false,
toggles: false
};
}
if (def.forms === true || newValue === true || newValue === "true") {
if (merged.forms === true || newValue === true || newValue === "true") {
def.forms = {
submit: true,
inputs: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/public/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var defaultConfig = require("../default-config");
var cliOptions = require("../cli/cli-options");

var merge = require("opt-merger").merge;
var merger = require("opt-merger");

/**
* @param {BrowserSync} browserSync
Expand All @@ -17,7 +17,7 @@ module.exports = function (browserSync, pjson) {
var args = Array.prototype.slice.call(arguments);
args = require("../args")(args);

var config = merge(defaultConfig, args.config || {}, false, cliOptions.callbacks);
var config = merger.set({ignoreCli: true}).merge(defaultConfig, args.config || {}, cliOptions.callbacks);

if (!config.server && !config.proxy) {
config.open = false;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"minimist": "^1.1.0",
"object-path": "^0.6.0",
"opn": "^1.0.0",
"opt-merger": "0.2.1",
"opt-merger": "^1.1.0",
"portscanner-plus": "0.1.0",
"resp-modifier": "~0.1.2",
"serve-index": "^1.1.5",
Expand Down
12 changes: 10 additions & 2 deletions test/specs/cli/cli.options.ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ var callbacks = options.callbacks;
var assert = require("chai").assert;

describe("CLI: Options: Merging Ghostmode options", function () {
it("should merge ghost mode set to false from cli", function () {
var arg = false;
var defaultVal = defaultConfig.ghostMode;
var actual = callbacks.ghostMode(defaultVal, arg, arg, {ghost: false});
assert.equal(actual.forms.submit, false);
assert.equal(actual.forms.inputs, false);
assert.equal(actual.forms.toggles, false);
});
it("should merge ghost mode set to false", function () {
var arg = false;
var defaultVal = defaultConfig.ghostMode;
var actual = callbacks.ghostMode(defaultVal, arg);
var actual = callbacks.ghostMode(defaultVal, arg, arg);
assert.equal(actual.forms.submit, false);
assert.equal(actual.forms.inputs, false);
assert.equal(actual.forms.toggles, false);
});
it("should merge ghost mode set to false as string", function () {
var arg = "false";
var defaultVal = defaultConfig.ghostMode;
var actual = callbacks.ghostMode(defaultVal, arg);
var actual = callbacks.ghostMode(defaultVal, arg, arg);
assert.equal(actual.forms.submit, false);
assert.equal(actual.forms.inputs, false);
assert.equal(actual.forms.toggles, false);
Expand Down
18 changes: 9 additions & 9 deletions test/specs/cli/cli.options.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ describe("CLI: Options: Merging Server Options", function () {
defaultValue = false;
});
it("should merge default + command line options", function () {
var arg = true;
var actual = options.callbacks.server(defaultValue, arg);
var value = true;
var actual = options.callbacks.server(defaultValue, value, value, undefined);
var expected = {
baseDir: "./"
};
assert.deepEqual(actual, expected);
});
it("should set the base dir if given", function () {
var arg = "app";
var actual = options.callbacks.server(defaultValue, arg);
var value = "app";
var actual = options.callbacks.server(defaultValue, value, value, undefined);
var expected = {
baseDir: "app"
};
Expand All @@ -31,7 +31,7 @@ describe("CLI: Options: Merging Server Options", function () {
var argv = {
index: "index.htm"
};
var actual = options.callbacks.server(defaultValue, arg, argv);
var actual = options.callbacks.server(defaultValue, arg, arg, argv);
var expected = {
baseDir: "app/dist",
index: "index.htm"
Expand All @@ -43,7 +43,7 @@ describe("CLI: Options: Merging Server Options", function () {
var argv = {
index: "index.htm"
};
var actual = options.callbacks.server(defaultValue, arg, argv);
var actual = options.callbacks.server(defaultValue, arg, arg, argv);
var expected = {
baseDir: "./",
index: "index.htm"
Expand All @@ -54,7 +54,7 @@ describe("CLI: Options: Merging Server Options", function () {
var arg = {
baseDir: "./app"
};
var actual = options.callbacks.server(defaultValue, arg, {});
var actual = options.callbacks.server(defaultValue, arg, arg, undefined);
var expected = {
baseDir: "./app"
};
Expand All @@ -65,7 +65,7 @@ describe("CLI: Options: Merging Server Options", function () {
baseDir: "./app",
index: "mypage.html"
};
var actual = options.callbacks.server(defaultValue, arg, {});
var actual = options.callbacks.server(defaultValue, arg, arg, undefined);
assert.deepEqual(actual, arg);
});
it("should set the directory if given on the command line", function () {
Expand All @@ -77,7 +77,7 @@ describe("CLI: Options: Merging Server Options", function () {
baseDir: "app",
directory: true
};
var actual = options.callbacks.server(defaultValue, arg, argv);
var actual = options.callbacks.server(defaultValue, arg, arg, argv);
assert.deepEqual(actual, expected);
});
});
2 changes: 1 addition & 1 deletion test/specs/e2e/e2e.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe("E2E GHOST OPTIONS (WITH GHOSTMODE)", function () {
it("Sets the ghostMode options in shorthand", function () {
var ghostMode = instance.options.ghostMode;
assert.deepEqual(ghostMode.clicks, true);
assert.deepEqual(ghostMode.location, true);
assert.deepEqual(ghostMode.location, false); // never turn this on, it's buggy as hell
assert.deepEqual(ghostMode.scroll, true);
assert.deepEqual(ghostMode.forms.submit, true);
assert.deepEqual(ghostMode.forms.inputs, true);
Expand Down

0 comments on commit 1de4a3b

Please sign in to comment.