From 5a6ed63aede732f23c3aa61fe31e69e295f03384 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. --- cli/view.js | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/cli/view.js b/cli/view.js index 852f9e89d..5c60c8746 100644 --- a/cli/view.js +++ b/cli/view.js @@ -77,25 +77,36 @@ const loadAndAddHandlers = ({app, handlersArg, datasetDir, narrativeDir}) => { const getAuspiceBuild = () => { 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\.main\.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) { + utils.error(`Auspice build files not found under ${cwd}. Did you run \`auspice build\` in this directory?`); + process.exit(1); + } + 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); + } + 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} (version ${version}).`, + baseDir, + distDir: path.join(baseDir, "dist") }; }; +function 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\.main\.bundle\.[a-z0-9]+\.js$/)).length === 1 + ) +} + const run = (args) => { /* Basic server set up */ const app = express();