Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

experiment with writing static files to dir

  • Loading branch information...
commit 4fb8586deaddef5430ae5fa76da279fc86e3a642 1 parent a4ad274
Tim Fischbach authored

Showing 1 changed file with 43 additions and 3 deletions. Show diff stats Hide diff stats

  1. +43 3 lib/buster-static.js
46 lib/buster-static.js
@@ -2,6 +2,8 @@ var B = require("buster-core");
2 2 var bCli = require("buster-cli");
3 3 var fs = require("fs");
4 4 var http = require("http");
  5 +var path = require("path");
  6 +var mkdirp = require("mkdirp");
5 7 var resourceMiddleware = require("ramp-resources").resourceMiddleware;
6 8
7 9 // TODO: add test coverage (integration test?)
@@ -59,7 +61,7 @@ function configureGroup(group, extensions) {
59 61
60 62 function runWithConfigGroup(cli, resourceSet, options) {
61 63 if (options["Output dir"].isSet) {
62   - cli.writeToDisk(resourceSet);
  64 + cli.writeToDisk(resourceSet, options["Output dir"].value);
63 65 } else {
64 66 cli.startServer(resourceSet, options["--port"].value);
65 67 }
@@ -81,6 +83,44 @@ function startServer(resourceSet, port, logger) {
81 83 return server;
82 84 }
83 85
  86 +function writeToDisk(resourceSet, outputDir, logger) {
  87 + logger.log("Writing static test suite to " + outputDir + ".");
  88 +
  89 + var middleware = resourceMiddleware.create();
  90 + middleware.setContextPath('.');
  91 + middleware.mount("/", resourceSet);
  92 +
  93 + resourceSet.forEach(function(resource) {
  94 + var req = {
  95 + url: resource.path
  96 + };
  97 +
  98 + var res = {
  99 + writeHead: function() {},
  100 + end: function(content) {
  101 + var fileName = path.join(outputDir, resource.path === "/" ? "index.html" : resource.path);
  102 + var dirName = path.dirname(fileName);
  103 +
  104 + mkdirp(dirName, function(err) {
  105 + if (err) {
  106 + logger.error("Could not create directory " + dirName + ".");
  107 + return;
  108 + }
  109 +
  110 + logger.error("Writing " + fileName + "...");
  111 + fs.writeFile(fileName, content, function(err) {
  112 + if (err) {
  113 + logger.error("Could not write " + fileName + ".");
  114 + }
  115 + });
  116 + });
  117 + }
  118 + };
  119 +
  120 + middleware.respond(req, res);
  121 + });
  122 +}
  123 +
84 124 function addCLIArgs(cli) {
85 125 cli.addConfigOption("buster");
86 126
@@ -135,7 +175,7 @@ module.exports = B.extend({
135 175 this.httpServer = startServer(resourceSet, port, this.logger);
136 176 },
137 177
138   - writeToDisk: function (resourceSet) {
139   - console.log("Not yet supported");
  178 + writeToDisk: function (resourceSet, outputDir) {
  179 + writeToDisk(resourceSet, outputDir, this.logger);
140 180 }
141 181 });

0 comments on commit 4fb8586

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