Skip to content
Browse files

experiment with writing static files to dir

  • Loading branch information...
1 parent a4ad274 commit 4fb8586deaddef5430ae5fa76da279fc86e3a642 @tf committed Mar 20, 2013
Showing with 43 additions and 3 deletions.
  1. +43 −3 lib/buster-static.js
View
46 lib/buster-static.js
@@ -2,6 +2,8 @@ var B = require("buster-core");
var bCli = require("buster-cli");
var fs = require("fs");
var http = require("http");
+var path = require("path");
+var mkdirp = require("mkdirp");
var resourceMiddleware = require("ramp-resources").resourceMiddleware;
// TODO: add test coverage (integration test?)
@@ -59,7 +61,7 @@ function configureGroup(group, extensions) {
function runWithConfigGroup(cli, resourceSet, options) {
if (options["Output dir"].isSet) {
- cli.writeToDisk(resourceSet);
+ cli.writeToDisk(resourceSet, options["Output dir"].value);
} else {
cli.startServer(resourceSet, options["--port"].value);
}
@@ -81,6 +83,44 @@ function startServer(resourceSet, port, logger) {
return server;
}
+function writeToDisk(resourceSet, outputDir, logger) {
+ logger.log("Writing static test suite to " + outputDir + ".");
+
+ var middleware = resourceMiddleware.create();
+ middleware.setContextPath('.');
+ middleware.mount("/", resourceSet);
+
+ resourceSet.forEach(function(resource) {
+ var req = {
+ url: resource.path
+ };
+
+ var res = {
+ writeHead: function() {},
+ end: function(content) {
+ var fileName = path.join(outputDir, resource.path === "/" ? "index.html" : resource.path);
+ var dirName = path.dirname(fileName);
+
+ mkdirp(dirName, function(err) {
+ if (err) {
+ logger.error("Could not create directory " + dirName + ".");
+ return;
+ }
+
+ logger.error("Writing " + fileName + "...");
+ fs.writeFile(fileName, content, function(err) {
+ if (err) {
+ logger.error("Could not write " + fileName + ".");
+ }
+ });
+ });
+ }
+ };
+
+ middleware.respond(req, res);
+ });
+}
+
function addCLIArgs(cli) {
cli.addConfigOption("buster");
@@ -135,7 +175,7 @@ module.exports = B.extend({
this.httpServer = startServer(resourceSet, port, this.logger);
},
- writeToDisk: function (resourceSet) {
- console.log("Not yet supported");
+ writeToDisk: function (resourceSet, outputDir) {
+ writeToDisk(resourceSet, outputDir, this.logger);
}
});

0 comments on commit 4fb8586

Please sign in to comment.
Something went wrong with that request. Please try again.