Skip to content

Commit eb4ebab

Browse files
committed
feat(config): Allow browser-sync property in package.json
feat(config): Allow flags to override config file/package.json when given on cli
1 parent 728c777 commit eb4ebab

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

lib/cli/command.start.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use strict";
22

3-
var info = require("./cli-info");
3+
var path = require("path");
4+
var fs = require("fs");
5+
var _ = require("lodash");
6+
var utils = require("../utils");
47

58
/**
69
* $ browser-sync start <options>
@@ -13,11 +16,31 @@ var info = require("./cli-info");
1316
*/
1417
module.exports = function (opts) {
1518

16-
var flags = stripUndefined(opts.cli.flags);
19+
var flags = stripUndefined(opts.cli.flags);
20+
var maybepkg = path.resolve(process.cwd(), "package.json");
21+
var input = flags;
22+
23+
if (flags.config) {
24+
var maybeconf = path.resolve(process.cwd(), flags.config);
25+
if (fs.existsSync(maybeconf)) {
26+
var conf = require(maybeconf);
27+
input = _.merge({}, conf, flags);
28+
} else {
29+
utils.fail(true, new Error("Configuration file '" + flags.config + "' not found"), opts.cb);
30+
}
31+
} else {
32+
if (fs.existsSync(maybepkg)) {
33+
var pkg = require(maybepkg);
34+
if (pkg["browser-sync"]) {
35+
console.log("> Configuration obtained from package.json");
36+
input = _.merge({}, pkg["browser-sync"], flags);
37+
}
38+
}
39+
}
1740

1841
return require("../../")
1942
.create("cli")
20-
.init(flags, opts.cb);
43+
.init(input, opts.cb);
2144
};
2245

2346
/**

lib/cli/opts.start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
},
6969
"config": {
7070
"type": "string",
71+
"alias": "c",
7172
"desc": "Specify a path to a configuration file"
7273
},
7374
"host": {

test/fixtures/config/si-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
files: ["test/fixtures/**/*.css", "test/fixtures/**/*.html"],
33
testConfig: true,
44
logLevel: "debug",
5+
open: false,
56
server: {
67
baseDir: "test/fixtures"
78
}

test/specs/e2e/cli/e2e.cli.conf.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
3+
var path = require("path");
4+
var browserSync = require(path.resolve("./"));
5+
var pkg = require(path.resolve("package.json"));
6+
var assert = require("chai").assert;
7+
var sinon = require("sinon");
8+
var fs = require("fs");
9+
var cli = require(path.resolve(pkg.bin));
10+
11+
// describe("CLI: merging package.json property with cli args", function () {
12+
// it("accepts key `browser-sync` from package.json", function (done) {
13+
// browserSync.reset();
14+
// cli({
15+
// cli: {
16+
// input: ["start"],
17+
// flags: {
18+
// logLevel: "silent",
19+
// open: false
20+
// }
21+
// },
22+
// cb: function (err, bs) {
23+
// assert.equal(bs.options.getIn(["server", "baseDir"]), "test/fixtures");
24+
// bs.cleanup();
25+
// done();
26+
// }
27+
// });
28+
// });
29+
// });

0 commit comments

Comments
 (0)