From 4c58541f20c5b5e2e2ddad42d891547adee0bcf4 Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Sun, 29 Mar 2015 22:51:40 +0100 Subject: [PATCH] feat(options): Allow any serve-static specific configuration under new property - closes #539 --- lib/cli/cli-options.js | 11 +++++ lib/cli/opts.json | 1 + lib/options.js | 19 +++++++- lib/server/static-server.js | 16 +++---- lib/server/utils.js | 10 ++-- test/specs/e2e/cli/e2e.cli.server.js | 70 +++++++++++++++++++++++++++- test/specs/server/server.utils.js | 6 +-- 7 files changed, 115 insertions(+), 18 deletions(-) diff --git a/lib/cli/cli-options.js b/lib/cli/cli-options.js index ea5594462..95f8370b6 100644 --- a/lib/cli/cli-options.js +++ b/lib/cli/cli-options.js @@ -254,6 +254,17 @@ opts.callbacks = { return Immutable.List([value]); } }); + }, + /** + * @param value + */ + extensions: function (value) { + if (_.isString(value)) { + var split = opts.utils.explodeFilesArg(value); + if (split.length) { + return Immutable.List(split); + } + } } }; diff --git a/lib/cli/opts.json b/lib/cli/opts.json index 67b2306f3..859d808ea 100644 --- a/lib/cli/opts.json +++ b/lib/cli/opts.json @@ -5,6 +5,7 @@ "exclude": "File patterns to ignore", "server": "Run a Local server (uses your cwd as the web root)", "index": "Specify which file should be used as the index page", + "extensions": "Specify file extension fallbacks", "startPath": "Specify the start path for the opened browser", "https": "Enable SSL for local development", "directory": "Show a directory listing for the server", diff --git a/lib/options.js b/lib/options.js index 9bed9600f..3e7441b58 100644 --- a/lib/options.js +++ b/lib/options.js @@ -103,12 +103,27 @@ function setNamespace(item) { * @param item */ function setServerOpts(item) { + if (item.get("server")) { + + var indexarg = item.get("index") || item.getIn(["server", "index"]) || "index.html"; + var optPath = ["server", "serveStaticOptions"]; + if (item.get("directory")) { item.setIn(["server", "directory"], true); } - if (item.get("index")) { - item.setIn(["server", "index"], item.get("index")); + + if (!item.getIn(optPath)) { + item.setIn(optPath, Immutable.Map({ + index: indexarg + })); + } else { + item.setIn(optPath.concat(["index"]), indexarg); + } + + // cli extensions + if (item.get("extensions")) { + item.setIn(optPath.concat(["extensions"]), item.get("extensions")); } } } diff --git a/lib/server/static-server.js b/lib/server/static-server.js index f6936e5b9..3bf2de691 100644 --- a/lib/server/static-server.js +++ b/lib/server/static-server.js @@ -11,11 +11,11 @@ var snippetUtils = require("./../snippet").utils; */ module.exports = function createServer (bs, scripts) { - var options = bs.options; - var server = options.get("server"); - var middleware = options.get("middleware"); - var index = server.get("index") || "index.html"; - var basedir = server.get("baseDir"); + var options = bs.options; + var server = options.get("server"); + var middleware = options.get("middleware"); + var basedir = server.get("baseDir"); + var serveStaticOptions = server.get("serveStaticOptions"); var app = connect(); @@ -25,10 +25,10 @@ module.exports = function createServer (bs, scripts) { app.use(utils.handleOldIE); /** - * Server the Client-side JS from both version and static paths + * Serve the Client-side JS from both version and static paths */ app.use(options.getIn(["scriptPaths", "versioned"]), scripts) - .use(options.getIn(["scriptPaths", "path"]), scripts); + .use(options.getIn(["scriptPaths", "path"]), scripts); /** * Add directory middleware @@ -56,7 +56,7 @@ module.exports = function createServer (bs, scripts) { /** * Add Serve static middlewares */ - utils.addBaseDir(app, basedir, index); + utils.addBaseDir(app, basedir, serveStaticOptions); /** * Add further Serve static middlewares for routes diff --git a/lib/server/utils.js b/lib/server/utils.js index f06dcecbb..f310ddd93 100644 --- a/lib/server/utils.js +++ b/lib/server/utils.js @@ -28,17 +28,19 @@ var utils = { /** * @param app * @param base - * @param index + * @param opts */ - addBaseDir: function (app, base, index) { + addBaseDir: function (app, base, opts) { + + opts = opts.toJS(); if (isList(base)) { base.forEach(function (item) { - app.use(serveStatic(filePath.resolve(item), {index: index})); + app.use(serveStatic(filePath.resolve(item), opts)); }); } else { if (_.isString(base)) { - app.use(serveStatic(filePath.resolve(base), {index: index})); + app.use(serveStatic(filePath.resolve(base), opts)); } } }, diff --git a/test/specs/e2e/cli/e2e.cli.server.js b/test/specs/e2e/cli/e2e.cli.server.js index b8e354917..3a58fe54d 100644 --- a/test/specs/e2e/cli/e2e.cli.server.js +++ b/test/specs/e2e/cli/e2e.cli.server.js @@ -88,6 +88,74 @@ describe("E2E CLI server test with directory listing/index ", function () { }); it("Sets the correct server options", function () { assert.equal(instance.options.getIn(["server", "directory"]), true); - assert.equal(instance.options.getIn(["server", "index"]), "index.htm"); + assert.equal(instance.options.getIn(["server", "serveStaticOptions", "index"]), "index.htm"); }); }); + +describe("E2E CLI server test with extensions option - single", function () { + + var instance; + + before(function (done) { + + browserSync.reset(); + + cli({ + cli: { + input: ["start"], + flags: { + server: "test/fixtures", + open: false, + online: false, + logLevel: "silent", + extensions: "html" + } + }, + cb: function (err, bs) { + instance = bs; + done(); + } + }); + }); + after(function () { + instance.cleanup(); + }); + it("Sets the extensions option (array) for serve static", function () { + assert.equal(instance.options.getIn(["server", "serveStaticOptions", "index"]), "index.html"); + assert.deepEqual(instance.options.getIn(["server", "serveStaticOptions", "extensions"]).toJS(), ["html"]); + }); +}); + +describe("E2E CLI server test with extensions option - multiple", function () { + + var instance; + + before(function (done) { + + browserSync.reset(); + + cli({ + cli: { + input: ["start"], + flags: { + server: "test/fixtures", + open: false, + online: false, + logLevel: "silent", + extensions: "html,css" + } + }, + cb: function (err, bs) { + instance = bs; + done(); + } + }); + }); + after(function () { + instance.cleanup(); + }); + it("Sets the extensions option (array) for serve static", function () { + assert.equal(instance.options.getIn(["server", "serveStaticOptions", "index"]), "index.html"); + assert.deepEqual(instance.options.getIn(["server", "serveStaticOptions", "extensions"]).toJS(), ["html", "css"]); + }); +}); \ No newline at end of file diff --git a/test/specs/server/server.utils.js b/test/specs/server/server.utils.js index dfd678de7..a459818e3 100644 --- a/test/specs/server/server.utils.js +++ b/test/specs/server/server.utils.js @@ -25,15 +25,15 @@ describe("Server: Server Utils: ", function () { base = "app"; }); it("Should add the static middleware", function () { - utils.addBaseDir(app, base, true); + utils.addBaseDir(app, base, Immutable.Map()); sinon.assert.calledOnce(spy); }); it("Should add the static middleware with multiple middlewares", function () { - utils.addBaseDir(app, Immutable.List(["app", "dist"]), true); + utils.addBaseDir(app, Immutable.List(["app", "dist"]), Immutable.Map()); sinon.assert.calledTwice(spy); }); it("Should add the static middleware with multiple middlewares", function () { - utils.addBaseDir(app, Immutable.List(["app", "dist", "alt"]), true); + utils.addBaseDir(app, Immutable.List(["app", "dist", "alt"]), Immutable.Map()); sinon.assert.calledThrice(spy); }); });