Skip to content

Commit

Permalink
feat: add directory listing for serve-static mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 26, 2017
1 parent 393309b commit b5cc56e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
13 changes: 7 additions & 6 deletions lib/browser-sync.js
Expand Up @@ -366,15 +366,16 @@ BrowserSync.prototype.serveFile = function(path, props) {

/**
* Add middlewares on the fly
* @param {{route: string, handle: function, id?: string}}
*/
BrowserSync.prototype._addMiddlewareToStack = function(entry) {
var bs = this;
if (bs.options.get("mode") === "proxy") {
bs.app.stack.splice(bs.app.stack.length - 1, 0, entry);
} else {
bs.app.stack.push(entry);
}

/**
* additional middlewares are always appended -1,
* this is to allow the proxy middlewares to remain,
* and the directory index to remain in serveStatic/snippet modes
*/
bs.app.stack.splice(bs.app.stack.length - 1, 0, entry);
};

var _addMiddlewareCount = 0;
Expand Down
6 changes: 4 additions & 2 deletions lib/server/static-server.js
Expand Up @@ -9,7 +9,6 @@ var serveIndex = require("serve-index");

/**
* @param {BrowserSync} bs
* @param scripts
* @returns {*}
*/
module.exports = function createServer(bs) {
Expand All @@ -31,7 +30,10 @@ module.exports = function createServer(bs) {

return mw.concat({
route: "",
handle: serveIndex(resolve(basedirs[0]), { icons: true }),
handle: serveIndex(resolve(basedirs[0]), {
icons: true,
view: "details"
}),
id: "Browsersync Server Directory Middleware"
});
})
Expand Down
26 changes: 18 additions & 8 deletions lib/server/utils.js
Expand Up @@ -13,6 +13,7 @@ var List = require("immutable").List;
var snippet = require("./../snippet").utils;
var _ = require("../lodash.custom");
var serveStatic = require("serve-static");
var serveIndex = require("serve-index");
var logger = require("../logger");
var snippetUtils = require("../snippet").utils;
var lrSnippet = require("resp-modifier");
Expand Down Expand Up @@ -224,15 +225,24 @@ var serverUtils = {
var beforeMiddlewares = userMiddlewares.filter(function(x) {
return x.override;
});
var afterMiddlewares = userMiddlewares.filter(function(x) {
return !x.override;
});
var afterMiddlewares = userMiddlewares
.filter(function(x) {
return !x.override;
})
.concat(
bs.options.get("mode") !== "proxy" && {
id: "Browsersync 404/index support",
route: "",
handle: serveIndex(bs.options.get("cwd"), {
icons: true,
view: "details"
})
}
);

return [].concat(
beforeMiddlewares,
defaultMiddlewares,
afterMiddlewares
);
return []
.concat(beforeMiddlewares, defaultMiddlewares, afterMiddlewares)
.filter(Boolean);

function normaliseMiddleware(item) {
/**
Expand Down
26 changes: 0 additions & 26 deletions test/specs/e2e/server/e2e.server.dirs.js
Expand Up @@ -133,29 +133,3 @@ describe("E2E server test with base dir array option + directory", function() {
});
});
});

describe("E2E server test with base dir array = false", function() {
it("should ignore the directory option if a falsey value was given", function(done) {
browserSync.reset();

var config = {
server: {
baseDir: ["test", "app"],
directory: false
},
logLevel: "silent",
open: false
};

browserSync.init(config, function(err, bs) {
request(bs.server)
.get("/")
.set("accept", "text/html")
.expect(200)
.end(function(err, res) {
assert.notInclude(res.text, "listing directory /");
done();
});
});
});
});

0 comments on commit b5cc56e

Please sign in to comment.