Skip to content

Commit

Permalink
feat(options): Allow any serve-static specific configuration under ne…
Browse files Browse the repository at this point in the history
…w property - closes #539
  • Loading branch information
bitjson authored and shakyShane committed Mar 29, 2015
1 parent bc5ed4a commit 4c58541
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 18 deletions.
11 changes: 11 additions & 0 deletions lib/cli/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/cli/opts.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 17 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/server/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
},
Expand Down
70 changes: 69 additions & 1 deletion test/specs/e2e/cli/e2e.cli.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
});
});
6 changes: 3 additions & 3 deletions test/specs/server/server.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down

0 comments on commit 4c58541

Please sign in to comment.