Skip to content

Commit 4c58541

Browse files
bitjsonshakyShane
authored andcommitted
feat(options): Allow any serve-static specific configuration under new property - closes #539
1 parent bc5ed4a commit 4c58541

File tree

7 files changed

+115
-18
lines changed

7 files changed

+115
-18
lines changed

lib/cli/cli-options.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ opts.callbacks = {
254254
return Immutable.List([value]);
255255
}
256256
});
257+
},
258+
/**
259+
* @param value
260+
*/
261+
extensions: function (value) {
262+
if (_.isString(value)) {
263+
var split = opts.utils.explodeFilesArg(value);
264+
if (split.length) {
265+
return Immutable.List(split);
266+
}
267+
}
257268
}
258269
};
259270

lib/cli/opts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"exclude": "File patterns to ignore",
66
"server": "Run a Local server (uses your cwd as the web root)",
77
"index": "Specify which file should be used as the index page",
8+
"extensions": "Specify file extension fallbacks",
89
"startPath": "Specify the start path for the opened browser",
910
"https": "Enable SSL for local development",
1011
"directory": "Show a directory listing for the server",

lib/options.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,27 @@ function setNamespace(item) {
103103
* @param item
104104
*/
105105
function setServerOpts(item) {
106+
106107
if (item.get("server")) {
108+
109+
var indexarg = item.get("index") || item.getIn(["server", "index"]) || "index.html";
110+
var optPath = ["server", "serveStaticOptions"];
111+
107112
if (item.get("directory")) {
108113
item.setIn(["server", "directory"], true);
109114
}
110-
if (item.get("index")) {
111-
item.setIn(["server", "index"], item.get("index"));
115+
116+
if (!item.getIn(optPath)) {
117+
item.setIn(optPath, Immutable.Map({
118+
index: indexarg
119+
}));
120+
} else {
121+
item.setIn(optPath.concat(["index"]), indexarg);
122+
}
123+
124+
// cli extensions
125+
if (item.get("extensions")) {
126+
item.setIn(optPath.concat(["extensions"]), item.get("extensions"));
112127
}
113128
}
114129
}

lib/server/static-server.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ var snippetUtils = require("./../snippet").utils;
1111
*/
1212
module.exports = function createServer (bs, scripts) {
1313

14-
var options = bs.options;
15-
var server = options.get("server");
16-
var middleware = options.get("middleware");
17-
var index = server.get("index") || "index.html";
18-
var basedir = server.get("baseDir");
14+
var options = bs.options;
15+
var server = options.get("server");
16+
var middleware = options.get("middleware");
17+
var basedir = server.get("baseDir");
18+
var serveStaticOptions = server.get("serveStaticOptions");
1919

2020
var app = connect();
2121

@@ -25,10 +25,10 @@ module.exports = function createServer (bs, scripts) {
2525
app.use(utils.handleOldIE);
2626

2727
/**
28-
* Server the Client-side JS from both version and static paths
28+
* Serve the Client-side JS from both version and static paths
2929
*/
3030
app.use(options.getIn(["scriptPaths", "versioned"]), scripts)
31-
.use(options.getIn(["scriptPaths", "path"]), scripts);
31+
.use(options.getIn(["scriptPaths", "path"]), scripts);
3232

3333
/**
3434
* Add directory middleware
@@ -56,7 +56,7 @@ module.exports = function createServer (bs, scripts) {
5656
/**
5757
* Add Serve static middlewares
5858
*/
59-
utils.addBaseDir(app, basedir, index);
59+
utils.addBaseDir(app, basedir, serveStaticOptions);
6060

6161
/**
6262
* Add further Serve static middlewares for routes

lib/server/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ var utils = {
2828
/**
2929
* @param app
3030
* @param base
31-
* @param index
31+
* @param opts
3232
*/
33-
addBaseDir: function (app, base, index) {
33+
addBaseDir: function (app, base, opts) {
34+
35+
opts = opts.toJS();
3436

3537
if (isList(base)) {
3638
base.forEach(function (item) {
37-
app.use(serveStatic(filePath.resolve(item), {index: index}));
39+
app.use(serveStatic(filePath.resolve(item), opts));
3840
});
3941
} else {
4042
if (_.isString(base)) {
41-
app.use(serveStatic(filePath.resolve(base), {index: index}));
43+
app.use(serveStatic(filePath.resolve(base), opts));
4244
}
4345
}
4446
},

test/specs/e2e/cli/e2e.cli.server.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,74 @@ describe("E2E CLI server test with directory listing/index ", function () {
8888
});
8989
it("Sets the correct server options", function () {
9090
assert.equal(instance.options.getIn(["server", "directory"]), true);
91-
assert.equal(instance.options.getIn(["server", "index"]), "index.htm");
91+
assert.equal(instance.options.getIn(["server", "serveStaticOptions", "index"]), "index.htm");
9292
});
9393
});
94+
95+
describe("E2E CLI server test with extensions option - single", function () {
96+
97+
var instance;
98+
99+
before(function (done) {
100+
101+
browserSync.reset();
102+
103+
cli({
104+
cli: {
105+
input: ["start"],
106+
flags: {
107+
server: "test/fixtures",
108+
open: false,
109+
online: false,
110+
logLevel: "silent",
111+
extensions: "html"
112+
}
113+
},
114+
cb: function (err, bs) {
115+
instance = bs;
116+
done();
117+
}
118+
});
119+
});
120+
after(function () {
121+
instance.cleanup();
122+
});
123+
it("Sets the extensions option (array) for serve static", function () {
124+
assert.equal(instance.options.getIn(["server", "serveStaticOptions", "index"]), "index.html");
125+
assert.deepEqual(instance.options.getIn(["server", "serveStaticOptions", "extensions"]).toJS(), ["html"]);
126+
});
127+
});
128+
129+
describe("E2E CLI server test with extensions option - multiple", function () {
130+
131+
var instance;
132+
133+
before(function (done) {
134+
135+
browserSync.reset();
136+
137+
cli({
138+
cli: {
139+
input: ["start"],
140+
flags: {
141+
server: "test/fixtures",
142+
open: false,
143+
online: false,
144+
logLevel: "silent",
145+
extensions: "html,css"
146+
}
147+
},
148+
cb: function (err, bs) {
149+
instance = bs;
150+
done();
151+
}
152+
});
153+
});
154+
after(function () {
155+
instance.cleanup();
156+
});
157+
it("Sets the extensions option (array) for serve static", function () {
158+
assert.equal(instance.options.getIn(["server", "serveStaticOptions", "index"]), "index.html");
159+
assert.deepEqual(instance.options.getIn(["server", "serveStaticOptions", "extensions"]).toJS(), ["html", "css"]);
160+
});
161+
});

test/specs/server/server.utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ describe("Server: Server Utils: ", function () {
2525
base = "app";
2626
});
2727
it("Should add the static middleware", function () {
28-
utils.addBaseDir(app, base, true);
28+
utils.addBaseDir(app, base, Immutable.Map());
2929
sinon.assert.calledOnce(spy);
3030
});
3131
it("Should add the static middleware with multiple middlewares", function () {
32-
utils.addBaseDir(app, Immutable.List(["app", "dist"]), true);
32+
utils.addBaseDir(app, Immutable.List(["app", "dist"]), Immutable.Map());
3333
sinon.assert.calledTwice(spy);
3434
});
3535
it("Should add the static middleware with multiple middlewares", function () {
36-
utils.addBaseDir(app, Immutable.List(["app", "dist", "alt"]), true);
36+
utils.addBaseDir(app, Immutable.List(["app", "dist", "alt"]), Immutable.Map());
3737
sinon.assert.calledThrice(spy);
3838
});
3939
});

0 commit comments

Comments
 (0)