diff --git a/index.js b/index.js index 4a4ddba..bf0571d 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,12 @@ module.exports = async (request, response) => { return boop.result } + if (boop.type == symbols.redirect) { + response.statusCode = 302 + response.setHeader("Location", boop.to) + response.end() + } + if (boop.type == symbols.static) { return serve(boop) } @@ -20,7 +26,9 @@ module.exports = async (request, response) => { return websiteExists ? serve({request, response, websiteDirectory}) - : new Error(`i couldnt boop such a snoot and your website directory (${websiteDirectory}) did not exist`) + : new Error( + `i couldnt boop such a snoot and your website directory (${websiteDirectory}) did not exist` + ) } return new Error(`i refuse to apply to a snoot a boop of a type i don't know`) diff --git a/library/commands.js b/library/commands.js index 574d8ce..ae25b48 100755 --- a/library/commands.js +++ b/library/commands.js @@ -6,18 +6,17 @@ let execa = require("execa") let boopsDirectory = "boops" let directoryReducer = (names, entry) => - entry.isDirectory() - ? names.concat(entry.name) - : names + entry.isDirectory() ? names.concat(entry.name) : names let getBoopNames = async () => - await checkFileExists(boopsDirectory) - ? (await readdir(boopsDirectory, {withFileTypes: true})) - .reduce(directoryReducer, []) + (await checkFileExists(boopsDirectory)) + ? (await readdir(boopsDirectory, {withFileTypes: true})).reduce( + directoryReducer, + [] + ) : [] -let getBoopPathFromName = name => - path.resolve(boopsDirectory, name) +let getBoopPathFromName = name => path.resolve(boopsDirectory, name) let npm = async (commandName, boopName) => { let manifestPath = path.resolve(boopsDirectory, boopName, "package.json") @@ -38,32 +37,23 @@ let npm = async (commandName, boopName) => { let command = isInstall ? ["install"] - : [ - "run-script", - "--scripts-prepend-node-path=true", - commandName - ] + : ["run-script", "--scripts-prepend-node-path=true", commandName] - let prefix = [ - "--prefix", - getBoopPathFromName(boopName) - ] + let prefix = ["--prefix", getBoopPathFromName(boopName)] console.log(`running ${commandName} in ${boopName} 🐕`) - return execa("npm", [ - ...prefix, - ...command - ], { + return execa("npm", [...prefix, ...command], { extendEnv: true, env: { BOOP_WEBSITE_DIRECTORY: "website", BOOP_PUBLIC_URL: `/${boopName}`, - // deprecated, but for old canvas sketches - SNOOT_OUTPUT_DIRECTORY: "website", - SNOOT_PUBLIC_URL: `/${boopName}` - } - }).then(result => (console.log(`completed ${commandName} in ${boopName} 🦔`), result)) + }, + }).then( + result => ( + console.log(`completed ${commandName} in ${boopName} 🦔`), result + ) + ) } let boopInParallel = async fn => { @@ -73,9 +63,7 @@ let boopInParallel = async fn => { } let createTask = taskName => () => - boopInParallel(async name => - npm(taskName, name) - ) + boopInParallel(async name => npm(taskName, name)) exports.build = createTask("build") exports.watch = createTask("watch") diff --git a/library/serve.js b/library/serve.js index 7671e17..30699da 100644 --- a/library/serve.js +++ b/library/serve.js @@ -4,5 +4,5 @@ module.exports = async ({request, response, websiteDirectory}) => serveHandler(request, response, { public: websiteDirectory, directoryListing: false, - cleanUrls: true + cleanUrls: true, }) diff --git a/route.js b/route.js index a4c2e69..9ade0e3 100644 --- a/route.js +++ b/route.js @@ -3,11 +3,10 @@ let path = require("path") let symbols = require("./symbols.js") let boopRoot = "boops" -let getName = request => - request.url.split("/")[1] +let getName = request => request.url.split("/")[1] let getNextUrl = request => - request.url.slice(getName(request).length + 1) || '/' + request.url.slice(getName(request).length + 1) || "/" let getNextRequest = request => { request.url = getNextUrl(request) @@ -19,7 +18,8 @@ module.exports = async (request, response) => { let boopDirectory = boopName && path.resolve(boopRoot, boopName) - if (boopName && await checkFileExists(boopDirectory)) { + if (boopName && (await checkFileExists(boopDirectory))) { + // at this point we know there's a file let boopManifestPath = path.resolve(boopDirectory, "package.json") let boopWebsitePath = path.resolve(boopDirectory, "website") @@ -28,13 +28,20 @@ module.exports = async (request, response) => { let nextRequest = getNextRequest(request) if (main && main.endsWith(".js")) { // we found a javascript module, hopefully can handle (request, repons) + // let's redirect to `module/` because that's expected for a root + if (request.url.endsWith("/") != true) { + return { + type: symbols.redirect, + to: request.url + "/", + } + } let boop = require(boopDirectory) return { type: symbols.module, module: boop, request: nextRequest, response: response, - result: boop(nextRequest, response) + result: boop(nextRequest, response), } } else if (await checkFileExists(boopWebsitePath)) { // no javascript module, but we have some static files maybe? @@ -42,7 +49,7 @@ module.exports = async (request, response) => { type: symbols.static, request: nextRequest, response: response, - websiteDirectory: boopWebsitePath + websiteDirectory: boopWebsitePath, } } } @@ -50,6 +57,6 @@ module.exports = async (request, response) => { // found nothing return { - type: symbols.nothing + type: symbols.nothing, } } diff --git a/symbols.js b/symbols.js index 231730b..f3de78d 100644 --- a/symbols.js +++ b/symbols.js @@ -1,4 +1,5 @@ module.exports = { + redirect: Symbol("boop/redirect"), module: Symbol("boop/module"), static: Symbol("boop/static"), nothing: Symbol("boop/nothing")