Skip to content

Commit

Permalink
view: Improve error handling for missing build files
Browse files Browse the repository at this point in the history
When running from Auspice's root directory, this replaces an unhandled
scandir error with a more meaningful message.
  • Loading branch information
victorlin committed Nov 30, 2023
1 parent 05a9958 commit 5a6ed63
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions cli/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 5a6ed63

Please sign in to comment.