From b26aeeb4c7223644c3ae3bb931d440a68285235c Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Mon, 5 Dec 2022 11:21:24 -0800 Subject: [PATCH] view: Improve error handling for missing build files When running from Auspice's root directory, this replaces an unhandled scandir error with a more meaningful message. A new flag (--customBuildOnly) is introduced for running from an external project. When specified, this prevents falling back to running from build files in Auspice's root directory, which can be unintentional. --- cli/view.js | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/cli/view.js b/cli/view.js index 142e4ec8f..74cee0505 100644 --- a/cli/view.js +++ b/cli/view.js @@ -24,6 +24,7 @@ const addParser = (parser) => { subparser.addArgument('--handlers', {action: "store", metavar: "JS", help: "Overwrite the provided server handlers for client requests. See documentation for more details."}); subparser.addArgument('--datasetDir', {metavar: "PATH", help: "Directory where datasets (JSONs) are sourced. This is ignored if you define custom handlers."}); subparser.addArgument('--narrativeDir', {metavar: "PATH", help: "Directory where narratives (Markdown files) are sourced. This is ignored if you define custom handlers."}); + subparser.addArgument('--customBuildOnly', {action: "storeTrue", help: "Error if a custom build is not found."}); /* there are some options which we deliberately do not document via `--help`. */ subparser.addArgument('--gh-pages', {action: "store", help: SUPPRESS}); /* related to the "static-site-generation" or "github-pages" */ }; @@ -74,28 +75,39 @@ const loadAndAddHandlers = ({app, handlersArg, datasetDir, narrativeDir}) => { `Looking for datasets in ${datasetsPath}\nLooking for narratives in ${narrativesPath}`; }; -const getAuspiceBuild = () => { +const getAuspiceBuild = (customBuildOnly) => { const cwd = path.resolve(process.cwd()); const sourceDir = path.resolve(__dirname, ".."); - if ( - cwd !== sourceDir && - fs.existsSync(path.join(cwd, "index.html")) && - fs.existsSync(path.join(cwd, "dist")) && - fs.readdirSync(path.join(cwd, "dist")).filter((fn) => fn.match(/^auspice.bundle.[a-z0-9]+.js$/)).length === 1 - ) { - return { - message: "Serving the auspice build which exists in this directory.", - baseDir: cwd, - distDir: path.join(cwd, "dist") - }; + + // Default to current working directory. + let baseDir = cwd; + if (!hasAuspiceBuild(cwd)) { + if (cwd === sourceDir || customBuildOnly) { + utils.error(`Auspice build files not found under ${cwd}. Did you run \`auspice build\` in this directory?`); + process.exit(1); + } else if (!hasAuspiceBuild(sourceDir)) { + utils.error(`Auspice build files not found under ${cwd} or ${sourceDir}. Did you run \`auspice build\` in either directory?`); + process.exit(1); + } else if (cwd !== sourceDir) { + utils.log(`Auspice build files not found under ${cwd}. Using build files under ${sourceDir}.`) + baseDir = sourceDir; + } } return { - message: `Serving auspice version ${version}`, - baseDir: sourceDir, - distDir: path.join(sourceDir, "dist") + message: `Serving the auspice build in ${baseDir}.`, + baseDir: baseDir, + distDir: path.join(baseDir, "dist") }; }; +const hasAuspiceBuild = (directory) => { + return ( + fs.existsSync(path.join(directory, "dist")) && + fs.existsSync(path.join(directory, "dist/index.html")) && + fs.readdirSync(path.join(directory, "dist")).filter((fn) => fn.match(/^auspice.bundle.[a-z0-9]+.js$/)).length === 1 + ) +} + const run = (args) => { /* Basic server set up */ const app = express(); @@ -104,7 +116,7 @@ const run = (args) => { app.use(compression()); app.use(nakedRedirect({reverse: true})); /* redirect www.name.org to name.org */ - const auspiceBuild = getAuspiceBuild(); + const auspiceBuild = getAuspiceBuild(args.customBuildOnly); utils.verbose(`Serving favicon from "${auspiceBuild.baseDir}"`); utils.verbose(`Serving index and built javascript from "${auspiceBuild.distDir}"`); app.get("/favicon.png", (req, res) => {res.sendFile(path.join(auspiceBuild.baseDir, "favicon.png"));});