const fs = require("fs-extra"); const TemplatePath = require("./TemplatePath"); const config = require("./Config"); const debug = require("debug")("EleventyServe"); class EleventyServe { constructor() {} get config() { return this.configOverride || config.getConfig(); } set config(config) { this.configOverride = config; } setOutputDir(outputDir) { this.outputDir = outputDir; } getPathPrefix() { return this.config.pathPrefix || "/"; } getRedirectDir(dirName) { return TemplatePath.join(this.outputDir, dirName); } getRedirectDirOverride() { // has a pathPrefix, add a /index.html template to redirect to /pathPrefix/ if (this.getPathPrefix() !== "/") { return "_eleventy_redirect"; } } getRedirectFilename(dirName) { return TemplatePath.join(this.getRedirectDir(dirName), "index.html"); } getOptions(port) { let pathPrefix = this.getPathPrefix(); // TODO customize this in Configuration API? let serverConfig = { baseDir: this.outputDir }; let redirectDirName = this.getRedirectDirOverride(); // has a pathPrefix, add a /index.html template to redirect to /pathPrefix/ if (redirectDirName) { serverConfig.baseDir = this.getRedirectDir(redirectDirName); serverConfig.routes = {}; serverConfig.routes[pathPrefix] = this.outputDir; // if has a savedPathPrefix, use the /savedPathPrefix/index.html template to redirect to /pathPrefix/ if (this.savedPathPrefix) { serverConfig.routes[this.savedPathPrefix] = TemplatePath.join( this.outputDir, this.savedPathPrefix ); } } return Object.assign( { server: serverConfig, port: port || 8080, ignore: ["node_modules"], watch: false, open: false, notify: false, index: "index.html" }, this.config.browserSyncConfig ); } cleanupRedirect(dirName) { if (dirName && dirName !== "/") { let savedPathFilename = this.getRedirectFilename(dirName); setTimeout(function() { if (!fs.existsSync(savedPathFilename)) { debug(`Cleanup redirect: Could not find ${savedPathFilename}`); return; } let savedPathContent = fs.readFileSync(savedPathFilename, "utf-8"); if ( savedPathContent.indexOf("Browsersync pathPrefix Redirect") === -1 ) { debug( `Cleanup redirect: Found ${savedPathFilename} but it wasn’t an eleventy redirect.` ); return; } fs.unlink(savedPathFilename, err => { if (!err) { debug(`Cleanup redirect: Deleted ${savedPathFilename}`); } }); }, 2000); } } serveRedirect(dirName) { fs.outputFile( this.getRedirectFilename(dirName), `