const fs = require('fs');
const path = require('path');
const Libraries = require('../libraries');

module.exports = class FrontRouter {
  constructor(opts = {}) {
    this.options = {
      pageRoot: opts.pageRoot || process.cwd()
    }
    this.routes = [];

    // Figure out what library to load
    switch (typeof opts.library) {
      // String: pull a library out of the built-in ones
      case 'string': {
        const lib = Libraries[opts.library];
        if (typeof lib === 'undefined') {
          throw new Error(`Front Router: there's no built-in plugin for "${opts.library}"`);
        }
        else {
          this.options.library = lib;
        }
        break;
      }
      // Function: add as-is
      case 'function': {
        this.options.library = opts.library;
        break;
      }
      // Nothing: use the default library adapter
      case 'undefined': {
        this.options.library = Libraries.default;
        break;
      }
      // Other weird values? Nope
      default: {
        throw new Error('Front Router: library must be a string or function.');
      }
    }
  }

  addRoute(route) {
    if (typeof route !== 'object' || route === null) {
      throw new Error('Front Router: routes must be objects.');
    }
    this.routes.push(Object.assign({}, route, {
      path: path.relative(this.options.pageRoot, route.path)
    }));
  }

  writeRoutes(filePath) {
    const routes = this.routes.sort(function(a, b) {
      return a.url < b.url;
    });

    const contents = this.options.library(routes);

    return new Promise((resolve, reject) => {
      fs.appendFile(filePath, contents, err => {
        if (err) reject(err);
        else resolve();
      });
    });
  }
}

@@ -97,44 +97,550 @@
 );%0A%0A
-module.exports = class FrontRouter %7B
+/**%0A * Options that can be passed to the %60FrontRouter%60 class.%0A * @typedef %7BObject%7D FrontRouterOptions%0A * @prop %7BString%7D pageRoot - Root path to HTML files.%0A * @prop %7BString%7CFunction%7D library - Library to print routes for. Can be a string pointing to a built-in library adapter, or a function for a custom adapter.%0A */%0A%0A/**%0A * Class to manage routes and write them to disk as a routes file.%0A */%0Amodule.exports = class FrontRouter %7B%0A  /**%0A   * Creates a new instance of %60FrontRouter%60.%0A   * @prop %7BFrontRouterOptions%7D opts - Class options.%0A   */
 %0A  c
@@ -1611,16 +1611,93 @@
  %7D%0A  %7D%0A%0A
+  /**%0A   * Add a new route.%0A   * @param %7BObject%7D route - Route to add.%0A   */%0A
   addRou
@@ -1956,16 +1956,272 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Write all routes to disk as a JavaScript file, using the set library adapter.%0A   * @param %7BString%7D filePath - Path to write to.%0A   * @returns %7BPromise%7D Promise which resolves when the file has been written, or rejects if there's an error.%0A   */%0A
   writeR

Add comments to FrontRouter class methods